| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qstackedwidget.h" | 
|---|
| 43 |  | 
|---|
| 44 | #ifndef QT_NO_STACKEDWIDGET | 
|---|
| 45 |  | 
|---|
| 46 | #include <qstackedlayout.h> | 
|---|
| 47 | #include <qevent.h> | 
|---|
| 48 | #include <private/qframe_p.h> | 
|---|
| 49 |  | 
|---|
| 50 | QT_BEGIN_NAMESPACE | 
|---|
| 51 |  | 
|---|
| 52 | class QStackedWidgetPrivate : public QFramePrivate | 
|---|
| 53 | { | 
|---|
| 54 | Q_DECLARE_PUBLIC(QStackedWidget) | 
|---|
| 55 | public: | 
|---|
| 56 | QStackedWidgetPrivate():layout(0){} | 
|---|
| 57 | QStackedLayout *layout; | 
|---|
| 58 | bool blockChildAdd; | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 | /*! | 
|---|
| 62 | \class QStackedWidget | 
|---|
| 63 | \brief The QStackedWidget class provides a stack of widgets where | 
|---|
| 64 | only one widget is visible at a time. | 
|---|
| 65 |  | 
|---|
| 66 | \ingroup organizers | 
|---|
| 67 | \ingroup geomanagement | 
|---|
| 68 | \ingroup appearance | 
|---|
| 69 | \mainclass | 
|---|
| 70 |  | 
|---|
| 71 | QStackedWidget can be used to create a user interface similar to | 
|---|
| 72 | the one provided by QTabWidget. It is a convenience layout widget | 
|---|
| 73 | built on top of the QStackedLayout class. | 
|---|
| 74 |  | 
|---|
| 75 | Like QStackedLayout, QStackedWidget can be constructed and | 
|---|
| 76 | populated with a number of child widgets ("pages"): | 
|---|
| 77 |  | 
|---|
| 78 | \snippet doc/src/snippets/qstackedwidget/main.cpp 0 | 
|---|
| 79 | \snippet doc/src/snippets/qstackedwidget/main.cpp 2 | 
|---|
| 80 | \snippet doc/src/snippets/qstackedwidget/main.cpp 3 | 
|---|
| 81 |  | 
|---|
| 82 | QStackedWidget 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 QStackedWidget's pages. For | 
|---|
| 85 | example: | 
|---|
| 86 |  | 
|---|
| 87 | \snippet doc/src/snippets/qstackedwidget/main.cpp 1 | 
|---|
| 88 |  | 
|---|
| 89 | When populating a stacked widget, the widgets are added to an | 
|---|
| 90 | internal list. The indexOf() function returns the index of a | 
|---|
| 91 | widget in that list. The widgets can either be added to the end of | 
|---|
| 92 | the list using the addWidget() function, or inserted at a given | 
|---|
| 93 | index using the insertWidget() function. The removeWidget() | 
|---|
| 94 | function removes a widget from the stacked widget. The number of | 
|---|
| 95 | widgets contained in the stacked widget, can | 
|---|
| 96 | be obtained using the count() 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 stacked widget changes or a | 
|---|
| 106 | widget is removed from the stacked widget, the currentChanged() | 
|---|
| 107 | and widgetRemoved() signals are emitted respectively. | 
|---|
| 108 |  | 
|---|
| 109 | \sa QStackedLayout, QTabWidget, {Config Dialog Example} | 
|---|
| 110 | */ | 
|---|
| 111 |  | 
|---|
| 112 | /*! | 
|---|
| 113 | \fn void QStackedWidget::currentChanged(int index) | 
|---|
| 114 |  | 
|---|
| 115 | This signal is emitted whenever the current widget changes. | 
|---|
| 116 |  | 
|---|
| 117 | The parameter holds the \a index of the new current widget, or -1 | 
|---|
| 118 | if there isn't a new one (for example, if there are no widgets in | 
|---|
| 119 | the QStackedWidget). | 
|---|
| 120 |  | 
|---|
| 121 | \sa currentWidget(), setCurrentWidget() | 
|---|
| 122 | */ | 
|---|
| 123 |  | 
|---|
| 124 | /*! | 
|---|
| 125 | \fn void QStackedWidget::widgetRemoved(int index) | 
|---|
| 126 |  | 
|---|
| 127 | This signal is emitted whenever a widget is removed. The widget's | 
|---|
| 128 | \a index is passed as parameter. | 
|---|
| 129 |  | 
|---|
| 130 | \sa removeWidget() | 
|---|
| 131 | */ | 
|---|
| 132 |  | 
|---|
| 133 | /*! | 
|---|
| 134 | Constructs a QStackedWidget with the given \a parent. | 
|---|
| 135 |  | 
|---|
| 136 | \sa addWidget(), insertWidget() | 
|---|
| 137 | */ | 
|---|
| 138 | QStackedWidget::QStackedWidget(QWidget *parent) | 
|---|
| 139 | : QFrame(*new QStackedWidgetPrivate, parent) | 
|---|
| 140 | { | 
|---|
| 141 | Q_D(QStackedWidget); | 
|---|
| 142 | d->layout = new QStackedLayout(this); | 
|---|
| 143 | connect(d->layout, SIGNAL(widgetRemoved(int)), this, SIGNAL(widgetRemoved(int))); | 
|---|
| 144 | connect(d->layout, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int))); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | /*! | 
|---|
| 148 | Destroys this stacked widget, and frees any allocated resources. | 
|---|
| 149 | */ | 
|---|
| 150 | QStackedWidget::~QStackedWidget() | 
|---|
| 151 | { | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /*! | 
|---|
| 155 | Appends the given \a widget to the QStackedWidget and returns the | 
|---|
| 156 | index position. Ownership of \a widget is passed on to the | 
|---|
| 157 | QStackedWidget. | 
|---|
| 158 |  | 
|---|
| 159 | If the QStackedWidget is empty before this function is called, | 
|---|
| 160 | \a widget becomes the current widget. | 
|---|
| 161 |  | 
|---|
| 162 | \sa insertWidget(), removeWidget(), setCurrentWidget() | 
|---|
| 163 | */ | 
|---|
| 164 | int QStackedWidget::addWidget(QWidget *widget) | 
|---|
| 165 | { | 
|---|
| 166 | return d_func()->layout->addWidget(widget); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | /*! | 
|---|
| 170 | Inserts the given \a widget at the given \a index in the | 
|---|
| 171 | QStackedWidget. Ownership of \a widget is passed on to the | 
|---|
| 172 | QStackedWidget. If \a index is out of range, the \a widget is | 
|---|
| 173 | appended (in which case it is the actual index of the \a widget | 
|---|
| 174 | that is returned). | 
|---|
| 175 |  | 
|---|
| 176 | If the QStackedWidget was empty before this function is called, | 
|---|
| 177 | the given \a widget becomes the current widget. | 
|---|
| 178 |  | 
|---|
| 179 | Inserting a new widget at an index less than or equal to the current index | 
|---|
| 180 | will increment the current index, but keep the current widget. | 
|---|
| 181 |  | 
|---|
| 182 | \sa addWidget(), removeWidget(), setCurrentWidget() | 
|---|
| 183 | */ | 
|---|
| 184 | int QStackedWidget::insertWidget(int index, QWidget *widget) | 
|---|
| 185 | { | 
|---|
| 186 | return d_func()->layout->insertWidget(index, widget); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /*! | 
|---|
| 190 | Removes the given \a widget from the QStackedWidget. The widget | 
|---|
| 191 | is \e not deleted. | 
|---|
| 192 |  | 
|---|
| 193 | \sa addWidget(), insertWidget(), currentWidget() | 
|---|
| 194 | */ | 
|---|
| 195 | void QStackedWidget::removeWidget(QWidget *widget) | 
|---|
| 196 | { | 
|---|
| 197 | d_func()->layout->removeWidget(widget); | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | /*! | 
|---|
| 201 | \property QStackedWidget::currentIndex | 
|---|
| 202 | \brief the index position of the widget that is visible | 
|---|
| 203 |  | 
|---|
| 204 | The current index is -1 if there is no current widget. | 
|---|
| 205 |  | 
|---|
| 206 | By default, this property contains a value of -1 because the stack | 
|---|
| 207 | is initially empty. | 
|---|
| 208 |  | 
|---|
| 209 | \sa currentWidget(), indexOf() | 
|---|
| 210 | */ | 
|---|
| 211 |  | 
|---|
| 212 | void QStackedWidget::setCurrentIndex(int index) | 
|---|
| 213 | { | 
|---|
| 214 | d_func()->layout->setCurrentIndex(index); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | int QStackedWidget::currentIndex() const | 
|---|
| 218 | { | 
|---|
| 219 | return d_func()->layout->currentIndex(); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | /*! | 
|---|
| 223 | Returns the current widget, or 0 if there are no child widgets. | 
|---|
| 224 |  | 
|---|
| 225 | \sa currentIndex(), setCurrentWidget() | 
|---|
| 226 | */ | 
|---|
| 227 | QWidget *QStackedWidget::currentWidget() const | 
|---|
| 228 | { | 
|---|
| 229 | return d_func()->layout->currentWidget(); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 |  | 
|---|
| 233 | /*! | 
|---|
| 234 | \fn void QStackedWidget::setCurrentWidget(QWidget *widget) | 
|---|
| 235 |  | 
|---|
| 236 | Sets the current widget to be the specified \a widget. The new | 
|---|
| 237 | current widget must already be contained in this stacked widget. | 
|---|
| 238 |  | 
|---|
| 239 | \sa currentWidget(), setCurrentIndex() | 
|---|
| 240 | */ | 
|---|
| 241 | void QStackedWidget::setCurrentWidget(QWidget *widget) | 
|---|
| 242 | { | 
|---|
| 243 | Q_D(QStackedWidget); | 
|---|
| 244 | if (d->layout->indexOf(widget) == -1) { | 
|---|
| 245 | qWarning("QStackedWidget::setCurrentWidget: widget %p not contained in stack", widget); | 
|---|
| 246 | return; | 
|---|
| 247 | } | 
|---|
| 248 | d->layout->setCurrentWidget(widget); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | /*! | 
|---|
| 252 | Returns the index of the given \a widget, or -1 if the given \a | 
|---|
| 253 | widget is not a child of the QStackedWidget. | 
|---|
| 254 |  | 
|---|
| 255 | \sa currentIndex(), widget() | 
|---|
| 256 | */ | 
|---|
| 257 | int QStackedWidget::indexOf(QWidget *widget) const | 
|---|
| 258 | { | 
|---|
| 259 | return d_func()->layout->indexOf(widget); | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | /*! | 
|---|
| 263 | Returns the widget at the given \a index, or 0 if there is no such | 
|---|
| 264 | widget. | 
|---|
| 265 |  | 
|---|
| 266 | \sa currentWidget(), indexOf() | 
|---|
| 267 | */ | 
|---|
| 268 | QWidget *QStackedWidget::widget(int index) const | 
|---|
| 269 | { | 
|---|
| 270 | return d_func()->layout->widget(index); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | /*! | 
|---|
| 274 | \property QStackedWidget::count | 
|---|
| 275 | \brief the number of widgets contained by this stacked widget | 
|---|
| 276 |  | 
|---|
| 277 | By default, this property contains a value of 0. | 
|---|
| 278 |  | 
|---|
| 279 | \sa currentIndex(), widget() | 
|---|
| 280 | */ | 
|---|
| 281 | int QStackedWidget::count() const | 
|---|
| 282 | { | 
|---|
| 283 | return d_func()->layout->count(); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | /*! \reimp */ | 
|---|
| 287 | bool QStackedWidget::event(QEvent *e) | 
|---|
| 288 | { | 
|---|
| 289 | return QFrame::event(e); | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | QT_END_NAMESPACE | 
|---|
| 293 |  | 
|---|
| 294 | #endif // QT_NO_STACKEDWIDGET | 
|---|