collapsable_groupbox.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
26 #include <QFrame>
27 #include <QHBoxLayout>
28 #include <QPropertyAnimation>
29 #include <QToolButton>
30 #include <QVBoxLayout>
31 
32 #include <corbo-core/console.h>
33 
34 namespace corbo {
35 namespace gui {
36 
37 CollapsableGroupBox::CollapsableGroupBox(const QString& title, QWidget* parent) : QWidget(parent)
38 {
39  _layout = new QVBoxLayout(this);
40  _layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
41  _layout->setContentsMargins(0, 0, 0, 0);
42  _layout->setSpacing(0);
43 
44  createTitle(title);
45  createContentArea();
46 
47  setCollapsed(false);
48 
49  connect(_button, &QToolButton::clicked, [this](bool checked) { setCollapsed(!checked); });
50 }
51 
52 void CollapsableGroupBox::createTitle(const QString& title)
53 {
54  QHBoxLayout* title_layout = new QHBoxLayout;
55  title_layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
56  title_layout->setContentsMargins(0, 0, 0, 0);
57 
58  // create button plus label
59  _button = new QToolButton;
60  _button->setStyleSheet("QToolButton { border: none; }");
61  _button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
62  _button->setArrowType(Qt::ArrowType::RightArrow);
63  _button->setText(title);
64  _button->setCheckable(true);
65  _button->setChecked(false);
66  title_layout->addWidget(_button);
67 
68  // create horizontal line
69  QFrame* hline = new QFrame;
70  hline->setFrameShape(QFrame::HLine);
71  hline->setFrameShadow(QFrame::Sunken);
72  hline->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
73  title_layout->addWidget(hline);
74 
75  _layout->addLayout(title_layout);
76 }
77 
78 void CollapsableGroupBox::createContentArea()
79 {
80  _content = new QGroupBox;
81  _content->setFlat(true);
82  _content->setStyleSheet("QGroupBox { border: none; }");
83 
84  // set collapsed
85  _content->hide();
86 
87  _layout->addWidget(_content);
88 }
89 
90 void CollapsableGroupBox::setCollapsed(bool collapsed)
91 {
92  if (_button->isChecked() == collapsed)
93  {
94  _button->setChecked(!collapsed);
95  return; // retrigger signal to synchronize button status if this method has been called programmatically
96  }
97 
98  _collapsed = collapsed;
99 
100  _button->setArrowType(_collapsed ? Qt::ArrowType::RightArrow : Qt::ArrowType::DownArrow);
101 
102  // int collapsed_height = sizeHint().height() - _content->maximumHeight();
103  if (_collapsed)
104  {
105  _content->hide();
106  }
107  else
108  {
109  _content->show();
110  }
111 }
112 
113 } // namespace gui
114 } // namespace corbo
corbo
Definition: communication/include/corbo-communication/utilities.h:37
console.h
collapsable_groupbox.h
corbo::gui::CollapsableGroupBox::CollapsableGroupBox
CollapsableGroupBox(const QString &title, QWidget *parent=0)
Definition: collapsable_groupbox.cpp:81


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:05:38