fw4spl
MenuRegistrar.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/MenuRegistrar.hpp"
8 
9 #include "fwGui/GuiRegistry.hpp"
10 #include "fwGui/IActionSrv.hpp"
11 
12 #include <fwServices/macros.hpp>
13 #include <fwServices/op/Get.hpp>
14 
15 #include <fwTools/fwID.hpp>
16 
17 #include <utility>
18 
19 namespace fwGui
20 {
21 namespace registrar
22 {
23 
24 //-----------------------------------------------------------------------------
25 
26 MenuRegistrar::MenuRegistrar(const std::string& sid) :
27  m_sid(sid)
28 {
29 }
30 
31 //-----------------------------------------------------------------------------
32 
34 {
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 ::fwGui::container::fwMenu::sptr MenuRegistrar::getParent()
40 {
41  return ::fwGui::GuiRegistry::getSIDMenu(m_sid);
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 ::fwGui::container::fwMenuItem::sptr MenuRegistrar::getFwMenuItem(std::string actionSid,
47  std::vector< ::fwGui::container::fwMenuItem::sptr > menuItems)
48 {
49  SLM_ASSERT("menuItem not found", m_actionSids.find(actionSid) != m_actionSids.end());
50  ::fwGui::container::fwMenuItem::sptr menuItem = menuItems.at( m_actionSids[actionSid].first );
51  return menuItem;
52 }
53 
54 //-----------------------------------------------------------------------------
55 
56 void MenuRegistrar::initialize( ::fwRuntime::ConfigurationElement::sptr configuration)
57 {
58  OSLM_ASSERT("Bad configuration name "<<configuration->getName()<< ", must be registry",
59  configuration->getName() == "registry");
60 
61  // index represents associated menu with position in menus vector
62  unsigned int index = 0;
63  m_callbacks.clear();
64  // initialize m_actionSids map with configuration
65  std::vector < ConfigurationType > vectMenuItems = configuration->find("menuItem");
66  for( ConfigurationType menuItem : vectMenuItems)
67  {
68  SLM_ASSERT("<menuItem> tag must have sid attribute", menuItem->hasAttribute("sid"));
69  if(menuItem->hasAttribute("sid"))
70  {
71  bool start = false;
72  if(menuItem->hasAttribute("start"))
73  {
74  std::string startValue = menuItem->getAttributeValue("start");
75  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
76  startValue == "yes" || startValue == "no");
77  start = (startValue == "yes");
78  }
79  std::string sid = menuItem->getAttributeValue("sid");
80  OSLM_ASSERT("Action " << sid << " already exists for this menu", m_actionSids.find(
81  sid) == m_actionSids.end());
82  m_actionSids[sid] = SIDMenuMapType::mapped_type(index, start);
83 
84  ::fwGui::ActionCallbackBase::sptr callback;
85  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(ActionCallbackBase::REGISTRY_KEY);
86  callback = ::fwGui::ActionCallbackBase::dynamicCast(guiObj);
87  OSLM_ASSERT("ClassFactoryRegistry failed for class "<< ::fwGui::ActionCallbackBase::REGISTRY_KEY, callback);
88 
89  callback->setSID(sid);
90  m_callbacks.push_back(callback);
91  }
92  index++;
93  }
94 
95  index = 0;
96  // initialize m_actionSids map with configuration
97  std::vector < ConfigurationType > vectMenus = configuration->find("menu");
98  for( ConfigurationType menu : vectMenus)
99  {
100  SLM_ASSERT("<menu> tag must have sid attribute", menu->hasAttribute("sid"));
101  if(menu->hasAttribute("sid"))
102  {
103  bool start = false;
104  if(menu->hasAttribute("start"))
105  {
106  std::string startValue = menu->getAttributeValue("start");
107  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
108  startValue == "yes" || startValue == "no");
109  start = (startValue == "yes");
110  }
111  std::string sid = menu->getAttributeValue("sid");
112  std::pair<int, bool> indexStart = std::make_pair( index, start);
113  OSLM_ASSERT("Action " << sid << " already exists for this toolBar", m_actionSids.find(
114  sid) == m_actionSids.end());
115  m_menuSids[sid] = indexStart;
116  }
117  index++;
118  }
119 }
120 
121 //-----------------------------------------------------------------------------
122 
123 void MenuRegistrar::manage(std::vector< ::fwGui::container::fwMenuItem::sptr > menuItems )
124 {
125  ::fwGui::container::fwMenuItem::sptr menuItem;
126  for( SIDMenuMapType::value_type sid : m_actionSids)
127  {
128  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
129  sid.second.first < menuItems.size());
130  menuItem = menuItems.at( sid.second.first );
132  if(sid.second.second) //service is auto started?
133  {
134  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
135  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
136  OSLM_ASSERT("Service "<<sid.first <<" must be stopped.", service->isStopped() );
137  service->start();
138  }
139  else
140  {
141  bool service_exists = ::fwTools::fwID::exist(sid.first );
142  if (!service_exists || ::fwServices::get( sid.first )->isStopped())
143  {
145  }
146  else
147  {
149  }
150  }
151  }
152 }
153 
154 //-----------------------------------------------------------------------------
155 
156 void MenuRegistrar::manage(std::vector< ::fwGui::container::fwMenu::sptr > menus )
157 {
158  ::fwGui::container::fwMenu::sptr menu;
159  for( SIDMenuMapType::value_type sid : m_menuSids)
160  {
161  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
162  sid.second.first < menus.size());
163  menu = menus.at( sid.second.first );
164  ::fwGui::GuiRegistry::registerSIDMenu(sid.first, menu);
165  if(sid.second.second) //service is auto started?
166  {
167  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
168  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
169  OSLM_ASSERT("Service "<<sid.first <<" must be stopped.", service->isStopped() );
170  service->start();
171  }
172  }
173 }
174 
175 //-----------------------------------------------------------------------------
176 
178 {
179  for( SIDMenuMapType::value_type sid : m_actionSids)
180  {
181  if(sid.second.second) //service is auto started?
182  {
183  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
184  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
185  service->stop();
186  }
188  }
189  for( SIDMenuMapType::value_type sid : m_menuSids)
190  {
191  if(sid.second.second) //service is auto started?
192  {
193  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
194  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
195  service->stop();
196  }
198  }
199 }
200 
201 //-----------------------------------------------------------------------------
202 
204 {
205  SLM_WARN("TODO: MenuRegistrar::onItemAction not yet implemented");
206 }
207 
208 //-----------------------------------------------------------------------------
209 
210 } // namespace registrar
211 } //namespace fwGui
FWGUI_API MenuRegistrar(const std::string &sid)
Constructor.
static FWGUI_API void unregisterActionSIDToParentSID(std::string actionSid, std::string parentSid)
Unregisters action sid associted with a parent sid.
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
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
#define SLM_WARN(message)
Definition: spyLog.hpp:261
virtual FWGUI_API::fwGui::container::fwMenu::sptr getParent()
Return the parent container.
SIDMenuMapType m_actionSids
All menu services ID (sid) managed is associated with pair containing: an action&#39;s position in the me...
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 ~MenuRegistrar()
Destructor. Do nothing.
virtual FWGUI_API void onItemAction()
This method is called when an action is clicked.
std::string m_sid
Main service ID associate with this MenuRegistrar.
SIDMenuMapType m_menuSids
All toolBar services ID managed and associated with pair containing: menu&#39;s index vector and boolean ...
virtual FWGUI_API void unmanage()
Stopping menu items manager. All services managed in local menu items will be stopped.
#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
virtual FWGUI_API void manage(std::vector< ::fwGui::container::fwMenuItem::sptr > menuItems)
manages action service associated with menuItem of menu.
virtual FWGUI_API::fwGui::container::fwMenuItem::sptr getFwMenuItem(std::string actionSid, std::vector< ::fwGui::container::fwMenuItem::sptr > menuItems)
Return the fwMenuItem associated with the actionSid.
virtual FWGUI_API void initialize(::fwRuntime::ConfigurationElement::sptr configuration)
Initialize registry managers.
static FWGUI_API void registerActionSIDToParentSID(std::string actionSid, std::string parentSid)
Registers action sid associted with a parent sid.
CallbacksType m_callbacks
Callback associate with menu items.
static FWGUI_API void actionServiceStarting(std::string actionSid)
Method called when the action service is starting. Call parent service actionServiceStarting() method...