fw4spl
MenuBarRegistrar.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/registrar/MenuBarRegistrar.hpp"
8 
9 #include "fwGui/GuiRegistry.hpp"
10 
11 #include <fwServices/macros.hpp>
12 #include <fwServices/op/Get.hpp>
13 
14 #include <fwTools/fwID.hpp>
15 
16 #include <utility>
17 
18 namespace fwGui
19 {
20 namespace registrar
21 {
22 
23 //-----------------------------------------------------------------------------
24 
25 MenuBarRegistrar::MenuBarRegistrar(const std::string& sid) :
26  m_sid(sid)
27 {
28 }
29 
30 //-----------------------------------------------------------------------------
31 
33 {
34 }
35 
36 //-----------------------------------------------------------------------------
37 
38 ::fwGui::container::fwMenuBar::sptr MenuBarRegistrar::getParent()
39 {
40  return ::fwGui::GuiRegistry::getSIDMenuBar(m_sid);
41 }
42 
43 //-----------------------------------------------------------------------------
44 
45 ::fwGui::container::fwMenu::sptr MenuBarRegistrar::getFwMenu(std::string menuSid,
46  std::vector< ::fwGui::container::fwMenu::sptr > menus)
47 {
48  SLM_ASSERT("menu not found", m_menuSids.find(menuSid) != m_menuSids.end());
49  ::fwGui::container::fwMenu::sptr menu = menus.at( m_menuSids[menuSid].first );
50  return menu;
51 }
52 
53 //-----------------------------------------------------------------------------
54 
55 void MenuBarRegistrar::initialize( ::fwRuntime::ConfigurationElement::sptr configuration)
56 {
57  OSLM_ASSERT("Bad configuration name "<<configuration->getName()<< ", must be registry",
58  configuration->getName() == "registry");
59 
60  // index represents associated menu with position in menus vector
61  unsigned int index = 0;
62  // initialize m_menuSids map with configuration
63  std::vector < ConfigurationType > vectMenus = configuration->find("menu");
64  for( ConfigurationType menu : vectMenus)
65  {
66  SLM_ASSERT("<menu> tag must have sid attribute", menu->hasAttribute("sid"));
67 
68  if(menu->hasAttribute("sid"))
69  {
70  bool start = false;
71  if(menu->hasAttribute("start"))
72  {
73  std::string startValue = menu->getAttributeValue("start");
74  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
75  startValue == "yes" || startValue == "no");
76  start = (startValue == "yes");
77  }
78  std::string sid = menu->getAttributeValue("sid");
79  m_menuSids[sid] = SIDMenuMapType::mapped_type(index, start);
80  }
81  index++;
82  }
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 void MenuBarRegistrar::manage(std::vector< ::fwGui::container::fwMenu::sptr > menus )
88 {
89  ::fwGui::container::fwMenu::sptr menu;
90  for( SIDMenuMapType::value_type sid : m_menuSids)
91  {
92  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
93  sid.second.first < menus.size());
94  menu = menus.at( sid.second.first );
96  if(sid.second.second) //service is auto started?
97  {
98  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
99  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
100  service->start();
101  }
102  else
103  {
104  bool service_exists = ::fwTools::fwID::exist(sid.first );
105  if (!service_exists || ::fwServices::get( sid.first )->isStopped())
106  {
108  }
109  }
110  }
111 }
112 
113 //-----------------------------------------------------------------------------
114 
116 {
117  for( SIDMenuMapType::value_type sid : m_menuSids)
118  {
119  if(sid.second.second) //service is auto started?
120  {
121  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
122  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
123  service->stop();
124  }
126  }
127 }
128 
129 //-----------------------------------------------------------------------------
130 
131 } // namespace registrar
132 } //namespace fwGui
FWSERVICES_API::fwServices::IService::sptr get(::fwData::Object::sptr obj, std::string serviceType)
Get the first service of type serviceType which is attached to obj.
Definition: Get.cpp:18
static FWGUI_API void registerSIDMenu(std::string sid,::fwGui::container::fwMenu::sptr menu)
Registers fwMenu associate with service ID.
#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
SIDMenuMapType m_menuSids
All menu services ID managed and associated with pair containing: menu&#39;s index vector and boolean des...
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
virtual FWGUI_API void manage(std::vector< ::fwGui::container::fwMenu::sptr > menus)
Starting menu bar registrar. All services managed in local menus and with start="yes" in configuratio...
static FWGUI_API void actionServiceStopping(std::string actionSid)
Method called when the action service is stopping. Call parent service actionServiceStopping() method...
static FWGUI_API void unregisterSIDMenu(std::string sid)
Unregisters fwMenu associate with service ID.
virtual FWGUI_API ~MenuBarRegistrar()
Destructor. Do nothing.
virtual FWGUI_API::fwGui::container::fwMenu::sptr getFwMenu(std::string menuSid, std::vector< ::fwGui::container::fwMenu::sptr > menus)
Return the fwMenu associated with the menuSid.
#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 MenuBarRegistrar(const std::string &sid)
Constructor.
std::string m_sid
Main service ID associate with this MenuBarRegistrar.
virtual FWGUI_API void initialize(::fwRuntime::ConfigurationElement::sptr configuration)
Initialize registry managers.
virtual FWGUI_API void unmanage()
Stopping view manager. All services managed in local menus will be stopped.
virtual FWGUI_API::fwGui::container::fwMenuBar::sptr getParent()
Return the parent container.