fw4spl
IActionSrv.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 "fwGui/IActionSrv.hpp"
8 
9 #include "fwGui/dialog/IMessageDialog.hpp"
10 #include "fwGui/dialog/MessageDialog.hpp"
11 
12 #include <fwCom/Signal.hxx>
13 #include <fwCom/Slot.hxx>
14 #include <fwCom/Slots.hxx>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <fwTools/fwID.hpp>
19 
20 namespace fwGui
21 {
22 
23 const ::fwCom::Slots::SlotKeyType IActionSrv::s_SET_IS_ACTIVE_SLOT = "setIsActive";
24 const ::fwCom::Slots::SlotKeyType IActionSrv::s_ACTIVATE_SLOT = "activate";
25 const ::fwCom::Slots::SlotKeyType IActionSrv::s_DEACTIVATE_SLOT = "deactivate";
26 const ::fwCom::Slots::SlotKeyType IActionSrv::s_SET_IS_EXECUTABLE_SLOT = "setIsExecutable";
27 const ::fwCom::Slots::SlotKeyType IActionSrv::s_SET_EXECUTABLE_SLOT = "setExecutable";
28 const ::fwCom::Slots::SlotKeyType IActionSrv::s_SET_INEXECUTABLE_SLOT = "setInexecutable";
29 const ::fwCom::Slots::SlotKeyType IActionSrv::s_SET_VISIBLE_SLOT = "setVisible";
30 const ::fwCom::Slots::SlotKeyType IActionSrv::s_SHOW_SLOT = "show";
31 const ::fwCom::Slots::SlotKeyType IActionSrv::s_HIDE_SLOT = "hide";
32 
33 const ::fwCom::Signals::SignalKeyType IActionSrv::s_ENABLED_SIG = "enabled";
34 const ::fwCom::Signals::SignalKeyType IActionSrv::s_DISABLED_SIG = "disabled";
35 const ::fwCom::Signals::SignalKeyType IActionSrv::s_CHECKED_SIG = "checked";
36 const ::fwCom::Signals::SignalKeyType IActionSrv::s_UNCHECKED_SIG = "unchecked";
37 
38 IActionSrv::IActionSrv() :
39  m_activeStateValue(true),
40  m_isActive(false),
41  m_isExecutable(true),
42  m_isVisible(true),
43  m_confirmAction(false)
44 {
45  newSlot(s_SET_IS_ACTIVE_SLOT, &IActionSrv::setIsActive, this);
46  newSlot(s_ACTIVATE_SLOT, &IActionSrv::activate, this);
47  newSlot(s_DEACTIVATE_SLOT, &IActionSrv::deactivate, this);
48  newSlot(s_SET_IS_EXECUTABLE_SLOT, &IActionSrv::setIsExecutable, this);
49  newSlot(s_SET_EXECUTABLE_SLOT, &IActionSrv::setExecutable, this);
50  newSlot(s_SET_INEXECUTABLE_SLOT, &IActionSrv::setInexecutable, this);
51  newSlot(s_SET_VISIBLE_SLOT, &IActionSrv::setVisible, this);
52  newSlot(s_SHOW_SLOT, &IActionSrv::show, this);
53  newSlot(s_HIDE_SLOT, &IActionSrv::hide, this);
54 
55  m_sigEnabled = newSignal< EnabledSignalType >(s_ENABLED_SIG);
56  m_sigDisabled = newSignal< DisabledSignalType >(s_DISABLED_SIG);
57  m_sigChecked = newSignal< CheckedSignalType >(s_CHECKED_SIG);
58  m_sigUnchecked = newSignal< UncheckedSignalType >(s_UNCHECKED_SIG);
59 }
60 
61 //-----------------------------------------------------------------------------
62 
63 IActionSrv::~IActionSrv()
64 {
65 }
66 
67 //-----------------------------------------------------------------------------
68 
69 void IActionSrv::initialize()
70 {
71  m_registrar = ::fwGui::registrar::ActionRegistrar::New(this->getID());
72 
73  OSLM_ASSERT("Depreciated tag <name> in "<< this->getID() << " configuration.", !m_configuration->hasAttribute(
74  "name"));
75  OSLM_ASSERT("Depreciated tag <shortcut> in "<< this->getID() << " configuration.",
76  !m_configuration->hasAttribute("shortcut"));
77  OSLM_ASSERT("Depreciated tag <enable> in "<< this->getID() << " configuration.",
78  !m_configuration->hasAttribute("enable"));
80  "Depreciated tag <specialAction> in "<< this->getID() << " configuration.",
81  !m_configuration->hasAttribute("specialAction"));
82  OSLM_ASSERT("Depreciated tag <style> in "<< this->getID() << " configuration.",
83  !m_configuration->hasAttribute("style"));
84  OSLM_ASSERT("Depreciated tag <state> in "<< this->getID() << " configuration.",
85  !m_configuration->hasAttribute("state"));
86 
88  for( iter = m_configuration->begin(); iter != m_configuration->end(); ++iter )
89  {
90  if( (*iter)->getName() == "state" )
91  {
92  ConfigurationType stateCfg = *iter;
93 
94  if( stateCfg->hasAttribute("inverse") )
95  {
96  std::string invertState = stateCfg->getExistingAttributeValue("inverse");
97  SLM_ASSERT("Wrong attribute value : must be 'true' or 'false'",
98  (invertState == "true") || (invertState == "false"));
99  m_activeStateValue = !(invertState == "true");
100  }
101 
102  if( stateCfg->hasAttribute("active") )
103  {
104  std::string isActive = stateCfg->getExistingAttributeValue("active");
105  SLM_ASSERT("Wrong attribute value : must be 'true' or 'false'",
106  (isActive == "true") || (isActive == "false"));
107  m_isActive = (isActive == "true");
108  }
109 
110  if( stateCfg->hasAttribute("executable") )
111  {
112  std::string isExecutable = stateCfg->getExistingAttributeValue("executable");
113  SLM_ASSERT("Wrong attribute value : must be 'true' or 'false'",
114  (isExecutable == "true") || (isExecutable == "false"));
115  m_isExecutable = (isExecutable == "true");
116  }
117 
118  if( stateCfg->hasAttribute("visible") )
119  {
120  std::string isVisible = stateCfg->getExistingAttributeValue("visible");
121  SLM_ASSERT("Wrong attribute value : must be 'true' or 'false'",
122  (isVisible == "true") || (isVisible == "false"));
123  m_isVisible = (isVisible == "true");
124  }
125  }
126 
127  if( (*iter)->getName() == "confirmation" )
128  {
129  ConfigurationType cfg = *iter;
130 
131  SLM_ASSERT("Missing attribute 'value'", cfg->hasAttribute("value"));
132  std::string confirm = cfg->getExistingAttributeValue("value");
133  SLM_ASSERT("Wrong attribute value : must be 'true' or 'false'",
134  (confirm == "true") || (confirm == "false"));
135  m_confirmAction = (confirm == "true");
136 
137  if( cfg->hasAttribute("message") )
138  {
139  m_confirmMessage = cfg->getExistingAttributeValue("message");
140  }
141 
142  if( cfg->hasAttribute("defaultbutton") )
143  {
144  m_defaultButton = cfg->getExistingAttributeValue("defaultbutton");
145  }
146 
147  }
148  }
149 }
150 
151 //-----------------------------------------------------------------------------
152 
153 void IActionSrv::actionServiceStopping()
154 {
155  this->m_registrar->actionServiceStopping();
156 }
157 
158 //-----------------------------------------------------------------------------
159 
160 void IActionSrv::actionServiceStarting()
161 {
162  this->m_registrar->actionServiceStarting();
163  this->setIsActive(m_isActive);
164 }
165 
166 //-----------------------------------------------------------------------------
167 
168 void IActionSrv::setIsActive(bool isActive)
169 {
170  m_isActive = isActive;
171  this->m_registrar->actionServiceSetActive(m_activeStateValue == isActive);
172  if(isActive)
173  {
174  m_sigChecked->asyncEmit();
175  }
176  else
177  {
178  m_sigUnchecked->asyncEmit();
179  }
180 }
181 
182 //-----------------------------------------------------------------------------
183 
184 void IActionSrv::activate()
185 {
186  this->setIsActive(true);
187 }
188 
189 //-----------------------------------------------------------------------------
190 
191 void IActionSrv::deactivate()
192 {
193  this->setIsActive(false);
194 }
195 
196 //-----------------------------------------------------------------------------
197 
198 bool IActionSrv::getIsActive() const
199 {
200  return m_isActive;
201 }
202 
203 //-----------------------------------------------------------------------------
204 
205 void IActionSrv::setIsExecutable(bool isExecutable)
206 {
207  m_isExecutable = isExecutable;
208  this->m_registrar->actionServiceSetExecutable(isExecutable);
209  if(isExecutable)
210  {
211  m_sigEnabled->asyncEmit();
212  }
213  else
214  {
215  m_sigDisabled->asyncEmit();
216  }
217 }
218 
219 //-----------------------------------------------------------------------------
220 
221 void IActionSrv::setExecutable()
222 {
223  this->setIsExecutable(true);
224 }
225 
226 //-----------------------------------------------------------------------------
227 
228 void IActionSrv::setInexecutable()
229 {
230  this->setIsExecutable(false);
231 }
232 
233 //-----------------------------------------------------------------------------
234 
235 bool IActionSrv::getIsExecutable() const
236 {
237  return m_isExecutable;
238 }
239 
240 //-----------------------------------------------------------------------------
241 
242 void IActionSrv::setVisible(bool isVisible)
243 {
244  m_isVisible = isVisible;
245  this->m_registrar->actionServiceSetVisible(isVisible);
246 }
247 
248 //-----------------------------------------------------------------------------
249 
250 void IActionSrv::show()
251 {
252  this->setVisible(true);
253 }
254 
255 //-----------------------------------------------------------------------------
256 
257 void IActionSrv::hide()
258 {
259  this->setVisible(false);
260 }
261 
262 //-----------------------------------------------------------------------------
263 
264 bool IActionSrv::isVisible() const
265 {
266  return m_isVisible;
267 }
268 
269 //-----------------------------------------------------------------------------
270 
271 bool IActionSrv::confirmAction()
272 {
273  bool actionIsConfirmed = true;
274 
275  if (m_confirmAction)
276  {
278  dialog.setTitle("Confirmation");
279  std::stringstream ss;
280  ss << "Do you really want to execute this action ? ";
281  if (!m_confirmMessage.empty())
282  {
283  ss << std::endl << m_confirmMessage;
284  }
285  dialog.setMessage( ss.str() );
286 
287  if(m_defaultButton == "yes")
288  {
289  dialog.setDefaultButton( ::fwGui::dialog::IMessageDialog::YES );
290  }
291  else if(m_defaultButton == "no")
292  {
293  dialog.setDefaultButton( ::fwGui::dialog::IMessageDialog::NO );
294  }
295  else if(!m_defaultButton.empty())
296  {
297  SLM_WARN("unknown button: " + m_defaultButton);
298  }
299 
300  dialog.setIcon( ::fwGui::dialog::IMessageDialog::QUESTION );
301  dialog.addButton( ::fwGui::dialog::IMessageDialog::YES_NO );
303 
304  actionIsConfirmed = (button == ::fwGui::dialog::IMessageDialog::YES);
305  }
306 
307  return actionIsConfirmed;
308 }
309 //-----------------------------------------------------------------------------
310 
311 }
static const ::fwCom::Slots::SlotKeyType s_SET_VISIBLE_SLOT
Slot to show/hide the action.
Definition: IActionSrv.hpp:60
#define OSLM_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:310
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
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 const ::fwCom::Signals::SignalKeyType s_DISABLED_SIG
Signal emitted when action is enabled.
Definition: IActionSrv.hpp:42
static const ::fwCom::Signals::SignalKeyType s_CHECKED_SIG
Signal emitted when action is enabled.
Definition: IActionSrv.hpp:46
#define SLM_WARN(message)
Definition: spyLog.hpp:261
static const ::fwCom::Slots::SlotKeyType s_SET_INEXECUTABLE_SLOT
Slot to disable the action.
Definition: IActionSrv.hpp:84
static const ::fwCom::Slots::SlotKeyType s_HIDE_SLOT
Slot to disable the action.
Definition: IActionSrv.hpp:66
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
static const ::fwCom::Slots::SlotKeyType s_SET_EXECUTABLE_SLOT
Slot to enable the action.
Definition: IActionSrv.hpp:81
static const ::fwCom::Slots::SlotKeyType s_SET_IS_ACTIVE_SLOT
Slot to activate/deactivate the action.
Definition: IActionSrv.hpp:69
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
#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
static const ::fwCom::Signals::SignalKeyType s_UNCHECKED_SIG
Signal emitted when action is enabled.
Definition: IActionSrv.hpp:50
static const ::fwCom::Slots::SlotKeyType s_DEACTIVATE_SLOT
Slot to desactivate the action.
Definition: IActionSrv.hpp:75
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
virtual FWGUI_API void setDefaultButton(IMessageDialog::Buttons button) override
Set the default button.
static const ::fwCom::Slots::SlotKeyType s_ACTIVATE_SLOT
Slot to activate the action.
Definition: IActionSrv.hpp:72
static const ::fwCom::Slots::SlotKeyType s_SHOW_SLOT
Slot to disable the action.
Definition: IActionSrv.hpp:63
Container::iterator Iterator
Defines the configuration element container type.
static const ::fwCom::Signals::SignalKeyType s_ENABLED_SIG
Signal emitted when action is enabled.
Definition: IActionSrv.hpp:38
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.
static const ::fwCom::Slots::SlotKeyType s_SET_IS_EXECUTABLE_SLOT
Slot to enable/disable the action.
Definition: IActionSrv.hpp:78