Qt 6.x
The Qt SDK
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
67{
68 QV4::ExecutionEngine *v4 = args->v4engine();
69 auto context = v4->callingQmlContext();
70 QV4::Scope scope(v4);
71
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
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
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
172{
173 if (url.isRelative())
174 return context->resolvedUrl(url).toString();
175 return url;
176}
177
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) {
200 error);
201 }
202 }
203
204 return nullptr;
205}
206
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
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 Q_Q(QQuickStackView);
254 const QString operationName = QStringLiteral("pop");
255 if (modifyingElements) {
256 warnOfInterruption(operationName);
257 return nullptr;
258 }
259
260 QScopedValueRollback<bool> modifyingElementsRollback(modifyingElements, true);
261 QScopedValueRollback<QString> operationNameRollback(this->operation, operationName);
262 if (elements.isEmpty()) {
263 warn(QStringLiteral("no items to pop"));
264 return nullptr;
265 }
266
267 if (!item) {
268 warn(QStringLiteral("item cannot be null"));
269 return nullptr;
270 }
271
272 const int oldDepth = elements.size();
274 // top() here will be the item below the previously current item, since we just popped above.
276
277 bool nothingToDo = false;
278 if (item != currentItem) {
279 if (!item) {
280 // Popping down to the first item.
281 enter = elements.value(0);
282 } else {
283 // Popping down to an arbitrary item.
284 enter = findElement(item);
285 if (!enter) {
286 warn(QStringLiteral("can't find item to pop: ") + QDebug::toString(item));
287 nothingToDo = true;
288 }
289 }
290 } else {
291 if (currentItemPolicy == CurrentItemPolicy::DoNotPop) {
292 // popToItem was called with the currentItem, which means there are no items
293 // to pop because it's already at the top.
294 nothingToDo = true;
295 }
296 // else: popToItem was called by popCurrentItem, and so we _should_ pop.
297 }
298 if (nothingToDo) {
299 // Restore the element we popped earlier.
300 elements.push(exit);
301 return nullptr;
302 }
303
304 QQuickItem *previousItem = nullptr;
305 if (popElements(enter)) {
306 if (exit) {
307 exit->removal = true;
308 removing.insert(exit);
309 previousItem = exit->item;
310 }
311 depthChange(elements.size(), oldDepth);
312#if QT_CONFIG(quick_viewtransitions)
313 startTransition(QQuickStackTransition::popExit(operation, exit, q),
316#endif
317 setCurrentItem(enter);
318 }
319 return previousItem;
320}
321
322#if QT_CONFIG(quick_viewtransitions)
323void QQuickStackViewPrivate::ensureTransitioner()
324{
325 if (!transitioner) {
326 transitioner = new QQuickItemViewTransitioner;
327 transitioner->setChangeListener(this);
328 }
329}
330
331void QQuickStackViewPrivate::startTransition(const QQuickStackTransition &first, const QQuickStackTransition &second, bool immediate)
332{
333 if (first.element)
334 first.element->transitionNextReposition(transitioner, first.type, first.target);
335 if (second.element)
336 second.element->transitionNextReposition(transitioner, second.type, second.target);
337
338 if (first.element) {
339 // Let the check for immediate happen after prepareTransition() is
340 // called, because we need the prepared transition in both branches.
341 // Same for the second element.
342 if (!first.element->item || !first.element->prepareTransition(transitioner, first.viewBounds) || immediate)
343 completeTransition(first.element, first.transition, first.status);
344 else
345 first.element->startTransition(transitioner, first.status);
346 }
347 if (second.element) {
348 if (!second.element->item || !second.element->prepareTransition(transitioner, second.viewBounds) || immediate)
349 completeTransition(second.element, second.transition, second.status);
350 else
351 second.element->startTransition(transitioner, second.status);
352 }
353
354 if (transitioner) {
355 setBusy(!transitioner->runningJobs.isEmpty());
356 transitioner->resetTargetLists();
357 }
358}
359
360void QQuickStackViewPrivate::completeTransition(QQuickStackElement *element, QQuickTransition *transition, QQuickStackView::Status status)
361{
362 element->setStatus(status);
363 if (transition) {
364 if (element->prepared) {
365 // Here we force reading all the animations, even if the desired
366 // transition type is StackView.Immediate. After that we force
367 // all the animations to complete immediately, without waiting for
368 // the animation timer.
369 // This allows us to correctly restore all the properties affected
370 // by the push/pop animations.
371 ACTION_IF_DELETED(element, element->completeTransition(transition), return);
372 } else if (element->item) {
373 // At least try to move the item to its desired place. This,
374 // however, is only a partly correct solution, because a lot more
375 // properties can be affected by the transition
376 element->item->setPosition(element->nextTransitionTo);
377 }
378 }
379 viewItemTransitionFinished(element);
380}
381
382void QQuickStackViewPrivate::viewItemTransitionFinished(QQuickItemViewTransitionableItem *transitionable)
383{
384 QQuickStackElement *element = static_cast<QQuickStackElement *>(transitionable);
385 if (element->status == QQuickStackView::Activating) {
387 } else if (element->status == QQuickStackView::Deactivating) {
389 QQuickStackElement *existingElement = element->item ? findElement(element->item) : nullptr;
390 // If a different element with the same item is found,
391 // do not call setVisible(false) since it needs to be visible.
392 if (!existingElement || element == existingElement)
393 element->setVisible(false);
394 if (element->removal || element->isPendingRemoval())
395 removed += element;
396 }
397
398 if (transitioner && transitioner->runningJobs.isEmpty()) {
399 // ~QQuickStackElement() emits QQuickStackViewAttached::removed(), which may be used
400 // to modify the stack. Set the status first and make a copy of the destroyable stack
401 // elements to exclude any modifications that may happen during qDeleteAll(). (QTBUG-62153)
402 setBusy(false);
403 QList<QQuickStackElement*> removedElements = removed;
404 removed.clear();
405
406 for (QQuickStackElement *removedElement : std::as_const(removedElements)) {
407 // If an element with the same item is found in the active stack list,
408 // forget about the item so that we don't hide it.
409 if (removedElement->item && findElement(removedElement->item)) {
411 removedElement->item = nullptr;
412 }
413 }
414
415 qDeleteAll(removedElements);
416 }
417
418 removing.remove(element);
419}
420#endif
421
423{
424 Q_Q(QQuickStackView);
425 if (busy == b)
426 return;
427
428 busy = b;
429 q->setFiltersChildMouseEvents(busy);
430 emit q->busyChanged();
431}
432
433void QQuickStackViewPrivate::depthChange(int newDepth, int oldDepth)
434{
435 Q_Q(QQuickStackView);
436 if (newDepth == oldDepth)
437 return;
438
439 emit q->depthChanged();
440 if (newDepth == 0 || oldDepth == 0)
441 emit q->emptyChanged();
442}
443
void setFocus(Qt::FocusReason focusReason=Qt::OtherFocusReason)
Gives keyboard input focus to this item.
QV4::ExecutionEngine * handle() const
Definition qjsengine.h:292
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
qsizetype length() const noexcept
Definition qlist.h:388
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
T value(qsizetype i) const
Definition qlist.h:661
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
bool isEmpty() const
Definition qmap.h:268
void removeItemChangeListener(QQuickItemChangeListener *, ChangeTypes types)
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:64
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 * > removed
QQuickStackElement * createElement(const QV4::Value &value, const QQmlRefPointer< QQmlContextData > &context, QString *error)
void warn(const QString &error)
void setCurrentItem(QQuickStackElement *element)
QList< QQuickStackElement * > parseElements(int from, QQmlV4Function *args, QStringList *errors)
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:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\inmodule QtCore
Definition qurl.h:94
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition qurl.cpp:2797
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
Definition qurl.cpp:1874
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2828
void set(ExecutionEngine *engine, const Value &value)
\inmodule QtCore
Definition qvariant.h:64
QString str
[2]
qDeleteAll(list.begin(), list.end())
double e
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
GLenum target
GLenum GLuint GLsizei const GLenum * props
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
GLenum array
GLenum GLsizei len
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLdouble s
[6]
Definition qopenglext.h:235
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:76
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:483
static bool initProperties(QQuickStackElement *element, const QV4::Value &props, QQmlV4Function *args)
static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer< QQmlContextData > &context)
SSL_CTX int(*) void arg)
#define QStringLiteral(str)
#define emit
unsigned int uint
Definition qtypes.h:29
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
QV4::ReturnedValue fromVariant(const QVariant &)
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()