-
Notifications
You must be signed in to change notification settings - Fork 12
/
MultiToolBox.cpp
323 lines (252 loc) · 6.77 KB
/
MultiToolBox.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//
// MultiToolBox.cpp: Toolbox that enables multiple opened sections
// Copyright (C) 2021 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include "MultiToolBox.h"
#include "ui_MultiToolBox.h"
#include <QLayout>
#include <QLayoutItem>
#include <QPushButton>
MultiToolBoxItem::MultiToolBoxItem(
QString const &name,
QWidget *child,
bool visible,
QObject *parent) : QObject(parent),
name(name),
child(child)
{
this->child->setProperty("collapsed", QVariant::fromValue<bool>(!visible));
this->setName(name);
}
void
MultiToolBoxItem::setName(QString const &name)
{
if (name != this->child->windowTitle())
this->child->setProperty("windowTitle", QVariant::fromValue(name));
this->name = name;
}
MultiToolBoxItem::~MultiToolBoxItem(void)
{
}
void
MultiToolBoxItem::setVisible(bool visible)
{
bool propVisible = this->isVisible();
if (propVisible != visible) {
this->child->setProperty("collapsed", QVariant::fromValue<bool>(!visible));
emit stateChanged();
}
}
bool
MultiToolBoxItem::isVisible(void) const
{
return !this->child->property("collapsed").value<bool>();
}
QWidget *
MultiToolBoxItem::getChild(void) const
{
return this->child;
}
QString
MultiToolBoxItem::getName(void) const
{
return this->name;
}
///////////////////////////////// MultiToolBox /////////////////////////////////
MultiToolBox::MultiToolBox(QWidget *parent) :
QWidget(parent),
ui(new Ui::MultiToolBox)
{
ui->setupUi(this);
}
MultiToolBox::~MultiToolBox()
{
delete ui;
}
void
MultiToolBox::refreshVisibility(void)
{
int i;
for (i = 0; i < this->itemList.count(); ++i) {
QPushButton *button = this->buttonList[i];
MultiToolBoxItem *p = this->itemList[i];
if (p->isVisible())
button->setText(" ▼ " + p->getChild()->windowTitle());
else
button->setText(" ▶ " + p->getChild()->windowTitle());
p->getChild()->setVisible(p->isVisible());
}
}
int
MultiToolBox::addItem(MultiToolBoxItem *item)
{
QPushButton *button = nullptr;
if (this->itemLayout == nullptr) {
this->itemLayout = new QVBoxLayout(this->ui->scrollAreaWidgetContents);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
this->itemLayout->setMargin(0);
#endif
this->itemLayout->setSpacing(1);
this->itemLayout->setAlignment(Qt::AlignTop);
}
button = new QPushButton();
button->setProperty(
"multiIndex",
QVariant::fromValue<int>(this->itemList.size()));
item->getChild()->setProperty(
"multiIndex",
QVariant::fromValue<int>(this->itemList.size()));
item->getChild()->installEventFilter(this);
button->setStyleSheet("text-align: left; font-weight: bold");
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
this->itemLayout->addWidget(button);
this->itemLayout->addWidget(item->getChild());
this->itemList.push_back(item);
this->buttonList.push_back(button);
item->setParent(this);
connect(
button,
SIGNAL(clicked(bool)),
this,
SLOT(onToggleVisibility(void)));
connect(
item,
SIGNAL(stateChanged(void)),
this,
SLOT(onStateChanged(void)));
connect(
item->getChild(),
&QWidget::windowTitleChanged,
this,
&MultiToolBox::pageWindowTitleChanged);
this->refreshVisibility();
return this->itemList.size() - 1;
}
void
MultiToolBox::addPage(QWidget *page)
{
int index;
index = this->addItem(new MultiToolBoxItem(page->windowTitle(), page));
this->setCurrentIndex(index);
}
int
MultiToolBox::currentIndex(void) const
{
return this->index;
}
void
MultiToolBox::setCurrentIndex(int index)
{
int i;
if (index != this->index) {
this->index = index;
for (i = 0; i < this->itemList.size(); ++i)
this->itemList[i]->setVisible(i == index);
if (index != -1)
emit currentIndexChanged(index);
}
}
QString
MultiToolBox::pageTitle(void) const
{
MultiToolBoxItem *item;
if ((item = this->itemAt(this->index)) == nullptr)
return "(no page)";
return item->getName();
}
void
MultiToolBox::setPageTitle(QString name)
{
MultiToolBoxItem *item;
if ((item = this->itemAt(this->index)) == nullptr)
return;
item->setName(name);
this->refreshVisibility();
emit pageTitleChanged(name);
}
bool
MultiToolBox::showItem(int index)
{
MultiToolBoxItem *item = this->itemAt(index);
if (item == nullptr)
return false;
item->getChild()->setVisible(true);
return true;
}
bool
MultiToolBox::hideItem(int index)
{
MultiToolBoxItem *item = this->itemAt(index);
if (item == nullptr)
return false;
item->getChild()->setVisible(false);
return true;
}
int
MultiToolBox::count(void) const
{
return this->itemList.size();
}
MultiToolBoxItem *
MultiToolBox::itemAt(int index) const
{
if (index < 0 || index >= this->itemList.size())
return nullptr;
return this->itemList[index];
}
//////////////////////////////////// Slots ////////////////////////////////////
void
MultiToolBox::onToggleVisibility(void)
{
QPushButton *button = static_cast<QPushButton *>(QObject::sender());
QVariant index = button->property("multiIndex");
MultiToolBoxItem *item = this->itemAt(index.value<int>());
if (item != nullptr) {
item->setVisible(!item->isVisible());
if (item->isVisible())
this->index = index.value<int>();
}
}
void
MultiToolBox::onStateChanged(void)
{
this->refreshVisibility();
}
void MultiToolBox::pageWindowTitleChanged()
{
MultiToolBoxItem *item = this->itemAt(this->index);
if (item != nullptr)
this->setPageTitle(item->getChild()->windowTitle());
}
bool
MultiToolBox::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::DynamicPropertyChange) {
QDynamicPropertyChangeEvent *const propEvent = static_cast<QDynamicPropertyChangeEvent*>(event);
QString propName = propEvent->propertyName();
if (propName == "collapsed") {
int index = obj->property("multiIndex").value<int>();
bool visible = !obj->property("collapsed").value<bool>();
if (visible)
this->showItem(index);
else
this->hideItem(index);
this->refreshVisibility();
}
}
return QObject::eventFilter(obj, event);
}