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