fw4spl
ioVTK/src/ioVTK/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 "ioVTK/SImageSeriesWriter.hpp"
8 
9 #include "ioVTK/SImageWriter.hpp"
10 
11 #include <fwCom/HasSignals.hpp>
12 #include <fwCom/Signal.hpp>
13 #include <fwCom/Signal.hxx>
14 
15 #include <fwCore/base.hpp>
16 
17 #include <fwData/Image.hpp>
18 #include <fwData/location/Folder.hpp>
19 #include <fwData/location/SingleFile.hpp>
20 
21 #include <fwDataIO/reader/IObjectReader.hpp>
22 
23 #include <fwGui/Cursor.hpp>
24 #include <fwGui/dialog/LocationDialog.hpp>
25 #include <fwGui/dialog/MessageDialog.hpp>
26 #include <fwGui/dialog/ProgressDialog.hpp>
27 
28 #include <fwIO/IWriter.hpp>
29 
30 #include <fwJobs/IJob.hpp>
31 #include <fwJobs/Job.hpp>
32 
33 #include <fwMedData/ImageSeries.hpp>
34 
35 #include <fwServices/macros.hpp>
36 
37 #include <fwTools/Failed.hpp>
38 
39 #include <fwVtkIO/ImageWriter.hpp>
40 #include <fwVtkIO/MetaImageWriter.hpp>
41 #include <fwVtkIO/VtiImageWriter.hpp>
42 
43 #include <boost/algorithm/string.hpp>
44 
45 namespace ioVTK
46 {
47 
48 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioVTK::SImageSeriesWriter, ::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 {
71  static ::boost::filesystem::path _sDefaultPath("");
72 
74  dialogFile.setTitle(m_windowTitle.empty() ? "Choose an file to save an image" : m_windowTitle);
75  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
76  dialogFile.addFilter("Vtk", "*.vtk");
77  dialogFile.addFilter("Vti", "*.vti");
78  dialogFile.addFilter("MetaImage", "*.mhd");
79  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
80 
81  ::fwData::location::SingleFile::sptr result;
82  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
83  if (result)
84  {
85  _sDefaultPath = result->getPath().parent_path();
86  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
87  this->setFile(result->getPath());
88  }
89  else
90  {
91  this->clearLocations();
92  }
93 
94 }
95 
96 //------------------------------------------------------------------------------
97 
99 {
100  SLM_TRACE_FUNC();
101 }
102 
103 //------------------------------------------------------------------------------
104 
106 {
107  SLM_TRACE_FUNC();
108 }
109 
110 //------------------------------------------------------------------------------
111 
113 {
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 void SImageSeriesWriter::info(std::ostream& _sstream )
120 {
121  _sstream << "SImageSeriesWriter::info";
122 }
123 
124 //------------------------------------------------------------------------------
125 
127 {
128  SLM_TRACE_FUNC();
129 
130  if( this->hasLocationDefined() )
131  {
132  // Retrieve dataStruct associated with this service
133  ::fwMedData::ImageSeries::csptr imageSeries = this->getInput< ::fwMedData::ImageSeries >(::fwIO::s_DATA_KEY);
134  if (!imageSeries)
135  {
136  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
137  imageSeries = this->getObject< ::fwMedData::ImageSeries >();
138  }
139  SLM_ASSERT("ImageSeries is not instanced", imageSeries);
140  SLM_ASSERT("Image from ImageSeries is not instanced", imageSeries->getImage());
141 
142  ::fwGui::Cursor cursor;
143  cursor.setCursor(::fwGui::ICursor::BUSY);
144 
145  try
146  {
147  SImageWriter::saveImage(this->getFile(), imageSeries->getImage(), m_sigJobCreated);
148  }
149  catch(::fwTools::Failed& e)
150  {
151  OSLM_TRACE("Error : " << e.what());
152  FW_RAISE_EXCEPTION(e);
153  }
154  cursor.setDefaultCursor();
155  }
156 }
157 
158 //------------------------------------------------------------------------------
159 
160 } // 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.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual IOVTK_API void starting() override
Starting method.
IOVTK_API void info(std::ostream &_sstream) override
Info method.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
IOVTK_API void updating() override
Updating method.
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:
IOVTK_API SImageSeriesWriter() noexcept
Constructor. Do nothing.
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IWriter.cpp:49
virtual IOVTK_API void configureWithIHM() override
Configure the image path.
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IWriter.cpp:40
static IOVTK_API bool saveImage(const ::boost::filesystem::path &imgFile, const std::shared_ptr< const ::fwData::Image > &image, const std::shared_ptr< JobCreatedSignalType > &sigJobCreated)
Save a VTK image.
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
virtual IOVTK_API void stopping() override
Stopping method.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
Implements a failed exception class.
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.
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
virtual IOVTK_API void configuring() override
Configuring method.
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IWriter.cpp:184
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IWriter.hpp:171
virtual IOVTK_API::fwIO::IOPathType getIOPathType() const override
This method must be implemented by concrete service writers that use path file system to write data...