fw4spl
ToolBarRegistrar.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/ToolBarRegistrar.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 ToolBarRegistrar::ToolBarRegistrar(const std::string& sid) :
27  m_sid(sid)
28 {
29 }
30 
31 //-----------------------------------------------------------------------------
32 
34 {
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 ::fwGui::container::fwToolBar::sptr ToolBarRegistrar::getParent()
40 {
41  return ::fwGui::GuiRegistry::getSIDToolBar(m_sid);
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 ::fwGui::container::fwMenuItem::sptr ToolBarRegistrar::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 ToolBarRegistrar::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 toolBar with position in toolBars 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 toolBar", m_actionSids.find(
81  sid) == m_actionSids.end());
82  m_actionSids[sid] = SIDToolBarMapType::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  index = 0;
95  // initialize m_menuSids map with configuration
96  std::vector < ConfigurationType > vectMenus = configuration->find("menu");
97  for( ConfigurationType menu : vectMenus)
98  {
99  SLM_ASSERT("<menu> tag must have sid attribute", menu->hasAttribute("sid"));
100  if(menu->hasAttribute("sid"))
101  {
102  bool start = false;
103  if(menu->hasAttribute("start"))
104  {
105  std::string startValue = menu->getAttributeValue("start");
106  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
107  startValue == "yes" || startValue == "no");
108  start = (startValue == "yes");
109  }
110  std::string sid = menu->getAttributeValue("sid");
111  OSLM_ASSERT("Action " << sid << " already exists for this toolBar",
112  m_menuSids.find(sid) == m_menuSids.end());
113  m_menuSids[sid] = SIDToolBarMapType::mapped_type(index, start);
114  }
115  index++;
116  }
117 
118  index = 0;
119  // initialize m_menuSids map with configuration
120  std::vector < ConfigurationType > vectEditors = configuration->find("editor");
121  for( ConfigurationType editor : vectEditors)
122  {
123  SLM_ASSERT("<editor> tag must have sid attribute", editor->hasAttribute("sid") || editor->hasAttribute("wid"));
124  if(editor->hasAttribute("sid"))
125  {
126  bool start = false;
127  if(editor->hasAttribute("start"))
128  {
129  std::string startValue = editor->getAttributeValue("start");
130  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
131  startValue == "yes" || startValue == "no");
132  start = (startValue == "yes");
133  }
134  std::string sid = editor->getAttributeValue("sid");
135  OSLM_ASSERT("Action " << sid << " already exists for this toolBar", m_editorSids.find(
136  sid) == m_editorSids.end());
137  m_editorSids[sid] = SIDToolBarMapType::mapped_type(index, start);
138  }
139  else if(editor->hasAttribute("wid"))
140  {
141  std::string wid = editor->getAttributeValue("wid");
142  m_editorWids[wid] = index;
143  }
144  index++;
145  }
146 }
147 
148 //-----------------------------------------------------------------------------
149 
150 void ToolBarRegistrar::manage(std::vector< ::fwGui::container::fwMenuItem::sptr > menuItems )
151 {
152  ::fwGui::container::fwMenuItem::sptr menuItem;
153  for( SIDToolBarMapType::value_type sid : m_actionSids)
154  {
155  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
156  sid.second.first < menuItems.size());
157  menuItem = menuItems.at( sid.second.first );
159  if(sid.second.second) //service is auto started?
160  {
161  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
162  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
163  OSLM_ASSERT("Service "<<sid.first <<" must be stopped.", service->isStopped() );
164  service->start();
165  }
166  else
167  {
168  bool service_exists = ::fwTools::fwID::exist(sid.first );
169  if (!service_exists || ::fwServices::get( sid.first )->isStopped())
170  {
172  }
173  else
174  {
176  }
177  }
178  }
179 }
180 
181 //-----------------------------------------------------------------------------
182 
183 void ToolBarRegistrar::manage(std::vector< ::fwGui::container::fwMenu::sptr > menus )
184 {
185  ::fwGui::container::fwMenu::sptr menu;
186  for( SIDToolBarMapType::value_type sid : m_menuSids)
187  {
188  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
189  sid.second.first < menus.size());
190  menu = menus.at( sid.second.first );
191  ::fwGui::GuiRegistry::registerSIDMenu(sid.first, menu);
192  if(sid.second.second) //service is auto started?
193  {
194  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
195  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
196  OSLM_ASSERT("Service "<<sid.first <<" must be stopped.", service->isStopped() );
197  service->start();
198  }
199  }
200 }
201 
202 //-----------------------------------------------------------------------------
203 
204 void ToolBarRegistrar::manage(std::vector< ::fwGui::container::fwContainer::sptr > containers )
205 {
206  ::fwGui::container::fwContainer::sptr container;
207  for( SIDToolBarMapType::value_type sid : m_editorSids)
208  {
209  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
210  sid.second.first < containers.size());
211  container = containers.at( sid.second.first );
212  ::fwGui::GuiRegistry::registerSIDContainer(sid.first, container);
213  if(sid.second.second) //service is auto started?
214  {
215  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
216  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
217  OSLM_ASSERT("Service "<<sid.first <<" must be stopped.", service->isStopped() );
218  service->start();
219  }
220  }
221 
222  for( WIDToolBarMapType::value_type wid : m_editorWids)
223  {
224  OSLM_ASSERT("Container index "<< wid.second <<" is bigger than subViews size!", wid.second < containers.size());
225  container = containers.at( wid.second );
226  ::fwGui::GuiRegistry::registerWIDContainer(wid.first, container);
227  }
228 }
229 
230 //-----------------------------------------------------------------------------
231 
233 {
234  for( SIDToolBarMapType::value_type sid : m_actionSids)
235  {
236  if(sid.second.second) //service is auto started?
237  {
238  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
239  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
240  service->stop();
241  }
243  }
244  for( SIDToolBarMapType::value_type sid : m_menuSids)
245  {
246  if(sid.second.second) //service is auto started?
247  {
248  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
249  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
250  service->stop();
251  }
253  }
254  for( SIDToolBarMapType::value_type sid : m_editorSids)
255  {
256  if(sid.second.second) //service is auto started?
257  {
258  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
259  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
260  service->stop();
261  }
263  }
264  for( WIDToolBarMapType::value_type wid : m_editorWids)
265  {
267  }
268 }
269 
270 //-----------------------------------------------------------------------------
271 
273 {
274  SLM_WARN("TODO: ToolBarRegistrar::onItemAction not yet implemented");
275 }
276 
277 //-----------------------------------------------------------------------------
278 
279 } // namespace registrar
280 } //namespace fwGui
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.
static FWGUI_API void unregisterWIDContainer(std::string wid)
Unregisters container associate with window ID.
Definition: GuiRegistry.cpp:96
virtual FWGUI_API void initialize(::fwRuntime::ConfigurationElement::sptr configuration)
Initialize registry managers.
FWGUI_API ToolBarRegistrar(const std::string &sid)
Constructor.
#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
CallbacksType m_callbacks
Callback associate with toolBar items.
WIDToolBarMapType m_editorWids
All toolBar servicesID managed (and associated with subViews index vector).
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
static FWGUI_API void unregisterSIDContainer(std::string sid)
Unregisters container associate with service ID.
Definition: GuiRegistry.cpp:48
virtual FWGUI_API void onItemAction()
This method is called when an action is clicked.
static FWGUI_API void registerSIDContainer(std::string sid,::fwGui::container::fwContainer::sptr container)
Registers container associate with service ID.
Definition: GuiRegistry.cpp:39
#define SLM_WARN(message)
Definition: spyLog.hpp:261
virtual FWGUI_API void manage(std::vector< ::fwGui::container::fwMenuItem::sptr > toolBarItems)
manages action service associated with menuItem of toolbar.
SIDToolBarMapType m_menuSids
All toolBar services ID managed and associated with pair containing: menus index vector and boolean d...
virtual FWGUI_API void unmanage()
Stopping toolBar items manager. All services managed in local toolBar items will be stopped...
virtual FWGUI_API::fwGui::container::fwToolBar::sptr getParent()
Return the parent container.
static FWGUI_API void actionServiceStopping(std::string actionSid)
Method called when the action service is stopping. Call parent service actionServiceStopping() method...
virtual FWGUI_API ~ToolBarRegistrar()
Destructor. Do nothing.
static FWGUI_API void registerWIDContainer(std::string wid,::fwGui::container::fwContainer::sptr container)
Registers container associate with window ID.
Definition: GuiRegistry.cpp:87
static FWGUI_API void unregisterSIDMenu(std::string sid)
Unregisters fwMenu associate with service ID.
#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
SIDToolBarMapType m_editorSids
All toolBar services ID managed and associated with pair containing: editors index vector and boolean...
std::string m_sid
Main service ID associate with this ToolBarRegistrar.
SIDToolBarMapType m_actionSids
All toolBar services ID managed and associated with pair containing: action&#39;s index vector and boolea...
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.
static FWGUI_API void registerActionSIDToParentSID(std::string actionSid, std::string parentSid)
Registers action sid associted with a parent sid.
static FWGUI_API void actionServiceStarting(std::string actionSid)
Method called when the action service is starting. Call parent service actionServiceStarting() method...