fw4spl
SSlotCaller.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/SSlotCaller.hpp"
8 
9 #include <fwCom/Slot.hpp>
10 #include <fwCom/Slot.hxx>
11 #include <fwCom/Slots.hpp>
12 #include <fwCom/Slots.hxx>
13 
14 #include <fwCore/base.hpp>
15 
16 #include <fwGui/dialog/MessageDialog.hpp>
17 
18 #include <fwRuntime/Extension.hpp>
19 #include <fwRuntime/helper.hpp>
20 
21 #include <fwServices/macros.hpp>
22 
23 #include <fwTools/fwID.hpp>
24 
25 #include <boost/regex.hpp>
26 
27 namespace gui
28 {
29 namespace action
30 {
31 
32 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::gui::action::SSlotCaller );
33 
34 //-----------------------------------------------------------------------------
35 
36 SSlotCaller::SSlotCaller() noexcept
37 {
38 }
39 
40 //-----------------------------------------------------------------------------
41 
43 {
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
51  this->actionServiceStarting();
52 }
53 
54 //-----------------------------------------------------------------------------
55 
57 {
59 
60  this->actionServiceStopping();
61 }
62 
63 //-----------------------------------------------------------------------------
64 
65 void SSlotCaller::info(std::ostream& _sstream )
66 {
67  _sstream << "Starter Action" << std::endl;
68 }
69 
70 //-----------------------------------------------------------------------------
71 
73 {
75 
76  if(this->confirmAction())
77  {
78  for(const SlotInfoType& info : m_slotInfos)
79  {
80  const HasSlotIDType& HasSlotId = info.first;
81  const ::fwCom::Slots::SlotKeyType& slotKey = info.second;
82 
83  if (::fwTools::fwID::exist(HasSlotId))
84  {
85  const ::fwTools::Object::sptr obj = ::fwTools::fwID::getObject(HasSlotId);
86  const ::fwCom::HasSlots::csptr hasSlots = std::dynamic_pointer_cast< ::fwCom::HasSlots >(obj);
87  SLM_ASSERT("Object with id " << HasSlotId << " is not a HasSlots", hasSlots);
88 
89  const ::fwCom::SlotBase::csptr slot = hasSlots->slot(slotKey);
90 
91  slot->asyncRun();
92  }
93  }
94  }
95 }
96 
97 //-----------------------------------------------------------------------------
98 
100 {
101  SLM_TRACE_FUNC();
102  this->initialize();
103 
104  OSLM_ASSERT("Missing slots configuration element in " << this->getID(),
105  m_configuration->hasConfigurationElement("slots"));
106  ConfigurationType cfg = m_configuration->findConfigurationElement("slots");
107 
109 
110  ::boost::regex re("(.*)/(.*)");
111  ::boost::smatch match;
112  std::string src, uid, key;
113 
114  for(ConfigurationType elem : slotCfgs.getElements())
115  {
116  src = elem->getValue();
117  if( ::boost::regex_match(src, match, re) )
118  {
119  OSLM_ASSERT("Wrong value for attribute src: "<<src, match.size() >= 3);
120  uid.assign(match[1].first, match[1].second);
121  key.assign(match[2].first, match[2].second);
122 
123  SLM_ASSERT("Missing hasSlotsId attribute", !uid.empty());
124  SLM_ASSERT("Missing slotKey attribute", !key.empty());
125 
126  m_slotInfos.push_back( std::make_pair(uid, key) );
127  }
128  }
129 }
130 
131 //-----------------------------------------------------------------------------
132 
133 } // namespace action
134 } // namespace gui
FWGUI_API bool confirmAction()
Confirm that the action must be executed.
Definition: IActionSrv.cpp:271
virtual GUI_API ~SSlotCaller() noexcept
Destructor. Does nothing.
Definition: SSlotCaller.cpp:42
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
Run a slot given by HasSlot id and slot key.
Definition: SSlotCaller.hpp:38
#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
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
The namespace gui contains the basic services to build the application IHM.
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
This class proposes a mapping between a SlotKeyType and a SlotBase.
Definition: HasSlots.hpp:22
FWRUNTIME_API ConfigurationElementContainer findAllConfigurationElement(const std::string &name) const
Retrieves the configurations corresponding to the specified name.
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
GUI_API void configuring() override
This method is used to configure the service parameters: specifies which slots must be called...
Definition: SSlotCaller.cpp:99
FWTOOLS_API IDType getID(Policy policy=GENERATE) const
Returns the id of the object. If it is not set and the policy value is.
Definition: fwID.cpp:78
virtual GUI_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SSlotCaller.cpp:56
#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
::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 GUI_API void info(std::ostream &_sstream) override
This method gives information about the class.
Definition: SSlotCaller.cpp:65
GUI_API void updating() override
This method run the specified slots.
Definition: SSlotCaller.cpp:72
Defines the generic configuration element container class.
FWRUNTIME_API const Container & getElements() const
Returns the configuration element container.
virtual GUI_API void starting() override
Initialize the service activity.
Definition: SSlotCaller.cpp:48
GUI_API SSlotCaller() noexcept
Constructor. Does nothing.
static FWTOOLS_API std::shared_ptr< ::fwTools::Object > getObject(IDType requestID)
Retrieve the object attached to the given id. Return a null sptr if no correspondence exist...
Definition: fwID.cpp:117