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