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 | #ifndef QQMLINSTANCEMODEL_P_H |
5 | #define QQMLINSTANCEMODEL_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <private/qtqmlmodelsglobal_p.h> |
19 | #include <private/qqmlincubator_p.h> |
20 | #include <QtQml/qqml.h> |
21 | #include <QtCore/qobject.h> |
22 | |
23 | QT_REQUIRE_CONFIG(qml_object_model); |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QObject; |
28 | class QQmlChangeSet; |
29 | class QAbstractItemModel; |
30 | |
31 | class Q_QMLMODELS_PRIVATE_EXPORT QQmlInstanceModel : public QObject |
32 | { |
33 | Q_OBJECT |
34 | |
35 | Q_PROPERTY(int count READ count NOTIFY countChanged FINAL) |
36 | QML_ANONYMOUS |
37 | QML_ADDED_IN_VERSION(2, 0) |
38 | |
39 | public: |
40 | enum ReusableFlag { |
41 | NotReusable, |
42 | Reusable |
43 | }; |
44 | |
45 | enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02, Pooled = 0x04 }; |
46 | Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag) |
47 | |
48 | virtual int count() const = 0; |
49 | virtual bool isValid() const = 0; |
50 | virtual QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) = 0; |
51 | virtual ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) = 0; |
52 | virtual void cancel(int) {} |
53 | QString stringValue(int index, const QString &role) { return variantValue(index, role).toString(); } |
54 | virtual QVariant variantValue(int, const QString &) = 0; |
55 | virtual void setWatchedRoles(const QList<QByteArray> &roles) = 0; |
56 | virtual QQmlIncubator::Status incubationStatus(int index) = 0; |
57 | |
58 | virtual void drainReusableItemsPool(int maxPoolTime) { Q_UNUSED(maxPoolTime); } |
59 | virtual int poolSize() { return 0; } |
60 | |
61 | virtual int indexOf(QObject *object, QObject *objectContext) const = 0; |
62 | virtual const QAbstractItemModel *abstractItemModel() const { return nullptr; } |
63 | |
64 | virtual bool setRequiredProperty(int index, const QString &name, const QVariant &value); |
65 | |
66 | Q_SIGNALS: |
67 | void countChanged(); |
68 | void modelUpdated(const QQmlChangeSet &changeSet, bool reset); |
69 | void createdItem(int index, QObject *object); |
70 | void initItem(int index, QObject *object); |
71 | void destroyingItem(QObject *object); |
72 | Q_REVISION(2, 15) void itemPooled(int index, QObject *object); |
73 | Q_REVISION(2, 15) void itemReused(int index, QObject *object); |
74 | |
75 | protected: |
76 | QQmlInstanceModel(QObjectPrivate &dd, QObject *parent = nullptr) |
77 | : QObject(dd, parent) {} |
78 | |
79 | private: |
80 | Q_DISABLE_COPY(QQmlInstanceModel) |
81 | }; |
82 | |
83 | class QQmlObjectModelAttached; |
84 | class QQmlObjectModelPrivate; |
85 | class Q_QMLMODELS_PRIVATE_EXPORT QQmlObjectModel : public QQmlInstanceModel |
86 | { |
87 | Q_OBJECT |
88 | Q_DECLARE_PRIVATE(QQmlObjectModel) |
89 | |
90 | Q_PROPERTY(QQmlListProperty<QObject> children READ children NOTIFY childrenChanged DESIGNABLE false) |
91 | Q_CLASSINFO("DefaultProperty" , "children" ) |
92 | QML_NAMED_ELEMENT(ObjectModel) |
93 | QML_ADDED_IN_VERSION(2, 1) |
94 | QML_ATTACHED(QQmlObjectModelAttached) |
95 | |
96 | public: |
97 | QQmlObjectModel(QObject *parent=nullptr); |
98 | ~QQmlObjectModel() {} |
99 | |
100 | int count() const override; |
101 | bool isValid() const override; |
102 | QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override; |
103 | ReleaseFlags release(QObject *object, ReusableFlag reusable = NotReusable) override; |
104 | QVariant variantValue(int index, const QString &role) override; |
105 | void setWatchedRoles(const QList<QByteArray> &) override {} |
106 | QQmlIncubator::Status incubationStatus(int index) override; |
107 | |
108 | int indexOf(QObject *object, QObject *objectContext) const override; |
109 | |
110 | QQmlListProperty<QObject> children(); |
111 | |
112 | static QQmlObjectModelAttached *qmlAttachedProperties(QObject *obj); |
113 | |
114 | Q_REVISION(2, 3) Q_INVOKABLE QObject *get(int index) const; |
115 | Q_REVISION(2, 3) Q_INVOKABLE void append(QObject *object); |
116 | Q_REVISION(2, 3) Q_INVOKABLE void insert(int index, QObject *object); |
117 | Q_REVISION(2, 3) Q_INVOKABLE void move(int from, int to, int n = 1); |
118 | Q_REVISION(2, 3) Q_INVOKABLE void remove(int index, int n = 1); |
119 | |
120 | public Q_SLOTS: |
121 | Q_REVISION(2, 3) void clear(); |
122 | |
123 | Q_SIGNALS: |
124 | void childrenChanged(); |
125 | |
126 | private: |
127 | Q_DISABLE_COPY(QQmlObjectModel) |
128 | }; |
129 | |
130 | class QQmlObjectModelAttached : public QObject |
131 | { |
132 | Q_OBJECT |
133 | |
134 | public: |
135 | QQmlObjectModelAttached(QObject *parent) |
136 | : QObject(parent), m_index(-1) {} |
137 | ~QQmlObjectModelAttached() { |
138 | attachedProperties.remove(key: parent()); |
139 | } |
140 | |
141 | Q_PROPERTY(int index READ index NOTIFY indexChanged FINAL) |
142 | int index() const { return m_index; } |
143 | void setIndex(int idx) { |
144 | if (m_index != idx) { |
145 | m_index = idx; |
146 | Q_EMIT indexChanged(); |
147 | } |
148 | } |
149 | |
150 | static QQmlObjectModelAttached *properties(QObject *obj) { |
151 | QQmlObjectModelAttached *rv = attachedProperties.value(key: obj); |
152 | if (!rv) { |
153 | rv = new QQmlObjectModelAttached(obj); |
154 | attachedProperties.insert(key: obj, value: rv); |
155 | } |
156 | return rv; |
157 | } |
158 | |
159 | Q_SIGNALS: |
160 | void indexChanged(); |
161 | |
162 | public: |
163 | int m_index; |
164 | |
165 | static QHash<QObject*, QQmlObjectModelAttached*> attachedProperties; |
166 | }; |
167 | |
168 | |
169 | QT_END_NAMESPACE |
170 | |
171 | QML_DECLARE_TYPE(QQmlInstanceModel) |
172 | QML_DECLARE_TYPE(QQmlObjectModel) |
173 | |
174 | #endif // QQMLINSTANCEMODEL_P_H |
175 | |