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 | |
4 | #include "qquickstackview_p_p.h" |
5 | #include "qquickstackelement_p_p.h" |
6 | #if QT_CONFIG(quick_viewtransitions) |
7 | #include "qquickstacktransition_p_p.h" |
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 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | void QQuickStackViewPrivate::warn(const QString &error) |
23 | { |
24 | Q_Q(QQuickStackView); |
25 | if (operation.isEmpty()) |
26 | qmlWarning(me: q) << error; |
27 | else |
28 | qmlWarning(me: q) << operation << ": "<< error; |
29 | } |
30 | |
31 | void QQuickStackViewPrivate::warnOfInterruption(const QString &attemptedOperation) |
32 | { |
33 | Q_Q(QQuickStackView); |
34 | qmlWarning(me: q) << "cannot "<< attemptedOperation << " while already in the process of completing a "<< operation; |
35 | } |
36 | |
37 | void QQuickStackViewPrivate::setCurrentItem(QQuickStackElement *element) |
38 | { |
39 | Q_Q(QQuickStackView); |
40 | QQuickItem *item = element ? element->item : nullptr; |
41 | if (currentItem == item) |
42 | return; |
43 | |
44 | currentItem = item; |
45 | if (element) |
46 | element->setVisible(true); |
47 | if (item) |
48 | item->setFocus(true); |
49 | emit q->currentItemChanged(); |
50 | } |
51 | |
52 | static bool initProperties(QQuickStackElement *element, const QV4::Value &props, QQmlV4FunctionPtr args) |
53 | { |
54 | if (props.isObject()) { |
55 | const QV4::QObjectWrapper *wrapper = props.as<QV4::QObjectWrapper>(); |
56 | if (!wrapper) { |
57 | QV4::ExecutionEngine *v4 = args->v4engine(); |
58 | element->properties.set(engine: v4, value: props); |
59 | element->qmlCallingContext.set(engine: v4, obj: v4->qmlContext()); |
60 | return true; |
61 | } |
62 | } |
63 | return false; |
64 | } |
65 | |
66 | QList<QQuickStackElement *> QQuickStackViewPrivate::parseElements(int from, QQmlV4FunctionPtr args, QStringList *errors) |
67 | { |
68 | QV4::ExecutionEngine *v4 = args->v4engine(); |
69 | auto context = v4->callingQmlContext(); |
70 | QV4::Scope scope(v4); |
71 | |
72 | QList<QQuickStackElement *> elements; |
73 | |
74 | int argc = args->length(); |
75 | for (int i = from; i < argc; ++i) { |
76 | QV4::ScopedValue arg(scope, (*args)[i]); |
77 | if (QV4::ArrayObject *array = arg->as<QV4::ArrayObject>()) { |
78 | const uint len = uint(array->getLength()); |
79 | for (uint j = 0; j < len; ++j) { |
80 | QString error; |
81 | QV4::ScopedValue value(scope, array->get(idx: j)); |
82 | QQuickStackElement *element = createElement(value, context, error: &error); |
83 | if (element) { |
84 | if (j < len - 1) { |
85 | QV4::ScopedValue props(scope, array->get(idx: 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 { |
95 | QString error; |
96 | QQuickStackElement *element = createElement(value: arg, context, error: &error); |
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 | |
112 | QList<QQuickStackElement *> QQuickStackViewPrivate::parseElements(const QList<QQuickStackViewArg> &args) |
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); |
118 | QVariantMap properties; |
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: 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(item: 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(me: q) << "Properties must come after an Item, Component or URL"; |
141 | return {}; |
142 | } |
143 | |
144 | QQuickStackElement *element = QQuickStackElement::fromStackViewArg(view: q, arg); |
145 | QV4::ExecutionEngine *v4Engine = qmlEngine(q)->handle(); |
146 | element->properties.set(engine: v4Engine, value: v4Engine->fromVariant(properties)); |
147 | element->qmlCallingContext.set(engine: v4Engine, obj: v4Engine->qmlContext()); |
148 | stackElements.append(t: element); |
149 | } |
150 | return stackElements; |
151 | } |
152 | |
153 | QQuickStackElement *QQuickStackViewPrivate::findElement(QQuickItem *item) const |
154 | { |
155 | if (item) { |
156 | for (QQuickStackElement *e : std::as_const(t: elements)) { |
157 | if (e->item == item) |
158 | return e; |
159 | } |
160 | } |
161 | return nullptr; |
162 | } |
163 | |
164 | QQuickStackElement *QQuickStackViewPrivate::findElement(const QV4::Value &value) const |
165 | { |
166 | if (const QV4::QObjectWrapper *o = value.as<QV4::QObjectWrapper>()) |
167 | return findElement(item: qobject_cast<QQuickItem *>(o: o->object())); |
168 | return nullptr; |
169 | } |
170 | |
171 | static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer<QQmlContextData> &context) |
172 | { |
173 | if (url.isRelative()) |
174 | return context->resolvedUrl(url).toString(); |
175 | return url; |
176 | } |
177 | |
178 | static QString resolvedUrl(const QString &str, const QQmlRefPointer<QQmlContextData> &context) |
179 | { |
180 | QUrl url(str); |
181 | if (url.isRelative()) |
182 | return context->resolvedUrl(url).toString(); |
183 | return str; |
184 | } |
185 | |
186 | QQuickStackElement *QQuickStackViewPrivate::createElement(const QV4::Value &value, const QQmlRefPointer<QQmlContextData> &context, QString *error) |
187 | { |
188 | Q_Q(QQuickStackView); |
189 | if (const QV4::String *s = value.as<QV4::String>()) |
190 | return QQuickStackElement::fromString(str: resolvedUrl(str: s->toQString(), context), view: q, error); |
191 | if (const QV4::QObjectWrapper *o = value.as<QV4::QObjectWrapper>()) |
192 | return QQuickStackElement::fromObject(object: o->object(), view: q, error); |
193 | if (const QV4::UrlObject *u = value.as<QV4::UrlObject>()) |
194 | return QQuickStackElement::fromString(str: resolvedUrl(str: u->href(), context), view: q, error); |
195 | |
196 | if (value.as<QV4::Object>()) { |
197 | const QVariant data = QV4::ExecutionEngine::toVariant(value, typeHint: QMetaType::fromType<QUrl>()); |
198 | if (data.typeId() == QMetaType::QUrl) { |
199 | return QQuickStackElement::fromString(str: resolvedUrl(url: data.toUrl(), context).toString(), view: q, |
200 | error); |
201 | } |
202 | } |
203 | |
204 | return nullptr; |
205 | } |
206 | |
207 | bool QQuickStackViewPrivate::pushElements(const QList<QQuickStackElement *> &elems) |
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(parent: q); |
216 | } |
217 | return false; |
218 | } |
219 | |
220 | bool QQuickStackViewPrivate::pushElement(QQuickStackElement *element) |
221 | { |
222 | if (element) |
223 | return pushElements(elems: QList<QQuickStackElement *>() << element); |
224 | return false; |
225 | } |
226 | |
227 | bool QQuickStackViewPrivate::popElements(QQuickStackElement *element) |
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(parent: q); |
236 | } |
237 | |
238 | bool QQuickStackViewPrivate::replaceElements(QQuickStackElement *target, const QList<QQuickStackElement *> &elems) |
239 | { |
240 | if (target) { |
241 | while (!elements.isEmpty()) { |
242 | QQuickStackElement* top = elements.pop(); |
243 | delete top; |
244 | if (top == target) |
245 | break; |
246 | } |
247 | } |
248 | return pushElements(elems); |
249 | } |
250 | |
251 | QQuickItem *QQuickStackViewPrivate::popToItem(QQuickItem *item, QQuickStackView::Operation operation, CurrentItemPolicy currentItemPolicy) |
252 | { |
253 | const QString operationName = QStringLiteral("pop"); |
254 | if (modifyingElements) { |
255 | warnOfInterruption(attemptedOperation: operationName); |
256 | return nullptr; |
257 | } |
258 | |
259 | QScopedValueRollback<bool> modifyingElementsRollback(modifyingElements, true); |
260 | QScopedValueRollback<QString> operationNameRollback(this->operation, operationName); |
261 | if (elements.isEmpty()) { |
262 | warn(QStringLiteral("no items to pop")); |
263 | return nullptr; |
264 | } |
265 | |
266 | if (!item) { |
267 | warn(QStringLiteral("item cannot be null")); |
268 | return nullptr; |
269 | } |
270 | |
271 | const int oldDepth = elements.size(); |
272 | QQuickStackElement *exit = elements.pop(); |
273 | // top() here will be the item below the previously current item, since we just popped above. |
274 | QQuickStackElement *enter = elements.top(); |
275 | |
276 | bool nothingToDo = false; |
277 | if (item != currentItem) { |
278 | if (!item) { |
279 | // Popping down to the first item. |
280 | enter = elements.value(i: 0); |
281 | } else { |
282 | // Popping down to an arbitrary item. |
283 | enter = findElement(item); |
284 | if (!enter) { |
285 | warn(QStringLiteral("can't find item to pop: ") + QDebug::toString(object: item)); |
286 | nothingToDo = true; |
287 | } |
288 | } |
289 | } else { |
290 | if (currentItemPolicy == CurrentItemPolicy::DoNotPop) { |
291 | // popToItem was called with the currentItem, which means there are no items |
292 | // to pop because it's already at the top. |
293 | nothingToDo = true; |
294 | } |
295 | // else: popToItem was called by popCurrentItem, and so we _should_ pop. |
296 | } |
297 | if (nothingToDo) { |
298 | // Restore the element we popped earlier. |
299 | elements.push(t: exit); |
300 | return nullptr; |
301 | } |
302 | |
303 | QQuickItem *previousItem = nullptr; |
304 | if (popElements(element: enter)) { |
305 | if (exit) { |
306 | exit->removal = true; |
307 | removing.insert(value: exit); |
308 | previousItem = exit->item; |
309 | } |
310 | depthChange(newDepth: elements.size(), oldDepth); |
311 | #if QT_CONFIG(quick_viewtransitions) |
312 | Q_Q(QQuickStackView); |
313 | startTransition(first: QQuickStackTransition::popExit(operation, element: exit, view: q), |
314 | second: QQuickStackTransition::popEnter(operation, element: enter, view: q), |
315 | immediate: operation == QQuickStackView::Immediate); |
316 | #else |
317 | Q_UNUSED(operation); |
318 | #endif |
319 | setCurrentItem(enter); |
320 | } |
321 | return previousItem; |
322 | } |
323 | |
324 | #if QT_CONFIG(quick_viewtransitions) |
325 | void QQuickStackViewPrivate::ensureTransitioner() |
326 | { |
327 | if (!transitioner) { |
328 | transitioner = new QQuickItemViewTransitioner; |
329 | transitioner->setChangeListener(this); |
330 | } |
331 | } |
332 | |
333 | void QQuickStackViewPrivate::startTransition(const QQuickStackTransition &first, const QQuickStackTransition &second, bool immediate) |
334 | { |
335 | if (first.element) |
336 | first.element->transitionNextReposition(transitioner, type: first.type, asTarget: first.target); |
337 | if (second.element) |
338 | second.element->transitionNextReposition(transitioner, type: second.type, asTarget: second.target); |
339 | |
340 | if (first.element) { |
341 | // Let the check for immediate happen after prepareTransition() is |
342 | // called, because we need the prepared transition in both branches. |
343 | // Same for the second element. |
344 | if (!first.element->item || !first.element->prepareTransition(transitioner, viewBounds: first.viewBounds) || immediate) |
345 | completeTransition(element: first.element, transition: first.transition, status: first.status); |
346 | else |
347 | first.element->startTransition(transitioner, status: first.status); |
348 | } |
349 | if (second.element) { |
350 | if (!second.element->item || !second.element->prepareTransition(transitioner, viewBounds: second.viewBounds) || immediate) |
351 | completeTransition(element: second.element, transition: second.transition, status: second.status); |
352 | else |
353 | second.element->startTransition(transitioner, status: second.status); |
354 | } |
355 | |
356 | if (transitioner) { |
357 | setBusy(!transitioner->runningJobs.isEmpty()); |
358 | transitioner->resetTargetLists(); |
359 | } |
360 | } |
361 | |
362 | void QQuickStackViewPrivate::completeTransition(QQuickStackElement *element, QQuickTransition *transition, QQuickStackView::Status status) |
363 | { |
364 | element->setStatus(status); |
365 | if (transition) { |
366 | if (element->prepared) { |
367 | // Here we force reading all the animations, even if the desired |
368 | // transition type is StackView.Immediate. After that we force |
369 | // all the animations to complete immediately, without waiting for |
370 | // the animation timer. |
371 | // This allows us to correctly restore all the properties affected |
372 | // by the push/pop animations. |
373 | ACTION_IF_DELETED(element, element->completeTransition(transition), return); |
374 | } else if (element->item) { |
375 | // At least try to move the item to its desired place. This, |
376 | // however, is only a partly correct solution, because a lot more |
377 | // properties can be affected by the transition |
378 | element->item->setPosition(element->nextTransitionTo); |
379 | } |
380 | } |
381 | viewItemTransitionFinished(item: element); |
382 | } |
383 | |
384 | void QQuickStackViewPrivate::viewItemTransitionFinished(QQuickItemViewTransitionableItem *transitionable) |
385 | { |
386 | QQuickStackElement *element = static_cast<QQuickStackElement *>(transitionable); |
387 | if (element->status == QQuickStackView::Activating) { |
388 | element->setStatus(QQuickStackView::Active); |
389 | } else if (element->status == QQuickStackView::Deactivating) { |
390 | element->setStatus(QQuickStackView::Inactive); |
391 | QQuickStackElement *existingElement = element->item ? findElement(item: element->item) : nullptr; |
392 | // If a different element with the same item is found, |
393 | // do not call setVisible(false) since it needs to be visible. |
394 | if (!existingElement || element == existingElement) |
395 | element->setVisible(false); |
396 | if (element->removal || element->isPendingRemoval()) |
397 | removed += element; |
398 | } |
399 | |
400 | if (transitioner && transitioner->runningJobs.isEmpty()) { |
401 | // ~QQuickStackElement() emits QQuickStackViewAttached::removed(), which may be used |
402 | // to modify the stack. Set the status first and make a copy of the destroyable stack |
403 | // elements to exclude any modifications that may happen during qDeleteAll(). (QTBUG-62153) |
404 | setBusy(false); |
405 | QList<QQuickStackElement*> removedElements = removed; |
406 | removed.clear(); |
407 | |
408 | for (QQuickStackElement *removedElement : std::as_const(t&: removedElements)) { |
409 | // If an element with the same item is found in the active stack list, |
410 | // forget about the item so that we don't hide it. |
411 | if (removedElement->item && findElement(item: removedElement->item)) { |
412 | QQuickItemPrivate::get(item: removedElement->item)->removeItemChangeListener(removedElement, types: QQuickItemPrivate::Destroyed); |
413 | removedElement->item = nullptr; |
414 | } |
415 | } |
416 | |
417 | qDeleteAll(c: removedElements); |
418 | } |
419 | |
420 | removing.remove(value: element); |
421 | } |
422 | #endif |
423 | |
424 | void QQuickStackViewPrivate::setBusy(bool b) |
425 | { |
426 | Q_Q(QQuickStackView); |
427 | if (busy == b) |
428 | return; |
429 | |
430 | busy = b; |
431 | q->setFiltersChildMouseEvents(busy); |
432 | emit q->busyChanged(); |
433 | } |
434 | |
435 | void QQuickStackViewPrivate::depthChange(int newDepth, int oldDepth) |
436 | { |
437 | Q_Q(QQuickStackView); |
438 | if (newDepth == oldDepth) |
439 | return; |
440 | |
441 | emit q->depthChanged(); |
442 | if (newDepth == 0 || oldDepth == 0) |
443 | emit q->emptyChanged(); |
444 | } |
445 | |
446 | QT_END_NAMESPACE |
447 |
Definitions
Learn Advanced QML with KDAB
Find out more