fw4spl
SModelSeriesObjWriter.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 #ifndef ANDROID
8 
9 #include "ioVTK/SModelSeriesObjWriter.hpp"
10 
11 #include "ioVTK/SMeshWriter.hpp"
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/location/Folder.hpp>
16 #include <fwData/Mesh.hpp>
17 #include <fwData/Reconstruction.hpp>
18 
19 #include <fwGui/Cursor.hpp>
20 #include <fwGui/dialog/ILocationDialog.hpp>
21 #include <fwGui/dialog/LocationDialog.hpp>
22 #include <fwGui/dialog/MessageDialog.hpp>
23 #include <fwGui/dialog/ProgressDialog.hpp>
24 
25 #include <fwMedData/ModelSeries.hpp>
26 
27 #include <fwServices/macros.hpp>
28 
29 #include <fwTools/UUID.hpp>
30 
31 #include <fwVtkIO/ModelSeriesObjWriter.hpp>
32 
33 #include <boost/filesystem/operations.hpp>
34 
35 namespace ioVTK
36 {
37 
38 fwServicesRegisterMacro( ::fwIO::IWriter, ::ioVTK::SModelSeriesObjWriter, ::fwMedData::ModelSeries );
39 
40 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL = "jobCreated";
41 
42 //------------------------------------------------------------------------------
43 
45 {
46  m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
47 }
48 
49 //------------------------------------------------------------------------------
50 
52 {
53  return ::fwIO::FOLDER;
54 }
55 
56 //------------------------------------------------------------------------------
57 
59 {
61  static ::boost::filesystem::path _sDefaultPath("");
62 
64  dialog.setTitle(m_windowTitle.empty() ? "Choose a directory to save meshes" : m_windowTitle);
65  dialog.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
66  dialog.setOption(::fwGui::dialog::ILocationDialog::WRITE);
67  dialog.setType(::fwGui::dialog::ILocationDialog::FOLDER);
68 
69  ::fwData::location::Folder::sptr result;
70 
71  while (result = ::fwData::location::Folder::dynamicCast( dialog.show() ))
72  {
73  if( ::boost::filesystem::is_empty(result->getFolder()) )
74  {
75  break;
76  }
77  // message box
79  messageBox.setTitle("Overwrite confirmation");
80  messageBox.setMessage("The selected directory is not empty. Write anyway ?");
81  messageBox.setIcon(::fwGui::dialog::IMessageDialog::QUESTION);
82  messageBox.addButton(::fwGui::dialog::IMessageDialog::YES);
83  messageBox.addButton(::fwGui::dialog::IMessageDialog::CANCEL);
84  if( messageBox.show() == ::fwGui::dialog::IMessageDialog::YES)
85  {
86  break;
87  }
88  }
89 
90  if (result)
91  {
92  _sDefaultPath = result->getFolder().parent_path();
93  dialog.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
94  this->setFolder(result->getFolder());
95  }
96  else
97  {
98  this->clearLocations();
99  }
100 }
101 
102 //------------------------------------------------------------------------------
103 
105 {
106  SLM_TRACE_FUNC();
107 }
108 
109 //------------------------------------------------------------------------------
110 
112 {
113  SLM_TRACE_FUNC();
114 }
115 
116 //------------------------------------------------------------------------------
117 
119 {
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 void SModelSeriesObjWriter::info(std::ostream& _sstream )
126 {
127  _sstream << "SModelSeriesObjWriter::info";
128 }
129 
130 //------------------------------------------------------------------------------
131 
133 {
134  SLM_TRACE_FUNC();
135 
136  if( this->hasLocationDefined() )
137  {
138  // Retrieve dataStruct associated with this service
139  ::fwMedData::ModelSeries::csptr modelSeries = this->getInput< ::fwMedData::ModelSeries >(::fwIO::s_DATA_KEY);
140  if (!modelSeries)
141  {
142  FW_DEPRECATED_KEY(::fwIO::s_DATA_KEY, "inout", "18.0");
143  modelSeries = this->getObject< ::fwMedData::ModelSeries >();
144  }
145  SLM_ASSERT("ModelSeries is not instanced", modelSeries);
146 
147  ::fwVtkIO::ModelSeriesObjWriter::sptr writer = ::fwVtkIO::ModelSeriesObjWriter::New();
148  writer->setObject(modelSeries);
149  writer->setFolder(this->getFolder());
150 
151  ::fwGui::Cursor cursor;
152  cursor.setCursor(::fwGui::ICursor::BUSY);
153 
154  try
155  {
156  m_sigJobCreated->emit(writer->getJob());
157  writer->write();
158  }
159  catch (const std::exception& e)
160  {
161  std::stringstream ss;
162  ss << "Warning during saving : " << e.what();
163 
165  messageBox.setTitle("Warning");
166  messageBox.setMessage( ss.str() );
167  messageBox.setIcon(::fwGui::dialog::IMessageDialog::WARNING);
168  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
169  messageBox.show();
170  }
171  catch( ... )
172  {
173  std::stringstream ss;
174  ss << "Warning during saving.";
175 
177  messageBox.setTitle("Warning");
178  messageBox.setMessage( ss.str() );
179  messageBox.setIcon(::fwGui::dialog::IMessageDialog::WARNING);
180  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
181  messageBox.show();
182  }
183 
184  cursor.setDefaultCursor();
185  }
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 } // namespace ioVtk
191 
192 #endif // ANDROID
#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.
virtual IOVTK_API void starting() override
Starting method.
virtual IOVTK_API void stopping() override
Stopping method.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
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.
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.
IOVTK_API void info(std::ostream &_sstream) override
Info method.
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
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
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
IOVTK_API void updating() override
Updating method.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
virtual IOVTK_API void configureWithIHM() override
Configure the mesh path.
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.
virtual IOVTK_API void configuring() override
Configuring method.
FWIO_API void setFolder(const ::boost::filesystem::path &folder)
Sets folder path.
Definition: IWriter.cpp:84
IOVTK_API SModelSeriesObjWriter() noexcept
Constructor. Do 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)
The namespace ioVTK contains reader, writer and helper using the fwVtkIO lib for output and input act...
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 IOVTK_API::fwIO::IOPathType getIOPathType() const override
This method must be implemented by concrete service writers that use path file system to write data...
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