fw4spl
ContainerBuilder.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 "fwGuiQt/builder/ContainerBuilder.hpp"
8 
9 #include "fwGuiQt/container/QtContainer.hpp"
10 
11 #include <fwGui/registry/macros.hpp>
12 
13 #include <QMainWindow>
14 #include <QVBoxLayout>
15 #include <QWidget>
16 
17 fwGuiRegisterMacro(::fwGui::builder::ContainerBuilder, ::fwGui::builder::IContainerBuilder::REGISTRY_KEY);
18 
19 namespace fwGui
20 {
21 namespace builder
22 {
23 
24 //-----------------------------------------------------------------------------
25 
26 ContainerBuilder::ContainerBuilder(::fwGui::GuiBaseObject::Key key)
27 {
28 }
29 
30 //-----------------------------------------------------------------------------
31 
32 ContainerBuilder::~ContainerBuilder()
33 {
34 }
35 
36 //-----------------------------------------------------------------------------
37 
38 void ContainerBuilder::createContainer( ::fwGui::container::fwContainer::sptr parent )
39 {
40  m_parent = ::fwGuiQt::container::QtContainer::dynamicCast(parent);
41  SLM_ASSERT("The parent container is not a QtContainer", m_parent);
42 
43  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::New();
44  QWidget* widget = new QWidget();
45  qtContainer->setQtContainer(widget);
46 
47  QVBoxLayout* layout = new QVBoxLayout();
48  layout->setContentsMargins(0, 0, 0, 0);
49  layout->addWidget(widget);
50 
51  m_parent->setLayout(layout);
52 
53  m_container = qtContainer;
54 }
55 
56 //-----------------------------------------------------------------------------
57 
59 {
60  SLM_ASSERT("The Container is not initialized", m_container);
61  SLM_ASSERT("The parent container is not a QtContainer", m_parent);
62 
63  m_container->destroyContainer();
64 }
65 
66 //-----------------------------------------------------------------------------
67 
68 void ContainerBuilder::setParent(::fwGui::container::fwContainer::sptr parent)
69 {
70  SLM_ASSERT("The QtContainer is not yet initialized, cleaning is thus impossible", m_container);
71  ::fwGuiQt::container::QtContainer::sptr parentContainer = ::fwGuiQt::container::QtContainer::dynamicCast(parent);
72  SLM_ASSERT("dynamicCast fwContainer to QtContainer failed", parentContainer);
73  ::fwGuiQt::container::QtContainer::sptr container = ::fwGuiQt::container::QtContainer::dynamicCast(m_container);
74  SLM_ASSERT("dynamicCast fwContainer to QtContainer failed", container);
75 
76  QWidget* qtContainer = container->getQtContainer();
77  SLM_ASSERT("The QtContainer is not yet initialized", qtContainer);
78  QWidget* qtParent = parentContainer->getQtContainer();
79  SLM_ASSERT("The parent's QtContainer is not yet initialized", qtParent);
80 
81  if(qtParent != m_parent->getQtContainer() )
82  {
83  if(!qtParent->layout())
84  {
85  SLM_TRACE("New parent container hasn't layout");
86  QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom);
87  layout->setContentsMargins(0, 0, 0, 0);
88  qtParent->setLayout(layout);
89  }
90  qtParent->layout()->addWidget(qtContainer);
91  qtContainer->update();
92 
93  m_parent = parentContainer;
94  }
95 }
96 
97 //-----------------------------------------------------------------------------
98 
99 } // namespace builder
100 } // namespace fwGui
101 
virtual FWGUIQT_API void createContainer(::fwGui::container::fwContainer::sptr parent) override
Instantiate layout with parent container.
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
virtual FWGUIQT_API void setParent(::fwGui::container::fwContainer::sptr parent) override
Replace parent container.
virtual FWGUIQT_API void destroyContainer() override
Destroy local layout with sub containers.
#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
Defines the generic layout manager for IHM.
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
::fwGui::container::fwContainer::sptr m_container
Container.