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