7#if QT_CONFIG(quick_viewtransitions)
8#include "qquickstacktransition_p_p.h"
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>
26 if (operation.isEmpty())
27 qmlWarning(q) << error;
29 qmlWarning(q) << operation <<
": " << error;
35 qmlWarning(q) <<
"cannot " << attemptedOperation <<
" while already in the process of completing a " << operation;
41 QQuickItem *item = element ? element->item :
nullptr;
42 if (currentItem == item)
50 emit q->currentItemChanged();
55 if (props.isObject()) {
56 const QV4::QObjectWrapper *wrapper = props.as<QV4::QObjectWrapper>();
58 QV4::ExecutionEngine *v4 = args->v4engine();
59 element->properties.set(v4, props);
60 element->qmlCallingContext.set(v4, v4->qmlContext());
69 QV4::ExecutionEngine *v4 = args->v4engine();
70 auto context = v4->callingQmlContext();
73 QList<QQuickStackElement *> elements;
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) {
82 QV4::ScopedValue value(scope, array->get(j));
86 QV4::ScopedValue props(scope, array->get(j + 1));
87 if (initProperties(element, props, args))
91 }
else if (!error.isEmpty()) {
100 QV4::ScopedValue props(scope, (*args)[i + 1]);
101 if (initProperties(element, props, args))
105 }
else if (!error.isEmpty()) {
114 QQmlEngine *engine,
const QList<QQuickStackViewArg> &args)
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;
123 if (i < args.size() - 1) {
124 const QQuickStackViewArg &nextArg = args.at(i + 1);
128 if (!nextArg.mProperties.isEmpty()
129 || (!nextArg.mItem && !nextArg.mComponent && !nextArg.mUrl.isValid())) {
130 properties = nextArg.mProperties;
136 if (findElement(arg.mItem))
141 if (!arg.mProperties.isEmpty()) {
142 qmlWarning(q) <<
"Properties must come after an Item, Component or URL";
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);
152 return stackElements;
158 for (QQuickStackElement *e : std::as_const(elements)) {
168 if (
const QV4::QObjectWrapper *o = value.as<QV4::QObjectWrapper>())
169 return findElement(qobject_cast<QQuickItem *>(o->object()));
173static QUrl resolvedUrl(
const QUrl &url,
const QQmlRefPointer<QQmlContextData> &context)
175 if (url.isRelative())
176 return context->resolvedUrl(url).toString();
183 if (url.isRelative())
184 return context->resolvedUrl(url).toString();
189 const QV4::Value &value,
const QQmlRefPointer<QQmlContextData> &context, QString *error)
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);
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,
214 QV4::ExecutionEngine *v4,
const QList<QQuickStackElement *> &elems)
216 Q_Q(QQuickStackView);
217 if (!elems.isEmpty()) {
218 for (QQuickStackElement *e : elems) {
219 e->setIndex(elements.size());
222 return elements.top()->load(v4, q);
230 return pushElements(v4, QList<QQuickStackElement *>() << element);
236 Q_Q(QQuickStackView);
237 while (elements.size() > 1 && elements.top() != element) {
238 delete elements.pop();
242 return elements.top()->load(v4, q);
247 const QList<QQuickStackElement *> &elems)
250 while (!elements.isEmpty()) {
257 return pushElements(v4, elems);
261 QV4::ExecutionEngine *v4, QQuickItem *item, QQuickStackView::Operation operation,
262 CurrentItemPolicy currentItemPolicy)
264 const QString operationName = QStringLiteral(
"pop");
265 if (modifyingElements) {
266 warnOfInterruption(operationName);
270 QScopedValueRollback<
bool> modifyingElementsRollback(modifyingElements,
true);
271 QScopedValueRollback<QString> operationNameRollback(
this->operation, operationName);
272 if (elements.isEmpty()) {
273 warn(QStringLiteral(
"no items to pop"));
278 warn(QStringLiteral(
"item cannot be null"));
282 const int oldDepth = elements.size();
283 QQuickStackElement *exit = elements.pop();
285 QQuickStackElement *enter = elements.top();
287 bool nothingToDo =
false;
288 if (item != currentItem) {
291 enter = elements.value(0);
294 enter = findElement(item);
296 warn(QStringLiteral(
"can't find item to pop: ") + QDebug::toString(item));
301 if (currentItemPolicy == CurrentItemPolicy::DoNotPop) {
314 QQuickItem *previousItem =
nullptr;
315 if (popElements(v4, enter)) {
317 exit->removal =
true;
318 removing.insert(exit);
319 previousItem = exit->item;
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);
330 setCurrentItem(enter);
335#if QT_CONFIG(quick_viewtransitions)
336void QQuickStackViewPrivate::ensureTransitioner()
339 transitioner =
new QQuickItemViewTransitioner;
340 transitioner->setChangeListener(
this);
344void QQuickStackViewPrivate::startTransition(
const QQuickStackTransition &first,
const QQuickStackTransition &second,
bool immediate)
347 first.element->transitionNextReposition(transitioner, first.type, first.target);
349 second.element->transitionNextReposition(transitioner, second.type, second.target);
355 if (!first.element->item || !first.element->prepareTransition(transitioner, first.viewBounds) || immediate)
356 completeTransition(first.element, first.transition, first.status);
358 first.element->startTransition(transitioner, first.status);
360 if (second.element) {
361 if (!second.element->item || !second.element->prepareTransition(transitioner, second.viewBounds) || immediate)
362 completeTransition(second.element, second.transition, second.status);
364 second.element->startTransition(transitioner, second.status);
368 setBusy(!transitioner->runningJobs.isEmpty());
369 transitioner->resetTargetLists();
373void QQuickStackViewPrivate::completeTransition(QQuickStackElement *element, QQuickTransition *transition, QQuickStackView::Status status)
375 element->setStatus(status);
377 if (element->prepared) {
384 ACTION_IF_DELETED(element, element->completeTransition(transition),
return);
385 }
else if (element->item) {
389 element->item->setPosition(element->nextTransitionTo);
392 viewItemTransitionFinished(element);
395void QQuickStackViewPrivate::viewItemTransitionFinished(QQuickItemViewTransitionableItem *transitionable)
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;
405 if (!existingElement || element == existingElement)
406 element->setVisible(
false);
407 if (element->removal || element->isPendingRemoval())
411 if (transitioner && transitioner->runningJobs.isEmpty()) {
416 QList<QQuickStackElement*> removedElements = removed;
419 for (QQuickStackElement *removedElement : std::as_const(removedElements)) {
422 if (removedElement->item && findElement(removedElement->item)) {
423 QQuickItemPrivate::get(removedElement->item)->removeItemChangeListener(removedElement, QQuickItemPrivate::Destroyed);
424 removedElement->item =
nullptr;
428 qDeleteAll(removedElements);
431 removing.remove(element);
437 Q_Q(QQuickStackView);
442 q->setFiltersChildMouseEvents(busy);
443 emit q->busyChanged();
448 Q_Q(QQuickStackView);
449 if (newDepth == oldDepth)
452 emit q->depthChanged();
453 if (newDepth == 0 || oldDepth == 0)
454 emit q->emptyChanged();
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)