BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
GroupBoxCollapser.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Tool/GroupBoxCollapser.cpp
6 //! @brief Implements class GroupBoxCollapser
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "GroupBoxCollapser.h"
17 #include <QAction>
18 #include <QBoxLayout>
19 #include <QGroupBox>
20 #include <QToolButton>
21 #include <QVariant>
22 
23 GroupBoxCollapser* GroupBoxCollapser::installIntoGroupBox(QGroupBox* groupBox, bool expanded)
24 {
25  auto* p = new GroupBoxCollapser(groupBox);
26 
27  if (!expanded) {
28  QSignalBlocker b(p->m_toggleButton);
29  p->m_toggleButton->setChecked(false);
30  p->m_toggleButton->setArrowType(Qt::ArrowType::RightArrow);
31  p->m_contentArea->hide();
32  }
33 
34  return p;
35 }
36 
38 {
39  if (groupBox == nullptr)
40  return nullptr;
41  return groupBox->findChild<GroupBoxCollapser*>();
42 }
43 
44 void GroupBoxCollapser::setTitle(const QString& title)
45 {
46  m_toggleButton->setText(title);
47 }
48 
49 void GroupBoxCollapser::addAction(QAction* action)
50 {
51  auto* btn = new QToolButton(m_titleWidget);
52  btn->setToolButtonStyle(Qt::ToolButtonIconOnly);
53  btn->setDefaultAction(action);
54  if (action->menu() != nullptr)
55  btn->setPopupMode(QToolButton::InstantPopup);
56 
57  m_titleLayout->addWidget(btn);
58 
59  connect(action, &QAction::changed, [=]() { btn->setVisible(action->isVisible()); });
60 }
61 
62 void GroupBoxCollapser::addWidget(QWidget* widget)
63 {
64  m_titleLayout->addWidget(widget);
65 }
66 
68 {
69  return m_contentArea;
70 }
71 
72 void GroupBoxCollapser::setExpanded(bool expanded)
73 {
74  if (m_toggleButton->isChecked() != expanded)
75  m_toggleButton->click();
76 }
77 
79  : QObject(groupBox)
80 {
81  auto* mainLayout = new QVBoxLayout;
82  mainLayout->setSpacing(0);
83  mainLayout->setContentsMargins(0, 0, 0, 0);
84 
85  m_contentArea = new QWidget(groupBox);
86  m_contentArea->setObjectName("ContentArea");
87  m_contentArea->setLayout(groupBox->layout());
88 
89  mainLayout->addWidget(m_contentArea);
90  groupBox->setLayout(mainLayout);
91 
92  m_toggleButton = new QToolButton(groupBox);
93  m_toggleButton->setObjectName("GroupBoxToggler");
95  m_toggleButton->setStyleSheet(
96  "QToolButton { border: none; text-align: left; font: bold; padding: 5px}");
97  m_toggleButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
98  m_toggleButton->setCheckable(true);
99  m_toggleButton->setText(groupBox->title());
100  m_toggleButton->setArrowType(Qt::ArrowType::DownArrow);
101  m_toggleButton->setChecked(true);
102  m_toggleButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
103 
104  m_titleWidget = new QWidget(groupBox);
105  m_titleWidget->setObjectName("GroupBoxTogglerTitleWidget");
106  m_titleWidget->setAttribute(Qt::WA_StyledBackground, true);
107 
108  m_titleLayout = new QHBoxLayout;
109  m_titleLayout->setContentsMargins(0, 0, 3, 0);
110  m_titleLayout->setSpacing(3);
111  m_titleLayout->setAlignment(Qt::AlignVCenter);
112  m_titleWidget->setLayout(m_titleLayout);
113  m_titleLayout->addWidget(m_toggleButton);
114 
115  groupBox->layout()->setMenuBar(m_titleWidget);
116  groupBox->setTitle("");
117  groupBox->setProperty("collapsible", true); // helps to distinguish GroupBoxes in stylesheets
118 
119  connect(m_toggleButton, &QAbstractButton::clicked, this, &GroupBoxCollapser::toggle);
120 }
121 
122 void GroupBoxCollapser::toggle(bool checked)
123 {
124  m_toggleButton->setArrowType(checked ? Qt::ArrowType::DownArrow : Qt::ArrowType::RightArrow);
125 
126  if (m_toggleButton->isChecked())
127  m_contentArea->show();
128  else
129  m_contentArea->hide();
130 }
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class GroupBoxCollapser.
ApplicationSettings::Style currentStyle()
Add-on to group boxes to make them collapsible.
QWidget * m_contentArea
widget to where the original group box content has been moved
QHBoxLayout * m_titleLayout
layout in the title widget
void setExpanded(bool expanded=true)
Expand/collapse the content area.
GroupBoxCollapser(QGroupBox *groupBox)
void toggle(bool checked)
QWidget * contentArea() const
The content area, to which the found group box content has been moved when the add-on has been instal...
QWidget * m_titleWidget
widget used to present the new groupbox title
void addAction(QAction *action)
Add a tool button to the title bar, connected to the given action.
void addWidget(QWidget *widget)
Add a widget to the title bar.
QToolButton * m_toggleButton
button to toggle between collapsed/expanded
static GroupBoxCollapser * installIntoGroupBox(QGroupBox *groupBox, bool expanded=true)
void setTitle(const QString &title)
Set the title of the group box. Do not use the method groupBox->setTitle() any more once the add-on i...
static GroupBoxCollapser * findInstalledCollapser(QGroupBox *groupBox)