fw4spl
SImageSeriesReader.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 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 "ioVTK/SImageSeriesReader.hpp"
8 
9 #include "ioVTK/SImageReader.hpp"
10 
11 #include <fwCom/Signal.hxx>
12 
13 #include <fwData/Image.hpp>
14 #include <fwData/location/Folder.hpp>
15 #include <fwData/location/SingleFile.hpp>
16 
17 #include <fwGui/Cursor.hpp>
18 #include <fwGui/dialog/LocationDialog.hpp>
19 #include <fwGui/dialog/MessageDialog.hpp>
20 #include <fwGui/dialog/ProgressDialog.hpp>
21 
22 #include <fwIO/ioTypes.hpp>
23 #include <fwIO/IReader.hpp>
24 
25 #include <fwJobs/IJob.hpp>
26 #include <fwJobs/Job.hpp>
27 
28 #include <fwMedData/Equipment.hpp>
29 #include <fwMedData/ImageSeries.hpp>
30 #include <fwMedData/Patient.hpp>
31 #include <fwMedData/Study.hpp>
32 
33 #include <fwServices/macros.hpp>
34 
35 #include <fwTools/dateAndTime.hpp>
36 #include <fwTools/Failed.hpp>
37 #include <fwTools/Os.hpp>
38 #include <fwTools/UUID.hpp>
39 
40 #include <fwVtkIO/BitmapImageReader.hpp>
41 
42 #include <boost/date_time/posix_time/posix_time.hpp>
43 #include <boost/filesystem/operations.hpp>
44 
45 namespace ioVTK
46 {
47 
48 fwServicesRegisterMacro( ::fwIO::IReader, ::ioVTK::SImageSeriesReader, ::fwMedData::ImageSeries );
49 
50 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
51 
52 //------------------------------------------------------------------------------
53 
55 {
56  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
57 }
58 
59 //------------------------------------------------------------------------------
60 
62 {
63  return ::fwIO::FILE;
64 }
65 
66 //------------------------------------------------------------------------------
67 
69 {
70  static ::boost::filesystem::path _sDefaultPath;
71 
72  // Initialize the available extensions for BitmapImageReader
73  std::vector<std::string> ext;
75  std::string availableExtensions = "";
76 
77  if(ext.size() > 0)
78  {
79  availableExtensions = "*" + ext.at(0);
80  for(size_t i = 1; i < ext.size(); i++)
81  {
82  availableExtensions = availableExtensions + " *" + ext.at(i);
83  }
84  }
85 
87  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a file to load an ImageSeries" : m_windowTitle);
88  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
89  dialogFile.addFilter("Vtk", "*.vtk");
90  dialogFile.addFilter("Vti", "*.vti");
91  dialogFile.addFilter("MetaImage", "*.mhd");
92  dialogFile.addFilter("Bitmap image", availableExtensions);
93  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
94  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
95 
96  ::fwData::location::SingleFile::sptr result;
97  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
98  if (result)
99  {
100  _sDefaultPath = result->getPath().parent_path();
101  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
102  this->setFile(result->getPath());
103  }
104  else
105  {
106  this->clearLocations();
107  }
108 }
109 
110 //------------------------------------------------------------------------------
111 
113 {
114 }
115 
116 //------------------------------------------------------------------------------
117 
119 {
120 }
121 
122 //------------------------------------------------------------------------------
123 
125 {
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 void SImageSeriesReader::info(std::ostream& _sstream )
132 {
133  _sstream << "SImageSeriesReader::info";
134 }
135 
136 //------------------------------------------------------------------------------
137 
138 void initSeries(::fwMedData::Series::sptr series)
139 {
140  const std::string instanceUID = ::fwTools::UUID::generateUUID();
141  const std::string unknown = "unknown";
142  const ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
143  const std::string date = ::fwTools::getDate(now);
144  const std::string time = ::fwTools::getTime(now);
145 
146  series->setModality("OT");
147  series->setDate(date);
148  series->setTime(time);
149  series->setDescription("Image imported with VTK");
150  ::fwMedData::DicomValuesType physicians = series->getPerformingPhysiciansName();
151  if(physicians.empty())
152  {
153  const std::string username = ::fwTools::os::getEnv("USERNAME", ::fwTools::os::getEnv("LOGNAME", "Unknown"));
154  physicians.push_back(username);
155  }
156  series->setPerformingPhysiciansName(physicians);
157  series->getEquipment()->setInstitutionName(unknown);
158 
159  series->getPatient()->setName(unknown);
160  series->getPatient()->setPatientId(unknown);
161  series->getPatient()->setBirthdate(unknown);
162  series->getPatient()->setSex(unknown);
163 
164  series->getStudy()->setInstanceUID(instanceUID);
165  series->getStudy()->setDate(date);
166  series->getStudy()->setTime(time);
167  series->getStudy()->setReferringPhysicianName(unknown);
168  series->getStudy()->setDescription(unknown);
169  series->getStudy()->setPatientAge(unknown);
170 }
171 
172 //------------------------------------------------------------------------------
173 
175 {
176  if( this->hasLocationDefined() )
177  {
178  // Retrieve dataStruct associated with this service
179  ::fwMedData::ImageSeries::sptr imageSeries = this->getInOut< ::fwMedData::ImageSeries >(::fwIO::s_DATA_KEY);
180  SLM_ASSERT("ImageSeries is not instanced", imageSeries);
181 
182  ::fwGui::Cursor cursor;
183  cursor.setCursor(::fwGui::ICursor::BUSY);
184 
185  try
186  {
187  ::fwData::Image::sptr image = ::fwData::Image::New();
188 
189  if ( SImageReader::loadImage( this->getFile(), image, m_sigJobCreated ) )
190  {
191  imageSeries->setImage(image);
192  initSeries(imageSeries);
193  this->notificationOfDBUpdate();
194  }
195  }
196  catch(::fwTools::Failed& e)
197  {
198  OSLM_TRACE("Error : " << e.what());
199  FW_RAISE_EXCEPTION(e);
200  }
201  cursor.setDefaultCursor();
202  }
203 }
204 
205 //------------------------------------------------------------------------------
206 
207 void SImageSeriesReader::notificationOfDBUpdate()
208 {
209  ::fwMedData::ImageSeries::sptr imageSeries = this->getInOut< ::fwMedData::ImageSeries >(::fwIO::s_DATA_KEY);
210  SLM_ASSERT("imageSeries not instanced", imageSeries);
211 
213  {
214  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
215  sig->asyncEmit();
216  }
217 }
218 
219 //------------------------------------------------------------------------------
220 
221 } // namespace ioVtk
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
static FWVTKIO_API void getAvailableExtensions(std::vector< std::string > &ext)
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
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
virtual IOVTK_API void stopping() override
Stopping method.
IOVTK_API SImageSeriesReader() noexcept
Constructor. Do nothing.
static IOVTK_API bool loadImage(const ::boost::filesystem::path &vtkFile, const std::shared_ptr< ::fwData::Image > &image, const std::shared_ptr< JobCreatedSignalType > &sigJobCreated)
This method is used to load an vtk image using a file path.
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.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IReader.cpp:62
virtual IOVTK_API::fwIO::IOPathType getIOPathType() const override
This method must be implemented by concrete service readers that use path file system to read data...
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
FWTOOLS_API std::string getEnv(const std::string &name, bool *ok=NULL)
Returns a environment variable value.
Definition: Os.cpp:20
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
Implements a failed exception class.
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.
virtual IOVTK_API void configuring() override
Configuring method.
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)
IOVTK_API void info(std::ostream &_sstream) override
Info method.
virtual IOVTK_API void configureWithIHM() override
Configure the image path.
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
virtual IOVTK_API void starting() override
Starting method.
IOVTK_API void updating() override
Updating method.
VTK ImageSeries Reader.
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114