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

source code of qtdeclarative/src/qmlmodels/qquickpackage.cpp