fw4spl
CardinalLayoutManager.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/layoutManager/CardinalLayoutManager.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGui/registry/macros.hpp>
12 
13 #include <QBoxLayout>
14 #include <QDockWidget>
15 #include <QGroupBox>
16 #include <QMainWindow>
17 #include <QScrollArea>
18 
19 fwGuiRegisterMacro( ::fwGui::CardinalLayoutManager,
20  ::fwGui::layoutManager::CardinalLayoutManagerBase::REGISTRY_KEY );
21 
22 namespace fwGui
23 {
24 
25 //-----------------------------------------------------------------------------
26 
27 CardinalLayoutManager::CardinalLayoutManager(::fwGui::GuiBaseObject::Key key)
28 {
29 }
30 
31 //-----------------------------------------------------------------------------
32 
33 CardinalLayoutManager::~CardinalLayoutManager()
34 {
35 }
36 
37 //-----------------------------------------------------------------------------
38 
39 void CardinalLayoutManager::createLayout( ::fwGui::container::fwContainer::sptr parent )
40 {
42  m_parentContainer = ::fwGuiQt::container::QtContainer::dynamicCast(parent);
43  SLM_ASSERT("dynamicCast fwContainer to QtContainer failed", m_parentContainer);
44 
45  m_qtWindow = new QMainWindow( );
46 
47  QBoxLayout* layout = new QBoxLayout(QBoxLayout::LeftToRight);
48  layout->setContentsMargins(0, 0, 0, 0);
49  layout->addWidget( m_qtWindow );
50 
51  m_parentContainer->setLayout(layout);
52 
53  const std::list< ViewInfo>& views = this->getViewsInfo();
54 
55  bool hasCentral = false;
56 
57  for ( ViewInfo viewInfo : views)
58  {
59  QWidget* insideWidget;
60  QScrollArea* scrollArea;
61 
62  if(viewInfo.m_align == CENTER)
63  {
64  if(viewInfo.m_caption.first)
65  {
66  QGroupBox* groupbox = new QGroupBox(m_qtWindow);
67  groupbox->setTitle(QString::fromStdString(viewInfo.m_caption.second));
68  insideWidget = groupbox;
69  }
70  else
71  {
72  insideWidget = new QWidget(m_qtWindow);
73  }
74 
75  QWidget* widget = insideWidget;
76  SLM_ASSERT("multiple center views are not managed in Qt version of CardinalLayoutManager",
77  !hasCentral);
78 
79  if (viewInfo.m_useScrollBar)
80  {
81  scrollArea = new QScrollArea(m_qtWindow);
82  scrollArea->setWidget(widget);
83  scrollArea->setWidgetResizable( true );
84  m_qtWindow->setCentralWidget(scrollArea);
85  }
86  else
87  {
88  m_qtWindow->setCentralWidget(widget);
89  }
90 
91  insideWidget->setVisible(viewInfo.m_visible);
92  hasCentral = true;
93  }
94  else
95  {
96  QDockWidget* dockWidget = new QDockWidget(m_qtWindow);
97  insideWidget = new QWidget(dockWidget);
98  QDockWidget::DockWidgetFeatures features;
99 
100  features = QDockWidget::DockWidgetMovable;
101 
102  Qt::DockWidgetArea area;
103 
104  if(viewInfo.m_align == RIGHT)
105  {
106  area = Qt::RightDockWidgetArea;
107  }
108  else if(viewInfo.m_align == LEFT)
109  {
110  area = Qt::LeftDockWidgetArea;
111  }
112  else if(viewInfo.m_align == BOTTOM)
113  {
114  area = Qt::BottomDockWidgetArea;
115  }
116  else if(viewInfo.m_align == TOP)
117  {
118  area = Qt::TopDockWidgetArea;
119  }
120 
121  m_qtWindow->addDockWidget(area, dockWidget);
122  dockWidget->setFeatures(features);
123 
124  if(viewInfo.m_caption.first)
125  {
126  dockWidget->setWindowTitle( QString::fromStdString(viewInfo.m_caption.second) );
127  dockWidget->setMinimumSize(0, 0);
128  }
129  else
130  {
131  // Remove title bar
132  // Ensure widget->sizeHint() doesn't return a -1 size that will trigger a runtime warning:
133  // As setTitleBarWidget require a widget that have a valid QWidget::sizeHint()
134  // and QWidget::sizeHint() return -1 for widget without a layout...
135  QHBoxLayout* layout = new QHBoxLayout;
136  layout->setSpacing(0);
137  layout->setMargin(0);
138 
139  QWidget* widget = new QWidget;
140  widget->setLayout(layout);
141 
142  dockWidget->setMinimumSize(std::max(viewInfo.m_minSize.first, 0),
143  std::max(viewInfo.m_minSize.second, 0));
144  dockWidget->setTitleBarWidget(widget);
145  }
146 
147  if (viewInfo.m_useScrollBar)
148  {
149  scrollArea = new QScrollArea(dockWidget);
150  scrollArea->setWidget(insideWidget);
151  scrollArea->setWidgetResizable(true);
152  dockWidget->setWidget(scrollArea);
153  }
154  else
155  {
156  dockWidget->setWidget(insideWidget);
157  }
158 
159  if(false == viewInfo.m_visible)
160  {
161  dockWidget->setVisible(false);
162  }
163  }
164 
165  if(!viewInfo.m_isResizable)
166  {
167  //TODO
168  }
169 
170  if (viewInfo.m_useScrollBar)
171  {
172  scrollArea->setMinimumSize(std::max(viewInfo.m_minSize.first, 0), std::max(viewInfo.m_minSize.second, 0));
173  }
174  else
175  {
176  insideWidget->setMinimumSize(std::max(viewInfo.m_minSize.first, 0), std::max(viewInfo.m_minSize.second, 0));
177  }
178 
179  //TODO
180  // - viewInfo.m_position
181  // - viewInfo.m_layer
182  // - viewInfo.m_row
183  // - viewInfo.m_visible
184 
185  ::fwGuiQt::container::QtContainer::sptr subContainer = ::fwGuiQt::container::QtContainer::New();
186  subContainer->setQtContainer(insideWidget);
187  m_subViews.push_back(subContainer);
188  }
189 
190  m_qtWindow->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
191 }
192 
193 //-----------------------------------------------------------------------------
194 
196 {
197  this->destroySubViews();
198  m_parentContainer->clean();
199 }
200 
201 //-----------------------------------------------------------------------------
202 
203 } // namespace fwGui
204 
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Defines the cardinal layout manager.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
virtual FWGUIQT_API void destroyLayout() 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
std::vector< ::fwGui::container::fwContainer::sptr > m_subViews
All sub containers managed by this layout.
virtual FWGUIQT_API void createLayout(::fwGui::container::fwContainer::sptr parent) override
Instantiate layout with parent container.
virtual FWGUI_API void destroySubViews()
Helper to destroy local sub views.