-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrolloutbox.cpp
198 lines (162 loc) · 5.93 KB
/
rolloutbox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// by Jan Eric Kyprianidis <www.kyprianidis.com>
// Copyright (C) 2010-2012 Computer Graphics Systems Group at the
// Hasso-Plattner-Institut, Potsdam, Germany <www.hpi3d.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
#include "rolloutbox.h"
RolloutBox::RolloutBox(QWidget *parent) : QFrame(parent) {
m_checkable = false;
m_checked = true;
m_layout = new QGridLayout(this);
m_layout->setContentsMargins(0,0,0,0);
m_layout->setSpacing(4);
m_layout->setColumnStretch(0, 50);
m_layout->setColumnStretch(1, 50);
m_toolButton = 0;
m_expanded = true;
m_toolButton = new QToolButton(this);
m_layout->addWidget(m_toolButton, 0, 0);
m_toolButton->setFocusPolicy(Qt::NoFocus);
m_toolButton->setText("open/close");
m_toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
m_toolButton->setVisible(false);
m_toolButton->setAutoRaise(true);
m_toolButton->setFixedHeight(fontMetrics().height()+6);
m_toolButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
m_toolButton->setArrowType(Qt::DownArrow);
QFont font(m_toolButton->font());
font.setWeight(99);
m_toolButton->setFont(font);
connect(m_toolButton, SIGNAL(clicked()), this, SLOT(toggle()));
m_checkBox = new QCheckBox(this);
m_checkBox->setChecked(true);
m_checkBox->setVisible(false);
m_checkBox->setFixedHeight(20);
m_checkBox->setText("enable");
m_layout->addWidget(m_checkBox, 0,1);
connect(m_checkBox, SIGNAL(toggled(bool)), this, SLOT(setChecked(bool)));
}
void RolloutBox::saveSettings(QSettings& settings) {
settings.setValue("expanded", isExpanded());
saveSettings(settings, this);
}
void RolloutBox::restoreSettings(QSettings& settings) {
setExpanded(settings.value("expanded", true).toBool());
restoreSettings(settings, this);
}
void RolloutBox::saveSettings(QSettings& settings, QObject *obj) {
const QObjectList& L = obj->children();
for (int i = 0; i < L.size(); ++i) {
RolloutBox *b = qobject_cast<RolloutBox*>(L[i]);
if (b) {
settings.beginGroup(b->objectName());
b->saveSettings(settings);
settings.endGroup();
}
}
}
void RolloutBox::restoreSettings(QSettings& settings, QObject *obj) {
const QObjectList& L = obj->children();
for (int i = 0; i < L.size(); ++i) {
RolloutBox *b = qobject_cast<RolloutBox*>(L[i]);
if (b) {
settings.beginGroup(b->objectName());
b->restoreSettings(settings);
settings.endGroup();
}
}
}
void RolloutBox::setFrame(bool frame) {
if (frame) {
setFrameShape(QFrame::StyledPanel);
m_layout->setContentsMargins(4,4,4,4);
} else {
setFrameShape(QFrame::NoFrame);
m_layout->setContentsMargins(0,0,0,0);
}
}
void RolloutBox::setTitle(const QString &title) {
if (!title.isEmpty()) {
setFrame(true);
}
m_toolButton->setText(title);
m_toolButton->setVisible(!title.isEmpty());
m_checkBox->setVisible(!title.isEmpty() && m_checkable);
}
void RolloutBox::setCheckable(bool checkable) {
m_checkable = checkable;
m_checkBox->setVisible(!m_toolButton->text().isEmpty() && m_checkable);
}
void RolloutBox::setChecked(bool checked) {
if (m_checked != checked) {
m_checked = checked;
m_checkBox->setChecked(checked);
QList<QWidget*> L = findChildren<QWidget*>();
for (int i = 0; i < L.size(); ++i) {
if ((L[i] != m_toolButton) && (L[i] != m_checkBox)) L[i]->setEnabled(checked);
}
checkChanged(m_checked);
}
}
void RolloutBox::setExpanded(bool expanded) {
if (m_expanded != expanded) {
QScrollArea *sa = 0;
QWidget *p = parentWidget();
while (p) {
sa = qobject_cast<QScrollArea*>(p);
if (sa) break;
p = p->parentWidget();
}
bool areUpdatesEnabled;
if (sa) {
areUpdatesEnabled = updatesEnabled();
sa->setUpdatesEnabled(false);
}
m_expanded = !m_expanded;
QList<QWidget*> L = findChildren<QWidget*>();
for (int i = 0; i < L.size(); ++i) {
if ((L[i] != m_toolButton) && (L[i] != m_checkBox)) L[i]->setVisible(m_expanded);
}
parentWidget()->updateGeometry();
if (sa) {
qApp->processEvents();
if (m_expanded) sa->ensureWidgetVisible(this);
sa->setUpdatesEnabled(true);
}
if (m_toolButton) {
m_toolButton->setArrowType(m_expanded? Qt::DownArrow : Qt::RightArrow);
}
toggled(m_expanded);
}
}
void RolloutBox::toggle() {
setExpanded(!m_expanded);
}
QWidgetList RolloutBox::widgets() const {
QWidgetList W;
QList<QWidget*> L = findChildren<QWidget*>();
for (int i = 0; i < L.size(); ++i) {
if ((L[i] != m_toolButton) && (L[i] != m_checkBox)) W.append(L[i]);
}
return W;
}
void RolloutBox::addWidget(QWidget *w) {
w->setParent(this);
m_layout->addWidget(w, m_layout->rowCount(), 0, 1, 2);
}
void RolloutBox::addWidget(QWidget *left, QWidget *right) {
left->setParent(this);
right->setParent(this);
m_layout->addWidget(left, m_layout->rowCount(), 0, Qt::AlignRight);
m_layout->addWidget(right, m_layout->rowCount()-1, 1);
}