fw4spl
ioVTK/src/ioVTK/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 "ioVTK/SSeriesDBReader.hpp"
8 
9 #include <fwCom/HasSignals.hpp>
10 #include <fwCom/Signal.hpp>
11 #include <fwCom/Signal.hxx>
12 
13 #include <fwData/location/Folder.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 <fwJobs/IJob.hpp>
22 #include <fwJobs/Job.hpp>
23 
24 #include <fwMedData/SeriesDB.hpp>
25 
26 #include <fwMedDataTools/helper/SeriesDB.hpp>
27 
28 #include <fwServices/macros.hpp>
29 
30 #include <fwVtkIO/SeriesDBReader.hpp>
31 
32 #include <boost/filesystem/operations.hpp>
33 
34 namespace ioVTK
35 {
36 
37 fwServicesRegisterMacro( ::fwIO::IReader, ::ioVTK::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  return ::fwIO::FILES;
53 }
54 
55 //------------------------------------------------------------------------------
56 
58 {
59  static ::boost::filesystem::path _sDefaultPath("");
60 
62  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
63  dialogFile.setType(::fwGui::dialog::ILocationDialog::MULTI_FILES);
64  dialogFile.setTitle(m_windowTitle.empty() ? "Choose vtk files to load Series" : m_windowTitle);
65  dialogFile.addFilter("Vtk", "*.vtk *.vti *.mhd *.vtu");
66  dialogFile.addFilter("Vtk files", "*.vtk");
67  dialogFile.addFilter("Vti files", "*.vti");
68  dialogFile.addFilter("Vtu files", "*.vtu");
69  dialogFile.addFilter("MetaImage files", "*.mhd");
70  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
71  dialogFile.setOption(::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
72 
73  ::fwData::location::MultiFiles::sptr result;
74  result = ::fwData::location::MultiFiles::dynamicCast( dialogFile.show() );
75  if (result)
76  {
77  const ::fwData::location::ILocation::VectPathType paths = result->getPaths();
78  if(!paths.empty())
79  {
80  _sDefaultPath = paths[0].parent_path();
81  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
82  }
83  this->setFiles(paths);
84  }
85  else
86  {
87  this->clearLocations();
88  }
89 }
90 
91 //------------------------------------------------------------------------------
92 
94 {
96 }
97 
98 //------------------------------------------------------------------------------
99 
101 {
102  SLM_TRACE_FUNC();
103 }
104 
105 //------------------------------------------------------------------------------
106 
108 {
110 }
111 
112 //------------------------------------------------------------------------------
113 
114 void SSeriesDBReader::info(std::ostream& _sstream )
115 {
116  _sstream << "SSeriesDBReader::info";
117 }
118 
119 //------------------------------------------------------------------------------
120 
121 void SSeriesDBReader::loadSeriesDB( const ::fwData::location::ILocation::VectPathType& vtkFiles,
122  const ::fwMedData::SeriesDB::sptr& seriesDB )
123 {
124  ::fwVtkIO::SeriesDBReader::sptr reader = ::fwVtkIO::SeriesDBReader::New();
125  reader->setObject(seriesDB);
126  reader->setFiles(vtkFiles);
127 
128  m_sigJobCreated->emit(reader->getJob());
129 
130  try
131  {
132  reader->read();
133  }
134  catch (const std::exception& e)
135  {
136  std::stringstream ss;
137  ss << "Warning during loading : " << e.what();
138 
140  "Warning",
141  ss.str(),
142  ::fwGui::dialog::IMessageDialog::WARNING);
143  }
144  catch( ... )
145  {
146  std::stringstream ss;
147  ss << "Warning during loading. ";
149  "Warning",
150  "Warning during loading.",
151  ::fwGui::dialog::IMessageDialog::WARNING);
152  }
153 }
154 
155 //------------------------------------------------------------------------------
156 
158 {
159  if( this->hasLocationDefined() )
160  {
161  // Retrieve dataStruct associated with this service
162  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
163  if (!seriesDB)
164  {
165  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
166  seriesDB = this->getObject< ::fwMedData::SeriesDB >();
167  }
168  SLM_ASSERT("SeriesDB not instanced", seriesDB);
169 
170  ::fwMedData::SeriesDB::sptr localSeriesDB = ::fwMedData::SeriesDB::New();
171 
172  ::fwGui::Cursor cursor;
173  cursor.setCursor(::fwGui::ICursor::BUSY);
174 
175  this->loadSeriesDB(this->getFiles(), localSeriesDB);
176 
177  ::fwMedDataTools::helper::SeriesDB sdbHelper(seriesDB);
178  sdbHelper.clear();
179  // Notify removal.
180  sdbHelper.notify();
181 
182  {
183  ::fwData::mt::ObjectWriteLock lock(seriesDB);
184  seriesDB->shallowCopy(localSeriesDB);
185  }
186 
187  ::fwMedData::SeriesDB::ContainerType addedSeries = seriesDB->getContainer();
188 
189  auto sig = seriesDB->signal< ::fwMedData::SeriesDB::AddedSeriesSignalType >(
191  sig->asyncEmit(addedSeries);
192 
193  cursor.setDefaultCursor();
194  }
195 }
196 
197 //------------------------------------------------------------------------------
198 
199 } // namespace ioVtk
#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
IOVTK_API SSeriesDBReader() noexcept
Constructor. Do nothing.
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...
#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 IOVTK_API void starting() override
Starting method.
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...
virtual IOVTK_API void configureWithIHM() override
Configure the vtk file path.
FWMEDDATATOOLS_API void clear()
Clear all series in the SeriesDB.
static FWMEDDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_SERIES_SIG
Type of signal when series are added.
IOVTK_API void updating() override
Updating method.
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
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.
virtual IOVTK_API void stopping() override
Stopping method.
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.
virtual IOVTK_API void configuring() override
Configuring method.
IOVTK_API void info(std::ostream &_sstream) override
Info method.
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 ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.