fw4spl
QtContainer.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/container/QtContainer.hpp"
8 
9 #include <QDockWidget>
10 #include <QLayout>
11 #include <QList>
12 #include <QMetaObject>
13 #include <QWidget>
14 
15 namespace fwGuiQt
16 {
17 namespace container
18 {
19 
20 //-----------------------------------------------------------------------------
21 
22 QtContainer::QtContainer(::fwGui::GuiBaseObject::Key key) noexcept :
23  m_container(0)
24 {
25 }
26 
27 //-----------------------------------------------------------------------------
28 
29 QtContainer::~QtContainer() noexcept
30 {
31  SLM_ASSERT(
32  "Error during destruction : The qt container included in this class is still allocated, please call destroyContainer() before.",
33  m_container == nullptr );
34 }
35 
36 //-----------------------------------------------------------------------------
37 
38 void QtContainer::setLayout( QLayout* const layout )
39 {
40  SLM_ASSERT("The container must be initialized before invoking setLayout().", m_container);
41 
42  // Recursively delete all children
43  QLayout* oldLayout = m_container->layout();
44 
45  if(nullptr != oldLayout)
46  {
47  this->clean();
48 
49  // Since we will set a new layout, delete the old one as stated in Qt documentation
50  delete oldLayout;
51  oldLayout = nullptr;
52  }
53 
54  // Assign the new layout manager
55  m_container->setLayout(layout);
56 }
57 
58 //-----------------------------------------------------------------------------
59 
60 void QtContainer::clean()
61 {
62  SLM_ASSERT("The container must be initialized before invoking clean().", m_container);
63 
64  // Recursively delete all children
65  QLayout* oldLayout = m_container->layout();
66 
67  if(nullptr != oldLayout)
68  {
69  // This block layouting when there is a lot of child
70  m_container->setUpdatesEnabled(false);
71 
72  for( QLayoutItem* child = oldLayout->takeAt(0); nullptr != child; child = oldLayout->takeAt(0))
73  {
74  delete child;
75  }
76 
77  // Restore update
78  m_container->setUpdatesEnabled(true);
79  }
80 }
81 
82 //-----------------------------------------------------------------------------
83 
84 void QtContainer::destroyContainer()
85 {
86  SLM_ASSERT("The container must be initialized before invoking destroyContainer().", m_container);
87 
88  if(m_container)
89  {
90  delete m_container;
91  m_container.clear();
92  }
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 void QtContainer::setQtContainer(QWidget* container)
98 {
99  m_container = container;
100 }
101 
102 //-----------------------------------------------------------------------------
103 
104 QWidget* QtContainer::getQtContainer()
105 {
106  return m_container;
107 }
108 
109 //-----------------------------------------------------------------------------
110 
111 bool QtContainer::isShownOnScreen()
112 {
113  SLM_ASSERT("The container must be initialized before invoking isShownOnScreen().", m_container);
114  return m_container->isVisible();
115 }
116 
117 //-----------------------------------------------------------------------------
118 
119 void QtContainer::setVisible(bool isVisible)
120 {
121  SLM_ASSERT("The container must be initialized before invoking setVisible().", m_container);
122 
123  QWidget* parent = m_container->parentWidget();
124  QDockWidget* dock = qobject_cast<QDockWidget*>(parent);
125 
126  if(dock)
127  {
128  dock->setVisible(isVisible);
129  }
130 
131  m_container->setVisible(isVisible);
132 }
133 
134 //-----------------------------------------------------------------------------
135 
136 void QtContainer::setEnabled(bool isEnabled)
137 {
138  SLM_ASSERT("The container must be initialized before invoking setEnabled().", m_container);
139 
140  QWidget* parent = m_container->parentWidget();
141  QDockWidget* dock = qobject_cast<QDockWidget*>(parent);
142 
143  if(dock)
144  {
145  dock->setEnabled(isEnabled);
146  }
147 
148  m_container->setEnabled(isEnabled);
149 }
150 
151 //-----------------------------------------------------------------------------
152 
153 } // namespace container
154 } // namespace fwGuiQt
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32
virtual FWGUIQT_API void setLayout(QLayout *const layout)
Assign a layout to the container widget. Previous layout and its children are deleted and the contain...
Definition: QtContainer.cpp:38
#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