fw4spl
SPushObject.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 "gui/action/SPushObject.hpp"
8 
9 #include <fwCom/Slots.hpp>
10 #include <fwCom/Slots.hxx>
11 
12 #include <fwData/Composite.hpp>
13 
14 #include <fwDataTools/helper/Composite.hpp>
15 
16 #include <fwRuntime/ConfigurationElement.hpp>
17 #include <fwRuntime/EConfigurationElement.hpp>
18 
19 #include <fwServices/macros.hpp>
20 #include <fwServices/registry/AppConfig.hpp>
21 #include <fwServices/registry/ObjectService.hpp>
22 
23 #include <fwTools/fwID.hpp>
24 
25 #include <boost/regex.hpp>
26 
27 namespace gui
28 {
29 namespace action
30 {
31 
32 static const ::fwCom::Slots::SlotKeyType s_UPDATE_OBJECTS_SLOT = "updateObject";
33 
34 static const std::string s_SOURCE_KEY = "source";
35 static const std::string s_DESTINATION_KEY = "destination";
36 
37 //------------------------------------------------------------------------------
38 
39 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::gui::action::SPushObject, ::fwData::Composite );
40 
41 //------------------------------------------------------------------------------
42 
43 SPushObject::SPushObject() noexcept
44 {
45  newSlot(s_UPDATE_OBJECTS_SLOT, &SPushObject::updateObjects, this);
46 }
47 
48 //------------------------------------------------------------------------------
49 
51 {
52 }
53 
54 //------------------------------------------------------------------------------
55 
57 {
58  this->initialize();
59 
60  const ConfigurationType pushCfg = m_configuration->findConfigurationElement("push");
61 
62  SLM_ASSERT("<push> tag must have one attribute 'srcKey'.", pushCfg->hasAttribute("srcKey"));
63  m_srcKey = pushCfg->getAttributeValue("srcKey");
64 }
65 
66 //------------------------------------------------------------------------------
67 
69 {
70  this->actionServiceStarting();
71  this->updateObjects();
72 }
73 
74 //------------------------------------------------------------------------------
75 
77 {
78  this->actionServiceStopping();
79 }
80 
81 //------------------------------------------------------------------------------
82 
84 {
85  ::fwData::Composite::sptr compositeSrc = this->getInOut< ::fwData::Composite >(s_SOURCE_KEY);
86  SLM_ASSERT( s_SOURCE_KEY + " doesn't exist or is not a composite", compositeSrc);
87 
88  ::fwData::Object::sptr obj = compositeSrc->at< ::fwData::Object>(m_srcKey);
89 
90  SLM_WARN_IF("'" + m_srcKey + "' not found in composite '" + compositeSrc->getID() + "'", obj == nullptr);
91  if (::fwServices::OSR::isRegistered(s_DESTINATION_KEY, ::fwServices::IService::AccessType::OUTPUT,
92  this->getSptr()))
93  {
94  this->setOutput(s_DESTINATION_KEY, nullptr);
95  }
96  else if (obj)
97  {
98  this->setOutput(s_DESTINATION_KEY, obj);
99  }
100 }
101 
102 //------------------------------------------------------------------------------
103 
104 void SPushObject::updateObjects()
105 {
106  ::fwData::Composite::sptr compositeSrc = this->getInOut< ::fwData::Composite >(s_SOURCE_KEY);
107  SLM_ASSERT( s_SOURCE_KEY + " doesn't exist or is not a composite", compositeSrc);
108 
109  const bool executable = (compositeSrc->find(m_srcKey) != compositeSrc->end());
110  OSLM_TRACE("start check : " << compositeSrc->getID() << "[" << m_srcKey << "] : " <<
111  (compositeSrc->find(m_srcKey) != compositeSrc->end()) );
112 
113  this->::fwGui::IActionSrv::setIsExecutable( executable );
114 
115  if(executable && this->::fwGui::IActionSrv::getIsActive())
116  {
117  this->updating();
118  }
119 }
120 
121 //------------------------------------------------------------------------------
122 
124 {
125  KeyConnectionsMap connections;
126  connections.push( s_SOURCE_KEY, ::fwData::Composite::s_ADDED_OBJECTS_SIG, s_UPDATE_OBJECTS_SLOT );
127  connections.push( s_SOURCE_KEY, ::fwData::Composite::s_REMOVED_OBJECTS_SIG, s_UPDATE_OBJECTS_SLOT );
128 
129  return connections;
130 }
131 
132 //------------------------------------------------------------------------------
133 
134 } // namespace action
135 
136 } // namespace gui
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
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
virtual void updating() override
Extract the object.
Definition: SPushObject.cpp:83
FWGUI_API bool getIsActive() const
Return true if action service is active.
Definition: IActionSrv.cpp:198
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
virtual GUI_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
To add or remove an object in composite with specific key.
Definition: SPushObject.hpp:37
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
GUI_API SPushObject() noexcept
Constructor. Do nothing.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_OBJECTS_SIG
Type of signal when objects are added.
#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
Base class for each data object.
virtual void configuring() override
Configure the service.
Definition: SPushObject.cpp:56
::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 starting() override
Register the action and check if the action is executable.
Definition: SPushObject.cpp:68
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_OBJECTS_SIG
Type of signal when objects are added.
virtual void stopping() override
Unregister the action.
Definition: SPushObject.cpp:76
This class defines a composite object.
virtual GUI_API ~SPushObject() noexcept
Destructor. Do nothing.
Definition: SPushObject.cpp:50
FWSERVICES_API bool isRegistered(const ::fwServices::IService::KeyType &objKey,::fwServices::IService::AccessType access,::fwServices::IService::sptr service)
Return true if a key is registered for a given service.
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265