fw4spl
SInrSeriesDBReader.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/SInrSeriesDBReader.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/Image.hpp>
12 #include <fwData/location/Folder.hpp>
13 #include <fwData/location/MultiFiles.hpp>
14 #include <fwData/mt/ObjectWriteLock.hpp>
15 
16 #include <fwGui/Cursor.hpp>
17 #include <fwGui/dialog/LocationDialog.hpp>
18 #include <fwGui/dialog/MessageDialog.hpp>
19 #include <fwGui/dialog/ProgressDialog.hpp>
20 
21 #include <fwIO/IReader.hpp>
22 
23 #include <fwItkIO/ImageReader.hpp>
24 
25 #include <fwMedData/Equipment.hpp>
26 #include <fwMedData/ImageSeries.hpp>
27 #include <fwMedData/Patient.hpp>
28 #include <fwMedData/SeriesDB.hpp>
29 #include <fwMedData/Study.hpp>
30 
31 #include <fwMedDataTools/helper/SeriesDB.hpp>
32 
33 #include <fwServices/macros.hpp>
34 
35 #include <fwTools/dateAndTime.hpp>
36 #include <fwTools/UUID.hpp>
37 
38 #include <boost/date_time/posix_time/posix_time.hpp>
39 
40 namespace ioITK
41 {
42 
43 fwServicesRegisterMacro( ::fwIO::IReader, ::ioITK::SInrSeriesDBReader, ::fwMedData::SeriesDB );
44 
45 //------------------------------------------------------------------------------
46 
47 SInrSeriesDBReader::SInrSeriesDBReader() noexcept
48 {
49 }
50 
51 //------------------------------------------------------------------------------
52 
53 SInrSeriesDBReader::~SInrSeriesDBReader() noexcept
54 {
55 }
56 
57 //------------------------------------------------------------------------------
58 
60 {
61  return ::fwIO::FILES;
62 }
63 
64 //------------------------------------------------------------------------------
65 
67 {
69 }
70 
71 //------------------------------------------------------------------------------
72 
74 {
76  static ::boost::filesystem::path _sDefaultPath;
77 
79  dialogFile.setTitle(m_windowTitle.empty() ? "Choose an Inrimage file" : m_windowTitle);
80  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
81  dialogFile.addFilter("Inrimage", "*.inr.gz");
82  dialogFile.setType(::fwGui::dialog::ILocationDialog::MULTI_FILES);
83  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
84  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
85 
86  ::fwData::location::MultiFiles::sptr result;
87  result = ::fwData::location::MultiFiles::dynamicCast( dialogFile.show() );
88  if (result)
89  {
90  const ::fwData::location::ILocation::VectPathType paths = result->getPaths();
91  if(!paths.empty())
92  {
93  _sDefaultPath = paths[0].parent_path();
94  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
95  }
96  this->setFiles(paths);
97  }
98  else
99  {
100  this->clearLocations();
101  }
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 bool SInrSeriesDBReader::createImage( const ::boost::filesystem::path inrFile, ::fwData::Image::sptr image )
107 {
108  SLM_TRACE_FUNC();
109  ::fwItkIO::ImageReader::sptr myLoader = ::fwItkIO::ImageReader::New();
110  bool ok = true;
111 
112  myLoader->setObject(image);
113  myLoader->setFile(inrFile);
114 
115  try
116  {
117  ::fwGui::dialog::ProgressDialog progressMeterGUI("Loading Image ");
118  myLoader->addHandler( progressMeterGUI );
119  myLoader->read();
120  }
121  catch (const std::exception& e)
122  {
123  std::stringstream ss;
124  ss << "Warning during loading : " << e.what();
126  ss.str(),
127  ::fwGui::dialog::IMessageDialog::WARNING);
128  ok = false;
129  }
130  catch( ... )
131  {
133  "Warning during loading",
134  ::fwGui::dialog::IMessageDialog::WARNING);
135  ok = false;
136  }
137  return ok;
138 }
139 
140 //------------------------------------------------------------------------------
141 
143 {
144  SLM_TRACE_FUNC();
145 
146  if( this->hasLocationDefined() )
147  {
148  // Retrieve dataStruct associated with this service
149  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
150  if (!seriesDB)
151  {
152  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
153  seriesDB = this->getObject< ::fwMedData::SeriesDB >();
154  }
155  SLM_ASSERT("SeriesDB not instanced", seriesDB);
156 
157  ::fwMedData::SeriesDB::sptr localSeriesDB = ::fwMedData::SeriesDB::New();
158 
159  ::fwGui::Cursor cursor;
160  cursor.setCursor(::fwGui::ICursor::BUSY);
161 
162  const std::string instanceUID = ::fwTools::UUID::generateUUID();
163 
164  for(const ::boost::filesystem::path& path : this->getFiles())
165  {
166  ::fwMedData::ImageSeries::sptr imgSeries = ::fwMedData::ImageSeries::New();
167  this->initSeries(imgSeries, instanceUID);
168 
169  ::fwData::Image::sptr image = ::fwData::Image::New();
170  this->createImage( path, image );
171  imgSeries->setImage(image);
172 
173  localSeriesDB->getContainer().push_back(imgSeries);
174  }
175 
176  ::fwMedDataTools::helper::SeriesDB sDBhelper(seriesDB);
177 
178  ::fwData::mt::ObjectWriteLock lock(seriesDB);
179  sDBhelper.merge(localSeriesDB);
180  sDBhelper.notify();
181 
182  cursor.setDefaultCursor();
183  }
184 }
185 
186 //------------------------------------------------------------------------------
187 
188 void SInrSeriesDBReader::initSeries(::fwMedData::Series::sptr series, const std::string& instanceUID)
189 {
190  const std::string unknown = "unknown";
191  series->setModality("OT");
192  ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
193  const std::string date = ::fwTools::getDate(now);
194  const std::string time = ::fwTools::getTime(now);
195  series->setDate(date);
196  series->setTime(time);
197  //series->setDescription(??);
198  //series->setPerformingPhysiciansName(??);
199 
200  series->getEquipment()->setInstitutionName(unknown);
201 
202  series->getPatient()->setName(unknown);
203  series->getPatient()->setPatientId(unknown);
204  series->getPatient()->setBirthdate(unknown);
205  series->getPatient()->setSex(unknown);
206 
207  series->getStudy()->setInstanceUID(instanceUID);
208  series->getStudy()->setDate(date);
209  series->getStudy()->setTime(time);
210  series->getStudy()->setReferringPhysicianName(unknown);
211  series->getStudy()->setDescription(unknown);
212  series->getStudy()->setPatientAge(unknown);
213 }
214 
215 //------------------------------------------------------------------------------
216 
217 } // namespace ioITK
virtual void configuring() override
Calls base class implementation.
#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
virtual IOITK_API void updating() override
Reads inr files specified by user (configure or configureWithIHM) and pushes them into SeriesDB...
#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)
FWIO_API void setFiles(const ::fwIO::LocationsType &files)
Sets file paths.
Definition: IReader.cpp:89
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.
A helper to lock object on exclusive mode.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
Defines an helper to modify an fwMedData::SeriesDB and create in parallel the message to announce thi...
Defines the generic progress dialog for IHM. Use the Delegate design pattern. The specific implementa...
IOITK_API::fwIO::IOPathType getIOPathType() const override
Returns managed file type, here FILES.
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
Reads inr files and pushes them into SeriesDB.
#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
virtual IOITK_API void configureWithIHM() override
Configure the inr files path.
FWIO_APIconst::fwIO::LocationsType & getFiles() const
Returns file paths set by the user or set during service configuration.
Definition: IReader.cpp:80
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.
FWMEDDATATOOLS_API void notify()
Send the signal of modification.
Defines the generic cursor for IHM. Use the Delegate design pattern.
FWGUI_API void setType(::fwGui::dialog::ILocationDialog::Types type) override
Set the type of location for the dialog (SINGLE_FILE, FORLDER, MULTI_FILES)
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.
FWMEDDATATOOLS_API void merge(::fwMedData::SeriesDB::sptr seriesDBIn)
Merge seriesDBIn all series from seriesDBIn to the SeriesDB.
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114