fw4spl
IMenuBarSrv.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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/IMenuBarSrv.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwServices/macros.hpp>
12 #include <fwServices/registry/ActiveWorkers.hpp>
13 
14 #include <fwThread/Worker.hpp>
15 #include <fwThread/Worker.hxx>
16 
17 #include <fwTools/fwID.hpp>
18 
19 namespace fwGui
20 {
21 
22 IMenuBarSrv::IMenuBarSrv() :
23  m_hideMenus(false)
24 {
25 }
26 
27 //-----------------------------------------------------------------------------
28 
29 IMenuBarSrv::~IMenuBarSrv()
30 {
31 }
32 
33 //-----------------------------------------------------------------------------
34 
36 {
37 
38  m_registrar = ::fwGui::registrar::MenuBarRegistrar::New(this->getID());
39  // find ViewRegistryManager configuration
40  std::vector < ConfigurationType > vectRegistrar = m_configuration->find("registry");
41  SLM_ASSERT("Registry section is mandatory.", !vectRegistrar.empty() );
42 
43  if(!vectRegistrar.empty())
44  {
45  m_registrarConfig = vectRegistrar.at(0);
46  m_registrar->initialize(m_registrarConfig);
47  }
48 
49  // find gui configuration
50  std::vector < ConfigurationType > vectGui = m_configuration->find("gui");
51  SLM_ASSERT("Gui section is mandatory.", !vectGui.empty() );
52 
53  if(!vectGui.empty())
54  {
55  // find LayoutManager configuration
56  std::vector < ConfigurationType > vectLayoutMng = vectGui.at(0)->find("layout");
57  SLM_ASSERT("layout section is mandatory.", !vectLayoutMng.empty() );
58  if(!vectLayoutMng.empty())
59  {
60  m_layoutConfig = vectLayoutMng.at(0);
61  this->initializeLayoutManager(m_layoutConfig);
62  }
63  }
64 }
65 
66 //-----------------------------------------------------------------------------
67 
69 {
70  ::fwGui::container::fwMenuBar::sptr menuBar = m_registrar->getParent();
71  SLM_ASSERT("Parent menuBar is unknown.", menuBar);
72 
73  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >( [&]
74  {
75  m_layoutManager->createLayout(menuBar);
76  }) ).wait();
77 
78  m_registrar->manage(m_layoutManager->getMenus());
79 }
80 
81 //-----------------------------------------------------------------------------
82 
84 {
85  m_registrar->unmanage();
86 
87  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
88  {
89  m_layoutManager->destroyLayout();
90  })).wait();
91 }
92 
93 //-----------------------------------------------------------------------------
94 
95 void IMenuBarSrv::menuServiceStopping(std::string menuSrvSID)
96 {
97  ::fwGui::container::fwMenu::sptr menu = m_registrar->getFwMenu(menuSrvSID, m_layoutManager->getMenus());
98 
99  if (m_hideMenus)
100  {
101  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >( [&]
102  {
103  m_layoutManager->menuIsVisible(menu, false);
104  }) ).wait();
105  }
106  else
107  {
108  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >(
109  [&] {
110  m_layoutManager->menuIsEnabled(menu, false);
111  })).wait();
112  }
113 }
114 
115 //-----------------------------------------------------------------------------
116 
117 void IMenuBarSrv::menuServiceStarting(std::string menuSrvSID)
118 {
119  ::fwGui::container::fwMenu::sptr menu = m_registrar->getFwMenu(menuSrvSID, m_layoutManager->getMenus());
120 
121  if (m_hideMenus)
122  {
123  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
124  {
125  m_layoutManager->menuIsVisible(menu, true);
126  })).wait();
127  }
128  else
129  {
130  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
131  {
132  m_layoutManager->menuIsEnabled(menu, true);
133  }) ).wait();
134  }
135 }
136 
137 //-----------------------------------------------------------------------------
138 
139 void IMenuBarSrv::initializeLayoutManager(ConfigurationType layoutConfig)
140 {
141  OSLM_ASSERT("Bad configuration name "<<layoutConfig->getName()<< ", must be layout",
142  layoutConfig->getName() == "layout");
143 
144  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(
145  ::fwGui::layoutManager::IMenuBarLayoutManager::REGISTRY_KEY);
146  m_layoutManager = ::fwGui::layoutManager::IMenuBarLayoutManager::dynamicCast(guiObj);
147  OSLM_ASSERT("ClassFactoryRegistry failed for class "<< ::fwGui::layoutManager::IMenuBarLayoutManager::REGISTRY_KEY,
148  m_layoutManager);
149 
150  m_layoutManager->initialize(layoutConfig);
151 }
152 
153 //-----------------------------------------------------------------------------
154 
155 }
FWGUI_API void menuServiceStopping(std::string menuSrvSID)
Method called when a menu service is stopping.
Definition: IMenuBarSrv.cpp:95
#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
FWGUI_API void initialize()
Initialize the layout and registry managers.
Definition: IMenuBarSrv.cpp:35
FWGUI_API void destroy()
Destroy the layout and stop the managed services.
Definition: IMenuBarSrv.cpp:83
#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
static FWSERVICES_API::fwThread::Worker::sptr getDefaultWorker()
Get the default registered worker.
FWGUI_API void create()
Create the layout and start the managed services.
Definition: IMenuBarSrv.cpp:68
FWGUI_API void menuServiceStarting(std::string menuSrvSID)
Method called when a menu service is starting.