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