fw4spl
SDicomSeriesWriter.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 "ioGdcm/SDicomSeriesWriter.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/location/Folder.hpp>
16 
17 #include <fwGdcmIO/helper/DicomSeriesWriter.hpp>
18 
19 #include <fwGui/Cursor.hpp>
20 #include <fwGui/dialog/LocationDialog.hpp>
21 #include <fwGui/dialog/MessageDialog.hpp>
22 #include <fwGui/dialog/ProgressDialog.hpp>
23 
24 #include <fwIO/IWriter.hpp>
25 
26 #include <fwJobs/IJob.hpp>
27 #include <fwJobs/Observer.hpp>
28 
29 #include <fwMedData/DicomSeries.hpp>
30 
31 #include <fwServices/macros.hpp>
32 
33 #include <fwTools/ProgressToLogger.hpp>
34 
35 namespace ioGdcm
36 {
37 
38 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioGdcm::SDicomSeriesWriter, ::fwMedData::DicomSeries );
39 
40 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
41 
42 //------------------------------------------------------------------------------
43 
45  m_cancelled(false)
46 {
47  m_sigJobCreated = newSignal<JobCreatedSignal>(JOB_CREATED_SIGNAL);
48 }
49 
50 //------------------------------------------------------------------------------
51 
53 {
54 }
55 
56 //------------------------------------------------------------------------------
57 
59 {
60  static ::boost::filesystem::path _sDefaultPath;
61 
63  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a directory for DICOM images" : m_windowTitle);
64  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
65  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
66  dialogFile.setType(::fwGui::dialog::LocationDialog::FOLDER);
67 
68  ::fwData::location::Folder::sptr result;
69  result = ::fwData::location::Folder::dynamicCast( dialogFile.show() );
70  if (result)
71  {
72  _sDefaultPath = result->getFolder();
73  this->setFolder( result->getFolder() );
74  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
75  }
76  else
77  {
78  this->clearLocations();
79  }
80 }
81 
82 //------------------------------------------------------------------------------
83 
85 {
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
92 }
93 
94 //------------------------------------------------------------------------------
95 
97 {
99 }
100 
101 //------------------------------------------------------------------------------
102 
104 {
105  if( this->hasLocationDefined() )
106  {
107  // Retrieve dataStruct associated with this service
108  ::fwMedData::DicomSeries::csptr series = this->getInput< ::fwMedData::DicomSeries >(::fwIO::s_DATA_KEY);
109  const ::boost::filesystem::path& folder = this->getFolder();
110  if(!::boost::filesystem::is_empty(folder))
111  {
113  dialog.setMessage("Folder '"+folder.string()+"' isn't empty, files can be overwritten."
114  "\nDo you want to continue ?");
115  dialog.setTitle("Folder not empty.");
116  dialog.setIcon(::fwGui::dialog::MessageDialog::QUESTION);
117  dialog.addButton( ::fwGui::dialog::MessageDialog::YES_NO );
119 
120  if(button == ::fwGui::dialog::MessageDialog::NO)
121  {
122  return;
123  }
124  }
125 
126  if (series->getModality() == "OT")
127  {
129  dialog.setMessage("Series modality is '" + series->getModality() + "' some information can be lost."
130  "\nDo you want to continue ?");
131  dialog.setTitle("Series modality.");
132  dialog.setIcon(::fwGui::dialog::MessageDialog::QUESTION);
133  dialog.addButton( ::fwGui::dialog::MessageDialog::YES_NO );
135 
136  if(button == ::fwGui::dialog::MessageDialog::NO)
137  {
138  return;
139  }
140  }
141  ::fwGui::Cursor cursor;
142  cursor.setCursor(::fwGui::ICursor::BUSY);
143  this->saveDicomSeries(folder, series);
144  cursor.setDefaultCursor();
145  }
146 }
147 
148 //------------------------------------------------------------------------------
149 
150 void SDicomSeriesWriter::saveDicomSeries( const ::boost::filesystem::path folder,
151  const ::fwMedData::DicomSeries::csptr& series ) const
152 {
153  ::fwGdcmIO::helper::DicomSeriesWriter::sptr writer = ::fwGdcmIO::helper::DicomSeriesWriter::New();
154 
155  writer->setObject(series);
156  ::fwData::location::Folder::sptr loc = ::fwData::location::Folder::New();
157  loc->setFolder(folder);
158  writer->setLocation(loc);
159  m_sigJobCreated->emit(writer->getJob());
160 
161  try
162  {
163  ::fwGui::dialog::ProgressDialog progressMeterGUI("Saving series ");
164  writer->write();
165  }
166  catch (const std::exception& e)
167  {
168  std::stringstream ss;
169  ss << "Warning during saving : " << e.what();
171  "Warning", ss.str(), ::fwGui::dialog::IMessageDialog::WARNING);
172  }
173  catch( ... )
174  {
176  "Warning", "Warning during saving", ::fwGui::dialog::IMessageDialog::WARNING);
177  }
178 }
179 
180 //-----------------------------------------------------------------------------
181 
183 {
184  return ::fwIO::FOLDER;
185 }
186 
187 //------------------------------------------------------------------------------
188 
189 } // namespace ioGdcm
virtual IOGDCM_API void configuring() override
Configuring method. This method is used to configure the service.
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
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 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.
Services to write an DicomSeries in DICOM format.
Defines the generic progress dialog for IHM. Use the Delegate design pattern. The specific implementa...
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
IOGDCM_API void updating() override
Write the DicomSeries in DICOM format.
IOGDCM_API::fwIO::IOPathType getIOPathType() const override
Return path type managed by the service, here FOLDER.
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
virtual IOGDCM_API void stopping() override
Does nothing.
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IWriter.cpp:115
FWIO_APIconst::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IWriter.cpp:75
IOGDCM_API SDicomSeriesWriter() noexcept
constructor
virtual IOGDCM_API void configureWithIHM() override
Propose select a directory where to save the DICOM files.
virtual IOGDCM_API ~SDicomSeriesWriter() noexcept override
destructor
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
ioGdcm contains services use to deal with DICOM using the GDCM library.
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
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 IOGDCM_API void starting() override
Does nothing.
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