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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qstackedlayout.h"
#include "qlayout_p.h"
#include <qlist.h>
#include <qwidget.h>
#include "private/qlayoutengine_p.h"
QT_BEGIN_NAMESPACE
class QStackedLayoutPrivate : public QLayoutPrivate
{
Q_DECLARE_PUBLIC(QStackedLayout)
public:
QStackedLayoutPrivate() : index(-1), stackingMode(QStackedLayout::StackOne) {}
QList<QLayoutItem *> list;
int index;
QStackedLayout::StackingMode stackingMode;
};
/*!
\class QStackedLayout
\brief The QStackedLayout class provides a stack of widgets where
only one widget is visible at a time.
\ingroup geomanagement
QStackedLayout can be used to create a user interface similar to
the one provided by QTabWidget. There is also a convenience
QStackedWidget class built on top of QStackedLayout.
A QStackedLayout can be populated with a number of child widgets
("pages"). For example:
\snippet doc/src/snippets/qstackedlayout/main.cpp 0
\codeline
\snippet doc/src/snippets/qstackedlayout/main.cpp 2
\snippet doc/src/snippets/qstackedlayout/main.cpp 3
QStackedLayout provides no intrinsic means for the user to switch
page. This is typically done through a QComboBox or a QListWidget
that stores the titles of the QStackedLayout's pages. For
example:
\snippet doc/src/snippets/qstackedlayout/main.cpp 1
When populating a layout, the widgets are added to an internal
list. The indexOf() function returns the index of a widget in that
list. The widgets can either be added to the end of the list using
the addWidget() function, or inserted at a given index using the
insertWidget() function. The removeWidget() function removes the
widget at the given index from the layout. The number of widgets
contained in the layout, can be obtained using the count()
function.
The widget() function returns the widget at a given index
position. The index of the widget that is shown on screen is given
by currentIndex() and can be changed using setCurrentIndex(). In a
similar manner, the currently shown widget can be retrieved using
the currentWidget() function, and altered using the
setCurrentWidget() function.
Whenever the current widget in the layout changes or a widget is
removed from the layout, the currentChanged() and widgetRemoved()
signals are emitted respectively.
\sa QStackedWidget, QTabWidget
*/
/*!
\fn void QStackedLayout::currentChanged(int index)
This signal is emitted whenever the current widget in the layout
changes. The \a index specifies the index of the new current
widget, or -1 if there isn't a new one (for example, if there
are no widgets in the QStackedLayout)
\sa currentWidget(), setCurrentWidget()
*/
/*!
\fn void QStackedLayout::widgetRemoved(int index)
This signal is emitted whenever a widget is removed from the
layout. The widget's \a index is passed as parameter.
\sa removeWidget()
*/
/*!
\fn QWidget *QStackedLayout::widget()
\internal
*/
/*!
Constructs a QStackedLayout with no parent.
This QStackedLayout must be installed on a widget later on to
become effective.
\sa addWidget(), insertWidget()
*/
QStackedLayout::QStackedLayout()
: QLayout(*new QStackedLayoutPrivate, 0, 0)
{
}
/*!
Constructs a new QStackedLayout with the given \a parent.
This layout will install itself on the \a parent widget and
manage the geometry of its children.
*/
QStackedLayout::QStackedLayout(QWidget *parent)
: QLayout(*new QStackedLayoutPrivate, 0, parent)
{
}
/*!
Constructs a new QStackedLayout and inserts it into
the given \a parentLayout.
*/
QStackedLayout::QStackedLayout(QLayout *parentLayout)
: QLayout(*new QStackedLayoutPrivate, parentLayout, 0)
{
}
/*!
Destroys this QStackedLayout. Note that the layout's widgets are
\e not destroyed.
*/
QStackedLayout::~QStackedLayout()
{
Q_D(QStackedLayout);
qDeleteAll(d->list);
}
/*!
Adds the given \a widget to the end of this layout and returns the
index position of the \a widget.
If the QStackedLayout is empty before this function is called,
the given \a widget becomes the current widget.
\sa insertWidget(), removeWidget(), setCurrentWidget()
*/
int QStackedLayout::addWidget(QWidget *widget)
{
Q_D(QStackedLayout);
return insertWidget(d->list.count(), widget);
}
/*!
Inserts the given \a widget at the given \a index in this
QStackedLayout. If \a index is out of range, the widget is
appended (in which case it is the actual index of the \a widget
that is returned).
If the QStackedLayout is empty before this function is called, the
given \a widget becomes the current widget.
Inserting a new widget at an index less than or equal to the current index
will increment the current index, but keep the current widget.
\sa addWidget(), removeWidget(), setCurrentWidget()
*/
int QStackedLayout::insertWidget(int index, QWidget *widget)
{
Q_D(QStackedLayout);
addChildWidget(widget);
index = qMin(index, d->list.count());
if (index < 0)
index = d->list.count();
QWidgetItem *wi = QLayoutPrivate::createWidgetItem(this, widget);
d->list.insert(index, wi);
invalidate();
if (d->index < 0) {
setCurrentIndex(index);
} else {
if (index <= d->index)
++d->index;
if (d->stackingMode == StackOne)
widget->hide();
widget->lower();
}
return index;
}
/*!
\reimp
*/
QLayoutItem *QStackedLayout::itemAt(int index) const
{
Q_D(const QStackedLayout);
return d->list.value(index);
}
// Code that enables proper handling of the case that takeAt() is
// called somewhere inside QObject destructor (can't call hide()
// on the object then)
class QtFriendlyLayoutWidget : public QWidget
{
public:
inline bool wasDeleted() const { return d_ptr->wasDeleted; }
};
static bool qt_wasDeleted(const QWidget *w) { return static_cast<const QtFriendlyLayoutWidget*>(w)->wasDeleted(); }
/*!
\reimp
*/
QLayoutItem *QStackedLayout::takeAt(int index)
{
Q_D(QStackedLayout);
if (index <0 || index >= d->list.size())
return 0;
QLayoutItem *item = d->list.takeAt(index);
if (index == d->index) {
d->index = -1;
if ( d->list.count() > 0 ) {
int newIndex = (index == d->list.count()) ? index-1 : index;
setCurrentIndex(newIndex);
} else {
emit currentChanged(-1);
}
} else if (index < d->index) {
--d->index;
}
emit widgetRemoved(index);
if (item->widget() && !qt_wasDeleted(item->widget()))
item->widget()->hide();
return item;
}
/*!
\property QStackedLayout::currentIndex
\brief the index position of the widget that is visible
The current index is -1 if there is no current widget.
\sa currentWidget(), indexOf()
*/
void QStackedLayout::setCurrentIndex(int index)
{
Q_D(QStackedLayout);
QWidget *prev = currentWidget();
QWidget *next = widget(index);
if (!next || next == prev)
return;
bool reenableUpdates = false;
QWidget *parent = parentWidget();
if (parent && parent->updatesEnabled()) {
reenableUpdates = true;
parent->setUpdatesEnabled(false);
}
QWidget *fw = parent ? parent->window()->focusWidget() : 0;
if (prev) {
prev->clearFocus();
if (d->stackingMode == StackOne)
prev->hide();
}
d->index = index;
next->raise();
next->show();
// try to move focus onto the incoming widget if focus
// was somewhere on the outgoing widget.
if (parent) {
if (fw && (prev && prev->isAncestorOf(fw))) { // focus was on old page
// look for the best focus widget we can find
if (QWidget *nfw = next->focusWidget())
nfw->setFocus();
else {
// second best: first child widget in the focus chain
QWidget *i = fw;
while ((i = i->nextInFocusChain()) != fw) {
if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus)
&& !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled()
&& next->isAncestorOf(i)) {
i->setFocus();
break;
}
}
// third best: incoming widget
if (i == fw )
next->setFocus();
}
}
}
if (reenableUpdates)
parent->setUpdatesEnabled(true);
emit currentChanged(index);
}
int QStackedLayout::currentIndex() const
{
Q_D(const QStackedLayout);
return d->index;
}
/*!
\fn void QStackedLayout::setCurrentWidget(QWidget *widget)
Sets the current widget to be the specified \a widget. The new
current widget must already be contained in this stacked layout.
\sa setCurrentIndex(), currentWidget()
*/
void QStackedLayout::setCurrentWidget(QWidget *widget)
{
int index = indexOf(widget);
if (index == -1) {
qWarning("QStackedLayout::setCurrentWidget: Widget %p not contained in stack", widget);
return;
}
setCurrentIndex(index);
}
/*!
Returns the current widget, or 0 if there are no widgets in this
layout.
\sa currentIndex(), setCurrentWidget()
*/
QWidget *QStackedLayout::currentWidget() const
{
Q_D(const QStackedLayout);
return d->index >= 0 ? d->list.at(d->index)->widget() : 0;
}
/*!
Returns the widget at the given \a index, or 0 if there is no
widget at the given position.
\sa currentWidget(), indexOf()
*/
QWidget *QStackedLayout::widget(int index) const
{
Q_D(const QStackedLayout);
if (index < 0 || index >= d->list.size())
return 0;
return d->list.at(index)->widget();
}
/*!
\property QStackedLayout::count
\brief the number of widgets contained in the layout
\sa currentIndex(), widget()
*/
int QStackedLayout::count() const
{
Q_D(const QStackedLayout);
return d->list.size();
}
/*!
\reimp
*/
void QStackedLayout::addItem(QLayoutItem *item)
{
QWidget *widget = item->widget();
if (widget) {
addWidget(widget);
delete item;
} else {
qWarning("QStackedLayout::addItem: Only widgets can be added");
}
}
/*!
\reimp
*/
QSize QStackedLayout::sizeHint() const
{
Q_D(const QStackedLayout);
QSize s(0, 0);
int n = d->list.count();
for (int i = 0; i < n; ++i)
if (QWidget *widget = d->list.at(i)->widget()) {
QSize ws(widget->sizeHint());
if (widget->sizePolicy().horizontalPolicy() == QSizePolicy::Ignored)
ws.setWidth(0);
if (widget->sizePolicy().verticalPolicy() == QSizePolicy::Ignored)
ws.setHeight(0);
s = s.expandedTo(ws);
}
return s;
}
/*!
\reimp
*/
QSize QStackedLayout::minimumSize() const
{
Q_D(const QStackedLayout);
QSize s(0, 0);
int n = d->list.count();
for (int i = 0; i < n; ++i)
if (QWidget *widget = d->list.at(i)->widget())
s = s.expandedTo(qSmartMinSize(widget));
return s;
}
/*!
\reimp
*/
void QStackedLayout::setGeometry(const QRect &rect)
{
Q_D(QStackedLayout);
switch (d->stackingMode) {
case StackOne:
if (QWidget *widget = currentWidget())
widget->setGeometry(rect);
break;
case StackAll:
if (const int n = d->list.count())
for (int i = 0; i < n; ++i)
if (QWidget *widget = d->list.at(i)->widget())
widget->setGeometry(rect);
break;
}
}
bool QStackedLayout::hasHeightForWidth() const
{
const int n = count();
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
if (item->hasHeightForWidth())
return true;
}
}
return false;
}
int QStackedLayout::heightForWidth(int width) const
{
const int n = count();
int hfw = 0;
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
hfw = qMax(hfw, item->heightForWidth(width));
}
}
return hfw;
}
/*!
\enum QStackedLayout::StackingMode
\since 4.4
This enum specifies how the layout handles its child widgets
regarding their visibility.
\value StackOne
Only the current widget is visible. This is the default.
\value StackAll
All widgets are visible. The current widget is merely raised.
*/
/*!
\property QStackedLayout::stackingMode
\brief determines the way visibility of child widgets are handled.
\since 4.4
The default value is StackOne. Setting the property to StackAll
allows you to make use of the layout for overlay widgets
that do additional drawing on top of other widgets, for example,
graphical editors.
*/
QStackedLayout::StackingMode QStackedLayout::stackingMode() const
{
Q_D(const QStackedLayout);
return d->stackingMode;
}
void QStackedLayout::setStackingMode(StackingMode stackingMode)
{
Q_D(QStackedLayout);
if (d->stackingMode == stackingMode)
return;
d->stackingMode = stackingMode;
const int n = d->list.count();
if (n == 0)
return;
switch (d->stackingMode) {
case StackOne:
if (const int idx = currentIndex())
for (int i = 0; i < n; ++i)
if (QWidget *widget = d->list.at(i)->widget())
widget->setVisible(i == idx);
break;
case StackAll: { // Turn overlay on: Make sure all widgets are the same size
QRect geometry;
if (const QWidget *widget = currentWidget())
geometry = widget->geometry();
for (int i = 0; i < n; ++i)
if (QWidget *widget = d->list.at(i)->widget()) {
if (!geometry.isNull())
widget->setGeometry(geometry);
widget->setVisible(true);
}
}
break;
}
}
QT_END_NAMESPACE
|