fw4spl
GuiRegistry.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/GuiRegistry.hpp"
8 
9 #include "fwGui/IMenuSrv.hpp"
10 #include "fwGui/IToolBarSrv.hpp"
11 
12 #include <fwServices/macros.hpp>
13 #include <fwServices/op/Get.hpp>
14 
15 #include <fwTools/fwID.hpp>
16 
17 namespace fwGui
18 {
19 
20 GuiRegistry::ContainerMapType GuiRegistry::m_globalSIDToFwContainer;
21 GuiRegistry::ContainerMapType GuiRegistry::m_globalWIDToFwContainer;
22 GuiRegistry::MenuBarMapType GuiRegistry::m_globalSIDToFwMenuBar;
23 GuiRegistry::ToolBarMapType GuiRegistry::m_globalSIDToFwToolBar;
24 GuiRegistry::MenuMapType GuiRegistry::m_globalSIDToFwMenu;
25 GuiRegistry::ActionToParentMapType GuiRegistry::m_actionSIDToParentSID;
26 
28 {
29 }
30 
31 //-----------------------------------------------------------------------------
32 
34 {
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 void GuiRegistry::registerSIDContainer(std::string sid, ::fwGui::container::fwContainer::sptr container)
40 {
41  OSLM_ASSERT("A fwContainer with the sid "<<sid<<" already exists in the SID container map.",
42  m_globalSIDToFwContainer.find(sid) == m_globalSIDToFwContainer.end());
43  m_globalSIDToFwContainer[sid] = container;
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
50  bool service_exists = ::fwTools::fwID::exist(sid );
51  OSLM_INFO_IF("Service "<<sid <<" does not exist.", !service_exists );
52  if(service_exists)
53  {
54  ::fwServices::IService::sptr service = ::fwServices::get( sid );
55  OSLM_ASSERT("Service "<<sid<<" must be stopped before unregistering the container.", service->isStopped());
56  }
57 
58  OSLM_ASSERT("No fwContainer with the sid "<<sid<<" exists in the SID container map.",
59  m_globalSIDToFwContainer.find(sid) != m_globalSIDToFwContainer.end());
60 
61  // Removes container in SID container map
62  m_globalSIDToFwContainer.erase(sid);
63 }
64 
65 //-----------------------------------------------------------------------------
66 
67 ::fwGui::container::fwContainer::sptr GuiRegistry::getSIDContainer(std::string sid)
68 {
69  // Returns container in SID container map, null if not found.
70  if (!hasSIDContainer(sid))
71  {
72  return nullptr;
73  }
74  return m_globalSIDToFwContainer[sid];
75 }
76 
77 //-----------------------------------------------------------------------------
78 
79 bool GuiRegistry::hasSIDContainer(std::string sid)
80 {
81  // Returns 'true' if the specified 'sid' is found in the SID container map, else 'false'.
82  return m_globalSIDToFwContainer.find(sid) != m_globalSIDToFwContainer.end();
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 void GuiRegistry::registerWIDContainer(std::string wid, ::fwGui::container::fwContainer::sptr container)
88 {
89  OSLM_ASSERT("A fwContainer with the wid "<<wid<<" already exists in the WID container map.",
90  m_globalWIDToFwContainer.find(wid) == m_globalWIDToFwContainer.end());
91  m_globalWIDToFwContainer[wid] = container;
92 }
93 
94 //-----------------------------------------------------------------------------
95 
97 {
98  OSLM_ASSERT("No fwContainer with the wid "<<wid<<" exists in the WID container map.",
99  m_globalWIDToFwContainer.find(wid) != m_globalWIDToFwContainer.end());
100 
101  // Removes container in WID container map
102  m_globalWIDToFwContainer.erase(wid);
103 }
104 
105 //-----------------------------------------------------------------------------
106 
107 ::fwGui::container::fwContainer::sptr GuiRegistry::getWIDContainer(std::string wid)
108 {
109  // Returns container in WID container map, null if not found.
110  if (!hasWIDContainer(wid))
111  {
112  return nullptr;
113  }
114  return m_globalWIDToFwContainer[wid];
115 }
116 
117 //-----------------------------------------------------------------------------
118 
119 bool GuiRegistry::hasWIDContainer(std::string wid)
120 {
121  // Returns 'true' if the specified 'wid' is found in the WID container map, else 'false'.
122  return m_globalWIDToFwContainer.find(wid) != m_globalWIDToFwContainer.end();
123 }
124 
125 //-----------------------------------------------------------------------------
126 
127 void GuiRegistry::registerSIDMenuBar(std::string sid, ::fwGui::container::fwMenuBar::sptr menuBar)
128 {
129  OSLM_ASSERT("A fwMenuBar with the sid "<<sid<<" already exists in the SID menuBar map.",
130  m_globalSIDToFwMenuBar.find(sid) == m_globalSIDToFwMenuBar.end());
131  m_globalSIDToFwMenuBar[sid] = menuBar;
132 }
133 
134 //-----------------------------------------------------------------------------
135 
137 {
138  bool service_exists = ::fwTools::fwID::exist(sid );
139  OSLM_INFO_IF("Service "<<sid <<" does not exist.", !service_exists );
140  if(service_exists)
141  {
142  ::fwServices::IService::sptr service = ::fwServices::get( sid );
143  OSLM_ASSERT("Service "<<sid<<" must be stopped before unregistering the menuBar.", service->isStopped());
144  }
145 
146  OSLM_ASSERT("No fwMenuBar with the sid "<<sid<<" exists in the SID menuBar map.",
147  m_globalSIDToFwMenuBar.find(sid) != m_globalSIDToFwMenuBar.end());
148 
149  // Removes container in SID container map
150  m_globalSIDToFwMenuBar.erase(sid);
151 }
152 
153 //-----------------------------------------------------------------------------
154 
155 ::fwGui::container::fwMenuBar::sptr GuiRegistry::getSIDMenuBar(std::string sid)
156 {
157  OSLM_ASSERT("No fwMenuBar with the sid "<<sid<<" exists in the SID menuBar map.",
158  m_globalSIDToFwMenuBar.find(sid) != m_globalSIDToFwMenuBar.end());
159  // returns container in SID container map
160  return m_globalSIDToFwMenuBar[sid];
161 }
162 
163 //-----------------------------------------------------------------------------
164 
165 void GuiRegistry::registerSIDToolBar(std::string sid, ::fwGui::container::fwToolBar::sptr toolBar)
166 {
167  OSLM_ASSERT("A fwToolBar with the sid "<<sid<<" already exists in the SID toolBar map.",
168  m_globalSIDToFwToolBar.find(sid) == m_globalSIDToFwToolBar.end());
169  m_globalSIDToFwToolBar[sid] = toolBar;
170 }
171 
172 //-----------------------------------------------------------------------------
173 
175 {
176  bool service_exists = ::fwTools::fwID::exist(sid );
177  OSLM_INFO_IF("Service "<<sid <<" does not exist.", !service_exists );
178  if(service_exists)
179  {
180  ::fwServices::IService::sptr service = ::fwServices::get( sid );
181  OSLM_ASSERT("Service "<<sid<<" must be stopped before unregistering the toolBar.", service->isStopped());
182  }
183 
184  OSLM_ASSERT("No fwToolBar with the sid "<<sid<<" exists in the SID toolBar map.",
185  m_globalSIDToFwToolBar.find(sid) != m_globalSIDToFwToolBar.end());
186 
187  // Removes container in SID container map
188  m_globalSIDToFwToolBar.erase(sid);
189 }
190 
191 //-----------------------------------------------------------------------------
192 
193 ::fwGui::container::fwToolBar::sptr GuiRegistry::getSIDToolBar(std::string sid)
194 {
195  OSLM_ASSERT("No fwToolBar with the sid "<<sid<<" exists in the SID toolBar map.",
196  m_globalSIDToFwToolBar.find(sid) != m_globalSIDToFwToolBar.end());
197  // returns container in SID container map
198  return m_globalSIDToFwToolBar[sid];
199 }
200 
201 //-----------------------------------------------------------------------------
202 
203 void GuiRegistry::registerSIDMenu(std::string sid, ::fwGui::container::fwMenu::sptr menu)
204 {
205  OSLM_ASSERT("A fwMenu with the "<<sid<<" already exists in the SID menu map.",
206  m_globalSIDToFwMenu.find(sid) == m_globalSIDToFwMenu.end());
207  m_globalSIDToFwMenu[sid] = menu;
208 }
209 
210 //-----------------------------------------------------------------------------
211 
212 void GuiRegistry::unregisterSIDMenu(std::string sid)
213 {
214  bool service_exists = ::fwTools::fwID::exist(sid );
215  OSLM_INFO_IF("Service "<<sid <<" does not exist.", !service_exists );
216  if(service_exists)
217  {
218  ::fwServices::IService::sptr service = ::fwServices::get( sid );
219  OSLM_ASSERT("Service "<<sid<<" must be stopped before unregistering the menu.", service->isStopped());
220  }
221 
222  OSLM_ASSERT("No fwMenu with the sid "<<sid<<" exists in the SID menu map.",
223  m_globalSIDToFwMenu.find(sid) != m_globalSIDToFwMenu.end());
224 
225  // Removes container in SID container map
226  m_globalSIDToFwMenu.erase(sid);
227 }
228 
229 //-----------------------------------------------------------------------------
230 
231 ::fwGui::container::fwMenu::sptr GuiRegistry::getSIDMenu(std::string sid)
232 {
233  OSLM_ASSERT("No fwMenu with the sid "<<sid<<" exists in the SID menu map.",
234  m_globalSIDToFwMenu.find(sid) != m_globalSIDToFwMenu.end());
235  // returns container in SID container map
236  return m_globalSIDToFwMenu[sid];
237 }
238 
239 //-----------------------------------------------------------------------------
240 
241 void GuiRegistry::registerActionSIDToParentSID(std::string actionSid, std::string parentSid)
242 {
243  if ( m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end())
244  {
245  // Action already exist in map
246  OSLM_ASSERT("An action with the sid " << actionSid << " already exists for the parent " << parentSid,
247  std::find(m_actionSIDToParentSID[actionSid].begin(), m_actionSIDToParentSID[actionSid].end(),
248  parentSid) == m_actionSIDToParentSID[actionSid].end());
249  }
250  m_actionSIDToParentSID[actionSid].push_back(parentSid);
251 }
252 
253 //-----------------------------------------------------------------------------
254 
255 void GuiRegistry::unregisterActionSIDToParentSID(std::string actionSid, std::string parentSid)
256 {
257 
258  OSLM_ASSERT("No action with the sid "<<actionSid<<" exists in the SID action map.",
259  m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end());
260 
261  if (m_actionSIDToParentSID[actionSid].size() == 1)
262  {
263  // Action has one parent
264  bool service_exists = ::fwTools::fwID::exist(actionSid );
265  OSLM_INFO_IF("Service "<<actionSid <<" does not exist.", !service_exists );
266  if(service_exists)
267  {
268  ::fwServices::IService::sptr service = ::fwServices::get( actionSid );
269  OSLM_WARN_IF("Service "<<actionSid<<" must be stopped before unregistering the action.",
270  !service->isStopped());
271  }
272  m_actionSIDToParentSID.erase(actionSid);
273  }
274  else
275  {
276  // Action has several parents
277  ParentSidsType::iterator iter =
278  std::find(m_actionSIDToParentSID[actionSid].begin(), m_actionSIDToParentSID[actionSid].end(), parentSid);
279  OSLM_ASSERT("The action with the sid "<<actionSid<<" has no parent named "<< parentSid,
280  iter != m_actionSIDToParentSID[actionSid].end());
281  m_actionSIDToParentSID[actionSid].erase(iter);
282  }
283 }
284 
285 //-----------------------------------------------------------------------------
286 
287 void GuiRegistry::actionServiceStopping(std::string actionSid)
288 {
289  if( m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end() )
290  {
291  ParentSidsType parentSids = m_actionSIDToParentSID[actionSid];
292 
293  for(std::string parentSid : parentSids)
294  {
295  bool service_exists = ::fwTools::fwID::exist(parentSid );
296  OSLM_INFO_IF("Service "<<parentSid <<" does not exist.", !service_exists );
297  if(service_exists)
298  {
299  ::fwServices::IService::sptr service = ::fwServices::get( parentSid );
300  ::fwGui::IMenuSrv::sptr menuSrv = ::fwGui::IMenuSrv::dynamicCast(service);
301  ::fwGui::IToolBarSrv::sptr toolbarSrv = ::fwGui::IToolBarSrv::dynamicCast(service);
302  if (menuSrv)
303  {
304  menuSrv->actionServiceStopping(actionSid);
305  }
306  else if (toolbarSrv)
307  {
308  toolbarSrv->actionServiceStopping(actionSid);
309  }
310  else
311  {
312  SLM_FATAL("Unknown service");
313  }
314  }
315  }
316 
317  }
318 }
319 
320 //-----------------------------------------------------------------------------
321 
322 void GuiRegistry::actionServiceStarting(std::string actionSid)
323 {
324  if( m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end() )
325  {
326  ParentSidsType parentSids = m_actionSIDToParentSID[actionSid];
327 
328  for(std::string parentSid : parentSids)
329  {
330  bool service_exists = ::fwTools::fwID::exist(parentSid );
331  OSLM_INFO_IF("Service "<<parentSid <<" does not exist.", !service_exists );
332  if(service_exists)
333  {
334  ::fwServices::IService::sptr service = ::fwServices::get( parentSid );
335  ::fwGui::IMenuSrv::sptr menuSrv = ::fwGui::IMenuSrv::dynamicCast(service);
336  ::fwGui::IToolBarSrv::sptr toolbarSrv = ::fwGui::IToolBarSrv::dynamicCast(service);
337  if (menuSrv)
338  {
339  menuSrv->actionServiceStarting(actionSid);
340  }
341  else if (toolbarSrv)
342  {
343  toolbarSrv->actionServiceStarting(actionSid);
344  }
345  else
346  {
347  SLM_FATAL("Unknown service");
348  }
349  }
350  }
351  }
352 }
353 
354 //-----------------------------------------------------------------------------
355 
356 void GuiRegistry::actionServiceSetActive(std::string actionSid, bool isActive)
357 {
358  if( m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end() )
359  {
360 
361  ParentSidsType parentSids = m_actionSIDToParentSID[actionSid];
362 
363  for(std::string parentSid : parentSids)
364  {
365  bool service_exists = ::fwTools::fwID::exist(parentSid );
366  OSLM_INFO_IF("Service "<<parentSid <<" does not exist.", !service_exists );
367  if(service_exists)
368  {
369  ::fwServices::IService::sptr service = ::fwServices::get( parentSid );
370  ::fwGui::IMenuSrv::sptr menuSrv = ::fwGui::IMenuSrv::dynamicCast(service);
371  ::fwGui::IToolBarSrv::sptr toolbarSrv = ::fwGui::IToolBarSrv::dynamicCast(service);
372  if (menuSrv)
373  {
374  menuSrv->actionServiceSetActive(actionSid, isActive);
375  }
376  else if (toolbarSrv)
377  {
378  toolbarSrv->actionServiceSetActive(actionSid, isActive);
379  }
380  else
381  {
382  SLM_FATAL("Unknown service");
383  }
384  }
385  }
386  }
387 }
388 
389 //-----------------------------------------------------------------------------
390 
391 void GuiRegistry::actionServiceSetExecutable(std::string actionSid, bool isExecutable)
392 {
393  if( m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end() )
394  {
395 
396  ParentSidsType parentSids = m_actionSIDToParentSID[actionSid];
397 
398  for(std::string parentSid : parentSids)
399  {
400  bool service_exists = ::fwTools::fwID::exist(parentSid );
401  OSLM_INFO_IF("Service "<<parentSid <<" does not exist.", !service_exists );
402  if(service_exists)
403  {
404  ::fwServices::IService::sptr service = ::fwServices::get( parentSid );
405  ::fwGui::IMenuSrv::sptr menuSrv = ::fwGui::IMenuSrv::dynamicCast(service);
406  ::fwGui::IToolBarSrv::sptr toolbarSrv = ::fwGui::IToolBarSrv::dynamicCast(service);
407  if (menuSrv)
408  {
409  menuSrv->actionServiceSetExecutable(actionSid, isExecutable);
410  }
411  else if (toolbarSrv)
412  {
413  toolbarSrv->actionServiceSetExecutable(actionSid, isExecutable);
414  }
415  else
416  {
417  SLM_FATAL("Unknown service");
418  }
419  }
420  }
421  }
422 }
423 
424 //-----------------------------------------------------------------------------
425 
426 void GuiRegistry::actionServiceSetVisible(std::string actionSid, bool isVisible)
427 {
428  if( m_actionSIDToParentSID.find(actionSid) != m_actionSIDToParentSID.end() )
429  {
430  ParentSidsType parentSids = m_actionSIDToParentSID[actionSid];
431 
432  for(std::string parentSid : parentSids)
433  {
434  bool service_exists = ::fwTools::fwID::exist(parentSid );
435  OSLM_INFO_IF("Service "<<parentSid <<" does not exist.", !service_exists );
436  if(service_exists)
437  {
438  ::fwServices::IService::sptr service = ::fwServices::get( parentSid );
439  ::fwGui::IMenuSrv::sptr menuSrv = ::fwGui::IMenuSrv::dynamicCast(service);
440  ::fwGui::IToolBarSrv::sptr toolbarSrv = ::fwGui::IToolBarSrv::dynamicCast(service);
441  if (menuSrv)
442  {
443  menuSrv->actionServiceSetVisible(actionSid, isVisible);
444  }
445  else if (toolbarSrv)
446  {
447  toolbarSrv->actionServiceSetVisible(actionSid, isVisible);
448  }
449  else
450  {
451  SLM_FATAL("Unknown service");
452  }
453  }
454  }
455  }
456 }
457 
458 //-----------------------------------------------------------------------------
459 
460 }
static FWGUI_API::fwGui::container::fwMenuBar::sptr getSIDMenuBar(std::string sid)
Returns fwMenuBar associate with service ID.
static FWGUI_API void registerSIDToolBar(std::string sid,::fwGui::container::fwToolBar::sptr toolBar)
Registers fwToolBar associate with service ID.
static FWGUI_API void unregisterActionSIDToParentSID(std::string actionSid, std::string parentSid)
Unregisters action sid associted with a parent sid.
static FWGUI_API::fwGui::container::fwContainer::sptr getWIDContainer(std::string wid)
Returns fwContainer associate with window ID, null if not found.
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
#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
static FWGUI_API void unregisterSIDContainer(std::string sid)
Unregisters container associate with service ID.
Definition: GuiRegistry.cpp:48
static FWGUI_API void actionServiceSetExecutable(std::string actionSid, bool isExecutable)
Method called when the action service is executable or not. Call parent service actionServiceSetExecu...
static FWGUI_API void registerSIDContainer(std::string sid,::fwGui::container::fwContainer::sptr container)
Registers container associate with service ID.
Definition: GuiRegistry.cpp:39
#define OSLM_INFO_IF(message, cond)
Definition: spyLog.hpp:256
static FWGUI_API void unregisterSIDToolBar(std::string sid)
Unregisters fwToolBar associate with service ID.
static FWGUI_API::fwGui::container::fwContainer::sptr getSIDContainer(std::string sid)
Returns fwContainer associate with service ID, null if not found.
Definition: GuiRegistry.cpp:67
static FWGUI_API::fwGui::container::fwMenu::sptr getSIDMenu(std::string sid)
Returns fwMenu associate with service ID.
static FWGUI_API bool hasWIDContainer(std::string wid)
Verifies if a WID exists in the global WID container.
static FWGUI_API void registerSIDMenuBar(std::string sid,::fwGui::container::fwMenuBar::sptr menuBar)
Registers fwMenuBar associate with service ID.
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 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_FATAL(message)
Definition: spyLog.hpp:283
FWGUI_API GuiRegistry()
Constructor.
Definition: GuiRegistry.cpp:27
static FWGUI_API void actionServiceSetActive(std::string actionSid, bool isActive)
Method called when the action service is activated or not. Call parent service actionServiceSetActive...
static FWGUI_API void unregisterSIDMenuBar(std::string sid)
Unregisters fwMenuBar associate with service ID.
virtual FWGUI_API ~GuiRegistry()
Destructor. Do nothing.
Definition: GuiRegistry.cpp:33
static FWGUI_API::fwGui::container::fwToolBar::sptr getSIDToolBar(std::string sid)
Returns fwToolBar associate with service ID.
static FWGUI_API bool hasSIDContainer(std::string sid)
Verifies if a SID exists in the global SID container.
Definition: GuiRegistry.cpp:79
static FWGUI_API void actionServiceSetVisible(std::string actionSid, bool isVisible)
Method called when the action service is visible or not. Call parent service actionServiceSetVisible(...
#define OSLM_WARN_IF(message, cond)
Definition: spyLog.hpp:267
static FWGUI_API void registerActionSIDToParentSID(std::string actionSid, std::string parentSid)
Registers action sid associted with a parent sid.
static ActionToParentMapType m_actionSIDToParentSID
Parent sid can be Menu sid or ToolBar sid.
static FWGUI_API void actionServiceStarting(std::string actionSid)
Method called when the action service is starting. Call parent service actionServiceStarting() method...