fw4spl
IMenuSrv.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/IMenuSrv.hpp"
8 
9 #include "fwGui/IActionSrv.hpp"
10 #include "fwGui/IMenuItemCallback.hpp"
11 
12 #include <fwServices/macros.hpp>
13 #include <fwServices/op/Get.hpp>
14 #include <fwServices/registry/ActiveWorkers.hpp>
15 
16 #include <fwThread/Worker.hpp>
17 #include <fwThread/Worker.hxx>
18 
19 #include <fwTools/fwID.hpp>
20 
21 namespace fwGui
22 {
23 
24 IMenuSrv::IMenuSrv() :
25  m_hideActions(false)
26 {
27 }
28 
29 //-----------------------------------------------------------------------------
30 
31 IMenuSrv::~IMenuSrv()
32 {
33 }
34 
35 //-----------------------------------------------------------------------------
36 
38 {
39  m_registrar = ::fwGui::registrar::MenuRegistrar::New(this->getID());
40  // find ViewRegistryManager configuration
41  std::vector < ConfigurationType > vectRegistrar = m_configuration->find("registry");
42  SLM_ASSERT("Registry section is mandatory.", !vectRegistrar.empty() );
43 
44  if(!vectRegistrar.empty())
45  {
46  m_registrarConfig = vectRegistrar.at(0);
47  m_registrar->initialize(m_registrarConfig);
48  }
49 
50  // find gui configuration
51  std::vector < ConfigurationType > vectGui = m_configuration->find("gui");
52  SLM_ASSERT("Gui section is mandatory.", !vectGui.empty() );
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  if (m_layoutConfig->hasAttribute("hideAction"))
64  {
65  std::string hideActions = m_layoutConfig->getAttributeValue("hideActions");
66  SLM_ASSERT("'hideActions' attribute value must be 'true' or 'false'",
67  hideActions == "true" || hideActions == "false");
68  m_hideActions = (hideActions == "true");
69  }
70  }
71  }
72 }
73 
74 //-----------------------------------------------------------------------------
75 
77 {
78  ::fwGui::container::fwMenu::sptr menu = m_registrar->getParent();
79  std::vector< ::fwGui::IMenuItemCallback::sptr > callbacks = m_registrar->getCallbacks();
80 
81  SLM_ASSERT("Parent menu is unknown.", menu);
82  m_layoutManager->setCallbacks(callbacks);
83 
84  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
85  {
86  m_layoutManager->createLayout(menu);
87  })).wait();
88 
89  m_registrar->manage(m_layoutManager->getMenuItems());
90  m_registrar->manage(m_layoutManager->getMenus());
91 }
92 
93 //-----------------------------------------------------------------------------
94 
96 {
97  m_registrar->unmanage();
98  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
99  {
100  m_layoutManager->destroyLayout();
101  })).wait();
102 }
103 
104 //-----------------------------------------------------------------------------
105 
106 void IMenuSrv::actionServiceStopping(std::string actionSrvSID)
107 {
108  ::fwGui::container::fwMenuItem::sptr menuItem = m_registrar->getFwMenuItem(actionSrvSID,
109  m_layoutManager->getMenuItems());
110 
111  if (m_hideActions)
112  {
113  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
114  {
115  m_layoutManager->menuItemSetVisible(menuItem, false);
116  })).wait();
117  }
118  else
119  {
120  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
121  {
122  m_layoutManager->menuItemSetEnabled(menuItem, false);
123  })).wait();
124  }
125 }
126 
127 //-----------------------------------------------------------------------------
128 
129 void IMenuSrv::actionServiceStarting(std::string actionSrvSID)
130 {
131  ::fwGui::container::fwMenuItem::sptr menuItem = m_registrar->getFwMenuItem(actionSrvSID,
132  m_layoutManager->getMenuItems());
133 
134  if (m_hideActions)
135  {
136  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
137  {
138  m_layoutManager->menuItemSetVisible(menuItem, true);
139  })).wait();
140  }
141  else
142  {
143  ::fwServices::IService::sptr service = ::fwServices::get( actionSrvSID );
144  ::fwGui::IActionSrv::sptr actionSrv = ::fwGui::IActionSrv::dynamicCast(service);
145 
146  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
147  {
148  m_layoutManager->menuItemSetEnabled(menuItem, actionSrv->getIsExecutable());
149  m_layoutManager->menuItemSetChecked(menuItem, actionSrv->getIsActive());
150  m_layoutManager->menuItemSetVisible(menuItem, actionSrv->isVisible());
151  })).wait();
152  }
153 }
154 
155 //-----------------------------------------------------------------------------
156 
157 void IMenuSrv::actionServiceSetActive(std::string actionSrvSID, bool isActive)
158 {
159  ::fwGui::container::fwMenuItem::sptr menuItem = m_registrar->getFwMenuItem(actionSrvSID,
160  m_layoutManager->getMenuItems());
161 
162  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >( [&]
163  {
164  m_layoutManager->menuItemSetChecked(menuItem, isActive);
165  })).wait();
166 }
167 
168 //-----------------------------------------------------------------------------
169 
170 void IMenuSrv::actionServiceSetExecutable(std::string actionSrvSID, bool isExecutable)
171 {
172  ::fwGui::container::fwMenuItem::sptr menuItem = m_registrar->getFwMenuItem(actionSrvSID,
173  m_layoutManager->getMenuItems());
174 
175  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
176  {
177  m_layoutManager->menuItemSetEnabled(menuItem, isExecutable);
178  })).wait();
179 }
180 
181 //-----------------------------------------------------------------------------
182 
183 void IMenuSrv::actionServiceSetVisible(std::string actionSrvSID, bool isVisible)
184 {
185  ::fwGui::container::fwMenuItem::sptr menuItem = m_registrar->getFwMenuItem(actionSrvSID,
186  m_layoutManager->getMenuItems());
187 
188  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
189  {
190  m_layoutManager->menuItemSetVisible(menuItem, isVisible);
191  })).wait();
192 }
193 
194 //-----------------------------------------------------------------------------
195 
196 void IMenuSrv::initializeLayoutManager(ConfigurationType layoutConfig)
197 {
198  OSLM_ASSERT("Bad configuration name "<<layoutConfig->getName()<< ", must be layout",
199  layoutConfig->getName() == "layout");
200 
201  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(
202  ::fwGui::layoutManager::IMenuLayoutManager::REGISTRY_KEY);
203  m_layoutManager = ::fwGui::layoutManager::IMenuLayoutManager::dynamicCast(guiObj);
204  OSLM_ASSERT("ClassFactoryRegistry failed for class "<< ::fwGui::layoutManager::IMenuLayoutManager::REGISTRY_KEY,
205  m_layoutManager);
206 
207  m_layoutManager->initialize(layoutConfig);
208 }
209 
210 //-----------------------------------------------------------------------------
211 
212 }
FWGUI_API void initialize()
Initialize the layout and registry managers.
Definition: IMenuSrv.cpp:37
#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 create()
Create the layout and start the managed services.
Definition: IMenuSrv.cpp:76
FWGUI_API void actionServiceSetVisible(std::string actionSrvSID, bool isVisible)
Method called when the action service is visible.
Definition: IMenuSrv.cpp:183
FWGUI_API void actionServiceSetExecutable(std::string actionSrvSID, bool isExecutable)
Method called when the action service is executable.
Definition: IMenuSrv.cpp:170
FWGUI_API void actionServiceSetActive(std::string actionSrvSID, bool isActive)
Method called when the action service is activated.
Definition: IMenuSrv.cpp:157
FWGUI_API void destroy()
Destroy the layout and stop the managed services.
Definition: IMenuSrv.cpp:95
FWGUI_API void actionServiceStopping(std::string actionSrvSID)
Method called when an action service is stopping.
Definition: IMenuSrv.cpp:106
#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 actionServiceStarting(std::string actionSrvSID)
Method called when an action service is starting.
Definition: IMenuSrv.cpp:129