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