fw4spl
SSeriesDBLazyReader.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 "ioVtkGdcm/SSeriesDBLazyReader.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwGui/Cursor.hpp>
15 #include <fwGui/dialog/LocationDialog.hpp>
16 #include <fwGui/dialog/MessageDialog.hpp>
17 
18 #include <fwIO/IReader.hpp>
19 
20 #include <fwJobs/IJob.hpp>
21 #include <fwJobs/Job.hpp>
22 
23 #include <fwMedData/Series.hpp>
24 #include <fwMedData/SeriesDB.hpp>
25 
26 #include <fwServices/macros.hpp>
27 
28 #include <fwTools/ProgressToLogger.hpp>
29 
30 #include <vtkGdcmIO/SeriesDBLazyReader.hpp>
31 
32 namespace ioVtkGdcm
33 {
34 
35 fwServicesRegisterMacro( ::fwIO::IReader, ::ioVtkGdcm::SSeriesDBLazyReader, ::fwMedData::SeriesDB );
36 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
37 
38 //------------------------------------------------------------------------------
39 
41 {
42  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
43 }
44 
45 //------------------------------------------------------------------------------
46 
48 {
49 }
50 
51 //------------------------------------------------------------------------------
52 
54 {
55  static ::boost::filesystem::path _sDefaultPath;
56 
58  dialogFile.setTitle(m_windowTitle.empty() ? this->getSelectorDialogTitle() : m_windowTitle);
59  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
60  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
61  dialogFile.setType(::fwGui::dialog::LocationDialog::FOLDER);
62 
63  ::fwData::location::Folder::sptr result;
64  result = ::fwData::location::Folder::dynamicCast( dialogFile.show() );
65  if (result)
66  {
67  _sDefaultPath = result->getFolder();
68  this->setFolder( result->getFolder() );
69  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
70  }
71 }
72 
73 //------------------------------------------------------------------------------
74 
76 {
78 }
79 
80 //------------------------------------------------------------------------------
81 
83 {
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 void SSeriesDBLazyReader::info(std::ostream& _sstream )
97 {
98  _sstream << "SSeriesDBLazyReader::info";
99 }
100 
101 //------------------------------------------------------------------------------
102 
103 SSeriesDBLazyReader::ExtensionsType SSeriesDBLazyReader::getSupportedExtensions()
104 {
105  ExtensionsType extensions;
106  return extensions;
107 }
108 
109 //------------------------------------------------------------------------------
110 
112 {
113  return "Choose a directory with DICOM images";
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 ::fwMedData::SeriesDB::sptr SSeriesDBLazyReader::createSeriesDB(const ::boost::filesystem::path& dicomDir)
119 {
120  SLM_TRACE_FUNC();
121  ::vtkGdcmIO::SeriesDBLazyReader::sptr myLoader = ::vtkGdcmIO::SeriesDBLazyReader::New();
122  ::fwMedData::SeriesDB::sptr dummy = ::fwMedData::SeriesDB::New();
123  myLoader->setObject(dummy);
124  myLoader->setFolder(dicomDir);
125 
126  m_sigJobCreated->emit(myLoader->getJob());
127 
128  try
129  {
130  myLoader->read();
131  }
132  catch (const std::exception& e)
133  {
134  std::stringstream ss;
135  ss << "Warning during loading : " << e.what();
137  "Warning", ss.str(), ::fwGui::dialog::IMessageDialog::WARNING);
138  }
139  catch( ... )
140  {
142  "Warning", "Warning during loading", ::fwGui::dialog::IMessageDialog::WARNING);
143  }
144 
145  return myLoader->getConcreteObject();
146 }
147 
148 //------------------------------------------------------------------------------
149 
151 {
152  if( this->hasLocationDefined() )
153  {
154  ::fwMedData::SeriesDB::sptr seriesDB = createSeriesDB( this->getFolder() );
155 
156  if( seriesDB->size() > 0 )
157  {
158  // Retrieve dataStruct associated with this service
159  ::fwMedData::SeriesDB::sptr associatedSeriesDB =
160  this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
161  if (!associatedSeriesDB)
162  {
163  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
164  associatedSeriesDB = this->getObject< ::fwMedData::SeriesDB >();
165  }
166 
167  SLM_ASSERT("associated SeriesDB not instanced", associatedSeriesDB);
168  associatedSeriesDB->shallowCopy( seriesDB );
169 
170  ::fwGui::Cursor cursor;
171  cursor.setCursor(::fwGui::ICursor::BUSY);
172  this->notificationOfDBUpdate();
173  cursor.setDefaultCursor();
174  }
175  else
176  {
178  "Image Reader", "This file can not be read. Retry with another file reader.",
179  ::fwGui::dialog::IMessageDialog::WARNING);
180  }
181  }
182 }
183 
184 //------------------------------------------------------------------------------
185 
186 void SSeriesDBLazyReader::notificationOfDBUpdate()
187 {
188  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
189  if (!seriesDB)
190  {
191  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
192  seriesDB = this->getObject< ::fwMedData::SeriesDB >();
193  }
194  SLM_ASSERT("Unable to get seriesDB", seriesDB);
195 
196  ::fwMedData::SeriesDB::ContainerType addedSeries;
197  for( ::fwMedData::Series::sptr s : seriesDB->getContainer() )
198  {
199  addedSeries.push_back(s);
200  }
201 
202  auto sig = seriesDB->signal< ::fwMedData::SeriesDB::AddedSeriesSignalType >(
204  sig->asyncEmit(addedSeries);
205 }
206 
207 //-----------------------------------------------------------------------------
208 
210 {
211  return ::fwIO::FOLDER;
212 }
213 
214 //------------------------------------------------------------------------------
215 
216 } // namespace ioInr
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
IOVTKGDCM_API void info(std::ostream &_sstream) override
Override.
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
virtual IOVTKGDCM_API ExtensionsType getSupportedExtensions() override
Override.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
#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 IOVTKGDCM_API void configureWithIHM() override
Override.
virtual IOVTKGDCM_API void configuring() override
Override.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
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.
FWIO_APIconst::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IReader.cpp:97
virtual IOVTKGDCM_API void starting() override
Override.
IOVTKGDCM_API SSeriesDBLazyReader() noexcept
constructor
static FWMEDDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_SERIES_SIG
Type of signal when series are added.
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
ioVtkGdcm contains services to read and write Series using vtk and gdcm libraries.
#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
IOVTKGDCM_API::fwIO::IOPathType getIOPathType() const override
Return path type managed by the service, here FOLDER.
virtual IOVTKGDCM_API std::string getSelectorDialogTitle() override
Override.
IOVTKGDCM_API void updating() override
Override.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
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.
FWIO_API void setFolder(const ::boost::filesystem::path &folder)
Sets folder path.
Definition: IReader.cpp:106
FWGUI_API void setType(::fwGui::dialog::ILocationDialog::Types type) override
Set the type of location for the dialog (SINGLE_FILE, FORLDER, MULTI_FILES)
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
virtual IOVTKGDCM_API ~SSeriesDBLazyReader() noexcept
destructor
virtual IOVTKGDCM_API void stopping() override
Override.
This service reads a dicom dir in lazy mode, and provides a fwMedData::SeriesDB.
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.