Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5#include "qquickstackview_p.h"
7
8#include <QtQml/qqmlinfo.h>
9#include <QtQml/qqmlengine.h>
10#include <QtQml/qqmlcomponent.h>
11#include <QtQml/qqmlincubator.h>
12#include <QtQml/private/qv4qobjectwrapper_p.h>
13#include <QtQml/private/qqmlcomponent_p.h>
14#include <QtQml/private/qqmlengine_p.h>
15#include <QtQml/private/qqmlincubator_p.h>
16
18
19#if QT_CONFIG(quick_viewtransitions)
20static QQuickStackViewAttached *attachedStackObject(QQuickStackElement *element)
21{
22 QQuickStackViewAttached *attached = qobject_cast<QQuickStackViewAttached *>(qmlAttachedPropertiesObject<QQuickStackView>(element->item, false));
23 if (attached)
24 QQuickStackViewAttachedPrivate::get(attached)->element = element;
25 return attached;
26}
27#endif
28
30{
31public:
34 element(element)
35 {
36 }
37
38protected:
39 void setInitialState(QObject *object) override
40 {
41 auto privIncubator = QQmlIncubatorPrivate::get(this);
42 element->incubate(object, privIncubator->requiredProperties());
43 }
44
45private:
46 QQuickStackElement *element;
47};
48
49QQuickStackElement::QQuickStackElement()
50#if QT_CONFIG(quick_viewtransitions)
52#endif
53{
54}
55
57{
58#if QT_CONFIG(quick_viewtransitions)
59 if (item)
60 QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Destroyed);
61#endif
62
63 if (ownComponent)
64 delete component;
65
66#if QT_CONFIG(quick_viewtransitions)
67 QQuickStackViewAttached *attached = attachedStackObject(this);
68 if (item) {
69 if (ownItem) {
70 item->setParentItem(nullptr);
72 item = nullptr;
73 } else {
74 setVisible(false);
75 if (!widthValid)
77 if (!heightValid)
79 if (item->parentItem() != originalParent) {
81 } else {
82 if (attached)
83 QQuickStackViewAttachedPrivate::get(attached)->itemParentChanged(item, nullptr);
84 }
85 }
86 }
87
88 if (attached)
89 emit attached->removed();
90#endif
91}
92
94{
95 QUrl url(str);
96 if (!url.isValid()) {
97 *error = QStringLiteral("invalid url: ") + str;
98 return nullptr;
99 }
100
101 if (url.isRelative())
103
105 element->component = new QQmlComponent(qmlEngine(view), url, view);
106 element->ownComponent = true;
107 return element;
108}
109
111{
112 Q_UNUSED(view);
113 QQmlComponent *component = qobject_cast<QQmlComponent *>(object);
115 if (!component && !item) {
116 *error = QQmlMetaType::prettyTypeName(object) + QStringLiteral(" is not supported. Must be Item or Component.");
117 return nullptr;
118 }
119
121 element->component = qobject_cast<QQmlComponent *>(object);
122#if QT_CONFIG(quick_viewtransitions)
123 element->item = qobject_cast<QQuickItem *>(object);
124 if (element->item)
125 element->originalParent = element->item->parentItem();
126#endif
127 return element;
128}
129
131{
133#if QT_CONFIG(quick_viewtransitions)
134 element->item = arg.mItem;
135 if (element->item) {
136 element->originalParent = element->item->parentItem();
137
138 Q_ASSERT(!arg.mComponent);
139 Q_ASSERT(!arg.mUrl.isValid());
140 } else
141#endif
142 if (arg.mComponent) {
143 element->component = arg.mComponent;
144
145 Q_ASSERT(!arg.mUrl.isValid());
146 } else if (arg.mUrl.isValid()) {
147 element->component = new QQmlComponent(qmlEngine(view), arg.mUrl, view);
148 element->ownComponent = true;
149 } else {
150 qFatal("No Item, Component or URL set on arg passed to fromStrictArg");
151 }
152 return element;
153}
154
156{
157 setView(parent);
158 if (!item) {
159 ownItem = true;
160
161 if (component->isLoading()) {
164 load(view);
165 else if (status == QQmlComponent::Error)
167 });
168 return true;
169 }
170
172 if (!context)
173 context = qmlContext(parent);
174
175 QQuickStackIncubator incubator(this);
176 component->create(incubator, context);
177 if (component->isError())
179 } else {
180 initialize(/*required properties=*/nullptr);
181 }
182 return item;
183}
184
186{
188 if (item) {
191 initialize(requiredProperties);
192 }
193}
194
196{
197 if (!item || init)
198 return;
199
201 if (!(widthValid = p->widthValid()))
202 item->setWidth(view->width());
203 if (!(heightValid = p->heightValid()))
206
207 if (!properties.isUndefined()) {
211 Q_ASSERT(v4);
212 QV4::Scope scope(v4);
213 QV4::ScopedValue ipv(scope, properties.value());
215 QV4::ScopedValue qmlObject(scope, QV4::QObjectWrapper::wrap(v4, item));
217 v4, qmlContext, qmlObject, ipv, requiredProperties, item,
218 component ? QQmlComponentPrivate::get(component)->state.creator() : nullptr);
220 }
221
222 if (requiredProperties && !requiredProperties->empty()) {
224 for (const auto &property: *requiredProperties) {
225 error += QLatin1String("Property %1 was marked as required but not set.\n")
226 .arg(property.propertyName);
227 }
229 item = nullptr;
230 } else {
231 p->addItemChangeListener(this, QQuickItemPrivate::Destroyed);
232 }
233
234 init = true;
235}
236
238{
239 if (index == value)
240 return;
241
242 index = value;
243#if QT_CONFIG(quick_viewtransitions)
244 QQuickStackViewAttached *attached = attachedStackObject(this);
245 if (attached)
246 emit attached->indexChanged();
247#endif
248}
249
251{
252 if (view == value)
253 return;
254
255 view = value;
256#if QT_CONFIG(quick_viewtransitions)
257 QQuickStackViewAttached *attached = attachedStackObject(this);
258 if (attached)
259 emit attached->viewChanged();
260#endif
261}
262
264{
265 if (status == value)
266 return;
267
268 status = value;
269#if QT_CONFIG(quick_viewtransitions)
270 QQuickStackViewAttached *attached = attachedStackObject(this);
271 if (!attached)
272 return;
273
274 switch (value) {
276 emit attached->deactivated();
277 break;
279 emit attached->deactivating();
280 break;
282 emit attached->activating();
283 break;
285 emit attached->activated();
286 break;
287 default:
288 Q_UNREACHABLE();
289 break;
290 }
291
292 emit attached->statusChanged();
293#endif
294}
295
297{
298#if QT_CONFIG(quick_viewtransitions)
299 QQuickStackViewAttached *attached = attachedStackObject(this);
300#endif
301 if (!item
302#if QT_CONFIG(quick_viewtransitions)
303 || (attached && QQuickStackViewAttachedPrivate::get(attached)->explicitVisible)
304#endif
305 )
306 return;
307
308 item->setVisible(visible);
309}
310
311#if QT_CONFIG(quick_viewtransitions)
312void QQuickStackElement::transitionNextReposition(QQuickItemViewTransitioner *transitioner, QQuickItemViewTransitioner::TransitionType type, bool asTarget)
313{
314 if (transitioner)
315 transitioner->transitionNextReposition(this, type, asTarget);
316}
317
318bool QQuickStackElement::prepareTransition(QQuickItemViewTransitioner *transitioner, const QRectF &viewBounds)
319{
320 if (transitioner) {
321 if (item) {
323 // TODO: expose QQuickAnchorLine so we can test for other conflicting anchors
324 if (anchors && (anchors->fill() || anchors->centerIn()))
325 qmlWarning(item) << "StackView has detected conflicting anchors. Transitions may not execute properly.";
326 }
327
328 // TODO: add force argument to QQuickItemViewTransitionableItem::prepareTransition()?
329 nextTransitionToSet = true;
330 nextTransitionFromSet = true;
331 nextTransitionFrom += QPointF(1, 1);
332 return QQuickItemViewTransitionableItem::prepareTransition(transitioner, index, viewBounds);
333 }
334 return false;
335}
336
337void QQuickStackElement::startTransition(QQuickItemViewTransitioner *transitioner, QQuickStackView::Status status)
338{
340 if (transitioner)
342}
343
344void QQuickStackElement::completeTransition(QQuickTransition *quickTransition)
345{
347}
348#endif
349
351{
352#if QT_CONFIG(quick_viewtransitions)
353 item = nullptr;
354#endif
355}
356
bool empty() const noexcept
This function is provided for STL compatibility.
Definition qhash.h:1349
static void setObjectOwnership(QObject *, ObjectOwnership)
Sets the ownership of object.
QString arg(Args &&...args) const
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\inmodule QtCore\reentrant
Definition qpoint.h:217
static QQmlComponentPrivate * get(QQmlComponent *c)
static void setInitialProperties(QV4::ExecutionEngine *engine, QV4::QmlContext *qmlContext, const QV4::Value &o, const QV4::Value &v, RequiredProperties *requiredProperties, QObject *createdComponent, QQmlObjectCreator *creator)
\qmlmethod QtObject Component::createObject(QtObject parent, object properties)
The QQmlComponent class encapsulates a QML component definition.
Status
\qmltype Component \instantiates QQmlComponent\inqmlmodule QtQml
bool isError() const
Returns true if status() == QQmlComponent::Error.
bool isLoading() const
Returns true if status() == QQmlComponent::Loading.
QQmlContext * creationContext() const
Returns the QQmlContext the component was created in.
Q_INVOKABLE QString errorString() const
\qmlmethod string Component::errorString()
virtual QObject * create(QQmlContext *context=nullptr)
Create an object instance from this component, within the specified context.
void statusChanged(QQmlComponent::Status)
Emitted whenever the component's status changes.
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
QUrl resolvedUrl(const QUrl &) const
Resolves the URL src relative to the URL of the containing component.
static QV4::ExecutionEngine * getV4Engine(QQmlEngine *e)
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
static QQmlIncubatorPrivate * get(QQmlIncubator *incubator)
The QQmlIncubator class allows QML objects to be created asynchronously.
static QString prettyTypeName(const QObject *object)
Returns the pretty QML type name (e.g.
QQuickItem * fill
QQuickItem * centerIn
static QQuickItemPrivate * get(QQuickItem *item)
bool prepareTransition(QQuickItemViewTransitioner *transitioner, int index, const QRectF &viewBounds)
void startTransition(QQuickItemViewTransitioner *transitioner, int index)
void completeTransition(QQuickTransition *quickTransition)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
void resetHeight()
void resetWidth()
void setParentItem(QQuickItem *parent)
void setHeight(qreal)
qreal width
This property holds the width of this item.
Definition qquickitem.h:75
QQuickItem * parentItem() const
void setVisible(bool)
qreal height
This property holds the height of this item.
Definition qquickitem.h:76
void setWidth(qreal)
void itemDestroyed(QQuickItem *item) override
QPointer< QQuickItem > originalParent
void initialize(RequiredProperties *requiredProperties)
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
void setView(QQuickStackView *view)
bool load(QQuickStackView *parent)
void incubate(QObject *object, RequiredProperties *requiredProperties)
QV4::PersistentValue qmlCallingContext
QQuickStackIncubator(QQuickStackElement *element)
void setInitialState(QObject *object) override
Called after the object is first created, but before complex property bindings are evaluated and,...
static QQuickStackViewAttachedPrivate * get(QQuickStackViewAttached *attached)
static QQuickStackViewPrivate * get(QQuickStackView *view)
\inmodule QtCore\reentrant
Definition qrect.h:484
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString trimmed() const &
Definition qstring.h:447
\inmodule QtCore
Definition qurl.h:94
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition qurl.cpp:2800
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
Definition qurl.cpp:1882
ReturnedValue value() const
QString str
[2]
else opt state
[0]
Combined button and popup list for selecting options.
static void * context
static bool initialize()
Definition qctf.cpp:94
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qFatal
Definition qlogging.h:168
GLuint index
[2]
GLenum type
GLfloat GLfloat p
[1]
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:80
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
QQuickItem * qmlobject_cast< QQuickItem * >(QObject *object)
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
QQuickAnchors * anchors(QQuickItem *item)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:492
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
SSL_CTX int void * arg
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
#define QT_CONFIG(feature)
#define emit
#define Q_UNUSED(x)
const char property[13]
Definition qwizard.cpp:101
QUrl url("example.com")
[constructor-url-reference]
QQuickView * view
[0]
QJSEngine engine
[0]
static ReturnedValue wrap(ExecutionEngine *engine, QObject *object)