fw4spl
LoadLandmark.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/LoadLandmark.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwData/location/Folder.hpp>
14 #include <fwData/location/SingleFile.hpp>
15 #include <fwData/Point.hpp>
16 #include <fwData/PointList.hpp>
17 #include <fwData/String.hpp>
18 
19 #include <fwDataTools/fieldHelper/Image.hpp>
20 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
21 
22 #include <fwGui/dialog/LocationDialog.hpp>
23 #include <fwGui/dialog/MessageDialog.hpp>
24 
25 #include <fwServices/IAppConfigManager.hpp>
26 #include <fwServices/macros.hpp>
27 #include <fwServices/registry/AppConfig.hpp>
28 #include <fwServices/registry/ServiceConfig.hpp>
29 
30 #include <exception>
31 #include <vector>
32 
33 namespace uiMeasurement
34 {
35 namespace action
36 {
37 
39 
40 //------------------------------------------------------------------------------
41 
42 LoadLandmark::LoadLandmark( ) noexcept
43 {
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 LoadLandmark::~LoadLandmark() noexcept
49 {
50 }
51 
52 //------------------------------------------------------------------------------
53 
54 void LoadLandmark::info(std::ostream& _sstream )
55 {
56  _sstream << "Action for load landmarks" << std::endl;
57 }
58 
59 //------------------------------------------------------------------------------
60 
62 {
64  ::fwData::Image::sptr image = this->getObject< ::fwData::Image >();
66  {
68  "Load landmarks",
69  "It is impossible to load image landmarks. There is no loaded image in the software.",
70  ::fwGui::dialog::IMessageDialog::WARNING);
71  return;
72  }
73  static ::boost::filesystem::path _sDefaultPath("");
75  dialogFile.setTitle("Choose a file to load landmarks");
76  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
77  dialogFile.addFilter("Landmark file", "*.json");
78  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
79 
80  ::fwData::location::SingleFile::sptr result;
81  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
82 
83  if( result )
84  {
85  ::boost::filesystem::path path = result->getPath();
86  _sDefaultPath = path.parent_path();
87  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
88 
89  this->load(path);
90 
91  // notify
93  sig->asyncEmit(::fwData::Point::sptr());
94  }
95 }
96 
97 //------------------------------------------------------------------------------
98 
100 {
102 }
103 
104 //------------------------------------------------------------------------------
105 
107 {
108  FW_DEPRECATED_MSG("This service is deprecated", "18.0.");
110 }
111 
112 //------------------------------------------------------------------------------
113 
115 {
117 }
118 
119 //------------------------------------------------------------------------------
120 
121 void LoadLandmark::load(const ::boost::filesystem::path& path)
122 {
123  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >("image");
124  SLM_ASSERT("In-Out 'image' is not found.", image);
125 
126  //get landmarks
128  ::fwData::PointList::sptr landmarks = image->getField< ::fwData::PointList >(
130  SLM_ASSERT("landmarks not instanced", landmarks);
131 
132  ::fwData::PointList::sptr newLandmarks = ::fwData::PointList::New();
134  replaceMap["GENERIC_UID"] = ::fwServices::registry::AppConfig::getUniqueIdentifier("LoadLandmarkApp");
135  replaceMap["landmark"] = newLandmarks->getID();
136  replaceMap["file"] = path.string();
137 
138  ::fwRuntime::ConfigurationElement::csptr config =
140  getAdaptedTemplateConfig("LoadLandmark2", replaceMap, true);
141 
142  ::fwServices::IAppConfigManager::sptr helper = ::fwServices::IAppConfigManager::New();
143  helper->setConfig( config );
144  helper->launch();
145  helper->stopAndDestroy();
146 
147  for(::fwData::Point::sptr landmark : newLandmarks->getPoints())
148  {
149  landmarks->getPoints().push_back( landmark );
151  sig->asyncEmit(landmark);
152  }
153 
154  {
156  sig->asyncEmit();
157  }
158 }
159 
160 //------------------------------------------------------------------------------
161 
162 } // namespace action
163 
164 } // namespace uiMeasurement
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
void info(std::ostream &_sstream) override
Write information in a stream.
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
void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
void starting() override
Initialize the service activity.
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_LANDMARK_ADDED_SIG
Type of signal when image&#39;s buffer is added.
This action allows to load landmarks from a file.
#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
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
void configuring() override
Configure the service before starting. Apply the configuration to service.
void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
This class defines a list of points.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
static FWDATATOOLS_API bool checkLandmarks(::fwData::Image::sptr _pImg)
Check if the image has a landmark field.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
static FWSERVICES_API std::shared_ptr< IAppConfigManager > New()
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.
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)