fw4spl
IGuiContainerSrv.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/IGuiContainerSrv.hpp"
8 
9 #include "fwGui/builder/IContainerBuilder.hpp"
10 #include "fwGui/builder/ISlideViewBuilder.hpp"
11 #include "fwGui/builder/IToolBarBuilder.hpp"
12 #include "fwGui/container/fwContainer.hpp"
14 #include "fwGui/registrar/ViewRegistrar.hpp"
15 
16 #include <fwCom/Slot.hpp>
17 #include <fwCom/Slot.hxx>
18 #include <fwCom/Slots.hpp>
19 #include <fwCom/Slots.hxx>
20 
21 #include <fwCore/base.hpp>
22 
23 #include <fwServices/macros.hpp>
24 #include <fwServices/registry/ActiveWorkers.hpp>
25 
26 #include <fwTools/fwID.hpp>
27 
28 namespace fwGui
29 {
30 
31 const ::fwCom::Slots::SlotKeyType IGuiContainerSrv::s_SET_ENABLED_SLOT = "setEnabled";
32 const ::fwCom::Slots::SlotKeyType IGuiContainerSrv::s_ENABLE_SLOT = "enable";
33 const ::fwCom::Slots::SlotKeyType IGuiContainerSrv::s_DISABLE_SLOT = "disable";
34 const ::fwCom::Slots::SlotKeyType IGuiContainerSrv::s_SET_VISIBLE_SLOT = "setVisible";
35 const ::fwCom::Slots::SlotKeyType IGuiContainerSrv::s_SHOW_SLOT = "show";
36 const ::fwCom::Slots::SlotKeyType IGuiContainerSrv::s_HIDE_SLOT = "hide";
37 
38 //-----------------------------------------------------------------------------
39 
40 IGuiContainerSrv::IGuiContainerSrv() :
41  m_viewLayoutManagerIsCreated(false),
42  m_hasToolBar(false)
43 {
44  newSlot(s_SET_ENABLED_SLOT, &IGuiContainerSrv::setEnabled, this);
45  newSlot(s_ENABLE_SLOT, &IGuiContainerSrv::enable, this);
46  newSlot(s_DISABLE_SLOT, &IGuiContainerSrv::disable, this);
48  newSlot(s_SHOW_SLOT, &IGuiContainerSrv::show, this);
49  newSlot(s_HIDE_SLOT, &IGuiContainerSrv::hide, this);
50 }
51 
52 //-----------------------------------------------------------------------------
53 
54 IGuiContainerSrv::~IGuiContainerSrv()
55 {
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61 {
62  SLM_ASSERT("Service hasn't configuration", m_configuration);
63 
64  // Create view registrar
65  m_viewRegistrar = ::fwGui::registrar::ViewRegistrar::New( this->getID() );
66  // find ViewRegistrar configuration
67  std::vector < ConfigurationType > vectViewMng = m_configuration->find("registry");
68  if ( !vectViewMng.empty() )
69  {
70  m_viewRegistrarConfig = vectViewMng.at(0);
71  m_viewRegistrar->initialize(m_viewRegistrarConfig);
72  }
73 
74  // Create initializeLayoutManager
75  // find gui configuration
76  std::vector < ConfigurationType > vectGui = m_configuration->find("gui");
77  if( !vectGui.empty() )
78  {
79  SLM_ASSERT("No <registry> tag is allowed in the <gui> section", vectGui.at(0)->find("registry").empty());
80 
81  // find view LayoutManager configuration
82  std::vector < ConfigurationType > vectLayoutMng = vectGui.at(0)->find("layout");
83  if( !vectLayoutMng.empty() )
84  {
85  m_viewLayoutConfig = vectLayoutMng.at(0);
86  this->initializeLayoutManager( m_viewLayoutConfig );
87  m_viewLayoutManagerIsCreated = true;
88  }
89 
90  // find toolBarBuilder configuration
91  std::vector < ConfigurationType > vectTBBuilder = vectGui.at(0)->find("toolBar");
92  if(!vectTBBuilder.empty())
93  {
94  m_toolBarConfig = vectTBBuilder.at(0);
95  this->initializeToolBarBuilder(m_toolBarConfig);
96 
97  m_hasToolBar = true;
98  }
99 
100  // find slideView configuration
101  std::vector < ConfigurationType > vectSlideCfg = vectGui.at(0)->find("slideView");
102  for (const auto& slideCfg : vectSlideCfg)
103  {
104  this->initializeSlideViewBuilder(slideCfg);
105  }
106  }
107 }
108 
109 //-----------------------------------------------------------------------------
110 
112 {
113  SLM_ASSERT("ViewRegistrar must be initialized.", m_viewRegistrar);
114  ::fwGui::container::fwContainer::sptr parent = m_viewRegistrar->getParent();
115  SLM_ASSERT("Parent container is unknown.", parent);
116 
117  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask< void >([this, &parent]
118  {
119  SLM_ASSERT("ViewRegistrar must be initialized.", m_viewRegistrar);
120 
121  ::fwGui::GuiBaseObject::sptr guiObj =
122  ::fwGui::factory::New(::fwGui::builder::IContainerBuilder::REGISTRY_KEY);
123  m_containerBuilder = ::fwGui::builder::IContainerBuilder::dynamicCast(guiObj);
124 
125  SLM_ASSERT("ClassFactoryRegistry failed for class "+ ::fwGui::builder::IContainerBuilder::REGISTRY_KEY,
126  m_containerBuilder);
127  m_containerBuilder->createContainer(parent);
128 
129  ::fwGui::container::fwContainer::sptr container = m_containerBuilder->getContainer();
130 
131  if ( m_viewLayoutManagerIsCreated )
132  {
133  if (m_hasToolBar)
134  {
136  {
137  m_toolBarBuilder->createToolBar(parent);
138  }).wait();
139 
140  m_viewRegistrar->manageToolBar(m_toolBarBuilder->getToolBar());
141  }
142 
144  {
145  m_viewLayoutManager->createLayout(container);
146  }).wait();
147 
148  std::vector< ::fwGui::container::fwContainer::sptr > views = m_viewLayoutManager->getSubViews();
149 
150  for (const auto& slideBuilder : m_slideViewBuilders)
151  {
152  SLM_ASSERT("Slide builder is not instantiated", slideBuilder);
154  {
155  slideBuilder->createContainer(container);
156  }).wait();
157  views.push_back(slideBuilder->getContainer());
158  }
159 
160  m_viewRegistrar->manage(views);
161  }
162  }).wait();
163 }
164 
165 //-----------------------------------------------------------------------------
166 
168 {
169  SLM_ASSERT("ViewRegistrar must be initialized.", m_viewRegistrar);
170 
171  if ( m_viewLayoutManagerIsCreated )
172  {
173  if (m_hasToolBar)
174  {
175  m_viewRegistrar->unmanageToolBar();
176  SLM_ASSERT("ToolBarBuilder must be initialized.", m_toolBarBuilder);
177 
179  {
180  m_toolBarBuilder->destroyToolBar();
181  }).wait();
182  }
183 
184  m_viewRegistrar->unmanage();
185  SLM_ASSERT("ViewLayoutManager must be initialized.", m_viewLayoutManager);
186 
188  {
189  m_viewLayoutManager->destroyLayout();
190  }).wait();
191  }
192 
193  for (const auto& slideBuilder : m_slideViewBuilders)
194  {
195  SLM_ASSERT("Slide builder is not instantiated", slideBuilder);
197  {
198  slideBuilder->destroyContainer();
199  }).wait();
200  }
201 
202  m_containerBuilder->destroyContainer();
203 }
204 
205 //-----------------------------------------------------------------------------
206 
207 void IGuiContainerSrv::initializeLayoutManager(ConfigurationType layoutConfig)
208 {
209  SLM_ASSERT("Bad configuration name "+layoutConfig->getName()+ ", must be layout",
210  layoutConfig->getName() == "layout");
211  SLM_ASSERT("<layout> tag must have type attribute", layoutConfig->hasAttribute("type"));
212  const std::string layoutManagerClassName = layoutConfig->getAttributeValue("type");
213 
214  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(layoutManagerClassName);
215  m_viewLayoutManager = ::fwGui::layoutManager::IViewLayoutManager::dynamicCast(guiObj);
216  SLM_ASSERT("ClassFactoryRegistry failed for class "+ layoutManagerClassName, m_viewLayoutManager);
217 
218  m_viewLayoutManager->initialize(layoutConfig);
219 }
220 
221 //-----------------------------------------------------------------------------
222 
223 void IGuiContainerSrv::initializeToolBarBuilder(ConfigurationType toolBarConfig)
224 {
225  SLM_ASSERT("Bad configuration name "+toolBarConfig->getName()+ ", must be toolBar",
226  toolBarConfig->getName() == "toolBar");
227 
228  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(::fwGui::builder::IToolBarBuilder::REGISTRY_KEY);
229  m_toolBarBuilder = ::fwGui::builder::IToolBarBuilder::dynamicCast(guiObj);
230  SLM_ASSERT("ClassFactoryRegistry failed for class "+ ::fwGui::builder::IToolBarBuilder::REGISTRY_KEY,
231  m_toolBarBuilder);
232 
233  m_toolBarBuilder->initialize(toolBarConfig);
234 }
235 
236 //-----------------------------------------------------------------------------
237 
238 void IGuiContainerSrv::initializeSlideViewBuilder(ConfigurationType slideViewConfig)
239 {
240  SLM_ASSERT("Bad configuration name " + slideViewConfig->getName() + ", must be layout",
241  slideViewConfig->getName() == "slideView");
242 
243  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(
244  ::fwGui::builder::ISlideViewBuilder::REGISTRY_KEY);
245  ::fwGui::builder::ISlideViewBuilder::sptr slideViewBuildfer = ::fwGui::builder::ISlideViewBuilder::dynamicCast(
246  guiObj);
247  SLM_ASSERT("ClassFactoryRegistry failed for class "+ ::fwGui::builder::ISlideViewBuilder::REGISTRY_KEY,
248  slideViewBuildfer);
249 
250  slideViewBuildfer->initialize(slideViewConfig);
251 
252  m_slideViewBuilders.push_back(slideViewBuildfer);
253 }
254 
255 //-----------------------------------------------------------------------------
256 
257 ::fwGui::container::fwContainer::sptr IGuiContainerSrv::getContainer()
258 {
259  return m_containerBuilder->getContainer();
260 }
261 
262 //-----------------------------------------------------------------------------
263 
264 void IGuiContainerSrv::setParent(std::string wid)
265 {
266  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask< void >(std::function< void() >([this, &wid]
267  {
268  m_viewRegistrar->setParent(wid);
269  ::fwGui::container::fwContainer::sptr parent = m_viewRegistrar->getParent();
270  SLM_ASSERT("Parent container is unknown.", parent);
271  m_containerBuilder->setParent(parent);
272  } ));
273 }
274 
275 //-----------------------------------------------------------------------------
276 
277 void IGuiContainerSrv::setEnabled(bool isEnabled)
278 {
279  ::fwGui::container::fwContainer::sptr container = m_viewRegistrar->getParent();
280  container->setEnabled(isEnabled);
281 }
282 
283 //-----------------------------------------------------------------------------
284 
286 {
287  this->setEnabled(true);
288 }
289 
290 //-----------------------------------------------------------------------------
291 
293 {
294  this->setEnabled(false);
295 }
296 
297 //-----------------------------------------------------------------------------
298 
299 void IGuiContainerSrv::setVisible(bool isVisible)
300 {
301  ::fwGui::container::fwContainer::sptr container = m_viewRegistrar->getParent();
302  container->setVisible(isVisible);
303 }
304 
305 //-----------------------------------------------------------------------------
306 
308 {
309  this->setVisible(true);
310 }
311 
312 //-----------------------------------------------------------------------------
313 
315 {
316  this->setVisible(false);
317 }
318 
319 //-----------------------------------------------------------------------------
320 
321 }
static FWGUI_APIconst::fwCom::Slots::SlotKeyType s_SET_VISIBLE_SLOT
Slot to show/hide the container.
FWGUI_API void setEnabled(bool isEnabled)
SLOT: enable/disable the container.
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
static const ::fwCom::Slots::SlotKeyType s_SHOW_SLOT
Slot to show the container.
Definition: IFrameSrv.hpp:98
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
This file defines the interface of the base class for managing a layout geometry. ...
static FWGUI_APIconst::fwCom::Slots::SlotKeyType s_HIDE_SLOT
Slot to hide the container.
static FWGUI_APIconst::fwCom::Slots::SlotKeyType s_DISABLE_SLOT
Slot to disable the container.
static const ::fwCom::Slots::SlotKeyType s_HIDE_SLOT
Slot to hide the container.
Definition: IFrameSrv.hpp:101
FWTOOLS_API IDType getID(Policy policy=GENERATE) const
Returns the id of the object. If it is not set and the policy value is.
Definition: fwID.cpp:78
static FWGUI_APIconst::fwCom::Slots::SlotKeyType s_ENABLE_SLOT
Slot to enable the container.
FWGUI_API void setVisible(bool isVisible)
SLOT: show/hide the container.
#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
FWGUI_API void enable()
SLOT: enable the container.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
static FWSERVICES_API::fwThread::Worker::sptr getDefaultWorker()
Get the default registered worker.
static FWGUI_APIconst::fwCom::Slots::SlotKeyType s_SET_ENABLED_SLOT
Slot to enable/disable the action.
static FWGUI_APIconst::fwCom::Slots::SlotKeyType s_SHOW_SLOT
Slot to show the container.
FWGUI_API void disable()
SLOT: disable the container.
FWGUI_API void hide()
SLOT: hide the container.
static const ::fwCom::Slots::SlotKeyType s_SET_VISIBLE_SLOT
Slot to show/hide the container.
Definition: IFrameSrv.hpp:95
FWGUI_API void show()
SLOT: show the container.
FWGUI_API void initialize()
Initialize managers.