fw4spl
RemoveLandmark.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/RemoveLandmark.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/Image.hpp>
12 #include <fwData/Point.hpp>
13 #include <fwData/PointList.hpp>
14 #include <fwData/String.hpp>
15 #include <fwData/Vector.hpp>
16 
17 #include <fwDataTools/fieldHelper/Image.hpp>
18 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
19 
20 #include <fwGui/dialog/SelectorDialog.hpp>
21 
22 #include <fwServices/macros.hpp>
23 
24 #include <boost/lexical_cast.hpp>
25 
26 #include <exception>
27 #include <sstream>
28 
29 namespace uiMeasurement
30 {
31 namespace action
32 {
33 
35 
36 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
37 
38 //------------------------------------------------------------------------------
39 
40 RemoveLandmark::RemoveLandmark( ) noexcept
41 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
46 RemoveLandmark::~RemoveLandmark() noexcept
47 {
48 }
49 
50 //------------------------------------------------------------------------------
51 
52 void RemoveLandmark::info(std::ostream& _sstream )
53 {
54  _sstream << "Action for remove landmark" << std::endl;
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 ::fwData::Point::sptr RemoveLandmark::getLandmarkToRemove(::fwData::Image::sptr image, bool& removeAll)
60 {
61  ::fwData::Point::sptr landmarkToRemove;
62  removeAll = false;
63 
64  ::fwData::PointList::sptr landmarksBackup = image->getField< ::fwData::PointList >(
66  SLM_ASSERT("No Field ImageLandmarks", landmarksBackup);
67 
68  std::vector< std::string > selections;
69  selections.push_back("ALL");
70  std::map< std::string, ::fwData::Point::sptr > correspondance;
71 
72  for(::fwData::Point::sptr landmark : landmarksBackup->getPoints())
73  {
74  ::fwData::String::sptr name = landmark->getField< ::fwData::String >(
76  SLM_ASSERT("No Field LabelId", name);
77  selections.push_back( *name );
78  correspondance[ *name ] = landmark;
79  }
80 
81  ::fwGui::dialog::SelectorDialog::sptr selector = ::fwGui::dialog::SelectorDialog::New();
82  selector->setTitle("Select a landmark to remove");
83  selector->setSelections(selections);
84  std::string selection = selector->show();
85  if( !selection.empty() )
86  {
87  if (selection == "ALL")
88  {
89  removeAll = true;
90  }
91  else
92  {
93  removeAll = false;
94  landmarkToRemove = correspondance[selection];
95  }
96  }
97 
98  return landmarkToRemove;
99 }
100 
101 //------------------------------------------------------------------------------
102 
103 void RemoveLandmark::notify( ::fwData::Image::sptr image, ::fwData::Point::sptr backup)
104 {
106  sig->asyncEmit(backup);
107 }
108 
109 //------------------------------------------------------------------------------
110 
112 {
113  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
114  if (!image)
115  {
116  FW_DEPRECATED_KEY(s_IMAGE_INOUT, "inout", "18.0");
117  image = this->getObject< ::fwData::Image >();
118  }
119  ::fwData::PointList::sptr landmarks = image->getField< ::fwData::PointList >(
121 
123  {
124  if (!landmarks->getPoints().empty())
125  {
126  bool requestAll;
127  ::fwData::Point::sptr landmarkToRemove = this->getLandmarkToRemove(image, requestAll );
128 
129  // perform action only available distance
130  if ( landmarkToRemove )
131  {
132 
133  ::fwData::PointList::PointListContainer::iterator itr;
134  itr = std::find( landmarks->getPoints().begin(), landmarks->getPoints().end(), landmarkToRemove );
135  if (itr != landmarks->getPoints().end())
136  {
137  landmarks->getPoints().erase(itr);
138  this->notify(image, landmarkToRemove);
139  }
140  }
141  if ( requestAll )
142  {
143  // backup
144  image->removeField( ::fwDataTools::fieldHelper::Image::m_imageLandmarksId ); // erase field
145  this->notify(image, landmarkToRemove);
146  }
147  }
148  }
149 }
150 
151 //------------------------------------------------------------------------------
152 
154 {
156 }
157 
158 //------------------------------------------------------------------------------
159 
161 {
163 }
164 
165 //------------------------------------------------------------------------------
166 
168 {
170 }
171 
172 //------------------------------------------------------------------------------
173 
174 } // namespace action
175 } // namespace uiMeasurement
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
void configuring() override
Configure the service before starting. Apply the configuration to service.
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
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
void info(std::ostream &_sstream) override
Write information in a stream.
void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
#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
static FWDATATOOLS_API const std::string m_labelId
to assign a label
void starting() override
Initialize the service activity.
This class defines a list of points.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
This class defines an image.
This class contains an std::string value.
This action removes landmarks from an image.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_LANDMARK_REMOVED_SIG
Type of signal when image&#39;s buffer is added.