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
qquickpackage.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
4
6
7#include <private/qobject_p.h>
8#include <private/qqmlguard_p.h>
9
11
12/*!
13 \qmltype Package
14 \nativetype QQuickPackage
15 \inqmlmodule QtQml.Models
16 \ingroup qtquick-models
17 \brief Specifies a collection of named items.
18
19 The Package type is used in conjunction with
20 DelegateModel to enable delegates with a shared context
21 to be provided to multiple views.
22
23 Any item within a Package may be assigned a name via the
24 \l{Package::name}{Package.name} attached property.
25
26 The example below creates a Package containing two named items;
27 \e list and \e grid. The third item in the package (the \l Rectangle) is parented to whichever
28 delegate it should appear in. This allows an item to move
29 between views.
30
31 \snippet package/Delegate.qml 0
32
33 These named items are used as the delegates by the two views who
34 reference the special \l{DelegateModel::parts} property to select
35 a model which provides the chosen delegate.
36
37 \snippet package/view.qml 0
38
39 \note Package is part of \l{QtQml.Models} since version 2.14 and
40 part of \l[QML]{QtQuick} since version 2.0.
41 Importing Package via \l[QML]{QtQuick} is deprecated since Qt 5.14.
42
43 \sa {Qt Quick Examples - Views}, {Qt Qml}
44*/
45
46/*!
47 \qmlattachedproperty string QtQml.Models::Package::name
48 This attached property holds the name of an item within a Package.
49*/
50
51
53{
54public:
56
57 struct DataGuard : public QQmlGuard<QObject>
58 {
59 DataGuard(QObject *obj, QList<DataGuard> *l) : QQmlGuard<QObject>(DataGuard::objectDestroyedImpl, nullptr), list(l) { (QQmlGuard<QObject>&)*this = obj; }
61
62 private:
63 static void objectDestroyedImpl(QQmlGuardImpl *guard) {
64 auto This = static_cast<DataGuard *>(guard);
65 // we assume priv will always be destroyed after objectDestroyed calls
66 This->list->removeOne(*This);
67 }
68 };
69
71 static void data_append(QQmlListProperty<QObject> *prop, QObject *o) {
72 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
73 list->append(DataGuard(o, list));
74 }
75 static void data_clear(QQmlListProperty<QObject> *prop) {
76 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
77 list->clear();
78 }
79 static QObject *data_at(QQmlListProperty<QObject> *prop, qsizetype index) {
80 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
81 return list->at(index);
82 }
83 static qsizetype data_count(QQmlListProperty<QObject> *prop) {
84 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
85 return list->size();
86 }
87 static void data_replace(QQmlListProperty<QObject> *prop, qsizetype index, QObject *o) {
88 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
89 list->replace(index, DataGuard(o, list));
90 }
91 static void data_removeLast(QQmlListProperty<QObject> *prop) {
92 QList<DataGuard> *list = static_cast<QList<DataGuard> *>(prop->data);
93 list->removeLast();
94 }
95};
96
98
99QQuickPackageAttached::QQuickPackageAttached(QObject *parent)
100: QObject(parent)
101{
102 attached.insert(parent, this);
103}
104
106{
107 attached.remove(parent());
108}
109
111{
112 return _name;
113}
114
115void QQuickPackageAttached::setName(const QString &n)
116{
117 _name = n;
118}
119
120QQuickPackage::QQuickPackage(QObject *parent)
121 : QObject(*(new QQuickPackagePrivate), parent)
122{
123}
124
125QQmlListProperty<QObject> QQuickPackage::data()
126{
127 Q_D(QQuickPackage);
128 return QQmlListProperty<QObject>(this, &d->dataList,
129 QQuickPackagePrivate::data_append,
130 QQuickPackagePrivate::data_count,
131 QQuickPackagePrivate::data_at,
132 QQuickPackagePrivate::data_clear,
133 QQuickPackagePrivate::data_replace,
134 QQuickPackagePrivate::data_removeLast);
135}
136
137bool QQuickPackage::hasPart(const QString &name)
138{
139 Q_D(QQuickPackage);
140 for (int ii = 0; ii < d->dataList.size(); ++ii) {
141 QObject *obj = d->dataList.at(ii);
142 QQuickPackageAttached *a = QQuickPackageAttached::attached.value(obj);
143 if (a && a->name() == name)
144 return true;
145 }
146 return false;
147}
148
149QObject *QQuickPackage::part(const QString &name)
150{
151 Q_D(QQuickPackage);
152 if (name.isEmpty() && !d->dataList.isEmpty())
153 return d->dataList.at(0);
154
155 for (int ii = 0; ii < d->dataList.size(); ++ii) {
156 QObject *obj = d->dataList.at(ii);
157 QQuickPackageAttached *a = QQuickPackageAttached::attached.value(obj);
158 if (a && a->name() == name)
159 return obj;
160 }
161
162 if (name == QLatin1String("default") && !d->dataList.isEmpty())
163 return d->dataList.at(0);
164
165 return nullptr;
166}
167
168QQuickPackageAttached *QQuickPackage::qmlAttachedProperties(QObject *o)
169{
170 return new QQuickPackageAttached(o);
171}
172
173
174
175QT_END_NAMESPACE
176
177#include "moc_qquickpackage_p.cpp"
void setName(const QString &n)
static QHash< QObject *, QQuickPackageAttached * > attached
\qmltype Package \nativetype QQuickPackage \inqmlmodule QtQml.Models
static void data_removeLast(QQmlListProperty< QObject > *prop)
static QObject * data_at(QQmlListProperty< QObject > *prop, qsizetype index)
static void data_append(QQmlListProperty< QObject > *prop, QObject *o)
static qsizetype data_count(QQmlListProperty< QObject > *prop)
static void data_clear(QQmlListProperty< QObject > *prop)
static void data_replace(QQmlListProperty< QObject > *prop, qsizetype index, QObject *o)
QList< DataGuard > dataList
Combined button and popup list for selecting options.
DataGuard(QObject *obj, QList< DataGuard > *l)