fw4spl
ioVtkGdcm/src/ioVtkGdcm/SImageSeriesWriter.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/SImageSeriesWriter.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 <fwData/location/Folder.hpp>
16 
17 #include <fwGui/backend.hpp>
18 #include <fwGui/Cursor.hpp>
19 #include <fwGui/dialog/LocationDialog.hpp>
20 #include <fwGui/dialog/MessageDialog.hpp>
21 
22 #include <fwIO/IWriter.hpp>
23 
24 #include <fwJobs/IJob.hpp>
25 #include <fwJobs/Job.hpp>
26 
27 #include <fwMedData/ImageSeries.hpp>
28 
29 #include <fwServices/macros.hpp>
30 
31 #include <vtkGdcmIO/ImageSeriesWriter.hpp>
32 
33 namespace ioVtkGdcm
34 {
35 
36 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioVtkGdcm::SImageSeriesWriter, ::fwMedData::ImageSeries );
37 
38 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
39 
40 //------------------------------------------------------------------------------
41 
43 {
44  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
45 }
46 
47 //------------------------------------------------------------------------------
48 
50 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
57  static ::boost::filesystem::path _sDefaultPath;
58 
60  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a directory for DICOM images" : m_windowTitle);
61  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
62  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
63  dialogFile.setType(::fwGui::dialog::LocationDialog::FOLDER);
64 
65  ::fwData::location::Folder::sptr result;
66  result = ::fwData::location::Folder::dynamicCast( dialogFile.show() );
67  if (result)
68  {
69  _sDefaultPath = result->getFolder();
70  this->setFolder( result->getFolder() );
71  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
72  }
73  else
74  {
75  this->clearLocations();
76  }
77 }
78 
79 //------------------------------------------------------------------------------
80 
82 {
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89 }
90 
91 //------------------------------------------------------------------------------
92 
94 {
96 }
97 
98 //------------------------------------------------------------------------------
99 
101 {
102  if( this->hasLocationDefined() )
103  {
104  // Retrieve dataStruct associated with this service
105  ::fwMedData::ImageSeries::csptr series = this->getInput< ::fwMedData::ImageSeries >(::fwIO::s_DATA_KEY);
106  if (!series)
107  {
108  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
109  series = this->getObject< ::fwMedData::ImageSeries >();
110  }
111  SLM_ASSERT("ImageSeries is not instanced", series);
112 
113  const ::boost::filesystem::path& folder = this->getFolder();
114  if(!::boost::filesystem::is_empty(folder))
115  {
117  dialog.setMessage("Folder '"+folder.string()+"' isn't empty, files can be overwritten."
118  "\nDo you want to continue ?");
119  dialog.setTitle("Folder not empty.");
120  dialog.setIcon(::fwGui::dialog::MessageDialog::QUESTION);
121  dialog.addButton( ::fwGui::dialog::MessageDialog::YES_NO );
123 
124  if(button == ::fwGui::dialog::MessageDialog::NO)
125  {
126  return;
127  }
128  }
129 
130  if (series->getModality() == "OT")
131  {
133  dialog.setMessage("Series modality is '" + series->getModality() + "' some information can be lost."
134  "\nDo you want to continue ?");
135  dialog.setTitle("Series modality.");
136  dialog.setIcon(::fwGui::dialog::MessageDialog::QUESTION);
137  dialog.addButton( ::fwGui::dialog::MessageDialog::YES_NO );
139 
140  if(button == ::fwGui::dialog::MessageDialog::NO)
141  {
142  return;
143  }
144  }
145  ::fwGui::Cursor cursor;
146  cursor.setCursor(::fwGui::ICursor::BUSY);
147  this->saveImageSeries(folder, series);
148  cursor.setDefaultCursor();
149  }
150 }
151 
152 //------------------------------------------------------------------------------
153 
154 void SImageSeriesWriter::saveImageSeries( const ::boost::filesystem::path folder,
155  ::fwMedData::ImageSeries::csptr series )
156 {
157  ::vtkGdcmIO::ImageSeriesWriter::sptr writer = ::vtkGdcmIO::ImageSeriesWriter::New();
158 
159  writer->setObject(series);
160  ::fwData::location::Folder::sptr loc = ::fwData::location::Folder::New();
161  loc->setFolder(folder);
162  writer->setLocation(loc);
163 
164  m_sigJobCreated->emit(writer->getJob());
165 
166  try
167  {
168  writer->write();
169  }
170  catch (const std::exception& e)
171  {
172  std::stringstream ss;
173  ss << "Warning during saving : " << e.what();
175  "Warning", ss.str(), ::fwGui::dialog::IMessageDialog::WARNING);
176  }
177  catch( ... )
178  {
180  "Warning", "Warning during saving", ::fwGui::dialog::IMessageDialog::WARNING);
181  }
182 }
183 
184 //-----------------------------------------------------------------------------
185 
187 {
188  return ::fwIO::FOLDER;
189 }
190 
191 //------------------------------------------------------------------------------
192 
193 } // namespace ioVtkGdcm
#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.
IOVTKGDCM_API void updating() override
Write the ImageSeries in DICOM format.
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
Defines the generic message box for IHM. Use the Delegate design pattern.
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual IOVTKGDCM_API ~SImageSeriesWriter() noexcept
destructor
virtual IOVTKGDCM_API void configureWithIHM() override
Propose select a directory where to save the DICOM files.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
IOVTKGDCM_API SImageSeriesWriter() noexcept
constructor
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.
virtual IOVTKGDCM_API void configuring() override
Does nothing.
IOVTKGDCM_API::fwIO::IOPathType getIOPathType() const override
Return path type managed by the service, here FOLDER.
virtual IOVTKGDCM_API void starting() override
Does nothing.
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
ioVtkGdcm contains services to read and write Series using vtk and gdcm libraries.
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IWriter.cpp:115
#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::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IWriter.cpp:75
Services to write an ImageSeries in DICOM format.
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
Writer service API. It manages extension points definition and extension configuration.
Definition: IWriter.hpp:33
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: IWriter.cpp:84
virtual IOVTKGDCM_API void stopping() override
Does nothing.
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 FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IWriter.cpp:184
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IWriter.hpp:171