CollapsibleDockWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include <iostream>
25 #include <assert.h>
26 #include <QPointer>
27 #include <QSizePolicy>
28 #include <qtreeview.h>
29 #include <QTimer>
30 
31 #include "CollapsibleDockWidget.h"
32 #include "SceneEditorMainWindow.h"
33 
34 gui::CollapsibleDockWidget::CollapsibleDockWidget(const QString& title, QWidget* parent, Qt::WindowFlags flags) :
35  QDockWidget(title, parent, flags)
36 {
37  gui::CollapsibleDockWidget::TitleBar* titleBar = new gui::CollapsibleDockWidget::TitleBar(this);
38  setTitleBarWidget(titleBar);
39 }
40 
41 gui::CollapsibleDockWidget::CollapsibleDockWidget(QWidget* parent, Qt::WindowFlags flags) :
42  QDockWidget(parent, flags)
43 {
44  gui::CollapsibleDockWidget::TitleBar* titleBar = new gui::CollapsibleDockWidget::TitleBar(this);
45  setTitleBarWidget(titleBar);
46 }
47 
49 {
50  gui::CollapsibleDockWidget::InnerWidgetWrapper* wid = new gui::CollapsibleDockWidget::InnerWidgetWrapper(this);
51  wid->setWidget(w);
52  QDockWidget::setWidget(wid);
53 
54  if (isVisible())
55  {
56  widget()->show();
57  }
58 }
59 
61 {
62  gui::CollapsibleDockWidget::InnerWidgetWrapper* innerWidget = dynamic_cast<gui::CollapsibleDockWidget::InnerWidgetWrapper*>(widget());
63 
64  if (innerWidget != NULL)
65  {
66  innerWidget->setCollapsed(collapsed);
67  }
68  else
69  {
70  std::cerr << "DockWidget is not collapsible" << std::endl;
71  }
72 }
73 
75 {
76  gui::CollapsibleDockWidget::InnerWidgetWrapper* innerWidget = dynamic_cast<gui::CollapsibleDockWidget::InnerWidgetWrapper*>(widget());
77  return innerWidget != NULL ? innerWidget->isCollapsed() : false;
78 }
79 
81 {
82  setCollapsed(!isCollapsed());
83 }
84 
86 {
87  gui::CollapsibleDockWidget::TitleBar* titleBar = dynamic_cast<gui::CollapsibleDockWidget::TitleBar*>(this->titleBarWidget());
88 
89  if (titleBar)
90  {
91  titleBar->windowTitleChanged();
92  }
93 }
94 
95 gui::CollapsibleDockWidget::InnerWidgetWrapper::InnerWidgetWrapper(QDockWidget* parent) :
96  QWidget(parent),
97  widget(NULL),
98  hlayout(new QHBoxLayout(this)),
99  widget_height(0),
100  oldSize(0, 0)
101 {
102  this->hlayout->setSpacing(0);
103  this->hlayout->setContentsMargins(0, 0, 0, 0);
104  this->setLayout(this->hlayout);
105  QDockWidget* parentDockWidget = dynamic_cast<QDockWidget*>(parent);
106  assert(parentDockWidget != NULL);
107  oldMinimumSizeParent = parentDockWidget->minimumSize();
108  oldMaximumSizeParent = parentDockWidget->maximumSize();
109  oldMinimumSize = minimumSize();
110  oldMaximumSize = maximumSize();
111 }
112 
113 void gui::CollapsibleDockWidget::InnerWidgetWrapper::setWidget(QWidget* widget)
114 {
115  this->widget = widget;
116  this->widget_height = widget->height();
117  this->layout()->addWidget(widget);
118  this->oldSize = this->size();
119  QDockWidget* parentDockWidget = dynamic_cast<QDockWidget*>(this->parent());
120  assert(parentDockWidget != NULL);
121  oldMinimumSizeParent = parentDockWidget->minimumSize();
122  oldMaximumSizeParent = parentDockWidget->maximumSize();
123  oldMinimumSize = minimumSize();
124  oldMaximumSize = maximumSize();
125 }
126 
127 bool gui::CollapsibleDockWidget::InnerWidgetWrapper::isCollapsed()
128 {
129  return !this->widget->isVisible();
130 }
131 
132 void gui::CollapsibleDockWidget::InnerWidgetWrapper::setCollapsed(bool collapsed)
133 {
134  QDockWidget* parentDockWidget = dynamic_cast<QDockWidget*>(this->parent());
135  assert(parentDockWidget != NULL);
136  gui::CollapsibleDockWidget::TitleBar* parentDockWidgetTitleBar = dynamic_cast<gui::CollapsibleDockWidget::TitleBar*>(parentDockWidget->titleBarWidget());
137  assert(parentDockWidgetTitleBar != NULL);
138 
139  if (!collapsed)
140  {
141  parentDockWidget->setMinimumSize(oldMinimumSizeParent);
142  parentDockWidget->setMaximumSize(oldMaximumSizeParent);
143  this->widget->show();
144  parentDockWidgetTitleBar->showTitle(true);
145  this->layout()->addWidget(this->widget);
146  this->setMinimumSize(oldMinimumSize);
147  this->setMaximumSize(oldMaximumSize);
148  this->setBaseSize(this->oldSize);
149  this->resize(this->oldSize);
150  }
151  else
152  {
153  this->oldSize = this->size();
154  oldMinimumSizeParent = parentDockWidget->minimumSize();
155  oldMaximumSizeParent = parentDockWidget->maximumSize();
156  oldMinimumSize = minimumSize();
157  oldMaximumSize = maximumSize();
158  this->layout()->removeWidget(this->widget);
159  this->widget->hide();
160  parentDockWidgetTitleBar->showTitle(false);
161  parentDockWidget->setMinimumSize(25, 25);
162  parentDockWidget->setMaximumSize(25, 25);
163  QTimer::singleShot(1, parentDockWidget, SLOT(setCollapsedSizes()));
164  }
165 }
166 
167 void gui::CollapsibleDockWidget::setCollapsedSizes()
168 {
169  gui::CollapsibleDockWidget::InnerWidgetWrapper* innerWidget = dynamic_cast<gui::CollapsibleDockWidget::InnerWidgetWrapper*>(widget());
170  assert(innerWidget != NULL);
171  setMinimumSize(25, 25);
172 
173  if (features() & QDockWidget::DockWidgetVerticalTitleBar)
174  {
175  setMaximumSize(25, innerWidget->getOldMaximumSizeParent().height());
176  }
177  else
178  {
179  setMaximumSize(innerWidget->getOldMaximumSizeParent().width(), 25);
180  }
181 }
182 
183 gui::CollapsibleDockWidget::TitleBar::TitleBar(QWidget* parent) :
184  QWidget(parent),
185  hlayout(new QHBoxLayout(this)),
186  collapse(new QPushButton(this)),
187  title(new QLabel(parent->windowTitle()))
188 {
189  this->hlayout->setDirection(QBoxLayout::Direction::RightToLeft);
190  this->hlayout->setSpacing(0);
191  this->hlayout->setContentsMargins(0, 0, 0, 0);
192  this->setLayout(this->hlayout);
193  this->hlayout->addWidget(collapse);
194  collapse->setIcon(QIcon(":/images/branch-open.png"));
195  collapse->setCheckable(false);
196  collapse->setFixedSize(20, 20);
197  connect(collapse, SIGNAL(released()), parent, SLOT(toggleCollapsed()));
198  this->hlayout->addStretch();
199  this->hlayout->addWidget(title);
200 }
201 
202 void gui::CollapsibleDockWidget::TitleBar::windowTitleChanged()
203 {
204  QDockWidget* parentDockWidget = dynamic_cast<QDockWidget*>(this->parent());
205  title->setText(parentDockWidget->windowTitle());
206 }
207 
208 void gui::CollapsibleDockWidget::TitleBar::showTitle(bool show)
209 {
210  title->setVisible(show);
211 
212  if (show)
213  {
214  collapse->setIcon(QIcon(":/images/branch-open.png"));
215  }
216  else
217  {
218  collapse->setIcon(QIcon(":/images/branch-closed.png"));
219  }
220 }
221 
222 QSize const& gui::CollapsibleDockWidget::InnerWidgetWrapper::getOldMaximumSizeParent() const
223 {
224  return oldMaximumSizeParent;
225 }
gui::CollapsibleDockWidget::toggleCollapsed
void toggleCollapsed()
Toggles the collapsed state of the widget.
Definition: CollapsibleDockWidget.cpp:80
gui::CollapsibleDockWidget::isCollapsed
bool isCollapsed()
Returns is the widget is collapsed.
Definition: CollapsibleDockWidget.cpp:74
gui::CollapsibleDockWidget::setCollapsibleWidget
void setCollapsibleWidget(QWidget *w)
Sets a QWidget to show in the DockWidget.
Definition: CollapsibleDockWidget.cpp:48
gui::CollapsibleDockWidget::CollapsibleDockWidget
CollapsibleDockWidget(const QString &title, QWidget *parent=0, Qt::WindowFlags flags=0)
Creates a new CollapsibleDockWidget.
Definition: CollapsibleDockWidget.cpp:34
gui::CollapsibleDockWidget::windowTitleChanged
void windowTitleChanged()
A method to inform the widget, that the window title has been changed.
Definition: CollapsibleDockWidget.cpp:85
SceneEditorMainWindow.h
gui::CollapsibleDockWidget::setCollapsed
void setCollapsed(bool collapsed)
Sets the collapsed state.
Definition: CollapsibleDockWidget.cpp:60
CollapsibleDockWidget.h