-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathctkCollapsibleGroupBox.cpp
332 lines (300 loc) · 11.3 KB
/
ctkCollapsibleGroupBox.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
323
324
325
326
327
328
329
330
331
332
/*=========================================================================
Library: CTK
Copyright (c) Kitware Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
// Qt includes
#include <QApplication>
#include <QDebug>
#include <QChildEvent>
#include <QMouseEvent>
#include <QStylePainter>
#include <QStyleOptionGroupBox>
#include <QStyle>
// CTK includes
#include "ctkCollapsibleGroupBox.h"
#include "ctkProxyStyle.h"
//-----------------------------------------------------------------------------
class ctkCollapsibleGroupBoxStyle:public ctkProxyStyle
{
public:
typedef ctkProxyStyle Superclass;
ctkCollapsibleGroupBoxStyle(QStyle* style = 0, QObject* parent =0)
: Superclass(style, parent)
{
}
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption * opt, QPainter * p, const QWidget * widget = 0) const
{
if (pe == QStyle::PE_IndicatorCheckBox)
{
const ctkCollapsibleGroupBox* groupBox= qobject_cast<const ctkCollapsibleGroupBox*>(widget);
if (groupBox)
{
this->Superclass::drawPrimitive(groupBox->isChecked() ? QStyle::PE_IndicatorArrowDown : QStyle::PE_IndicatorArrowRight, opt, p, widget);
return;
}
}
this->Superclass::drawPrimitive(pe, opt, p, widget);
}
virtual int pixelMetric(PixelMetric metric, const QStyleOption * option, const QWidget * widget) const
{
if (metric == QStyle::PM_IndicatorHeight)
{
const ctkCollapsibleGroupBox* groupBox= qobject_cast<const ctkCollapsibleGroupBox*>(widget);
if (groupBox)
{
return groupBox->fontMetrics().height();
}
}
return this->Superclass::pixelMetric(metric, option, widget);
}
};
//-----------------------------------------------------------------------------
class ctkCollapsibleGroupBoxPrivate
{
Q_DECLARE_PUBLIC(ctkCollapsibleGroupBox);
protected:
ctkCollapsibleGroupBox* const q_ptr;
public:
ctkCollapsibleGroupBoxPrivate(ctkCollapsibleGroupBox& object);
void init();
void setChildVisibility(QWidget* childWidget);
/// Size of the widget for collapsing
QSize OldSize;
/// Maximum allowed height
int MaxHeight;
int CollapsedHeight;
/// We change the visibility of the children in setChildrenVisibility
/// and we track when the visibility is changed to force it back to possibly
/// force the child to be hidden. To prevent infinite loop we need to know
/// who is changing children's visibility.
bool ForcingVisibility;
/// Sometimes the creation of the widget is not done inside setVisible,
/// as we need to do special processing the first time the groupBox is
/// setVisible, we track its created state with the variable
bool IsStateCreated;
/// Pointer to keep track of the proxy style
ctkCollapsibleGroupBoxStyle* GroupBoxStyle;
};
//-----------------------------------------------------------------------------
ctkCollapsibleGroupBoxPrivate::ctkCollapsibleGroupBoxPrivate(
ctkCollapsibleGroupBox& object)
:q_ptr(&object)
{
this->ForcingVisibility = false;
this->IsStateCreated = false;
this->MaxHeight = 0;
this->CollapsedHeight = 14;
this->GroupBoxStyle = 0;
}
//-----------------------------------------------------------------------------
void ctkCollapsibleGroupBoxPrivate::init()
{
Q_Q(ctkCollapsibleGroupBox);
q->setCheckable(true);
QObject::connect(q, SIGNAL(toggled(bool)), q, SLOT(expand(bool)));
this->MaxHeight = q->maximumHeight();
QWidget* parent = q->parentWidget();
QStyle* parentStyle = (parent) ? parent->style() : qApp->style();
this->GroupBoxStyle = new ctkCollapsibleGroupBoxStyle(parentStyle, qApp);
q->setStyle(this->GroupBoxStyle);
this->GroupBoxStyle->ensureBaseStyle();
}
//-----------------------------------------------------------------------------
void ctkCollapsibleGroupBoxPrivate::setChildVisibility(QWidget* childWidget)
{
Q_Q(ctkCollapsibleGroupBox);
// Don't hide children while the widget is not yet created (before show() is
// called). If we hide them (but don't set ExplicitShowHide), they would be
// shown anyway when they will be created (because ExplicitShowHide is not set).
// If we set ExplicitShowHide, then calling setVisible(false) on them would
// be a no (because they are already hidden and ExplicitShowHide is set).
// So we don't hide/show the children until the widget is created.
if (!q->testAttribute(Qt::WA_WState_Created))
{
return;
}
this->ForcingVisibility = true;
bool visible= !q->collapsed();
// if the widget has been explicitly hidden, then hide it.
if (childWidget->property("visibilityToParent").isValid()
&& !childWidget->property("visibilityToParent").toBool())
{
visible = false;
}
// Setting Qt::WA_WState_Visible to true during child construction can have
// undesirable side effects.
if (childWidget->testAttribute(Qt::WA_WState_Created) ||
!visible)
{
childWidget->setVisible(visible);
}
// setVisible() has set the ExplicitShowHide flag, restore it as we don't want
// to make it like it was an explicit visible set because we want
// to allow any children to be explicitly hidden by the user.
if ((!childWidget->property("visibilityToParent").isValid() ||
childWidget->property("visibilityToParent").toBool()))
{
childWidget->setAttribute(Qt::WA_WState_ExplicitShowHide, false);
}
this->ForcingVisibility = false;
}
//-----------------------------------------------------------------------------
ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(QWidget* _parent)
:QGroupBox(_parent)
, d_ptr(new ctkCollapsibleGroupBoxPrivate(*this))
{
Q_D(ctkCollapsibleGroupBox);
d->init();
}
//-----------------------------------------------------------------------------
ctkCollapsibleGroupBox::ctkCollapsibleGroupBox(const QString& title, QWidget* _parent)
:QGroupBox(title, _parent)
, d_ptr(new ctkCollapsibleGroupBoxPrivate(*this))
{
Q_D(ctkCollapsibleGroupBox);
d->init();
}
//-----------------------------------------------------------------------------
ctkCollapsibleGroupBox::~ctkCollapsibleGroupBox()
{
}
//-----------------------------------------------------------------------------
void ctkCollapsibleGroupBox::setCollapsedHeight(int heightInPixels)
{
Q_D(ctkCollapsibleGroupBox);
d->CollapsedHeight = heightInPixels;
}
//-----------------------------------------------------------------------------
int ctkCollapsibleGroupBox::collapsedHeight()const
{
Q_D(const ctkCollapsibleGroupBox);
return d->CollapsedHeight;
}
//-----------------------------------------------------------------------------
void ctkCollapsibleGroupBox::expand(bool _expand)
{
Q_D(ctkCollapsibleGroupBox);
if (!_expand)
{
d->OldSize = this->size();
}
// Update the visibility of all the children
// We can't use findChildren as it would return the grandchildren
foreach(QObject* childObject, this->children())
{
if (childObject->isWidgetType())
{
d->setChildVisibility(qobject_cast<QWidget*>(childObject));
}
}
if (_expand)
{
this->setMaximumHeight(d->MaxHeight);
this->resize(d->OldSize);
}
else
{
d->MaxHeight = this->maximumHeight();
QStyleOptionGroupBox option;
this->initStyleOption(&option);
QRect labelRect = this->style()->subControlRect(
QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this);
this->setMaximumHeight(labelRect.height() + d->CollapsedHeight);
}
}
//-----------------------------------------------------------------------------
void ctkCollapsibleGroupBox::childEvent(QChildEvent* c)
{
Q_D(ctkCollapsibleGroupBox);
QObject* child = c->child();
if (c && c->type() == QEvent::ChildAdded &&
child && child->isWidgetType())
{
QWidget *childWidget = qobject_cast<QWidget*>(c->child());
// Handle the case where the child has already it's visibility set before
// being added to the widget
if (childWidget->testAttribute(Qt::WA_WState_ExplicitShowHide) &&
childWidget->testAttribute(Qt::WA_WState_Hidden))
{
// if the widget has explicitly set to hidden, then mark it as such
childWidget->setProperty("visibilityToParent", false);
}
// We want to catch all the child's Show/Hide events.
child->installEventFilter(this);
// If the child is added while ctkCollapsibleButton is collapsed, then we
// need to hide the child.
d->setChildVisibility(childWidget);
}
this->QGroupBox::childEvent(c);
}
//-----------------------------------------------------------------------------
void ctkCollapsibleGroupBox::setVisible(bool show)
{
Q_D(ctkCollapsibleGroupBox);
// calling QWidget::setVisible() on ctkCollapsibleGroupBox will eventually
// call QWidget::showChildren() or hideChildren() which will generate
// ShowToParent/HideToParent events but we want to ignore that case in
// eventFilter().
d->ForcingVisibility = true;
this->QGroupBox::setVisible(show);
d->ForcingVisibility = false;
// We have been ignoring setChildVisibility() while the collapsible button
// is not yet created, now that it is created, ensure that the children
// are correctly shown/hidden depending on their explicit visibility and
// the collapsed property of the button.
if (!d->IsStateCreated && this->testAttribute(Qt::WA_WState_Created))
{
d->IsStateCreated = true;
foreach(QObject* child, this->children())
{
QWidget* childWidget = qobject_cast<QWidget*>(child);
if (childWidget)
{
d->setChildVisibility(childWidget);
}
}
}
}
//-----------------------------------------------------------------------------
bool ctkCollapsibleGroupBox::eventFilter(QObject* child, QEvent* e)
{
Q_D(ctkCollapsibleGroupBox);
Q_ASSERT(child && e);
// Make sure the Show/QHide events are not generated by one of our
// ctkCollapsibleButton function.
if (d->ForcingVisibility)
{
return false;
}
// When we are here, it's because somewhere (not in ctkCollapsibleButton),
// someone explicitly called setVisible() on a child widget.
// If the collapsible button is collapsed/closed, then even if someone
// request the widget to be visible, we force it back to be hidden because
// they meant to be hidden to its parent, the collapsible button. However the
// child will later be shown when the button will be expanded/opened.
// On the other hand, if the user explicitly hide the child when the button
// is collapsed/closed, then we want to keep it hidden next time the
// collapsible button is expanded/opened.
if (e->type() == QEvent::ShowToParent)
{
child->setProperty("visibilityToParent", true);
Q_ASSERT(qobject_cast<QWidget*>(child));
// force the widget to be hidden if the button is collapsed.
d->setChildVisibility(qobject_cast<QWidget*>(child));
}
else if(e->type() == QEvent::HideToParent)
{
// we don't need to force the widget to be visible here.
child->setProperty("visibilityToParent", false);
}
return this->QGroupBox::eventFilter(child, e);
}