1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QQMLINSTANCEMODEL_P_H |
41 | #define QQMLINSTANCEMODEL_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <private/qtqmlmodelsglobal_p.h> |
55 | #include <private/qqmlincubator_p.h> |
56 | #include <QtQml/qqml.h> |
57 | #include <QtCore/qobject.h> |
58 | |
59 | QT_REQUIRE_CONFIG(qml_object_model); |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | class QObject; |
64 | class QQmlChangeSet; |
65 | class QAbstractItemModel; |
66 | |
67 | class Q_QMLMODELS_PRIVATE_EXPORT QQmlInstanceModel : public QObject |
68 | { |
69 | Q_OBJECT |
70 | |
71 | Q_PROPERTY(int count READ count NOTIFY countChanged) |
72 | QML_ANONYMOUS |
73 | |
74 | public: |
75 | enum ReusableFlag { |
76 | NotReusable, |
77 | Reusable |
78 | }; |
79 | |
80 | virtual ~QQmlInstanceModel() {} |
81 | |
82 | enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02, Pooled = 0x04 }; |
83 | Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag) |
84 | |
85 | virtual int count() const = 0; |
86 | virtual bool isValid() const = 0; |
87 | virtual QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) = 0; |
88 | virtual ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) = 0; |
89 | virtual void cancel(int) {} |
90 | QString stringValue(int index, const QString &role) { return variantValue(index, role).toString(); } |
91 | virtual QVariant variantValue(int, const QString &) = 0; |
92 | virtual void setWatchedRoles(const QList<QByteArray> &roles) = 0; |
93 | virtual QQmlIncubator::Status incubationStatus(int index) = 0; |
94 | |
95 | virtual void drainReusableItemsPool(int maxPoolTime) { Q_UNUSED(maxPoolTime) } |
96 | virtual int poolSize() { return 0; } |
97 | |
98 | virtual int indexOf(QObject *object, QObject *objectContext) const = 0; |
99 | virtual const QAbstractItemModel *abstractItemModel() const { return nullptr; } |
100 | |
101 | Q_SIGNALS: |
102 | void countChanged(); |
103 | void modelUpdated(const QQmlChangeSet &changeSet, bool reset); |
104 | void createdItem(int index, QObject *object); |
105 | void initItem(int index, QObject *object); |
106 | void destroyingItem(QObject *object); |
107 | Q_REVISION(15) void itemPooled(int index, QObject *object); |
108 | Q_REVISION(15) void itemReused(int index, QObject *object); |
109 | |
110 | protected: |
111 | QQmlInstanceModel(QObjectPrivate &dd, QObject *parent = nullptr) |
112 | : QObject(dd, parent) {} |
113 | |
114 | private: |
115 | Q_DISABLE_COPY(QQmlInstanceModel) |
116 | }; |
117 | |
118 | class QQmlObjectModelAttached; |
119 | class QQmlObjectModelPrivate; |
120 | class Q_QMLMODELS_PRIVATE_EXPORT QQmlObjectModel : public QQmlInstanceModel |
121 | { |
122 | Q_OBJECT |
123 | Q_DECLARE_PRIVATE(QQmlObjectModel) |
124 | |
125 | Q_PROPERTY(QQmlListProperty<QObject> children READ children NOTIFY childrenChanged DESIGNABLE false) |
126 | Q_CLASSINFO("DefaultProperty" , "children" ) |
127 | QML_NAMED_ELEMENT(ObjectModel) |
128 | QML_ADDED_IN_MINOR_VERSION(1) |
129 | QML_ATTACHED(QQmlObjectModelAttached) |
130 | |
131 | public: |
132 | QQmlObjectModel(QObject *parent=nullptr); |
133 | ~QQmlObjectModel() {} |
134 | |
135 | int count() const override; |
136 | bool isValid() const override; |
137 | QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override; |
138 | ReleaseFlags release(QObject *object, ReusableFlag reusable = NotReusable) override; |
139 | QVariant variantValue(int index, const QString &role) override; |
140 | void setWatchedRoles(const QList<QByteArray> &) override {} |
141 | QQmlIncubator::Status incubationStatus(int index) override; |
142 | |
143 | int indexOf(QObject *object, QObject *objectContext) const override; |
144 | |
145 | QQmlListProperty<QObject> children(); |
146 | |
147 | static QQmlObjectModelAttached *qmlAttachedProperties(QObject *obj); |
148 | |
149 | Q_REVISION(3) Q_INVOKABLE QObject *get(int index) const; |
150 | Q_REVISION(3) Q_INVOKABLE void append(QObject *object); |
151 | Q_REVISION(3) Q_INVOKABLE void insert(int index, QObject *object); |
152 | Q_REVISION(3) Q_INVOKABLE void move(int from, int to, int n = 1); |
153 | Q_REVISION(3) Q_INVOKABLE void remove(int index, int n = 1); |
154 | |
155 | public Q_SLOTS: |
156 | Q_REVISION(3) void clear(); |
157 | |
158 | Q_SIGNALS: |
159 | void childrenChanged(); |
160 | |
161 | private: |
162 | Q_DISABLE_COPY(QQmlObjectModel) |
163 | }; |
164 | |
165 | class QQmlObjectModelAttached : public QObject |
166 | { |
167 | Q_OBJECT |
168 | |
169 | public: |
170 | QQmlObjectModelAttached(QObject *parent) |
171 | : QObject(parent), m_index(-1) {} |
172 | ~QQmlObjectModelAttached() { |
173 | attachedProperties.remove(akey: parent()); |
174 | } |
175 | |
176 | Q_PROPERTY(int index READ index NOTIFY indexChanged) |
177 | int index() const { return m_index; } |
178 | void setIndex(int idx) { |
179 | if (m_index != idx) { |
180 | m_index = idx; |
181 | Q_EMIT indexChanged(); |
182 | } |
183 | } |
184 | |
185 | static QQmlObjectModelAttached *properties(QObject *obj) { |
186 | QQmlObjectModelAttached *rv = attachedProperties.value(akey: obj); |
187 | if (!rv) { |
188 | rv = new QQmlObjectModelAttached(obj); |
189 | attachedProperties.insert(akey: obj, avalue: rv); |
190 | } |
191 | return rv; |
192 | } |
193 | |
194 | Q_SIGNALS: |
195 | void indexChanged(); |
196 | |
197 | public: |
198 | int m_index; |
199 | |
200 | static QHash<QObject*, QQmlObjectModelAttached*> attachedProperties; |
201 | }; |
202 | |
203 | |
204 | QT_END_NAMESPACE |
205 | |
206 | QML_DECLARE_TYPE(QQmlInstanceModel) |
207 | QML_DECLARE_TYPE(QQmlObjectModel) |
208 | |
209 | #endif // QQMLINSTANCEMODEL_P_H |
210 | |