aboutsummaryrefslogtreecommitdiffstats
path: root/src/controls/qquickabstractstackview.cpp
blob: a849bd943f6d7f1f9e4e6facca14aea5518b64e4 (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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Quick Controls 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 "qquickabstractstackview_p.h"
#include "qquickabstractcontainer_p_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype StackView
    \inherits Container
    \instantiates QQuickAbstractStackView
    \inqmlmodule QtQuick.Controls
    \ingroup navigation
    \brief A stack view control.

    TODO
*/

class QQuickStackElement
{
    QQuickStackElement() : ownItem(false), item(Q_NULLPTR), ownComponent(false), component(Q_NULLPTR) { }

public:
    static QQuickStackElement *fromString(const QString &str, QQmlEngine *engine, QObject *parent)
    {
        QQuickStackElement *element = new QQuickStackElement;
        element->component = new QQmlComponent(engine, QUrl(str), parent);
        element->ownComponent = true;
        return element;
    }

    static QQuickStackElement *fromObject(QObject *object)
    {
        QQuickStackElement *element = new QQuickStackElement;
        element->component = qobject_cast<QQmlComponent *>(object);
        element->item = qobject_cast<QQuickItem *>(object);
        return element;
    }

    bool ownItem;
    QQuickItem *item;

    bool ownComponent;
    QQmlComponent *component;
};

class QQuickAbstractStackViewPrivate : public QQuickAbstractContainerPrivate
{
    Q_DECLARE_PUBLIC(QQuickAbstractStackView)

public:
    QQuickAbstractStackViewPrivate() : busy(false), depth(0), currentItem(Q_NULLPTR) { }

    void setBusy(bool busy);
    void setDepth(int depth);
    void setCurrentItem(QQuickItem *item);

    QList<QQuickStackElement *> createElements(const QV4::ScopedValue &value);
    QQuickItem *pushElements(const QList<QQuickStackElement *> &elements, QQuickAbstractStackView::Operation operation);

    bool busy;
    int depth;
    QVariant initialItem;
    QQuickItem *currentItem;
    QStack<QQuickStackElement *> elements;
};

void QQuickAbstractStackViewPrivate::setBusy(bool value)
{
    Q_Q(QQuickAbstractStackView);
    if (busy != value) {
        busy = value;
        emit q->busyChanged();
    }
}

void QQuickAbstractStackViewPrivate::setDepth(int value)
{
    Q_Q(QQuickAbstractStackView);
    if (depth != value) {
        depth = value;
        emit q->depthChanged();
    }
}

void QQuickAbstractStackViewPrivate::setCurrentItem(QQuickItem *item)
{
    Q_Q(QQuickAbstractStackView);
    if (currentItem != item) {
        currentItem = item;
        emit q->currentItemChanged();
    }
}

QList<QQuickStackElement *> QQuickAbstractStackViewPrivate::createElements(const QV4::ScopedValue &value)
{
    Q_Q(QQuickAbstractStackView);
    QList<QQuickStackElement *> elements;
    if (QV4::String *s = value->asString()) {
        qDebug() << "### STRING:" << s->toQString();
        elements += QQuickStackElement::fromString(s->toQString(), qmlEngine(q), q);
    } else if (QV4::ArrayObject *a = value->asArrayObject()) {
        int len = a->getLength();
        qDebug() << "### ARRAY:" << len;
        for (int i = 0; i < len; ++i) {
            QV4::Scope scope(a->engine());
            QV4::ScopedValue v(scope, a->getIndexed(i));
            elements += createElements(v);
        }
    } else if (const QV4::QObjectWrapper *o =value->as<QV4::QObjectWrapper>()) {
        qDebug() << "### QOBJECT:" << o->object();
        elements += QQuickStackElement::fromObject(o->object());
    } else {
        qDebug("### UNKNOWN");
    }
    return elements;
}

QQuickItem *QQuickAbstractStackViewPrivate::pushElements(const QList<QQuickStackElement *> &elems, QQuickAbstractStackView::Operation operation)
{
    Q_Q(QQuickAbstractStackView);
    Q_UNUSED(operation); // TODO
    if (!elems.isEmpty()) {
        foreach (QQuickStackElement *elem, elems) {
            elements.push(elem);
        }
        emit q->depthChanged();
        // TODO: load
        return elems.last()->item;
    }
    return Q_NULLPTR;
}

QQuickAbstractStackView::QQuickAbstractStackView(QQuickItem *parent) :
    QQuickAbstractContainer(*(new QQuickAbstractStackViewPrivate), parent)
{
    setFlag(ItemIsFocusScope);
}

QQuickAbstractStackView::~QQuickAbstractStackView()
{
    Q_D(QQuickAbstractStackView);
    qDeleteAll(d->elements);
}

/*!
    \qmlproperty bool QtQuickControls2::StackView::busy
    \readonly
    \c true if a transition is running, and \c false otherwise.
*/
bool QQuickAbstractStackView::busy() const
{
    Q_D(const QQuickAbstractStackView);
    return d->busy;
}

// TODO: remove
void QQuickAbstractStackView::setBusy(bool busy)
{
    Q_D(QQuickAbstractStackView);
    d->setBusy(busy);
}

/*!
    \qmlproperty int QtQuickControls2::StackView::depth
    \readonly
    The number of items currently pushed onto the stack.
*/
int QQuickAbstractStackView::depth() const
{
    Q_D(const QQuickAbstractStackView);
    return d->depth;
}

// TODO: remove
void QQuickAbstractStackView::setDepth(int depth)
{
    Q_D(QQuickAbstractStackView);
    d->setDepth(depth);
}

/*!
    \qmlproperty Item QtQuickControls2::StackView::currentItem
    \readonly
    The currently top-most item in the stack.
*/
QQuickItem *QQuickAbstractStackView::currentItem() const
{
    Q_D(const QQuickAbstractStackView);
    return d->currentItem;
}

// TODO: remove
void QQuickAbstractStackView::setCurrentItem(QQuickItem *item)
{
    Q_D(QQuickAbstractStackView);
    d->setCurrentItem(item);
}

QQuickItem *QQuickAbstractStackView::qpush(QQmlV4Function *args)
{
    Q_D(QQuickAbstractStackView);
    QV4::ExecutionEngine *v4 = args->v4engine();
    QV4::Scope scope(v4);

    Operation operation = d->elements.isEmpty() ? Immediate : Transition;
    QList<QQuickStackElement *> elements;
    for (int i = 0; i < args->length(); ++i) {
        QV4::ScopedValue value(scope, (*args)[i]);
        if (value->isInt32())
            operation = static_cast<Operation>(value->toInt32());
        else
            elements += d->createElements(value);
    }
    return d->pushElements(elements, operation);
}

QQuickItem *QQuickAbstractStackView::qpop(QQmlV4Function *args)
{
    Q_UNUSED(args); // TODO
    return Q_NULLPTR;
}

void QQuickAbstractStackView::qclear()
{
    // TODO
}

/*!
    \qmlproperty var QtQuickControls2::StackView::initialItem

    The first \l item that should be shown when the StackView is created.
    \a initialItem can take same value as the first argument to \l{StackView::push()}
    {StackView.push()}. Note that this is just a convenience for writing
    \c{Component.onCompleted: stackView.push(myInitialItem)}

    Examples:

    \list
    \li initialItem: Qt.resolvedUrl("MyItem.qml")
    \li initialItem: myItem
    \li initialItem: {"item" : Qt.resolvedUrl("MyRectangle.qml"), "properties" : {"color" : "red"}}
    \endlist
    \sa push
*/
QVariant QQuickAbstractStackView::initialItem() const
{
    Q_D(const QQuickAbstractStackView);
    return d->initialItem;
}

void QQuickAbstractStackView::setInitialItem(const QVariant &item)
{
    Q_D(QQuickAbstractStackView);
    d->initialItem = item;
}

void QQuickAbstractStackView::componentComplete()
{
    QQuickAbstractContainer::componentComplete();

    Q_D(QQuickAbstractStackView);
    if (QObject *o = d->initialItem.value<QObject *>())
        d->pushElements(QList<QQuickStackElement *>() << QQuickStackElement::fromObject(o), Immediate);
    else if (d->initialItem.canConvert<QString>())
        d->pushElements(QList<QQuickStackElement *>() << QQuickStackElement::fromString(d->initialItem.toString(), qmlEngine(this), this), Immediate);
}

/*!
    \qmltype Stack
    \inherits QtObject
    \instantiates QQuickStackAttached
    \inqmlmodule QtQuick.Controls
    \ingroup navigation
    \brief TODO

    TODO
*/

class QQuickStackAttachedPrivate : public QObjectPrivate
{
public:
    QQuickStackAttachedPrivate() : status(QQuickStackAttached::Inactive) { }

    QQuickStackAttached::Status status;
};

QQuickStackAttached::QQuickStackAttached(QObject *parent) :
    QObject(*(new QQuickStackAttachedPrivate), parent)
{
}

QQuickStackAttached *QQuickStackAttached::qmlAttachedProperties(QObject *object)
{
    return new QQuickStackAttached(object);
}

/*!
    \qmlattachedproperty enumeration QtQuickControls2::Stack::status

    TODO
*/
QQuickStackAttached::Status QQuickStackAttached::status() const
{
    Q_D(const QQuickStackAttached);
    return d->status;
}

void QQuickStackAttached::setStatus(Status status)
{
    Q_D(QQuickStackAttached);
    if (d->status != status) {
        d->status = status;
        emit statusChanged();
    }
}

QT_END_NAMESPACE