fw4spl
InrImageReaderService.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "ioITK/InrImageReaderService.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwData/Image.hpp>
15 #include <fwData/location/Folder.hpp>
16 #include <fwData/location/SingleFile.hpp>
17 
18 #include <fwGui/Cursor.hpp>
19 #include <fwGui/dialog/LocationDialog.hpp>
20 #include <fwGui/dialog/MessageDialog.hpp>
21 #include <fwGui/dialog/ProgressDialog.hpp>
22 
23 #include <fwIO/IReader.hpp>
24 
25 #include <fwItkIO/ImageReader.hpp>
26 
27 #include <fwServices/macros.hpp>
28 
29 namespace ioITK
30 {
31 
32 fwServicesRegisterMacro( ::fwIO::IReader, ::ioITK::InrImageReaderService, ::fwData::Image );
33 
34 //------------------------------------------------------------------------------
35 
36 InrImageReaderService::InrImageReaderService() noexcept
37 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 InrImageReaderService::~InrImageReaderService() noexcept
43 {
44 }
45 
46 //------------------------------------------------------------------------------
47 
49 {
50  return ::fwIO::FILE;
51 }
52 //
53 //------------------------------------------------------------------------------
54 
56 {
58 }
59 
60 //------------------------------------------------------------------------------
61 
63 {
65  static ::boost::filesystem::path _sDefaultPath;
66 
68  dialogFile.setTitle(m_windowTitle.empty() ? "Choose an Inrimage file" : m_windowTitle);
69  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
70  dialogFile.addFilter("Inrimage", "*.inr.gz");
71  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
72  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
73 
74  ::fwData::location::SingleFile::sptr result;
75  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
76  if (result)
77  {
78  _sDefaultPath = result->getPath().parent_path();
79  this->setFile(result->getPath());
80  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
81  }
82  else
83  {
84  this->clearLocations();
85  }
86 }
87 
88 //------------------------------------------------------------------------------
89 
90 void InrImageReaderService::info(std::ostream& _sstream )
91 {
92  _sstream << "InrImageReaderService::info";
93 }
94 
95 //------------------------------------------------------------------------------
96 
97 bool InrImageReaderService::createImage( const ::boost::filesystem::path& inrFileDir,
98  const ::fwData::Image::sptr& _pImg )
99 {
100  SLM_TRACE_FUNC();
101  ::fwItkIO::ImageReader::sptr myLoader = ::fwItkIO::ImageReader::New();
102  bool ok = true;
103 
104  myLoader->setObject(_pImg);
105  myLoader->setFile(inrFileDir);
106 
107  try
108  {
109  ::fwGui::dialog::ProgressDialog progressMeterGUI("Loading Image ");
110  myLoader->addHandler( progressMeterGUI );
111  myLoader->read();
112  }
113  catch (const std::exception& e)
114  {
115  std::stringstream ss;
116  ss << "Warning during loading : " << e.what();
118  ss.str(),
119  ::fwGui::dialog::IMessageDialog::WARNING);
120  ok = false;
121  }
122  catch( ... )
123  {
125  "Warning during loading",
126  ::fwGui::dialog::IMessageDialog::WARNING);
127  ok = false;
128  }
129  return ok;
130 }
131 
132 //------------------------------------------------------------------------------
133 
135 {
136  SLM_TRACE_FUNC();
137 
138  if( this->hasLocationDefined() )
139  {
140  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(::fwIO::s_DATA_KEY);
141  if (!image)
142  {
143  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
144  image = this->getObject< ::fwData::Image >();
145  }
146  SLM_ASSERT("'" + ::fwIO::s_DATA_KEY + "' key is not defined", image);
147 
148  if ( this->createImage( this->getFile(), image) )
149  {
150  ::fwGui::Cursor cursor;
151  cursor.setCursor(::fwGui::ICursor::BUSY);
152  notificationOfDBUpdate();
153  cursor.setDefaultCursor();
154  }
155  }
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 void InrImageReaderService::notificationOfDBUpdate()
161 {
162  SLM_TRACE_FUNC();
163  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(::fwIO::s_DATA_KEY);
164  if (!image)
165  {
166  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
167  image = this->getObject< ::fwData::Image >();
168  }
169  SLM_ASSERT("'" + ::fwIO::s_DATA_KEY + "' key is not defined", image);
170 
172  {
173  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
174  sig->asyncEmit();
175  }
176 }
177 
178 //------------------------------------------------------------------------------
179 
180 } // namespace ioITK
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IReader.cpp:233
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IReader.cpp:144
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual IOITK_API void updating() override
Override.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
Reader for .inr.gz image.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
Defines the generic progress dialog for IHM. Use the Delegate design pattern. The specific implementa...
IOITK_API::fwIO::IOPathType getIOPathType() const override
Return managed file type, here FILE.
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IReader.cpp:62
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IReader.cpp:71
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
IOITK_API void info(std::ostream &_sstream) override
Override.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
virtual IOITK_API void configuring() override
Override.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
Defines the generic cursor for IHM. Use the Delegate design pattern.
This class defines an image.
virtual IOITK_API void configureWithIHM() override
Override.
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IReader.cpp:137
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
The namespace ioITK contains services for reader, writer and helper for itk image.
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.