fw4spl
SFocusLandmark.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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/SFocusLandmark.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 #include <fwCom/Slot.hpp>
13 #include <fwCom/Slot.hxx>
14 #include <fwCom/Slots.hpp>
15 #include <fwCom/Slots.hxx>
16 
17 #include <fwCore/base.hpp>
18 
19 #include <fwData/Image.hpp>
20 #include <fwData/Integer.hpp>
21 #include <fwData/Landmarks.hpp>
22 
23 #include <fwDataTools/fieldHelper/Image.hpp>
24 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
25 
26 #include <fwGui/dialog/MessageDialog.hpp>
27 
28 #include <fwServices/macros.hpp>
29 
30 namespace uiMeasurement
31 {
32 namespace action
33 {
34 
35 static const ::fwServices::IService::KeyType s_LANDMARKS_INPUT = "landmarks";
36 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
37 
38 static const ::fwCom::Slots::SlotKeyType s_SELECT_LANDMARK_SLOT = "selectLandmark";
39 static const ::fwCom::Slots::SlotKeyType s_DESELECT_LANDMARK_SLOT = "deselectLandmark";
40 static const ::fwCom::Slots::SlotKeyType s_DESELECT_FROM_GROUP_SLOT = "deselectFromGroup";
41 static const ::fwCom::Slots::SlotKeyType s_RENAME_GROUP_SLOT = "renameGroup";
42 
43 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::uiMeasurement::action::SFocusLandmark );
44 
45 //------------------------------------------------------------------------------
46 
47 SFocusLandmark::SFocusLandmark( ) noexcept :
48  m_index(0),
49  m_isSelected(false)
50 {
51  newSlot(s_SELECT_LANDMARK_SLOT, &SFocusLandmark::selectLandmark, this);
52  newSlot(s_DESELECT_LANDMARK_SLOT, &SFocusLandmark::deselectLandmark, this);
53  newSlot(s_DESELECT_FROM_GROUP_SLOT, &SFocusLandmark::deselectFromGroup, this);
54  newSlot(s_RENAME_GROUP_SLOT, &SFocusLandmark::renameGroup, this);
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 SFocusLandmark::~SFocusLandmark() noexcept
60 {
61 }
62 
63 //------------------------------------------------------------------------------
64 
66 {
68  this->setIsExecutable(m_isSelected);
69 }
70 
71 //------------------------------------------------------------------------------
72 
74 {
76 }
77 
78 //------------------------------------------------------------------------------
79 
81 {
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89  if (m_isSelected)
90  {
91  ::fwData::Image::sptr pImage = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
93  {
95  messageBox.setTitle("Add landmarks");
96  messageBox.setMessage(
97  "It is impossible to add image landmarks. There is no loaded image in the software." );
98  messageBox.setIcon(::fwGui::dialog::IMessageDialog::WARNING);
99  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
100  messageBox.show();
101  return;
102  }
103  else // Image is defined
104  {
105  ::fwData::Landmarks::csptr landmarks = this->getInput< ::fwData::Landmarks >(s_LANDMARKS_INPUT);
106 
107  try
108  {
109  const ::fwData::Landmarks::PointType& point = landmarks->getPoint(m_groupName, m_index);
110 
111  ::fwData::Integer::sptr paramA = ::fwData::Integer::New();
112  paramA->value() =
113  static_cast<int>((point[2] - pImage->getOrigin()[2] ) / pImage->getSpacing()[2] +0.5);
114  ::fwData::Integer::sptr paramF = ::fwData::Integer::New();
115  paramF->value() =
116  static_cast<int>((point[1] - pImage->getOrigin()[1]) / pImage->getSpacing()[1] +0.5);
117  ::fwData::Integer::sptr paramS = ::fwData::Integer::New();
118  paramS->value() =
119  static_cast<int>((point[0] - pImage->getOrigin()[0]) / pImage->getSpacing()[0] +0.5);
120  if( paramS->value() >= 0 &&
121  paramF->value() >= 0 &&
122  paramA->value() >= 0 &&
123  pImage->getSize()[0] > static_cast< ::fwData::Image::SizeType::value_type >(paramS->value()) &&
124  pImage->getSize()[1] > static_cast< ::fwData::Image::SizeType::value_type >(paramF->value()) &&
125  pImage->getSize()[2] > static_cast< ::fwData::Image::SizeType::value_type >(paramA->value()) )
126  {
127  pImage->setField( ::fwDataTools::fieldHelper::Image::m_axialSliceIndexId, paramA );
128  pImage->setField( ::fwDataTools::fieldHelper::Image::m_frontalSliceIndexId, paramF );
129  pImage->setField( ::fwDataTools::fieldHelper::Image::m_sagittalSliceIndexId, paramS );
130 
131  // notify
132  auto sig = pImage->signal< ::fwData::Image::SliceIndexModifiedSignalType >(
134  sig->asyncEmit(paramA->value(), paramF->value(), paramS->value());
135  }
136  else
137  {
139  "It is impossible to focus image landmarks: "
140  "landmark is outside image.",
141  ::fwGui::dialog::IMessageDialog::WARNING);
142  }
143  }
144  catch (::fwData::Exception& e )
145  {
147  "It is impossible to focus image landmarks: "
148  + std::string(e.what()),
149  ::fwGui::dialog::IMessageDialog::WARNING);
150  }
151 
152  }
153  }
154 }
155 
156 //------------------------------------------------------------------------------
157 
158 void SFocusLandmark::selectLandmark(std::string groupName, size_t index)
159 {
160  m_groupName = groupName;
161  m_index = index;
162  m_isSelected = true;
163  this->setIsExecutable(m_isSelected);
164 }
165 
166 //------------------------------------------------------------------------------
167 
168 void SFocusLandmark::deselectLandmark(std::string groupName, size_t index)
169 {
170  if (m_groupName == groupName && m_index == index)
171  {
172  m_groupName = "";
173  m_index = 0;
174  m_isSelected = false;
175  }
176  this->setIsExecutable(m_isSelected);
177 }
178 
179 //------------------------------------------------------------------------------
180 
181 void SFocusLandmark::deselectFromGroup(std::string groupName)
182 {
183  if (m_groupName == groupName)
184  {
185  m_groupName = "";
186  m_index = 0;
187  m_isSelected = false;
188  }
189  this->setIsExecutable(m_isSelected);
190 }
191 
192 //------------------------------------------------------------------------------
193 
194 void SFocusLandmark::renameGroup(std::string oldGroupName, std::string newGroupName)
195 {
196  if (m_groupName == oldGroupName)
197  {
198  m_groupName = newGroupName;
199  }
200 }
201 
202 //------------------------------------------------------------------------------
203 
205 {
206  KeyConnectionsMap connections;
207 
208  connections.push(s_LANDMARKS_INPUT, ::fwData::Landmarks::s_POINT_SELECTED_SIG, s_SELECT_LANDMARK_SLOT);
209  connections.push(s_LANDMARKS_INPUT, ::fwData::Landmarks::s_POINT_DESELECTED_SIG, s_DESELECT_LANDMARK_SLOT);
210  connections.push(s_LANDMARKS_INPUT, ::fwData::Landmarks::s_POINT_REMOVED_SIG, s_DESELECT_LANDMARK_SLOT);
211  connections.push(s_LANDMARKS_INPUT, ::fwData::Landmarks::s_GROUP_REMOVED_SIG, s_DESELECT_FROM_GROUP_SLOT);
212  connections.push(s_LANDMARKS_INPUT, ::fwData::Landmarks::s_GROUP_RENAMED_SIG, s_RENAME_GROUP_SLOT);
213 
214  return connections;
215 }
216 
217 //------------------------------------------------------------------------------
218 
219 } // namespace action
220 
221 } // namespace uiMeasurement
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_INDEX_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
virtual FWGUI_API void setIsExecutable(bool isExecutable)
Set the action service executable or not.
Definition: IActionSrv.cpp:205
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
This action moves the image slice on selected landmark.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_DESELECTED_SIG
Type of signal when a group is added.
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.
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
Implements data exception class.
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_GROUP_REMOVED_SIG
Type of signal when a group is added.
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_SELECTED_SIG
Type of signal when a group is added.
UIMEASUREMENT_API void configuring() override
Do nothing.
UIMEASUREMENT_API void starting() override
Do nothing.
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_GROUP_RENAMED_SIG
Type of signal when a group is added.
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
UIMEASUREMENT_API void stopping() override
Do nothing.
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
UIMEASUREMENT_API void updating() override
Focus the image slices on the selected landmark.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
UIMEASUREMENT_API KeyConnectionsMap getAutoConnections() const override
Defines connection to Landmarks data.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_REMOVED_SIG
Type of signal when a group is added.
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.