fw4spl
SPushField.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 "gui/action/SPushField.hpp"
8 
9 #include <fwCom/Slots.hpp>
10 #include <fwCom/Slots.hxx>
11 
12 #include <fwRuntime/ConfigurationElement.hpp>
13 #include <fwRuntime/EConfigurationElement.hpp>
14 
15 #include <fwServices/macros.hpp>
16 #include <fwServices/registry/AppConfig.hpp>
17 #include <fwServices/registry/ObjectService.hpp>
18 
19 #include <fwTools/fwID.hpp>
20 
21 namespace gui
22 {
23 namespace action
24 {
25 
26 //------------------------------------------------------------------------------
27 
28 static const ::fwCom::Slots::SlotKeyType s_UPDATE_OBJECTS_SLOT = "updateObject";
29 
30 static const std::string s_SOURCE_KEY = "source";
31 static const std::string s_DESTINATION_KEY = "destination";
32 
33 //------------------------------------------------------------------------------
34 
35 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::gui::action::SPushField );
36 
37 //------------------------------------------------------------------------------
38 
39 SPushField::SPushField() noexcept
40 {
41  newSlot(s_UPDATE_OBJECTS_SLOT, &SPushField::updateObjects, this);
42 }
43 
44 //------------------------------------------------------------------------------
45 
47 {
48 }
49 
50 //------------------------------------------------------------------------------
51 
53 {
54  this->initialize();
55 
56  const ConfigurationType pushCfg = m_configuration->findConfigurationElement("push");
57 
58  SLM_ASSERT("<push> tag must have one attribute 'field'.", pushCfg->hasAttribute("field"));
59  m_field = pushCfg->getAttributeValue("field");
60 }
61 
62 //------------------------------------------------------------------------------
63 
65 {
66  this->actionServiceStarting();
67  this->updateObjects();
68 }
69 
70 //------------------------------------------------------------------------------
71 
73 {
74  this->actionServiceStopping();
75 }
76 
77 //------------------------------------------------------------------------------
78 
80 {
81  ::fwData::Object::sptr objectSrc = this->getInOut< ::fwData::Object >(s_SOURCE_KEY);
82  SLM_ASSERT( s_SOURCE_KEY + " doesn't exist or is not a composite", objectSrc);
83 
84  ::fwData::Object::sptr obj = objectSrc->getField(m_field);
85 
86  SLM_WARN_IF("'" + m_field + "' not found in object '" + objectSrc->getID() + "'", obj == nullptr);
87  if (obj)
88  {
89  this->setOutput(s_DESTINATION_KEY, obj);
90  }
91 }
92 
93 //------------------------------------------------------------------------------
94 
95 void SPushField::updateObjects()
96 {
97  ::fwData::Object::sptr objectSrc = this->getInOut< ::fwData::Object >(s_SOURCE_KEY);
98  SLM_ASSERT( s_SOURCE_KEY + " doesn't exist or is not a composite", objectSrc);
99 
100  const bool executable = (objectSrc->getField(m_field) != nullptr);
101 
102  this->::fwGui::IActionSrv::setIsExecutable( executable );
103 }
104 
105 //------------------------------------------------------------------------------
106 
108 {
109  KeyConnectionsMap connections;
110  connections.push( s_SOURCE_KEY, ::fwData::Object::s_ADDED_FIELDS_SIG, s_UPDATE_OBJECTS_SLOT );
111  connections.push( s_SOURCE_KEY, ::fwData::Object::s_REMOVED_FIELDS_SIG, s_UPDATE_OBJECTS_SLOT );
112 
113  return connections;
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 } // namespace action
119 
120 } // namespace gui
GUI_API SPushField() noexcept
Constructor. Do nothing.
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
The namespace gui contains the basic services to build the application IHM.
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
To add or remove an object in composite with specific key.
Definition: SPushField.hpp:37
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_FIELDS_SIG
Type of signal m_sigModified.
FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType &key, const ::fwData::Object::sptr &object, size_t index=0)
Register an output object at a given key in the OSR, replacing it if it already exists.
Definition: IService.cpp:80
virtual void configuring() override
Configure the service.
Definition: SPushField.cpp:52
#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
virtual GUI_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: SPushField.cpp:107
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
virtual void updating() override
Extract the object.
Definition: SPushField.cpp:79
virtual GUI_API ~SPushField() noexcept
Destructor. Do nothing.
Definition: SPushField.cpp:46
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_FIELDS_SIG
Type of signal m_sigModified.
virtual void starting() override
Register the action and check if the action is executable.
Definition: SPushField.cpp:64
virtual void stopping() override
Unregister the action.
Definition: SPushField.cpp:72
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265