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 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) {
83 value = array->get(j);
87 QV4::ScopedValue props(scope, array->get(j + 1));
88 if (initProperties(element, props, args))
92 }
else if (!error.isEmpty()) {
101 QV4::ScopedValue props(scope, (*args)[i + 1]);
102 if (initProperties(element, props, args))
106 }
else if (!error.isEmpty()) {
115 QQmlEngine *engine,
const QList<QQuickStackViewArg> &args)
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;
124 if (i < args.size() - 1) {
125 const QQuickStackViewArg &nextArg = args.at(i + 1);
129 if (!nextArg.mProperties.isEmpty()
130 || (!nextArg.mItem && !nextArg.mComponent && !nextArg.mUrl.isValid())) {
131 properties = nextArg.mProperties;
137 if (findElement(arg.mItem))
142 if (!arg.mProperties.isEmpty()) {
143 qmlWarning(q) <<
"Properties must come after an Item, Component or URL";
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);
153 return stackElements;
159 for (QQuickStackElement *e : std::as_const(elements)) {
169 if (
const QV4::QObjectWrapper *o = value.as<QV4::QObjectWrapper>())
170 return findElement(qobject_cast<QQuickItem *>(o->object()));
174static QUrl resolvedUrl(
const QUrl &url,
const QQmlRefPointer<QQmlContextData> &context)
176 if (url.isRelative())
177 return context->resolvedUrl(url);
184 if (url.isRelative())
185 return context->resolvedUrl(url).toString();
190 const QV4::Value &value,
const QQmlRefPointer<QQmlContextData> &context, QString *error)
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);
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,
215 QV4::ExecutionEngine *v4,
const QList<QQuickStackElement *> &elems)
217 Q_Q(QQuickStackView);
218 if (!elems.isEmpty()) {
219 for (QQuickStackElement *e : elems) {
220 e->setIndex(elements.size());
223 return elements.top()->load(v4, q);
231 return pushElements(v4, QList<QQuickStackElement *>() << element);
237 Q_Q(QQuickStackView);
238 while (elements.size() > 1 && elements.top() != element) {
239 delete elements.pop();
243 return elements.top()->load(v4, q);
248 const QList<QQuickStackElement *> &elems)
251 while (!elements.isEmpty()) {
258 return pushElements(v4, elems);
262 QV4::ExecutionEngine *v4, QQuickItem *item, QQuickStackView::Operation operation,
263 CurrentItemPolicy currentItemPolicy)
265 const QString operationName = QStringLiteral(
"pop");
266 if (modifyingElements) {
267 warnOfInterruption(operationName);
271 QScopedValueRollback<
bool> modifyingElementsRollback(modifyingElements,
true);
272 QScopedValueRollback<QString> operationNameRollback(
this->operation, operationName);
273 if (elements.isEmpty()) {
274 warn(QStringLiteral(
"no items to pop"));
279 warn(QStringLiteral(
"item cannot be null"));
283 const int oldDepth = elements.size();
284 QQuickStackElement *exit = elements.pop();
286 QQuickStackElement *enter = elements.top();
288 bool nothingToDo =
false;
289 if (item != currentItem) {
292 enter = elements.value(0);
295 enter = findElement(item);
297 warn(QStringLiteral(
"can't find item to pop: ") + QDebug::toString(item));
302 if (currentItemPolicy == CurrentItemPolicy::DoNotPop) {
315 QQuickItem *previousItem =
nullptr;
316 if (popElements(v4, enter)) {
318 exit->removal =
true;
319 removing.insert(exit);
320 previousItem = exit->item;
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);
331 setCurrentItem(enter);
336#if QT_CONFIG(quick_viewtransitions)
337void QQuickStackViewPrivate::ensureTransitioner()
340 transitioner =
new QQuickItemViewTransitioner;
341 transitioner->setChangeListener(
this);
345void QQuickStackViewPrivate::startTransition(
const QQuickStackTransition &first,
const QQuickStackTransition &second,
bool immediate)
348 first.element->transitionNextReposition(transitioner, first.type, first.target);
350 second.element->transitionNextReposition(transitioner, second.type, second.target);
356 if (!first.element->item || !first.element->prepareTransition(transitioner, first.viewBounds) || immediate)
357 completeTransition(first.element, first.transition, first.status);
359 first.element->startTransition(transitioner, first.status);
361 if (second.element) {
362 if (!second.element->item || !second.element->prepareTransition(transitioner, second.viewBounds) || immediate)
363 completeTransition(second.element, second.transition, second.status);
365 second.element->startTransition(transitioner, second.status);
369 setBusy(!transitioner->runningJobs.isEmpty());
370 transitioner->resetTargetLists();
374void QQuickStackViewPrivate::completeTransition(QQuickStackElement *element, QQuickTransition *transition, QQuickStackView::Status status)
376 element->setStatus(status);
378 if (element->prepared) {
385 ACTION_IF_DELETED(element, element->completeTransition(transition),
return);
386 }
else if (element->item) {
390 element->item->setPosition(element->nextTransitionTo);
393 viewItemTransitionFinished(element);
396void QQuickStackViewPrivate::viewItemTransitionFinished(QQuickItemViewTransitionableItem *transitionable)
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;
406 if (!existingElement || element == existingElement)
407 element->setVisible(
false);
408 if (element->removal || element->isPendingRemoval())
412 if (transitioner && transitioner->runningJobs.isEmpty()) {
417 QList<QQuickStackElement*> removedElements = removed;
420 for (QQuickStackElement *removedElement : std::as_const(removedElements)) {
423 if (removedElement->item && findElement(removedElement->item)) {
424 QQuickItemPrivate::get(removedElement->item)->removeItemChangeListener(removedElement, QQuickItemPrivate::Destroyed);
425 removedElement->item =
nullptr;
429 qDeleteAll(removedElements);
432 removing.remove(element);
438 Q_Q(QQuickStackView);
443 q->setFiltersChildMouseEvents(busy);
444 emit q->busyChanged();
449 Q_Q(QQuickStackView);
450 if (newDepth == oldDepth)
453 emit q->depthChanged();
454 if (newDepth == 0 || oldDepth == 0)
455 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)
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)