aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickpage.cpp
blob: 0a72bad70e8348893ab78e47b27f695f0bbce5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qquickpage_p.h"
#include "qquickpage_p_p.h"
#include "qquicktabbar_p.h"
#include "qquicktoolbar_p.h"
#include "qquickdialogbuttonbox_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype Page
    \inherits Pane
//!     \instantiates QQuickPage
    \inqmlmodule QtQuick.Controls
    \since 5.7
    \ingroup qtquickcontrols2-containers
    \ingroup qtquickcontrols2-focusscopes
    \brief Styled page control with support for a header and footer.

    Page is a container control which makes it convenient to add
    a \l header and \l footer item to a page.

    \image qtquickcontrols2-page-wireframe.png

    The following example snippet illustrates how to use a page-specific
    toolbar header and an application-wide tabbar footer.

    \qml
    import QtQuick.Controls 2.12

    ApplicationWindow {
        visible: true

        StackView {
            anchors.fill: parent

            initialItem: Page {
                header: ToolBar {
                    // ...
                }
            }
        }

        footer: TabBar {
            // ...
        }
    }
    \endqml

    \sa ApplicationWindow, {Container Controls},
        {Focus Management in Qt Quick Controls}
*/

static const QQuickItemPrivate::ChangeTypes LayoutChanges = QQuickItemPrivate::Geometry | QQuickItemPrivate::Visibility | QQuickItemPrivate::Destroyed
                                                          | QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight;

namespace {
    enum Position {
        Header,
        Footer
    };

    Q_STATIC_ASSERT(int(Header) == int(QQuickTabBar::Header));
    Q_STATIC_ASSERT(int(Footer) == int(QQuickTabBar::Footer));

    Q_STATIC_ASSERT(int(Header) == int(QQuickToolBar::Header));
    Q_STATIC_ASSERT(int(Footer) == int(QQuickToolBar::Footer));

    Q_STATIC_ASSERT(int(Header) == int(QQuickDialogButtonBox::Header));
    Q_STATIC_ASSERT(int(Footer) == int(QQuickDialogButtonBox::Footer));

    static void setPos(QQuickItem *item, Position position)
    {
        if (QQuickToolBar *toolBar = qobject_cast<QQuickToolBar *>(item))
            toolBar->setPosition(static_cast<QQuickToolBar::Position>(position));
        else if (QQuickTabBar *tabBar = qobject_cast<QQuickTabBar *>(item))
            tabBar->setPosition(static_cast<QQuickTabBar::Position>(position));
        else if (QQuickDialogButtonBox *buttonBox = qobject_cast<QQuickDialogButtonBox *>(item))
            buttonBox->setPosition(static_cast<QQuickDialogButtonBox::Position>(position));
    }
}

void QQuickPagePrivate::relayout()
{
    Q_Q(QQuickPage);
    const qreal hh = header && header->isVisible() ? header->height() : 0;
    const qreal fh = footer && footer->isVisible() ? footer->height() : 0;
    const qreal hsp = hh > 0 ? spacing : 0;
    const qreal fsp = fh > 0 ? spacing : 0;

    if (contentItem) {
        contentItem->setY(q->topPadding() + hh + hsp);
        contentItem->setX(q->leftPadding());
        contentItem->setWidth(q->availableWidth());
        contentItem->setHeight(q->availableHeight() - hh - fh - hsp - fsp);
    }

    if (header)
        header->setWidth(q->width());

    if (footer) {
        footer->setY(q->height() - footer->height());
        footer->setWidth(q->width());
    }
}

void QQuickPagePrivate::resizeContent()
{
    relayout();
}

void QQuickPagePrivate::itemVisibilityChanged(QQuickItem *item)
{
    Q_Q(QQuickPage);
    QQuickPanePrivate::itemVisibilityChanged(item);
    if (item == header) {
        QBoolBlocker signalGuard(emittingImplicitSizeChangedSignals);
        emit q->implicitHeaderWidthChanged();
        emit q->implicitHeaderHeightChanged();
        relayout();
    } else if (item == footer) {
        QBoolBlocker signalGuard(emittingImplicitSizeChangedSignals);
        emit q->implicitFooterWidthChanged();
        emit q->implicitFooterHeightChanged();
        relayout();
    }
}

void QQuickPagePrivate::itemImplicitWidthChanged(QQuickItem *item)
{
    Q_Q(QQuickPage);
    QQuickPanePrivate::itemImplicitWidthChanged(item);

    // Avoid binding loops by skipping signal emission if we're already doing it.
    if (emittingImplicitSizeChangedSignals)
        return;

    if (item == header)
        emit q->implicitHeaderWidthChanged();
    else if (item == footer)
        emit q->implicitFooterWidthChanged();
}

void QQuickPagePrivate::itemImplicitHeightChanged(QQuickItem *item)
{
    Q_Q(QQuickPage);
    QQuickPanePrivate::itemImplicitHeightChanged(item);

    // Avoid binding loops by skipping signal emission if we're already doing it.
    if (emittingImplicitSizeChangedSignals)
        return;

    if (item == header)
        emit q->implicitHeaderHeightChanged();
    else if (item == footer)
        emit q->implicitFooterHeightChanged();
}

void QQuickPagePrivate::itemGeometryChanged(QQuickItem *item, QQuickGeometryChange change, const QRectF & diff)
{
    QQuickPanePrivate::itemGeometryChanged(item, change, diff);
    if (item == header || item == footer)
        relayout();
}

void QQuickPagePrivate::itemDestroyed(QQuickItem *item)
{
    Q_Q(QQuickPage);
    QQuickPanePrivate::itemDestroyed(item);
    if (item == header) {
        header = nullptr;
        relayout();
        emit q->implicitHeaderWidthChanged();
        emit q->implicitHeaderHeightChanged();
        emit q->headerChanged();
    } else if (item == footer) {
        footer = nullptr;
        relayout();
        emit q->implicitFooterWidthChanged();
        emit q->implicitFooterHeightChanged();
        emit q->footerChanged();
    }
}

QQuickPage::QQuickPage(QQuickItem *parent)
    : QQuickPane(*(new QQuickPagePrivate), parent)
{
}

QQuickPage::QQuickPage(QQuickPagePrivate &dd, QQuickItem *parent)
    : QQuickPane(dd, parent)
{
}

QQuickPage::~QQuickPage()
{
    Q_D(QQuickPage);
    if (d->header)
        QQuickItemPrivate::get(d->header)->removeItemChangeListener(d, LayoutChanges);
    if (d->footer)
        QQuickItemPrivate::get(d->footer)->removeItemChangeListener(d, LayoutChanges);
}

/*!
    \qmlproperty string QtQuick.Controls::Page::title

    This property holds the page title.

    The title is often displayed at the top of a page to give
    the user context about the page they are viewing.

    \code
    ApplicationWindow {
        visible: true
        width: 400
        height: 400

        header: Label {
            text: view.currentItem.title
            horizontalAlignment: Text.AlignHCenter
        }

        SwipeView {
            id: view
            anchors.fill: parent

            Page {
                title: qsTr("Home")
            }
            Page {
                title: qsTr("Discover")
            }
            Page {
                title: qsTr("Activity")
            }
        }
    }
    \endcode
*/

QString QQuickPage::title() const
{
    return d_func()->title;
}

void QQuickPage::setTitle(const QString &title)
{
    Q_D(QQuickPage);
    if (d->title == title)
        return;

    d->title = title;
    maybeSetAccessibleName(title);
    emit titleChanged();
}

/*!
    \qmlproperty Item QtQuick.Controls::Page::header

    This property holds the page header item. The header item is positioned to
    the top, and resized to the width of the page. The default value is \c null.

    \note Assigning a ToolBar, TabBar, or DialogButtonBox as a page header
    automatically sets the respective \l ToolBar::position, \l TabBar::position,
    or \l DialogButtonBox::position property to \c Header.

    \sa footer, ApplicationWindow::header
*/
QQuickItem *QQuickPage::header() const
{
    Q_D(const QQuickPage);
    return d->header;
}

void QQuickPage::setHeader(QQuickItem *header)
{
    Q_D(QQuickPage);
    if (d->header == header)
        return;

    if (d->header) {
        QQuickItemPrivate::get(d->header)->removeItemChangeListener(d, LayoutChanges);
        d->header->setParentItem(nullptr);
    }
    d->header = header;
    if (header) {
        header->setParentItem(this);
        QQuickItemPrivate::get(header)->addItemChangeListener(d, LayoutChanges);
        if (qFuzzyIsNull(header->z()))
            header->setZ(1);
        setPos(header, Header);
    }
    if (isComponentComplete())
        d->relayout();
    emit headerChanged();
}

/*!
    \qmlproperty Item QtQuick.Controls::Page::footer

    This property holds the page footer item. The footer item is positioned to
    the bottom, and resized to the width of the page. The default value is \c null.

    \note Assigning a ToolBar, TabBar, or DialogButtonBox as a page footer
    automatically sets the respective \l ToolBar::position, \l TabBar::position,
    or \l DialogButtonBox::position property to \c Footer.

    \sa header, ApplicationWindow::footer
*/
QQuickItem *QQuickPage::footer() const
{
    Q_D(const QQuickPage);
    return d->footer;
}

void QQuickPage::setFooter(QQuickItem *footer)
{
    Q_D(QQuickPage);
    if (d->footer == footer)
        return;

    if (d->footer) {
        QQuickItemPrivate::get(d->footer)->removeItemChangeListener(d, LayoutChanges);
        d->footer->setParentItem(nullptr);
    }
    d->footer = footer;
    if (footer) {
        footer->setParentItem(this);
        QQuickItemPrivate::get(footer)->addItemChangeListener(d, LayoutChanges);
        if (qFuzzyIsNull(footer->z()))
            footer->setZ(1);
        setPos(footer, Footer);
    }
    if (isComponentComplete())
        d->relayout();
    emit footerChanged();
}

/*!
    \since QtQuick.Controls 2.5 (Qt 5.12)
    \qmlproperty real QtQuick.Controls::Page::implicitHeaderWidth
    \readonly

    This property holds the implicit header width.

    The value is equal to \c {header && header.visible ? header.implicitWidth : 0}.

    \sa implicitHeaderHeight, implicitFooterWidth
*/
qreal QQuickPage::implicitHeaderWidth() const
{
    Q_D(const QQuickPage);
    if (!d->header || !d->header->isVisible())
        return 0;
    return d->header->implicitWidth();
}

/*!
    \since QtQuick.Controls 2.5 (Qt 5.12)
    \qmlproperty real QtQuick.Controls::Page::implicitHeaderHeight
    \readonly

    This property holds the implicit header height.

    The value is equal to \c {header && header.visible ? header.implicitHeight : 0}.

    \sa implicitHeaderWidth, implicitFooterHeight
*/
qreal QQuickPage::implicitHeaderHeight() const
{
    Q_D(const QQuickPage);
    if (!d->header || !d->header->isVisible())
        return 0;
    return d->header->implicitHeight();
}

/*!
    \since QtQuick.Controls 2.5 (Qt 5.12)
    \qmlproperty real QtQuick.Controls::Page::implicitFooterWidth
    \readonly

    This property holds the implicit footer width.

    The value is equal to \c {footer && footer.visible ? footer.implicitWidth : 0}.

    \sa implicitFooterHeight, implicitHeaderWidth
*/
qreal QQuickPage::implicitFooterWidth() const
{
    Q_D(const QQuickPage);
    if (!d->footer || !d->footer->isVisible())
        return 0;
    return d->footer->implicitWidth();
}

/*!
    \since QtQuick.Controls 2.5 (Qt 5.12)
    \qmlproperty real QtQuick.Controls::Page::implicitFooterHeight
    \readonly

    This property holds the implicit footer height.

    The value is equal to \c {footer && footer.visible ? footer.implicitHeight : 0}.

    \sa implicitFooterWidth, implicitHeaderHeight
*/
qreal QQuickPage::implicitFooterHeight() const
{
    Q_D(const QQuickPage);
    if (!d->footer || !d->footer->isVisible())
        return 0;
    return d->footer->implicitHeight();
}

void QQuickPage::componentComplete()
{
    Q_D(QQuickPage);
    QQuickPane::componentComplete();
    d->relayout();
}

void QQuickPage::spacingChange(qreal newSpacing, qreal oldSpacing)
{
    Q_D(QQuickPage);
    QQuickPane::spacingChange(newSpacing, oldSpacing);
    d->relayout();
}

#if QT_CONFIG(accessibility)
QAccessible::Role QQuickPage::accessibleRole() const
{
    return QAccessible::PageTab;
}

void QQuickPage::accessibilityActiveChanged(bool active)
{
    Q_D(QQuickPage);
    QQuickPane::accessibilityActiveChanged(active);

    if (active)
        maybeSetAccessibleName(d->title);
}
#endif

QT_END_NAMESPACE