fw4spl
ioVtkGdcm/src/ioVtkGdcm/SSeriesDBReader.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/SSeriesDBReader.hpp"
8 
9 #include <fwCom/HasSignals.hpp>
10 #include <fwCom/Signal.hpp>
11 #include <fwCom/Signal.hxx>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwGui/backend.hpp>
16 #include <fwGui/Cursor.hpp>
17 #include <fwGui/dialog/LocationDialog.hpp>
18 #include <fwGui/dialog/MessageDialog.hpp>
19 
20 #include <fwIO/IReader.hpp>
21 
22 #include <fwJobs/IJob.hpp>
23 #include <fwJobs/Job.hpp>
24 
25 #include <fwMedData/Series.hpp>
26 #include <fwMedData/SeriesDB.hpp>
27 
28 #include <fwServices/macros.hpp>
29 
30 #include <fwTools/ProgressToLogger.hpp>
31 
32 #include <vtkGdcmIO/SeriesDBReader.hpp>
33 
34 namespace ioVtkGdcm
35 {
36 
37 fwServicesRegisterMacro( ::fwIO::IReader, ::ioVtkGdcm::SSeriesDBReader, ::fwMedData::SeriesDB );
38 
39 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
40 
41 //------------------------------------------------------------------------------
42 
44 {
45  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
46 }
47 
48 //------------------------------------------------------------------------------
49 
51 {
52 }
53 
54 //------------------------------------------------------------------------------
55 
57 {
58  static ::boost::filesystem::path _sDefaultPath;
59 
61  dialogFile.setTitle(m_windowTitle.empty() ? this->getSelectorDialogTitle() : m_windowTitle);
62  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
63  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
64  dialogFile.setType(::fwGui::dialog::LocationDialog::FOLDER);
65 
66  ::fwData::location::Folder::sptr result;
67  result = ::fwData::location::Folder::dynamicCast( dialogFile.show() );
68  if (result)
69  {
70  _sDefaultPath = result->getFolder();
71  this->setFolder( result->getFolder() );
72  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
73  }
74 }
75 
76 //------------------------------------------------------------------------------
77 
79 {
81 }
82 
83 //------------------------------------------------------------------------------
84 
86 {
88 }
89 
90 //------------------------------------------------------------------------------
91 
93 {
95 }
96 
97 //------------------------------------------------------------------------------
98 
99 void SSeriesDBReader::info(std::ostream& _sstream )
100 {
101  _sstream << "SSeriesDBReader::info";
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 SSeriesDBReader::ExtensionsType SSeriesDBReader::getSupportedExtensions()
107 {
108  ExtensionsType extensions;
109  return extensions;
110 }
111 
112 //------------------------------------------------------------------------------
113 
115 {
116  return "Choose a directory with DICOM images";
117 }
118 
119 //------------------------------------------------------------------------------
120 
121 ::fwMedData::SeriesDB::sptr SSeriesDBReader::createSeriesDB(const ::boost::filesystem::path& dicomDir)
122 {
123  SLM_TRACE_FUNC();
124  ::vtkGdcmIO::SeriesDBReader::sptr reader = ::vtkGdcmIO::SeriesDBReader::New();
125  ::fwMedData::SeriesDB::sptr dummy = ::fwMedData::SeriesDB::New();
126  reader->setObject(dummy);
127  reader->setFolder(dicomDir);
128 
129  m_sigJobCreated->emit(reader->getJob());
130 
131  try
132  {
133  reader->read();
134  }
135  catch (const std::exception& e)
136  {
137  std::stringstream ss;
138  ss << "Warning during loading : " << e.what();
140  "Warning", ss.str(), ::fwGui::dialog::IMessageDialog::WARNING);
141  }
142  catch( ... )
143  {
145  "Warning", "Warning during loading", ::fwGui::dialog::IMessageDialog::WARNING);
146  }
147 
148  return reader->getConcreteObject();
149 }
150 
151 //------------------------------------------------------------------------------
152 
154 {
155  SLM_TRACE_FUNC();
156  if( this->hasLocationDefined() )
157  {
158  ::fwMedData::SeriesDB::sptr seriesDB = createSeriesDB( this->getFolder() );
159  if( seriesDB->size() > 0 )
160  {
161  if(this->isStopped()) // FIXME service might be stopped while updating in a worker
162  {
163  return;
164  }
165 
166  // Retrieve dataStruct associated with this service
167  ::fwMedData::SeriesDB::sptr associatedSeriesDB =
168  this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
169  if (!associatedSeriesDB)
170  {
171  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
172  associatedSeriesDB = this->getObject< ::fwMedData::SeriesDB >();
173  }
174  SLM_ASSERT("associated SeriesDB not instanced", associatedSeriesDB);
175  associatedSeriesDB->shallowCopy( seriesDB );
176 
177  ::fwGui::Cursor cursor;
178  cursor.setCursor(::fwGui::ICursor::BUSY);
179  this->notificationOfDBUpdate();
180  cursor.setDefaultCursor();
181  }
182  else
183  {
185  "Image Reader", "This file can not be read. Retry with another file reader.",
186  ::fwGui::dialog::IMessageDialog::WARNING);
187  }
188  }
189 }
190 
191 //------------------------------------------------------------------------------
192 
193 void SSeriesDBReader::notificationOfDBUpdate()
194 {
195  SLM_TRACE_FUNC();
196  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
197  if (!seriesDB)
198  {
199  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
200  seriesDB = this->getObject< ::fwMedData::SeriesDB >();
201  }
202  SLM_ASSERT("Unable to get seriesDB", seriesDB);
203 
204  ::fwMedData::SeriesDB::ContainerType addedSeries;
205  for( ::fwMedData::Series::sptr s : seriesDB->getContainer() )
206  {
207  addedSeries.push_back(s);
208  }
209 
210  auto sig = seriesDB->signal< ::fwMedData::SeriesDB::AddedSeriesSignalType >(
212  sig->asyncEmit(addedSeries);
213 }
214 
215 //-----------------------------------------------------------------------------
216 
218 {
219  return ::fwIO::FOLDER;
220 }
221 
222 //------------------------------------------------------------------------------
223 
224 } // namespace ioInr
virtual IOVTKGDCM_API void configuring() override
Override.
virtual IOVTKGDCM_API void stopping() override
Override.
#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.
FWSERVICES_API bool isStopped() const noexcept
Test if the service is stopped or not.
Definition: IService.cpp:390
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
IOVTKGDCM_API SSeriesDBReader() noexcept
constructor
#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 ~SSeriesDBReader() noexcept
destructor
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
IOVTKGDCM_API void info(std::ostream &_sstream) override
Override.
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 std::string getSelectorDialogTitle() override
Override.
virtual IOVTKGDCM_API void starting() override
Override.
static FWMEDDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_SERIES_SIG
Type of signal when series are added.
virtual IOVTKGDCM_API ExtensionsType getSupportedExtensions() override
Override.
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.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
IOVTKGDCM_API void updating() override
Override.
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
virtual IOVTKGDCM_API void configureWithIHM() override
Override.
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)
This service reads a dicom dir, and provides a fwMedData::SeriesDB.
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.