fw4spl
SaveLandmark.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 "uiMeasurement/action/SaveLandmark.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/location/Folder.hpp>
12 #include <fwData/location/SingleFile.hpp>
13 #include <fwData/Point.hpp>
14 #include <fwData/PointList.hpp>
15 #include <fwData/String.hpp>
16 
17 #include <fwDataTools/fieldHelper/Image.hpp>
18 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
19 
20 #include <fwGui/dialog/LocationDialog.hpp>
21 #include <fwGui/dialog/MessageDialog.hpp>
22 #include <fwGui/dialog/SelectorDialog.hpp>
23 
24 #include <fwServices/IAppConfigManager.hpp>
25 #include <fwServices/macros.hpp>
26 #include <fwServices/registry/AppConfig.hpp>
27 #include <fwServices/registry/ServiceConfig.hpp>
28 
29 #include <exception>
30 #include <vector>
31 
32 namespace uiMeasurement
33 {
34 namespace action
35 {
36 
38 
39 //------------------------------------------------------------------------------
40 
41 SaveLandmark::SaveLandmark( ) noexcept
42 {
43 }
44 
45 //------------------------------------------------------------------------------
46 
47 SaveLandmark::~SaveLandmark() noexcept
48 {
49 }
50 
51 //------------------------------------------------------------------------------
52 
53 void SaveLandmark::info(std::ostream& _sstream )
54 {
55  _sstream << "Action for save landmarks" << std::endl;
56 }
57 
58 //------------------------------------------------------------------------------
59 
61 {
63  ::fwData::Image::sptr image = this->getObject< ::fwData::Image >();
65  {
67  "Save landmarks",
68  "It is impossible to save image landmarks. There is no loaded image in the software.",
69  ::fwGui::dialog::IMessageDialog::WARNING);
70  return;
71  }
72  static ::boost::filesystem::path _sDefaultPath("");
74  dialogFile.setTitle("Choose a file to save landmarks");
75  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
76  dialogFile.addFilter("Landmark file", "*.json");
77  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
78 
79  ::fwData::location::SingleFile::sptr result;
80  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
81 
82  if( result )
83  {
84  ::boost::filesystem::path path = result->getPath();
85  _sDefaultPath = path.parent_path();
86  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
87 
88  this->save(path);
89  }
90 }
91 
92 //------------------------------------------------------------------------------
93 
95 {
97 }
98 
99 //------------------------------------------------------------------------------
100 
102 {
103  FW_DEPRECATED_MSG("This service is deprecated", "18.0.");
105 }
106 
107 //------------------------------------------------------------------------------
108 
110 {
112 }
113 
114 //------------------------------------------------------------------------------
115 
116 void SaveLandmark::save(const ::boost::filesystem::path& path)
117 {
118  ::fwData::Image::csptr image = this->getInput< ::fwData::Image >("image");
119 
120  ::fwData::PointList::csptr landmarks = image->getField< ::fwData::PointList >(
122  if (landmarks)
123  {
125  replaceMap["GENERIC_UID"] = ::fwServices::registry::AppConfig::getUniqueIdentifier("SaveLandmarkApp");
126  replaceMap["landmark"] = landmarks->getID();
127  replaceMap["file"] = path.string();
128 
129  ::fwRuntime::ConfigurationElement::csptr config =
131  getAdaptedTemplateConfig("SaveLandmark2", replaceMap, true);
132 
133  ::fwServices::IAppConfigManager::sptr helper = ::fwServices::IAppConfigManager::New();
134  helper->setConfig( config );
135  helper->launch();
136  helper->stopAndDestroy();
137  }
138  else
139  {
140  ::fwGui::dialog::MessageDialog::showMessageDialog("Save Landmarks", "There is no landmark to save.");
141  }
142 }
143 
144 //------------------------------------------------------------------------------
145 
146 } // namespace action
147 
148 } // namespace uiMeasurement
This action allows to save the landmarks stored in image.
void info(std::ostream &_sstream) override
Write information in a stream.
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
The namespace uiMeasurement contains actions to add/show/remove distances and landmarks.
Definition: AddDistance.hpp:13
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
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.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
#define FW_DEPRECATED_MSG(message, version)
Use this macro when deprecating a function to warn the developer.
Definition: spyLog.hpp:360
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
void starting() override
Initialize the service activity.
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
This class defines a list of points.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
static FWSERVICES_API std::shared_ptr< IAppConfigManager > New()
void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
static FWSERVICES_API AppConfig::sptr getDefault()
Return an instance of AppConfig.
Definition: AppConfig.cpp:37
static FWSERVICES_API std::string getUniqueIdentifier(const std::string &serviceUid="")
Create an unique identifier.
Definition: AppConfig.cpp:269
This class defines an image.
void configuring() override
Configure the service before starting. Apply the configuration to service.
std::map< std::string, std::string > FieldAdaptorType
Associations of <pattern, value>.
Definition: AppConfig.hpp:42
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...