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
qquickstackelement.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
8
9#include <QtQml/qqmlinfo.h>
10#include <QtQml/qqmlengine.h>
11#include <QtQml/qqmlcomponent.h>
12#include <QtQml/qqmlincubator.h>
13#include <QtQml/private/qv4qobjectwrapper_p.h>
14#include <QtQml/private/qqmlcomponent_p.h>
15#include <QtQml/private/qqmlengine_p.h>
16#include <QtQml/private/qqmlincubator_p.h>
17
19
20#if QT_CONFIG(quick_viewtransitions)
21static QQuickStackViewAttached *attachedStackObject(QQuickStackElement *element)
22{
23 QQuickStackViewAttached *attached = qobject_cast<QQuickStackViewAttached *>(qmlAttachedPropertiesObject<QQuickStackView>(element->item, false));
24 if (attached)
25 QQuickStackViewAttachedPrivate::get(attached)->element = element;
26 return attached;
27}
28#endif
29
31{
32public:
35 element(element)
36 {
37 }
38
39protected:
40 void setInitialState(QObject *object) override
41 {
42 auto privIncubator = QQmlIncubatorPrivate::get(this);
43 if (QQmlEnginePrivate *enginePriv = privIncubator->enginePriv) {
44 element->incubate(enginePriv->v4Engine.get(), object,
45 privIncubator->requiredProperties());
46 }
47 }
48
49private:
50 QQuickStackElement *element;
51};
52
53QQuickStackElement::QQuickStackElement()
54#if QT_CONFIG(quick_viewtransitions)
55 : QQuickItemViewTransitionableItem(nullptr)
56#endif
57{
58}
59
61{
62#if QT_CONFIG(quick_viewtransitions)
63 if (item)
64 QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Destroyed);
65#endif
66
67 if (ownComponent)
68 delete component;
69
70#if QT_CONFIG(quick_viewtransitions)
71 QQuickStackViewAttached *attached = attachedStackObject(this);
72 if (item) {
73 if (ownItem) {
74 item->setParentItem(nullptr);
75 item->deleteLater();
76 item = nullptr;
77 } else {
78 setVisible(false);
79 if (!widthValid)
80 item->resetWidth();
81 if (!heightValid)
82 item->resetHeight();
83 if (item->parentItem() != originalParent) {
84 item->setParentItem(originalParent);
85 } else {
86 if (attached)
87 QQuickStackViewAttachedPrivate::get(attached)->itemParentChanged(item, nullptr);
88 }
89 }
90 }
91
92 if (attached)
93 emit attached->removed();
94#endif
95}
96
98 QQmlEngine *engine, const QString &str, QQuickStackView *view, QString *error)
99{
100 QUrl url(str);
101 if (!url.isValid()) {
102 *error = QStringLiteral("invalid url: ") + str;
103 return nullptr;
104 }
105
106 if (url.isRelative())
107 url = qmlContext(view)->resolvedUrl(url);
108
110 element->component = new QQmlComponent(engine, url, view);
111 element->ownComponent = true;
112 return element;
113}
114
115QQuickStackElement *QQuickStackElement::fromObject(QObject *object, QQuickStackView *view, QString *error)
116{
117 Q_UNUSED(view);
118 QQmlComponent *component = qobject_cast<QQmlComponent *>(object);
119 QQuickItem *item = qobject_cast<QQuickItem *>(object);
120 if (!component && !item) {
121 *error = QQmlMetaType::prettyTypeName(object) + QStringLiteral(" is not supported. Must be Item or Component.");
122 return nullptr;
123 }
124
125 QQuickStackElement *element = new QQuickStackElement;
126 element->component = qobject_cast<QQmlComponent *>(object);
127#if QT_CONFIG(quick_viewtransitions)
128 element->item = qobject_cast<QQuickItem *>(object);
129 if (element->item)
130 element->originalParent = element->item->parentItem();
131#endif
132 return element;
133}
134
136 QQmlEngine *engine, QQuickStackView *view, QQuickStackViewArg arg)
137{
138 QQuickStackElement *element = new QQuickStackElement;
139#if QT_CONFIG(quick_viewtransitions)
140 element->item = arg.mItem;
141 if (element->item) {
142 element->originalParent = element->item->parentItem();
143
144 Q_ASSERT(!arg.mComponent);
145 Q_ASSERT(!arg.mUrl.isValid());
146 } else
147#endif
148 if (arg.mComponent) {
149 element->component = arg.mComponent;
150
151 Q_ASSERT(!arg.mUrl.isValid());
152 } else if (arg.mUrl.isValid()) {
153 element->component = new QQmlComponent(engine, arg.mUrl, view);
154 element->ownComponent = true;
155 } else {
156 qFatal("No Item, Component or URL set on arg passed to fromStrictArg");
157 }
158 return element;
159}
160
161bool QQuickStackElement::load(QV4::ExecutionEngine *v4, QQuickStackView *parent)
162{
163 setView(parent);
164 if (!item) {
165 ownItem = true;
166
167 if (component->isLoading()) {
168 QObject::connect(component, &QQmlComponent::statusChanged, component,
169 [this](QQmlComponent::Status status) {
170 if (status == QQmlComponent::Ready)
171 load(component->engine()->handle(), view);
172 else if (status == QQmlComponent::Error)
173 QQuickStackViewPrivate::get(view)->warn(component->errorString().trimmed());
174 });
175 return true;
176 }
177
178 QQmlContext *context = component->creationContext();
179 if (!context)
180 context = qmlContext(parent);
181
182 QQuickStackIncubator incubator(this);
183 component->create(incubator, context);
184 if (component->isError())
185 QQuickStackViewPrivate::get(parent)->warn(component->errorString().trimmed());
186 } else {
187 initialize(v4, /*required properties=*/nullptr);
188 }
189 return item;
190}
191
193 QV4::ExecutionEngine *v4, QObject *object, RequiredProperties *requiredProperties)
194{
195 item = qmlobject_cast<QQuickItem *>(object);
196 if (item) {
197 QQmlEngine::setObjectOwnership(item, QQmlEngine::CppOwnership);
198 item->setParent(view);
199 initialize(v4, requiredProperties);
200 }
201}
202
204 QV4::ExecutionEngine *v4, RequiredProperties *requiredProperties)
205{
206 if (!item || init)
207 return;
208
209 QQuickItemPrivate *p = QQuickItemPrivate::get(item);
210 if (!(widthValid = p->widthValid()))
211 item->setWidth(view->width());
212 if (!(heightValid = p->heightValid()))
213 item->setHeight(view->height());
214 item->setParentItem(view);
215
216 if (!properties.isUndefined()) {
217 QV4::Scope scope(v4);
218 Q_ASSERT(scope.engine);
219 QV4::ScopedValue ipv(scope, properties.value());
220 QV4::Scoped<QV4::QmlContext> qmlContext(scope, qmlCallingContext.value());
221 QV4::ScopedValue qmlObject(scope, QV4::QObjectWrapper::wrap(scope.engine, item));
222 QQmlComponentPrivate::setInitialProperties(
223 scope.engine, qmlContext, qmlObject, ipv, requiredProperties, item,
224 component ? QQmlComponentPrivate::get(component)->creator() : nullptr);
225 properties.clear();
226 }
227
228 if (requiredProperties && !requiredProperties->empty()) {
229 QString error;
230 for (const auto &property: *requiredProperties) {
231 error += QLatin1String("Property %1 was marked as required but not set.\n")
232 .arg(property.propertyName);
233 }
234 QQuickStackViewPrivate::get(view)->warn(error);
235 item = nullptr;
236 } else {
237 p->addItemChangeListener(this, QQuickItemPrivate::Destroyed);
238 }
239
240 init = true;
241}
242
244{
245 if (index == value)
246 return;
247
248 index = value;
249#if QT_CONFIG(quick_viewtransitions)
250 QQuickStackViewAttached *attached = attachedStackObject(this);
251 if (attached)
252 emit attached->indexChanged();
253#endif
254}
255
256void QQuickStackElement::setView(QQuickStackView *value)
257{
258 if (view == value)
259 return;
260
261 view = value;
262#if QT_CONFIG(quick_viewtransitions)
263 QQuickStackViewAttached *attached = attachedStackObject(this);
264 if (attached)
265 emit attached->viewChanged();
266#endif
267}
268
269void QQuickStackElement::setStatus(QQuickStackView::Status value)
270{
271 if (status == value)
272 return;
273
274 status = value;
275#if QT_CONFIG(quick_viewtransitions)
276 QQuickStackViewAttached *attached = attachedStackObject(this);
277 if (!attached)
278 return;
279
280 switch (value) {
281 case QQuickStackView::Inactive:
282 emit attached->deactivated();
283 break;
284 case QQuickStackView::Deactivating:
285 emit attached->deactivating();
286 break;
287 case QQuickStackView::Activating:
288 emit attached->activating();
289 break;
290 case QQuickStackView::Active:
291 emit attached->activated();
292 break;
293 default:
294 Q_UNREACHABLE();
295 break;
296 }
297
298 emit attached->statusChanged();
299#endif
300}
301
303{
304#if QT_CONFIG(quick_viewtransitions)
305 QQuickStackViewAttached *attached = attachedStackObject(this);
306#endif
307 if (!item
308#if QT_CONFIG(quick_viewtransitions)
309 || (attached && QQuickStackViewAttachedPrivate::get(attached)->explicitVisible)
310#endif
311 )
312 return;
313
314 item->setVisible(visible);
315}
316
317#if QT_CONFIG(quick_viewtransitions)
318void QQuickStackElement::transitionNextReposition(QQuickItemViewTransitioner *transitioner, QQuickItemViewTransitioner::TransitionType type, bool asTarget)
319{
320 if (transitioner)
321 transitioner->transitionNextReposition(this, type, asTarget);
322}
323
324bool QQuickStackElement::prepareTransition(QQuickItemViewTransitioner *transitioner, const QRectF &viewBounds)
325{
326 if (transitioner) {
327 if (item) {
328 QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors;
329 // TODO: expose QQuickAnchorLine so we can test for other conflicting anchors
330 if (anchors && (anchors->fill() || anchors->centerIn()))
331 qmlWarning(item) << "StackView has detected conflicting anchors. Transitions may not execute properly.";
332 }
333
334 // TODO: add force argument to QQuickItemViewTransitionableItem::prepareTransition()?
335 nextTransitionToSet = true;
336 nextTransitionFromSet = true;
337 nextTransitionFrom += QPointF(1, 1);
338 return QQuickItemViewTransitionableItem::prepareTransition(transitioner, index, viewBounds);
339 }
340 return false;
341}
342
343void QQuickStackElement::startTransition(QQuickItemViewTransitioner *transitioner, QQuickStackView::Status status)
344{
345 setStatus(status);
346 if (transitioner)
347 QQuickItemViewTransitionableItem::startTransition(transitioner, index);
348}
349
350void QQuickStackElement::completeTransition(QQuickTransition *quickTransition)
351{
352 QQuickItemViewTransitionableItem::completeTransition(quickTransition);
353}
354#endif
355
357{
358#if QT_CONFIG(quick_viewtransitions)
359 item = nullptr;
360#endif
361}
362
363QT_END_NAMESPACE
void itemDestroyed(QQuickItem *item) override
void initialize(QV4::ExecutionEngine *v4, RequiredProperties *requiredProperties)
void setVisible(bool visible)
void setStatus(QQuickStackView::Status status)
void incubate(QV4::ExecutionEngine *v4, QObject *object, RequiredProperties *requiredProperties)
QQuickStackIncubator(QQuickStackElement *element)
void setInitialState(QObject *object) override
Called after the object is first created, but before complex property bindings are evaluated and,...
Combined button and popup list for selecting options.