Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
| ** | |
| ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). | |
| ** Contact: http://www.qt-project.org/legal | |
| ** | |
| ** This file is part of the QtGui module of the Qt Toolkit. | |
| ** | |
| ** $QT_BEGIN_LICENSE:LGPL$ | |
| ** Commercial License Usage | |
| ** Licensees holding valid commercial Qt licenses may use this file in | |
| ** accordance with the commercial license agreement provided with the | |
| ** Software or, alternatively, in accordance with the terms contained in | |
| ** a written agreement between you and Digia. For licensing terms and | |
| ** conditions see http://qt.digia.com/licensing. For further information | |
| ** use the contact form at http://qt.digia.com/contact-us. | |
| ** | |
| ** 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, Digia gives you certain additional | |
| ** rights. These rights are described in the Digia Qt LGPL Exception | |
| ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | |
| ** | |
| ** GNU General Public License Usage | |
| ** Alternatively, this file may be used under the terms of the GNU | |
| ** General Public License version 3.0 as published by the Free Software | |
| ** Foundation and appearing in the file LICENSE.GPL included in the | |
| ** packaging of this file. Please review the following information to | |
| ** ensure the GNU General Public License version 3.0 requirements will be | |
| ** met: http://www.gnu.org/copyleft/gpl.html. | |
| ** | |
| ** | |
| ** $QT_END_LICENSE$ | |
| ** | |
| ****************************************************************************/**************************************************************************** | |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qstackedlayout.h" | - |
43 | #include "qlayout_p.h" | - |
44 | | - |
45 | #include <qlist.h> | - |
46 | #include <qwidget.h> | - |
47 | #include "private/qlayoutengine_p.h" | - |
48 | | - |
49 | QT_BEGIN_NAMESPACE | - |
50 | | - |
51 | class QStackedLayoutPrivate : public QLayoutPrivate | - |
52 | { | - |
53 | Q_DECLARE_PUBLIC(QStackedLayout) | - |
54 | public: | - |
55 | QStackedLayoutPrivate() : index(-1), stackingMode(QStackedLayout::StackOne) {} | - |
56 | QList<QLayoutItem *> list; | - |
57 | int index; | - |
58 | QStackedLayout::StackingMode stackingMode; | - |
59 | }; | - |
60 | | - |
61 | /*! | - |
62 | \class QStackedLayout | - |
63 | | - |
64 | \brief The QStackedLayout class provides a stack of widgets where | - |
65 | only one widget is visible at a time. | - |
66 | | - |
67 | \ingroup geomanagement | - |
68 | \inmodule QtWidgets | - |
69 | | - |
70 | QStackedLayout can be used to create a user interface similar to | - |
71 | the one provided by QTabWidget. There is also a convenience | - |
72 | QStackedWidget class built on top of QStackedLayout. | - |
73 | | - |
74 | A QStackedLayout can be populated with a number of child widgets | - |
75 | ("pages"). For example: | - |
76 | | - |
77 | \snippet qstackedlayout/main.cpp 0 | - |
78 | \codeline | - |
79 | \snippet qstackedlayout/main.cpp 2 | - |
80 | \snippet qstackedlayout/main.cpp 3 | - |
81 | | - |
82 | QStackedLayout provides no intrinsic means for the user to switch | - |
83 | page. This is typically done through a QComboBox or a QListWidget | - |
84 | that stores the titles of the QStackedLayout's pages. For | - |
85 | example: | - |
86 | | - |
87 | \snippet qstackedlayout/main.cpp 1 | - |
88 | | - |
89 | When populating a layout, the widgets are added to an internal | - |
90 | list. The indexOf() function returns the index of a widget in that | - |
91 | list. The widgets can either be added to the end of the list using | - |
92 | the addWidget() function, or inserted at a given index using the | - |
93 | insertWidget() function. The removeWidget() function removes the | - |
94 | widget at the given index from the layout. The number of widgets | - |
95 | contained in the layout, can be obtained using the count() | - |
96 | function. | - |
97 | | - |
98 | The widget() function returns the widget at a given index | - |
99 | position. The index of the widget that is shown on screen is given | - |
100 | by currentIndex() and can be changed using setCurrentIndex(). In a | - |
101 | similar manner, the currently shown widget can be retrieved using | - |
102 | the currentWidget() function, and altered using the | - |
103 | setCurrentWidget() function. | - |
104 | | - |
105 | Whenever the current widget in the layout changes or a widget is | - |
106 | removed from the layout, the currentChanged() and widgetRemoved() | - |
107 | signals are emitted respectively. | - |
108 | | - |
109 | \sa QStackedWidget, QTabWidget | - |
110 | */ | - |
111 | | - |
112 | /*! | - |
113 | \fn void QStackedLayout::currentChanged(int index) | - |
114 | | - |
115 | This signal is emitted whenever the current widget in the layout | - |
116 | changes. The \a index specifies the index of the new current | - |
117 | widget, or -1 if there isn't a new one (for example, if there | - |
118 | are no widgets in the QStackedLayout) | - |
119 | | - |
120 | \sa currentWidget(), setCurrentWidget() | - |
121 | */ | - |
122 | | - |
123 | /*! | - |
124 | \fn void QStackedLayout::widgetRemoved(int index) | - |
125 | | - |
126 | This signal is emitted whenever a widget is removed from the | - |
127 | layout. The widget's \a index is passed as parameter. | - |
128 | | - |
129 | \sa removeWidget() | - |
130 | */ | - |
131 | | - |
132 | /*! | - |
133 | \fn QWidget *QStackedLayout::widget() | - |
134 | \internal | - |
135 | */ | - |
136 | | - |
137 | /*! | - |
138 | Constructs a QStackedLayout with no parent. | - |
139 | | - |
140 | This QStackedLayout must be installed on a widget later on to | - |
141 | become effective. | - |
142 | | - |
143 | \sa addWidget(), insertWidget() | - |
144 | */ | - |
145 | QStackedLayout::QStackedLayout() | - |
146 | : QLayout(*new QStackedLayoutPrivate, 0, 0) | - |
147 | { | - |
148 | } | - |
149 | | - |
150 | /*! | - |
151 | Constructs a new QStackedLayout with the given \a parent. | - |
152 | | - |
153 | This layout will install itself on the \a parent widget and | - |
154 | manage the geometry of its children. | - |
155 | */ | - |
156 | QStackedLayout::QStackedLayout(QWidget *parent) | - |
157 | : QLayout(*new QStackedLayoutPrivate, 0, parent) | - |
158 | { | - |
159 | } | - |
160 | | - |
161 | /*! | - |
162 | Constructs a new QStackedLayout and inserts it into | - |
163 | the given \a parentLayout. | - |
164 | */ | - |
165 | QStackedLayout::QStackedLayout(QLayout *parentLayout) | - |
166 | : QLayout(*new QStackedLayoutPrivate, parentLayout, 0) | - |
167 | { | - |
168 | } | - |
169 | | - |
170 | /*! | - |
171 | Destroys this QStackedLayout. Note that the layout's widgets are | - |
172 | \e not destroyed. | - |
173 | */ | - |
174 | QStackedLayout::~QStackedLayout() | - |
175 | { | - |
176 | Q_D(QStackedLayout); | - |
177 | qDeleteAll(d->list); | - |
178 | } | - |
179 | | - |
180 | /*! | - |
181 | Adds the given \a widget to the end of this layout and returns the | - |
182 | index position of the \a widget. | - |
183 | | - |
184 | If the QStackedLayout is empty before this function is called, | - |
185 | the given \a widget becomes the current widget. | - |
186 | | - |
187 | \sa insertWidget(), removeWidget(), setCurrentWidget() | - |
188 | */ | - |
189 | int QStackedLayout::addWidget(QWidget *widget) | - |
190 | { | - |
191 | Q_D(QStackedLayout); | - |
192 | return insertWidget(d->list.count(), widget); | - |
193 | } | - |
194 | | - |
195 | /*! | - |
196 | Inserts the given \a widget at the given \a index in this | - |
197 | QStackedLayout. If \a index is out of range, the widget is | - |
198 | appended (in which case it is the actual index of the \a widget | - |
199 | that is returned). | - |
200 | | - |
201 | If the QStackedLayout is empty before this function is called, the | - |
202 | given \a widget becomes the current widget. | - |
203 | | - |
204 | Inserting a new widget at an index less than or equal to the current index | - |
205 | will increment the current index, but keep the current widget. | - |
206 | | - |
207 | \sa addWidget(), removeWidget(), setCurrentWidget() | - |
208 | */ | - |
209 | int QStackedLayout::insertWidget(int index, QWidget *widget) | - |
210 | { | - |
211 | Q_D(QStackedLayout); | - |
212 | addChildWidget(widget); | - |
213 | index = qMin(index, d->list.count()); | - |
214 | if (index < 0) | - |
215 | index = d->list.count(); | - |
216 | QWidgetItem *wi = QLayoutPrivate::createWidgetItem(this, widget); | - |
217 | d->list.insert(index, wi); | - |
218 | invalidate(); | - |
219 | if (d->index < 0) { | - |
220 | setCurrentIndex(index); | - |
221 | } else { | - |
222 | if (index <= d->index) | - |
223 | ++d->index; | - |
224 | if (d->stackingMode == StackOne) | - |
225 | widget->hide(); | - |
226 | widget->lower(); | - |
227 | } | - |
228 | return index; | - |
229 | } | - |
230 | | - |
231 | /*! | - |
232 | \reimp | - |
233 | */ | - |
234 | QLayoutItem *QStackedLayout::itemAt(int index) const | - |
235 | { | - |
236 | Q_D(const QStackedLayout); | - |
237 | return d->list.value(index); | - |
238 | } | - |
239 | | - |
240 | // Code that enables proper handling of the case that takeAt() is | - |
241 | // called somewhere inside QObject destructor (can't call hide() | - |
242 | // on the object then) | - |
243 | | - |
244 | class QtFriendlyLayoutWidget : public QWidget | - |
245 | { | - |
246 | public: | - |
247 | inline bool wasDeleted() const { return d_ptr->wasDeleted; } | - |
248 | }; | - |
249 | | - |
250 | static bool qt_wasDeleted(const QWidget *w) { return static_cast<const QtFriendlyLayoutWidget*>(w)->wasDeleted(); } | - |
251 | | - |
252 | | - |
253 | /*! | - |
254 | \reimp | - |
255 | */ | - |
256 | QLayoutItem *QStackedLayout::takeAt(int index) | - |
257 | { | - |
258 | Q_D(QStackedLayout); | - |
259 | if (index <0 || index >= d->list.size()) | - |
260 | return 0; | - |
261 | QLayoutItem *item = d->list.takeAt(index); | - |
262 | if (index == d->index) { | - |
263 | d->index = -1; | - |
264 | if ( d->list.count() > 0 ) { | - |
265 | int newIndex = (index == d->list.count()) ? index-1 : index; | - |
266 | setCurrentIndex(newIndex); | - |
267 | } else { | - |
268 | emit currentChanged(-1); | - |
269 | } | - |
270 | } else if (index < d->index) { | - |
271 | --d->index; | - |
272 | } | - |
273 | emit widgetRemoved(index); | - |
274 | if (item->widget() && !qt_wasDeleted(item->widget())) | - |
275 | item->widget()->hide(); | - |
276 | return item; | - |
277 | } | - |
278 | | - |
279 | /*! | - |
280 | \property QStackedLayout::currentIndex | - |
281 | \brief the index position of the widget that is visible | - |
282 | | - |
283 | The current index is -1 if there is no current widget. | - |
284 | | - |
285 | \sa currentWidget(), indexOf() | - |
286 | */ | - |
287 | void QStackedLayout::setCurrentIndex(int index) | - |
288 | { | - |
289 | Q_D(QStackedLayout); executed (the execution status of this line is deduced): QStackedLayoutPrivate * const d = d_func(); | - |
290 | QWidget *prev = currentWidget(); executed (the execution status of this line is deduced): QWidget *prev = currentWidget(); | - |
291 | QWidget *next = widget(index); executed (the execution status of this line is deduced): QWidget *next = widget(index); | - |
292 | if (!next || next == prev) evaluated: !next yes Evaluation Count:6 | yes Evaluation Count:635 |
evaluated: next == prev yes Evaluation Count:355 | yes Evaluation Count:280 |
| 6-635 |
293 | return; executed: return; Execution Count:361 | 361 |
294 | | - |
295 | bool reenableUpdates = false; executed (the execution status of this line is deduced): bool reenableUpdates = false; | - |
296 | QWidget *parent = parentWidget(); executed (the execution status of this line is deduced): QWidget *parent = parentWidget(); | - |
297 | | - |
298 | if (parent && parent->updatesEnabled()) { evaluated: parent yes Evaluation Count:278 | yes Evaluation Count:2 |
partially evaluated: parent->updatesEnabled() yes Evaluation Count:278 | no Evaluation Count:0 |
| 0-278 |
299 | reenableUpdates = true; executed (the execution status of this line is deduced): reenableUpdates = true; | - |
300 | parent->setUpdatesEnabled(false); executed (the execution status of this line is deduced): parent->setUpdatesEnabled(false); | - |
301 | } executed: } Execution Count:278 | 278 |
302 | | - |
303 | QPointer<QWidget*> fw = parent ? parent->window()->focusWidget() : 0; evaluated: parent yes Evaluation Count:278 | yes Evaluation Count:2 |
| 2-278 |
304 | const bool focusWasOnOldPage = fw && (prev && prev->isAncestorOf(fw)); evaluated: fw yes Evaluation Count:24 | yes Evaluation Count:256 |
evaluated: prev yes Evaluation Count:23 | yes Evaluation Count:1 |
evaluated: prev->isAncestorOf(fw) yes Evaluation Count:7 | yes Evaluation Count:16 |
| 1-256 |
305 | | - |
306 | if (prev) { evaluated: prev yes Evaluation Count:43 | yes Evaluation Count:237 |
| 43-237 |
307 | prev->clearFocus(); executed (the execution status of this line is deduced): prev->clearFocus(); | - |
308 | if (d->stackingMode == StackOne) partially evaluated: d->stackingMode == StackOne yes Evaluation Count:43 | no Evaluation Count:0 |
| 0-43 |
309 | prev->hide(); executed: prev->hide(); Execution Count:43 | 43 |
310 | } executed: } Execution Count:43 | 43 |
311 | | - |
312 | d->index = index; executed (the execution status of this line is deduced): d->index = index; | - |
313 | next->raise(); executed (the execution status of this line is deduced): next->raise(); | - |
314 | next->show(); executed (the execution status of this line is deduced): next->show(); | - |
315 | | - |
316 | // try to move focus onto the incoming widget if focus | - |
317 | // was somewhere on the outgoing widget. | - |
318 | | - |
319 | if (parent) { evaluated: parent yes Evaluation Count:278 | yes Evaluation Count:2 |
| 2-278 |
320 | if (fw && (prev && prev->isAncestorOf(fw)))focusWasOnOldPage) { evaluated: focusWasOnOldPage yes Evaluation Count:7 | yes Evaluation Count:271 |
| 7-271 |
321 | // focus was on old page | - |
| / look for the best focus widget we can find | |
322 | if (QWidget *nfw = next->focusWidget()) evaluated: QWidget *nfw = next->focusWidget() yes Evaluation Count:3 | yes Evaluation Count:4 |
| 3-4 |
323 | nfw->setFocus(); executed: nfw->setFocus(); Execution Count:3 | 3 |
324 | else { | - |
325 | // second best: first child widget in the focus chain | - |
326 | if (QWidget *i = fw;) { partially evaluated: QWidget *i = fw yes Evaluation Count:4 | no Evaluation Count:0 |
| 0-4 |
327 | while ((i = i->nextInFocusChain()) != fw) { evaluated: (i = i->nextInFocusChain()) != fw yes Evaluation Count:51 | yes Evaluation Count:1 |
| 1-51 |
328 | if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) evaluated: ((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) yes Evaluation Count:16 | yes Evaluation Count:35 |
| 16-35 |
329 | && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled() evaluated: !i->focusProxy() yes Evaluation Count:14 | yes Evaluation Count:2 |
evaluated: i->isVisibleTo(next) yes Evaluation Count:5 | yes Evaluation Count:9 |
partially evaluated: i->isEnabled() yes Evaluation Count:5 | no Evaluation Count:0 |
| 0-14 |
330 | && next->isAncestorOf(i)) { evaluated: next->isAncestorOf(i) yes Evaluation Count:3 | yes Evaluation Count:2 |
| 2-3 |
331 | i->setFocus(); executed (the execution status of this line is deduced): i->setFocus(); | - |
332 | break; executed: break; Execution Count:3 | 3 |
333 | } | - |
334 | } executed: } Execution Count:48 | 48 |
335 | // third best: incoming widget | - |
336 | if (i == fw ) evaluated: i == fw yes Evaluation Count:1 | yes Evaluation Count:3 |
| 1-3 |
337 | next->setFocus(); executed: next->setFocus(); Execution Count:1 | 1 |
338 | } executed: } Execution Count:4 | 4 |
339 | } executed: } Execution Count:4 | 4 |
340 | } | - |
341 | } executed: } Execution Count:278 | 278 |
342 | if (reenableUpdates) evaluated: reenableUpdates yes Evaluation Count:278 | yes Evaluation Count:2 |
| 2-278 |
343 | parent->setUpdatesEnabled(true); executed: parent->setUpdatesEnabled(true); Execution Count:278 | 278 |
344 | emit currentChanged(index); executed (the execution status of this line is deduced): currentChanged(index); | - |
345 | } executed: } Execution Count:280 | 280 |
346 | | - |
347 | int QStackedLayout::currentIndex() const | - |
348 | { | - |
349 | Q_D(const QStackedLayout); | - |
350 | return d->index; | - |
351 | } | - |
352 | | - |
353 | | - |
354 | /*! | - |
355 | \fn void QStackedLayout::setCurrentWidget(QWidget *widget) | - |
356 | | - |
357 | Sets the current widget to be the specified \a widget. The new | - |
358 | current widget must already be contained in this stacked layout. | - |
359 | | - |
360 | \sa setCurrentIndex(), currentWidget() | - |
361 | */ | - |
362 | void QStackedLayout::setCurrentWidget(QWidget *widget) | - |
363 | { | - |
364 | int index = indexOf(widget); | - |
365 | if (index == -1) { | - |
366 | qWarning("QStackedLayout::setCurrentWidget: Widget %p not contained in stack", widget); | - |
367 | return; | - |
368 | } | - |
369 | setCurrentIndex(index); | - |
370 | } | - |
371 | | - |
372 | | - |
373 | /*! | - |
374 | Returns the current widget, or 0 if there are no widgets in this | - |
375 | layout. | - |
376 | | - |
377 | \sa currentIndex(), setCurrentWidget() | - |
378 | */ | - |
379 | QWidget *QStackedLayout::currentWidget() const | - |
380 | { | - |
381 | Q_D(const QStackedLayout); | - |
382 | return d->index >= 0 ? d->list.at(d->index)->widget() : 0; | - |
383 | } | - |
384 | | - |
385 | /*! | - |
386 | Returns the widget at the given \a index, or 0 if there is no | - |
387 | widget at the given position. | - |
388 | | - |
389 | \sa currentWidget(), indexOf() | - |
390 | */ | - |
391 | QWidget *QStackedLayout::widget(int index) const | - |
392 | { | - |
393 | Q_D(const QStackedLayout); | - |
394 | if (index < 0 || index >= d->list.size()) | - |
395 | return 0; | - |
396 | return d->list.at(index)->widget(); | - |
397 | } | - |
398 | | - |
399 | /*! | - |
400 | \property QStackedLayout::count | - |
401 | \brief the number of widgets contained in the layout | - |
402 | | - |
403 | \sa currentIndex(), widget() | - |
404 | */ | - |
405 | int QStackedLayout::count() const | - |
406 | { | - |
407 | Q_D(const QStackedLayout); | - |
408 | return d->list.size(); | - |
409 | } | - |
410 | | - |
411 | | - |
412 | /*! | - |
413 | \reimp | - |
414 | */ | - |
415 | void QStackedLayout::addItem(QLayoutItem *item) | - |
416 | { | - |
417 | QWidget *widget = item->widget(); | - |
418 | if (widget) { | - |
419 | addWidget(widget); | - |
420 | delete item; | - |
421 | } else { | - |
422 | qWarning("QStackedLayout::addItem: Only widgets can be added"); | - |
423 | } | - |
424 | } | - |
425 | | - |
426 | /*! | - |
427 | \reimp | - |
428 | */ | - |
429 | QSize QStackedLayout::sizeHint() const | - |
430 | { | - |
431 | Q_D(const QStackedLayout); | - |
432 | QSize s(0, 0); | - |
433 | int n = d->list.count(); | - |
434 | | - |
435 | for (int i = 0; i < n; ++i) | - |
436 | if (QWidget *widget = d->list.at(i)->widget()) { | - |
437 | QSize ws(widget->sizeHint()); | - |
438 | if (widget->sizePolicy().horizontalPolicy() == QSizePolicy::Ignored) | - |
439 | ws.setWidth(0); | - |
440 | if (widget->sizePolicy().verticalPolicy() == QSizePolicy::Ignored) | - |
441 | ws.setHeight(0); | - |
442 | s = s.expandedTo(ws); | - |
443 | } | - |
444 | return s; | - |
445 | } | - |
446 | | - |
447 | /*! | - |
448 | \reimp | - |
449 | */ | - |
450 | QSize QStackedLayout::minimumSize() const | - |
451 | { | - |
452 | Q_D(const QStackedLayout); | - |
453 | QSize s(0, 0); | - |
454 | int n = d->list.count(); | - |
455 | | - |
456 | for (int i = 0; i < n; ++i) | - |
457 | if (QWidget *widget = d->list.at(i)->widget()) | - |
458 | s = s.expandedTo(qSmartMinSize(widget)); | - |
459 | return s; | - |
460 | } | - |
461 | | - |
462 | /*! | - |
463 | \reimp | - |
464 | */ | - |
465 | void QStackedLayout::setGeometry(const QRect &rect) | - |
466 | { | - |
467 | Q_D(QStackedLayout); | - |
468 | switch (d->stackingMode) { | - |
469 | case StackOne: | - |
470 | if (QWidget *widget = currentWidget()) | - |
471 | widget->setGeometry(rect); | - |
472 | break; | - |
473 | case StackAll: | - |
474 | if (const int n = d->list.count()) | - |
475 | for (int i = 0; i < n; ++i) | - |
476 | if (QWidget *widget = d->list.at(i)->widget()) | - |
477 | widget->setGeometry(rect); | - |
478 | break; | - |
479 | } | - |
480 | } | - |
481 | | - |
482 | /*! | - |
483 | \reimp | - |
484 | */ | - |
485 | bool QStackedLayout::hasHeightForWidth() const | - |
486 | { | - |
487 | const int n = count(); | - |
488 | | - |
489 | for (int i = 0; i < n; ++i) { | - |
490 | if (QLayoutItem *item = itemAt(i)) { | - |
491 | if (item->hasHeightForWidth()) | - |
492 | return true; | - |
493 | } | - |
494 | } | - |
495 | return false; | - |
496 | } | - |
497 | | - |
498 | /*! | - |
499 | \reimp | - |
500 | */ | - |
501 | int QStackedLayout::heightForWidth(int width) const | - |
502 | { | - |
503 | const int n = count(); | - |
504 | | - |
505 | int hfw = 0; | - |
506 | for (int i = 0; i < n; ++i) { | - |
507 | if (QLayoutItem *item = itemAt(i)) { | - |
508 | if (QWidget *w = item->widget()) | - |
509 | /* | - |
510 | Note: Does not query the layout item, but bypasses it and asks the widget | - |
511 | directly. This is consistent with how QStackedLayout::sizeHint() is | - |
512 | implemented. This also avoids an issue where QWidgetItem::heightForWidth() | - |
513 | returns -1 if the widget is hidden. | - |
514 | */ | - |
515 | hfw = qMax(hfw, w->heightForWidth(width)); | - |
516 | } | - |
517 | } | - |
518 | hfw = qMax(hfw, minimumSize().height()); | - |
519 | return hfw; | - |
520 | } | - |
521 | | - |
522 | /*! | - |
523 | \enum QStackedLayout::StackingMode | - |
524 | \since 4.4 | - |
525 | | - |
526 | This enum specifies how the layout handles its child widgets | - |
527 | regarding their visibility. | - |
528 | | - |
529 | \value StackOne | - |
530 | Only the current widget is visible. This is the default. | - |
531 | | - |
532 | \value StackAll | - |
533 | All widgets are visible. The current widget is merely raised. | - |
534 | */ | - |
535 | | - |
536 | | - |
537 | /*! | - |
538 | \property QStackedLayout::stackingMode | - |
539 | \brief determines the way visibility of child widgets are handled. | - |
540 | \since 4.4 | - |
541 | | - |
542 | The default value is StackOne. Setting the property to StackAll | - |
543 | allows you to make use of the layout for overlay widgets | - |
544 | that do additional drawing on top of other widgets, for example, | - |
545 | graphical editors. | - |
546 | */ | - |
547 | | - |
548 | QStackedLayout::StackingMode QStackedLayout::stackingMode() const | - |
549 | { | - |
550 | Q_D(const QStackedLayout); | - |
551 | return d->stackingMode; | - |
552 | } | - |
553 | | - |
554 | void QStackedLayout::setStackingMode(StackingMode stackingMode) | - |
555 | { | - |
556 | Q_D(QStackedLayout); | - |
557 | if (d->stackingMode == stackingMode) | - |
558 | return; | - |
559 | d->stackingMode = stackingMode; | - |
560 | | - |
561 | const int n = d->list.count(); | - |
562 | if (n == 0) | - |
563 | return; | - |
564 | | - |
565 | switch (d->stackingMode) { | - |
566 | case StackOne: | - |
567 | if (const int idx = currentIndex()) | - |
568 | for (int i = 0; i < n; ++i) | - |
569 | if (QWidget *widget = d->list.at(i)->widget()) | - |
570 | widget->setVisible(i == idx); | - |
571 | break; | - |
572 | case StackAll: { // Turn overlay on: Make sure all widgets are the same size | - |
573 | QRect geometry; | - |
574 | if (const QWidget *widget = currentWidget()) | - |
575 | geometry = widget->geometry(); | - |
576 | for (int i = 0; i < n; ++i) | - |
577 | if (QWidget *widget = d->list.at(i)->widget()) { | - |
578 | if (!geometry.isNull()) | - |
579 | widget->setGeometry(geometry); | - |
580 | widget->setVisible(true); | - |
581 | } | - |
582 | } | - |
583 | break; | - |
584 | } | - |
585 | } | - |
586 | | - |
587 | QT_END_NAMESPACE | - |
588 | | - |
| | |