Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickstackview_p.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#if QT_CONFIG(quick_viewtransitions)
8#endif
9
10#include <QtCore/qscopedvaluerollback.h>
11#include <QtQml/qqmlinfo.h>
12#include <QtQml/qqmllist.h>
13#include <QtQml/private/qv4qmlcontext_p.h>
14#include <QtQml/private/qv4qobjectwrapper_p.h>
15#include <QtQml/private/qv4variantobject_p.h>
16#include <QtQml/private/qv4urlobject_p.h>
17#include <QtQuick/private/qquickanimation_p.h>
18#include <QtQuick/private/qquicktransition_p.h>
19
21
23{
24 Q_Q(QQuickStackView);
25 if (operation.isEmpty())
26 qmlWarning(q) << error;
27 else
28 qmlWarning(q) << operation << ": " << error;
29}
30
32{
33 Q_Q(QQuickStackView);
34 qmlWarning(q) << "cannot " << attemptedOperation << " while already in the process of completing a " << operation;
35}
36
38{
39 Q_Q(QQuickStackView);
40 QQuickItem *item = element ? element->item : nullptr;
41 if (currentItem == item)
42 return;
43
45 if (element)
46 element->setVisible(true);
47 if (item)
48 item->setFocus(true);
49 emit q->currentItemChanged();
50}
51
53{
54 if (props.isObject()) {
56 if (!wrapper) {
57 QV4::ExecutionEngine *v4 = args->v4engine();
58 element->properties.set(v4, props);
59 element->qmlCallingContext.set(v4, v4->qmlContext());
60 return true;
61 }
62 }
63 return false;
64}
65
66QList<QQuickStackElement *> QQuickStackViewPrivate::parseElements(int from, QQmlV4FunctionPtr args, QStringList *errors)
67{
68 QV4::ExecutionEngine *v4 = args->v4engine();
69 auto context = v4->callingQmlContext();
70 QV4::Scope scope(v4);
71
72 QList<QQuickStackElement *> elements;
73
74 int argc = args->length();
75 for (int i = from; i < argc; ++i) {
76 QV4::ScopedValue arg(scope, (*args)[i]);
78 const uint len = uint(array->getLength());
79 for (uint j = 0; j < len; ++j) {
81 QV4::ScopedValue value(scope, array->get(j));
83 if (element) {
84 if (j < len - 1) {
85 QV4::ScopedValue props(scope, array->get(j + 1));
86 if (initProperties(element, props, args))
87 ++j;
88 }
89 elements += element;
90 } else if (!error.isEmpty()) {
91 *errors += error;
92 }
93 }
94 } else {
97 if (element) {
98 if (i < argc - 1) {
99 QV4::ScopedValue props(scope, (*args)[i + 1]);
100 if (initProperties(element, props, args))
101 ++i;
102 }
103 elements += element;
104 } else if (!error.isEmpty()) {
105 *errors += error;
106 }
107 }
108 }
109 return elements;
110}
111
112QList<QQuickStackElement *> QQuickStackViewPrivate::parseElements(const QList<QQuickStackViewArg> &args)
113{
114 Q_Q(QQuickStackView);
115 QList<QQuickStackElement *> stackElements;
116 for (int i = 0; i < args.size(); ++i) {
117 const QQuickStackViewArg &arg = args.at(i);
119 // Look ahead at the next arg in case it contains properties for this
120 // Item/Component/URL.
121 if (i < args.size() - 1) {
122 const QQuickStackViewArg &nextArg = args.at(i + 1);
123 // If mProperties isn't empty, the user passed properties.
124 // If it is empty, but mItem, mComponent and mUrl also are,
125 // then they passed an empty property map.
126 if (!nextArg.mProperties.isEmpty()
127 || (!nextArg.mItem && !nextArg.mComponent && !nextArg.mUrl.isValid())) {
128 properties = nextArg.mProperties;
129 ++i;
130 }
131 }
132
133 // Remove any items that are already in the stack, as they can't be in two places at once.
134 if (findElement(arg.mItem))
135 continue;
136
137 // We look ahead one index for each Item/Component/URL, so if this arg is
138 // a property map, the user has passed two or more in a row.
139 if (!arg.mProperties.isEmpty()) {
140 qmlWarning(q) << "Properties must come after an Item, Component or URL";
141 return {};
142 }
143
146 element->properties.set(v4Engine, v4Engine->fromVariant(properties));
147 element->qmlCallingContext.set(v4Engine, v4Engine->qmlContext());
148 stackElements.append(element);
149 }
150 return stackElements;
151}
152
154{
155 if (item) {
156 for (QQuickStackElement *e : std::as_const(elements)) {
157 if (e->item == item)
158 return e;
159 }
160 }
161 return nullptr;
162}
163
165{
167 return findElement(qobject_cast<QQuickItem *>(o->object()));
168 return nullptr;
169}
170
171static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer<QQmlContextData> &context)
172{
173 if (url.isRelative())
174 return context->resolvedUrl(url).toString();
175 return url;
176}
177
178static QString resolvedUrl(const QString &str, const QQmlRefPointer<QQmlContextData> &context)
179{
180 QUrl url(str);
181 if (url.isRelative())
182 return context->resolvedUrl(url).toString();
183 return str;
184}
185
187{
188 Q_Q(QQuickStackView);
189 if (const QV4::String *s = value.as<QV4::String>())
192 return QQuickStackElement::fromObject(o->object(), q, error);
193 if (const QV4::UrlObject *u = value.as<QV4::UrlObject>())
195
196 if (value.as<QV4::Object>()) {
197 const QVariant data = QV4::ExecutionEngine::toVariant(value, QMetaType::fromType<QUrl>());
198 if (data.typeId() == QMetaType::QUrl) {
199 return QQuickStackElement::fromString(resolvedUrl(data.toUrl(), context).toString(), q,
200 error);
201 }
202 }
203
204 return nullptr;
205}
206
207bool QQuickStackViewPrivate::pushElements(const QList<QQuickStackElement *> &elems)
208{
209 Q_Q(QQuickStackView);
210 if (!elems.isEmpty()) {
211 for (QQuickStackElement *e : elems) {
212 e->setIndex(elements.size());
213 elements += e;
214 }
215 return elements.top()->load(q);
216 }
217 return false;
218}
219
221{
222 if (element)
223 return pushElements(QList<QQuickStackElement *>() << element);
224 return false;
225}
226
228{
229 Q_Q(QQuickStackView);
230 while (elements.size() > 1 && elements.top() != element) {
231 delete elements.pop();
232 if (!element)
233 break;
234 }
235 return elements.top()->load(q);
236}
237
238bool QQuickStackViewPrivate::replaceElements(QQuickStackElement *target, const QList<QQuickStackElement *> &elems)
239{
240 if (target) {
241 while (!elements.isEmpty()) {
243 delete top;
244 if (top == target)
245 break;
246 }
247 }
248 return pushElements(elems);
249}
250
252{
253 const QString operationName = QStringLiteral("pop");
254 if (modifyingElements) {
256 return nullptr;
257 }
258
259 QScopedValueRollback<bool> modifyingElementsRollback(modifyingElements, true);
260 QScopedValueRollback<QString> operationNameRollback(this->operation, operationName);
261 if (elements.isEmpty()) {
262 warn(QStringLiteral("no items to pop"));
263 return nullptr;
264 }
265
266 if (!item) {
267 warn(QStringLiteral("item cannot be null"));
268 return nullptr;
269 }
270
271 const int oldDepth = elements.size();
273 // top() here will be the item below the previously current item, since we just popped above.
275
276 bool nothingToDo = false;
277 if (item != currentItem) {
278 if (!item) {
279 // Popping down to the first item.
280 enter = elements.value(0);
281 } else {
282 // Popping down to an arbitrary item.
283 enter = findElement(item);
284 if (!enter) {
285 warn(QStringLiteral("can't find item to pop: ") + QDebug::toString(item));
286 nothingToDo = true;
287 }
288 }
289 } else {
290 if (currentItemPolicy == CurrentItemPolicy::DoNotPop) {
291 // popToItem was called with the currentItem, which means there are no items
292 // to pop because it's already at the top.
293 nothingToDo = true;
294 }
295 // else: popToItem was called by popCurrentItem, and so we _should_ pop.
296 }
297 if (nothingToDo) {
298 // Restore the element we popped earlier.
299 elements.push(exit);
300 return nullptr;
301 }
302
303 QQuickItem *previousItem = nullptr;
304 if (popElements(enter)) {
305 if (exit) {
306 exit->removal = true;
307 removing.insert(exit);
308 previousItem = exit->item;
309 }
310 depthChange(elements.size(), oldDepth);
311#if QT_CONFIG(quick_viewtransitions)
312 Q_Q(QQuickStackView);
313 startTransition(QQuickStackTransition::popExit(operation, exit, q),
316#else
318#endif
319 setCurrentItem(enter);
320 }
321 return previousItem;
322}
323
324#if QT_CONFIG(quick_viewtransitions)
325void QQuickStackViewPrivate::ensureTransitioner()
326{
327 if (!transitioner) {
328 transitioner = new QQuickItemViewTransitioner;
329 transitioner->setChangeListener(this);
330 }
331}
332
333void QQuickStackViewPrivate::startTransition(const QQuickStackTransition &first, const QQuickStackTransition &second, bool immediate)
334{
335 if (first.element)
336 first.element->transitionNextReposition(transitioner, first.type, first.target);
337 if (second.element)
338 second.element->transitionNextReposition(transitioner, second.type, second.target);
339
340 if (first.element) {
341 // Let the check for immediate happen after prepareTransition() is
342 // called, because we need the prepared transition in both branches.
343 // Same for the second element.
344 if (!first.element->item || !first.element->prepareTransition(transitioner, first.viewBounds) || immediate)
345 completeTransition(first.element, first.transition, first.status);
346 else
347 first.element->startTransition(transitioner, first.status);
348 }
349 if (second.element) {
350 if (!second.element->item || !second.element->prepareTransition(transitioner, second.viewBounds) || immediate)
351 completeTransition(second.element, second.transition, second.status);
352 else
353 second.element->startTransition(transitioner, second.status);
354 }
355
356 if (transitioner) {
357 setBusy(!transitioner->runningJobs.isEmpty());
358 transitioner->resetTargetLists();
359 }
360}
361
362void QQuickStackViewPrivate::completeTransition(QQuickStackElement *element, QQuickTransition *transition, QQuickStackView::Status status)
363{
364 element->setStatus(status);
365 if (transition) {
366 if (element->prepared) {
367 // Here we force reading all the animations, even if the desired
368 // transition type is StackView.Immediate. After that we force
369 // all the animations to complete immediately, without waiting for
370 // the animation timer.
371 // This allows us to correctly restore all the properties affected
372 // by the push/pop animations.
373 ACTION_IF_DELETED(element, element->completeTransition(transition), return);
374 } else if (element->item) {
375 // At least try to move the item to its desired place. This,
376 // however, is only a partly correct solution, because a lot more
377 // properties can be affected by the transition
378 element->item->setPosition(element->nextTransitionTo);
379 }
380 }
381 viewItemTransitionFinished(element);
382}
383
384void QQuickStackViewPrivate::viewItemTransitionFinished(QQuickItemViewTransitionableItem *transitionable)
385{
386 QQuickStackElement *element = static_cast<QQuickStackElement *>(transitionable);
387 if (element->status == QQuickStackView::Activating) {
389 } else if (element->status == QQuickStackView::Deactivating) {
391 QQuickStackElement *existingElement = element->item ? findElement(element->item) : nullptr;
392 // If a different element with the same item is found,
393 // do not call setVisible(false) since it needs to be visible.
394 if (!existingElement || element == existingElement)
395 element->setVisible(false);
396 if (element->removal || element->isPendingRemoval())
397 removed += element;
398 }
399
400 if (transitioner && transitioner->runningJobs.isEmpty()) {
401 // ~QQuickStackElement() emits QQuickStackViewAttached::removed(), which may be used
402 // to modify the stack. Set the status first and make a copy of the destroyable stack
403 // elements to exclude any modifications that may happen during qDeleteAll(). (QTBUG-62153)
404 setBusy(false);
405 QList<QQuickStackElement*> removedElements = removed;
406 removed.clear();
407
408 for (QQuickStackElement *removedElement : std::as_const(removedElements)) {
409 // If an element with the same item is found in the active stack list,
410 // forget about the item so that we don't hide it.
411 if (removedElement->item && findElement(removedElement->item)) {
412 QQuickItemPrivate::get(removedElement->item)->removeItemChangeListener(removedElement, QQuickItemPrivate::Destroyed);
413 removedElement->item = nullptr;
414 }
415 }
416
417 qDeleteAll(removedElements);
418 }
419
420 removing.remove(element);
421}
422#endif
423
425{
426 Q_Q(QQuickStackView);
427 if (busy == b)
428 return;
429
430 busy = b;
431 q->setFiltersChildMouseEvents(busy);
432 emit q->busyChanged();
433}
434
435void QQuickStackViewPrivate::depthChange(int newDepth, int oldDepth)
436{
437 Q_Q(QQuickStackView);
438 if (newDepth == oldDepth)
439 return;
440
441 emit q->depthChanged();
442 if (newDepth == 0 || oldDepth == 0)
443 emit q->emptyChanged();
444}
445
void setFocus(Qt::FocusReason focusReason=Qt::OtherFocusReason)
Gives keyboard input focus to this item.
QV4::ExecutionEngine * handle() const
Definition qjsengine.h:298
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
qsizetype length() const noexcept
Definition qlist.h:399
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
T value(qsizetype i) const
Definition qlist.h:664
void clear()
Definition qlist.h:434
QQuickAnchorLine top() const
static QQuickItemPrivate * get(QQuickItem *item)
void setChangeListener(QQuickItemViewTransitionChangeListener *obj)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void setPosition(const QPointF &)
void setVisible(bool visible)
static QQuickStackElement * fromObject(QObject *object, QQuickStackView *view, QString *error)
void setStatus(QQuickStackView::Status status)
static QQuickStackElement * fromString(const QString &str, QQuickStackView *view, QString *error)
static QQuickStackElement * fromStackViewArg(QQuickStackView *view, QQuickStackViewArg arg)
QV4::PersistentValue properties
QQuickStackView::Status status
bool load(QQuickStackView *parent)
QV4::PersistentValue qmlCallingContext
void warnOfInterruption(const QString &attemptedOperation)
bool pushElements(const QList< QQuickStackElement * > &elements)
bool replaceElements(QQuickStackElement *element, const QList< QQuickStackElement * > &elements)
QList< QQuickStackElement * > parseElements(int from, QQmlV4FunctionPtr args, QStringList *errors)
QList< QQuickStackElement * > removed
QQuickStackElement * createElement(const QV4::Value &value, const QQmlRefPointer< QQmlContextData > &context, QString *error)
void warn(const QString &error)
void setCurrentItem(QQuickStackElement *element)
bool popElements(QQuickStackElement *element)
QStack< QQuickStackElement * > elements
QQuickStackElement * findElement(QQuickItem *item) const
void depthChange(int newDepth, int oldDepth)
QSet< QQuickStackElement * > removing
QQuickItem * popToItem(QQuickItem *item, QQuickStackView::Operation operation, CurrentItemPolicy currentItemPolicy)
bool pushElement(QQuickStackElement *element)
bool remove(const T &value)
Definition qset.h:63
iterator insert(const T &value)
Definition qset.h:155
T & top()
Returns a reference to the stack's top item.
Definition qstack.h:19
T pop()
Removes the top item from the stack and returns it.
Definition qstack.h:18
void push(const T &t)
Adds element t to the top of the stack.
Definition qstack.h:17
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
\inmodule QtCore
Definition qurl.h:94
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition qurl.cpp:2800
void set(ExecutionEngine *engine, const Value &value)
\inmodule QtCore
Definition qvariant.h:65
QString str
[2]
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
static void * context
#define ACTION_IF_DELETED(p, func, action)
static const QCssKnownValue properties[NumProperties - 1]
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static QV4::ExecutionEngine * v4Engine(QV4::Value *d)
GLboolean GLboolean GLboolean b
GLdouble GLdouble GLdouble GLdouble top
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum target
GLenum GLuint GLsizei const GLenum * props
GLint first
GLdouble s
[6]
Definition qopenglext.h:235
GLenum array
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLsizei len
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:80
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer< QQmlContextData > &context)
static bool initProperties(QQuickStackElement *element, const QV4::Value &props, QQmlV4FunctionPtr args)
static QLatin1StringView operationName(QNetworkAccessManager::Operation operation)
SSL_CTX int void * arg
#define QStringLiteral(str)
#define emit
#define Q_UNUSED(x)
unsigned int uint
Definition qtypes.h:34
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr
QGraphicsItem * item
QJSValueList args
QQuickItemViewTransitioner::TransitionType type
QQuickStackView::Status status
static QQuickStackTransition popExit(QQuickStackView::Operation operation, QQuickStackElement *element, QQuickStackView *view)
static QQuickStackTransition popEnter(QQuickStackView::Operation operation, QQuickStackElement *element, QQuickStackView *view)
QQmlRefPointer< QQmlContextData > callingQmlContext() const
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols=true)
static Heap::ExecutionContext * qmlContext(Heap::ExecutionContext *ctx)
const T * as() const
Definition qv4value_p.h:132
void wrapper()