~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/widgets/qstackedwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the widgets module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qstackedwidget.h"
 
30
 
 
31
#include <qstackedlayout.h>
 
32
#include <qevent.h>
 
33
#include <private/qframe_p.h>
 
34
 
 
35
class QStackedWidgetPrivate : public QFramePrivate
 
36
{
 
37
    Q_DECLARE_PUBLIC(QStackedWidget)
 
38
public:
 
39
    QStackedWidgetPrivate():layout(0){}
 
40
    QStackedLayout *layout;
 
41
    bool blockChildAdd;
 
42
};
 
43
 
 
44
/*!
 
45
    \class QStackedWidget
 
46
    \brief The QStackedWidget class provides a stack of widgets where
 
47
    only one widget is visible at a time.
 
48
 
 
49
    \ingroup organizers
 
50
    \ingroup geomanagement
 
51
    \ingroup appearance
 
52
    \mainclass
 
53
 
 
54
    QStackedWidget can be used to create a user interface similar to
 
55
    the one provided by QTabWidget. It is a convenience layout widget
 
56
    built on top of QStackedLayout.
 
57
 
 
58
    A QStackedWidget can be constructed and populated with a number
 
59
    of child widgets ("pages"), each of which can be created without
 
60
    a parent widget:
 
61
 
 
62
    \code
 
63
        QWidget *firstPageWidget = new QWidget;
 
64
        QWidget *secondPageWidget = new QWidget;
 
65
        QWidget *thirdPageWidget = new QWidget;
 
66
        ...
 
67
 
 
68
        QStackedWidget *stackedWidget = new QStackedWidget(this);
 
69
        stackedWidget->addWidget(firstPageWidget);
 
70
        stackedWidget->addWidget(secondPageWidget);
 
71
        stackedWidget->addWidget(thirdPageWidget);
 
72
    \endcode
 
73
 
 
74
    When inserted, the widgets are added to an internal list. The
 
75
    indexOf() function returns the index of a widget in that list.
 
76
    The widget() function returns the widget at a given index
 
77
    position. The widget that is shown on screen is the
 
78
    currentWidget().  It can be changed using setCurrentWidget() or
 
79
    setCurrentIndex()
 
80
 
 
81
    QStackedWidget provides no intrinsic means for the user to switch
 
82
    page. This is typically done through a QComboBox or a QListWidget
 
83
    that stores the titles of the QStackedWidget's pages. For
 
84
    example:
 
85
 
 
86
    \code
 
87
        QComboBox *pageComboBox = new QComboBox;
 
88
        pageComboBox->addItem(tr("Page 1"));
 
89
        pageComboBox->addItem(tr("Page 2"));
 
90
        pageComboBox->addItem(tr("Page 3"));
 
91
        connect(pageComboBox, SIGNAL(activated(int)),
 
92
                stackedWidget, SLOT(setCurrentIndex(int)));
 
93
    \endcode
 
94
 
 
95
    \sa QStackedLayout, QTabWidget
 
96
*/
 
97
 
 
98
/*!
 
99
    \fn void QStackedWidget::currentChanged(int index)
 
100
 
 
101
    This signal is emitted when the current widget is changed. The
 
102
    parameter holds the \a index of the new current widget, or -1 if
 
103
    there isn't a new one (for example, if there are no widgets in
 
104
    the QStackedWidget).
 
105
 
 
106
    \sa widgetRemoved(), indexOf()
 
107
*/
 
108
 
 
109
/*!
 
110
    \fn void QStackedWidget::widgetRemoved(int index)
 
111
 
 
112
    This signal is emitted when the widget at position \a index is
 
113
    removed.
 
114
 
 
115
    \sa currentChanged()
 
116
*/
 
117
 
 
118
/*!
 
119
    Constructs a QStackedWidget with the given \a parent.
 
120
*/
 
121
QStackedWidget::QStackedWidget(QWidget *parent)
 
122
    : QFrame(*new QStackedWidgetPrivate, parent)
 
123
{
 
124
    Q_D(QStackedWidget);
 
125
    d->layout = new QStackedLayout(this);
 
126
    connect(d->layout, SIGNAL(widgetRemoved(int)), this, SIGNAL(widgetRemoved(int)));
 
127
    connect(d->layout, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
 
128
}
 
129
 
 
130
/*!
 
131
    Destroys the object and frees any allocated resources.
 
132
*/
 
133
QStackedWidget::~QStackedWidget()
 
134
{
 
135
}
 
136
 
 
137
/*!
 
138
    Adds \a widget to this QStackedWidget and returns the index
 
139
    position of \a widget.
 
140
 
 
141
    If the QStackedWidget is empty before this function is called,
 
142
    \a widget becomes the current widget.
 
143
 
 
144
    \sa insertWidget(), removeWidget(), currentWidget()
 
145
*/
 
146
int QStackedWidget::addWidget(QWidget *widget)
 
147
{
 
148
    return d_func()->layout->addWidget(widget);
 
149
}
 
150
 
 
151
/*!
 
152
    Inserts \a widget at position \a index in this QStackedWidget. If
 
153
    \a index is out of range, the widget is appended. Returns the
 
154
    actual index of \a widget.
 
155
 
 
156
    If the QStackedWidget was empty before this function is called,
 
157
    \a widget becomes the current widget.
 
158
 
 
159
    \sa addWidget(), removeWidget(), count(), currentWidget()
 
160
*/
 
161
int QStackedWidget::insertWidget(int index, QWidget *widget)
 
162
{
 
163
    return d_func()->layout->insertWidget(index, widget);
 
164
}
 
165
 
 
166
/*!
 
167
    Removes \a widget from the QStackedWidget's layout. The widget is
 
168
    \e not deleted.
 
169
 
 
170
    \sa addWidget(), insertWidget(), currentWidget()
 
171
*/
 
172
void QStackedWidget::removeWidget(QWidget *widget)
 
173
{
 
174
    d_func()->layout->removeWidget(widget);
 
175
}
 
176
 
 
177
/*!
 
178
    \property QStackedWidget::currentIndex
 
179
    \brief the index position of the widget that is visible
 
180
 
 
181
    The current index is -1 if there is no current widget.
 
182
 
 
183
    \sa currentWidget(), indexOf()
 
184
*/
 
185
 
 
186
void QStackedWidget::setCurrentIndex(int index)
 
187
{
 
188
    d_func()->layout->setCurrentIndex(index);
 
189
}
 
190
 
 
191
int QStackedWidget::currentIndex() const
 
192
{
 
193
    return d_func()->layout->currentIndex();
 
194
}
 
195
 
 
196
/*!
 
197
    Returns the current widget, or 0 if there are no child widgets.
 
198
 
 
199
    Equivalent to widget(currentIndex()).
 
200
 
 
201
    \sa currentIndex(), setCurrentWidget()
 
202
*/
 
203
QWidget *QStackedWidget::currentWidget() const
 
204
{
 
205
    return d_func()->layout->currentWidget();
 
206
}
 
207
 
 
208
 
 
209
/*!
 
210
 
 
211
  Sets \a w to be the current widget. \a w must be contained in this
 
212
  stacked widget.
 
213
 
 
214
  Equivalent to
 
215
  \code
 
216
        stackedWidget->setCurrentIndex(stackedWidget->indexOf(w));
 
217
  \endcode
 
218
 
 
219
  \sa addWidget(), setCurrentIndex()
 
220
 */
 
221
void QStackedWidget::setCurrentWidget(QWidget *w)
 
222
{
 
223
    d_func()->layout->setCurrentWidget(w);
 
224
}
 
225
 
 
226
/*!
 
227
    Returns the index of \a widget, or -1 if \a widget is not a child
 
228
    of QStackedWidget.
 
229
 
 
230
    \sa currentIndex(), widget()
 
231
*/
 
232
int QStackedWidget::indexOf(QWidget *widget) const
 
233
{
 
234
    return d_func()->layout->indexOf(widget);
 
235
}
 
236
 
 
237
/*!
 
238
    Returns the widget at position \a index, or 0 if there is no such
 
239
    widget.
 
240
 
 
241
    \sa indexOf()
 
242
*/
 
243
QWidget *QStackedWidget::widget(int index) const
 
244
{
 
245
    return d_func()->layout->widget(index);
 
246
}
 
247
 
 
248
/*!
 
249
    \property QStackedWidget::count
 
250
    \brief the number of child widgets
 
251
 
 
252
    \sa currentIndex(), widget()
 
253
*/
 
254
int QStackedWidget::count() const
 
255
{
 
256
    return d_func()->layout->count();
 
257
}
 
258