fw4spl
JpgImageWriterService.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 "ioITK/JpgImageWriterService.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/Image.hpp>
12 #include <fwData/location/Folder.hpp>
13 
14 #include <fwGui/Cursor.hpp>
15 #include <fwGui/dialog/LocationDialog.hpp>
16 #include <fwGui/dialog/MessageDialog.hpp>
17 #include <fwGui/dialog/ProgressDialog.hpp>
18 
19 #include <fwIO/IWriter.hpp>
20 
21 #include <fwItkIO/JpgImageWriter.hpp>
22 
23 #include <fwServices/macros.hpp>
24 
25 namespace ioITK
26 {
27 
28 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioITK::JpgImageWriterService, ::fwData::Image );
29 
30 //------------------------------------------------------------------------------
31 
32 JpgImageWriterService::JpgImageWriterService() noexcept
33 {
34 }
35 
36 //------------------------------------------------------------------------------
37 
38 JpgImageWriterService::~JpgImageWriterService() noexcept
39 {
40 }
41 
42 //------------------------------------------------------------------------------
43 
45 {
46  return ::fwIO::FOLDER;
47 }
48 
49 //------------------------------------------------------------------------------
50 
52 {
54 }
55 
56 //------------------------------------------------------------------------------
57 
59 {
61  static ::boost::filesystem::path _sDefaultPath;
62 
64  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a directory to save image" : m_windowTitle);
65  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
66  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
67  dialogFile.setType(::fwGui::dialog::ILocationDialog::FOLDER);
68 
69  ::fwData::location::Folder::sptr result;
70  result = ::fwData::location::Folder::dynamicCast( dialogFile.show() );
71  if (result)
72  {
73  _sDefaultPath = result->getFolder();
74  this->setFolder(result->getFolder());
75  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
76  }
77  else
78  {
79  this->clearLocations();
80  }
81 }
82 
83 //------------------------------------------------------------------------------
84 
86 {
88 }
89 
90 //------------------------------------------------------------------------------
91 
93 {
95 }
96 
97 //------------------------------------------------------------------------------
98 
99 void JpgImageWriterService::info(std::ostream& _sstream )
100 {
101  _sstream << "JpgImageWriterService::info";
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 void JpgImageWriterService::saveImage(const ::boost::filesystem::path& imgPath, const CSPTR(::fwData::Image)& img)
107 {
108  SLM_TRACE_FUNC();
109  ::fwItkIO::JpgImageWriter::sptr writer = ::fwItkIO::JpgImageWriter::New();
110  ::fwGui::dialog::ProgressDialog progressMeterGUI("Saving image... ");
111 
112  ::fwData::location::Folder::sptr loc = ::fwData::location::Folder::New();
113  loc->setFolder(imgPath);
114  writer->setLocation(loc);
115  writer->setObject(img);
116 
117  try
118  {
119  writer->addHandler( progressMeterGUI );
120  writer->write();
121 
122  }
123  catch (const std::exception& e)
124  {
125  std::stringstream ss;
126  ss << "Warning during saving : " << e.what();
128  ss.str(),
129  ::fwGui::dialog::IMessageDialog::WARNING);
130  }
131  catch( ... )
132  {
134  "Warning during saving",
135  ::fwGui::dialog::IMessageDialog::WARNING);
136  }
137 }
138 
139 //------------------------------------------------------------------------------
140 
142 {
143  SLM_TRACE_FUNC();
144 
145  if( this->hasLocationDefined() )
146  {
147  // Retrieve dataStruct associated with this service
148  ::fwData::Image::csptr image = this->getInput< ::fwData::Image >(::fwIO::s_DATA_KEY);
149  if (!image)
150  {
151  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
152  image = this->getObject< ::fwData::Image >();
153  }
154  SLM_ASSERT("'" + ::fwIO::s_DATA_KEY + "' key is not defined", image);
155 
156  ::fwGui::Cursor cursor;
157  cursor.setCursor(::fwGui::ICursor::BUSY);
158  saveImage(this->getFolder(), image);
159  cursor.setDefaultCursor();
160  }
161 }
162 
163 //------------------------------------------------------------------------------
164 
165 } // namespace ioITK
#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 CSPTR(_cls_)
virtual IOITK_API void stopping() override
Override.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual IOITK_API void starting() override
Override.
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.
Defines the generic progress dialog for IHM. Use the Delegate design pattern. The specific implementa...
Write an image to jpg format.
virtual IOITK_API void configureWithIHM() override
Override.
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
IOITK_API void updating() override
Override.
virtual IOITK_API void configuring() override
Override.
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
IOITK_API::fwIO::IOPathType getIOPathType() const override
Return managed file type, here FOLDER.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
IOITK_API void info(std::ostream &_sstream) override
Override.
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.
This class defines an image.
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)
The namespace ioITK contains services for reader, writer and helper for itk image.
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