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 QQMLLISTMODEL_P_P_H |
41 | #define QQMLLISTMODEL_P_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 "qqmllistmodel_p.h" |
55 | #include <private/qtqmlmodelsglobal_p.h> |
56 | #include <private/qqmlengine_p.h> |
57 | #include <private/qqmlopenmetaobject_p.h> |
58 | #include <private/qv4qobjectwrapper_p.h> |
59 | #include <qqml.h> |
60 | |
61 | QT_REQUIRE_CONFIG(qml_list_model); |
62 | |
63 | QT_BEGIN_NAMESPACE |
64 | |
65 | |
66 | class DynamicRoleModelNode; |
67 | |
68 | class DynamicRoleModelNodeMetaObject : public QQmlOpenMetaObject |
69 | { |
70 | public: |
71 | DynamicRoleModelNodeMetaObject(DynamicRoleModelNode *object); |
72 | ~DynamicRoleModelNodeMetaObject(); |
73 | |
74 | bool m_enabled; |
75 | |
76 | protected: |
77 | void propertyWrite(int index) override; |
78 | void propertyWritten(int index) override; |
79 | |
80 | private: |
81 | DynamicRoleModelNode *m_owner; |
82 | }; |
83 | |
84 | class DynamicRoleModelNode : public QObject |
85 | { |
86 | Q_OBJECT |
87 | public: |
88 | DynamicRoleModelNode(QQmlListModel *owner, int uid); |
89 | |
90 | static DynamicRoleModelNode *create(const QVariantMap &obj, QQmlListModel *owner); |
91 | |
92 | void updateValues(const QVariantMap &object, QVector<int> &roles); |
93 | |
94 | QVariant getValue(const QString &name) const |
95 | { |
96 | return m_meta->value(name.toUtf8()); |
97 | } |
98 | |
99 | bool setValue(const QByteArray &name, const QVariant &val) |
100 | { |
101 | return m_meta->setValue(name, val); |
102 | } |
103 | |
104 | void setNodeUpdatesEnabled(bool enable) |
105 | { |
106 | m_meta->m_enabled = enable; |
107 | } |
108 | |
109 | int getUid() const |
110 | { |
111 | return m_uid; |
112 | } |
113 | |
114 | static QVector<int> sync(DynamicRoleModelNode *src, DynamicRoleModelNode *target); |
115 | |
116 | private: |
117 | QQmlListModel *m_owner; |
118 | int m_uid; |
119 | DynamicRoleModelNodeMetaObject *m_meta; |
120 | |
121 | friend class DynamicRoleModelNodeMetaObject; |
122 | }; |
123 | |
124 | class ModelNodeMetaObject : public QQmlOpenMetaObject |
125 | { |
126 | public: |
127 | ModelNodeMetaObject(QObject *object, QQmlListModel *model, int elementIndex); |
128 | ~ModelNodeMetaObject(); |
129 | |
130 | QAbstractDynamicMetaObject *toDynamicMetaObject(QObject *object) override; |
131 | |
132 | static ModelNodeMetaObject *get(QObject *obj); |
133 | |
134 | bool m_enabled; |
135 | QQmlListModel *m_model; |
136 | int m_elementIndex; |
137 | |
138 | void updateValues(); |
139 | void updateValues(const QVector<int> &roles); |
140 | |
141 | bool initialized() const { return m_initialized; } |
142 | |
143 | protected: |
144 | void propertyWritten(int index) override; |
145 | |
146 | private: |
147 | using QQmlOpenMetaObject::setValue; |
148 | |
149 | void emitDirectNotifies(const int *changedRoles, int roleCount); |
150 | |
151 | void initialize(); |
152 | bool m_initialized; |
153 | }; |
154 | |
155 | namespace QV4 { |
156 | |
157 | namespace Heap { |
158 | |
159 | struct ModelObject : public QObjectWrapper { |
160 | void init(QObject *object, QQmlListModel *model) |
161 | { |
162 | QObjectWrapper::init(object); |
163 | m_model = model; |
164 | QObjectPrivate *op = QObjectPrivate::get(o: object); |
165 | m_nodeModelMetaObject = static_cast<ModelNodeMetaObject *>(op->metaObject); |
166 | } |
167 | void destroy() { QObjectWrapper::destroy(); } |
168 | int elementIndex() const { return m_nodeModelMetaObject->m_elementIndex; } |
169 | QQmlListModel *m_model; |
170 | ModelNodeMetaObject *m_nodeModelMetaObject; |
171 | }; |
172 | |
173 | } |
174 | |
175 | struct ModelObject : public QObjectWrapper |
176 | { |
177 | V4_OBJECT2(ModelObject, QObjectWrapper) |
178 | V4_NEEDS_DESTROY |
179 | |
180 | ListModel *listModel() const { return d()->m_model->m_listModel; } |
181 | |
182 | protected: |
183 | static bool virtualPut(Managed *m, PropertyKey id, const Value& value, Value *receiver); |
184 | static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty); |
185 | static ReturnedValue virtualResolveLookupGetter(const Object *object, ExecutionEngine *engine, Lookup *lookup); |
186 | static ReturnedValue lookupGetter(Lookup *l, ExecutionEngine *engine, const Value &object); |
187 | static OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target); |
188 | }; |
189 | |
190 | } // namespace QV4 |
191 | |
192 | class ListLayout |
193 | { |
194 | public: |
195 | ListLayout() : currentBlock(0), currentBlockOffset(0) {} |
196 | ListLayout(const ListLayout *other); |
197 | ~ListLayout(); |
198 | |
199 | class Role |
200 | { |
201 | public: |
202 | |
203 | Role() : type(Invalid), blockIndex(-1), blockOffset(-1), index(-1), subLayout(0) {} |
204 | explicit Role(const Role *other); |
205 | ~Role(); |
206 | |
207 | // This enum must be kept in sync with the roleTypeNames variable in qqmllistmodel.cpp |
208 | enum DataType |
209 | { |
210 | Invalid = -1, |
211 | |
212 | String, |
213 | Number, |
214 | Bool, |
215 | List, |
216 | QObject, |
217 | VariantMap, |
218 | DateTime, |
219 | Function, |
220 | |
221 | MaxDataType |
222 | }; |
223 | |
224 | QString name; |
225 | DataType type; |
226 | int blockIndex; |
227 | int blockOffset; |
228 | int index; |
229 | ListLayout *subLayout; |
230 | }; |
231 | |
232 | const Role *getRoleOrCreate(const QString &key, const QVariant &data); |
233 | const Role &getRoleOrCreate(QV4::String *key, Role::DataType type); |
234 | const Role &getRoleOrCreate(const QString &key, Role::DataType type); |
235 | |
236 | const Role &getExistingRole(int index) const { return *roles.at(i: index); } |
237 | const Role *getExistingRole(const QString &key) const; |
238 | const Role *getExistingRole(QV4::String *key) const; |
239 | |
240 | int roleCount() const { return roles.count(); } |
241 | |
242 | static void sync(ListLayout *src, ListLayout *target); |
243 | |
244 | private: |
245 | const Role &createRole(const QString &key, Role::DataType type); |
246 | |
247 | int currentBlock; |
248 | int currentBlockOffset; |
249 | QVector<Role *> roles; |
250 | QStringHash<Role *> roleHash; |
251 | }; |
252 | |
253 | struct StringOrTranslation |
254 | { |
255 | explicit StringOrTranslation(const QString &s); |
256 | explicit StringOrTranslation(const QV4::CompiledData::Binding *binding); |
257 | ~StringOrTranslation(); |
258 | bool isSet() const { return d.flag(); } |
259 | bool isTranslation() const { return d.isT2(); } |
260 | void setString(const QString &s); |
261 | void setTranslation(const QV4::CompiledData::Binding *binding); |
262 | QString toString(const QQmlListModel *owner) const; |
263 | QString asString() const; |
264 | private: |
265 | void clear(); |
266 | QBiPointer<QStringData, const QV4::CompiledData::Binding> d; |
267 | }; |
268 | |
269 | /*! |
270 | \internal |
271 | */ |
272 | class ListElement |
273 | { |
274 | public: |
275 | |
276 | ListElement(); |
277 | ListElement(int existingUid); |
278 | ~ListElement(); |
279 | |
280 | static QVector<int> sync(ListElement *src, ListLayout *srcLayout, ListElement *target, ListLayout *targetLayout); |
281 | |
282 | enum |
283 | { |
284 | BLOCK_SIZE = 64 - sizeof(int) - sizeof(ListElement *) - sizeof(ModelNodeMetaObject *) |
285 | }; |
286 | |
287 | private: |
288 | |
289 | void destroy(ListLayout *layout); |
290 | |
291 | int setVariantProperty(const ListLayout::Role &role, const QVariant &d); |
292 | |
293 | int setJsProperty(const ListLayout::Role &role, const QV4::Value &d, QV4::ExecutionEngine *eng); |
294 | |
295 | int setStringProperty(const ListLayout::Role &role, const QString &s); |
296 | int setDoubleProperty(const ListLayout::Role &role, double n); |
297 | int setBoolProperty(const ListLayout::Role &role, bool b); |
298 | int setListProperty(const ListLayout::Role &role, ListModel *m); |
299 | int setQObjectProperty(const ListLayout::Role &role, QObject *o); |
300 | int setVariantMapProperty(const ListLayout::Role &role, QV4::Object *o); |
301 | int setVariantMapProperty(const ListLayout::Role &role, QVariantMap *m); |
302 | int setDateTimeProperty(const ListLayout::Role &role, const QDateTime &dt); |
303 | int setFunctionProperty(const ListLayout::Role &role, const QJSValue &f); |
304 | int setTranslationProperty(const ListLayout::Role &role, const QV4::CompiledData::Binding *b); |
305 | |
306 | void setStringPropertyFast(const ListLayout::Role &role, const QString &s); |
307 | void setDoublePropertyFast(const ListLayout::Role &role, double n); |
308 | void setBoolPropertyFast(const ListLayout::Role &role, bool b); |
309 | void setQObjectPropertyFast(const ListLayout::Role &role, QObject *o); |
310 | void setListPropertyFast(const ListLayout::Role &role, ListModel *m); |
311 | void setVariantMapFast(const ListLayout::Role &role, QV4::Object *o); |
312 | void setDateTimePropertyFast(const ListLayout::Role &role, const QDateTime &dt); |
313 | void setFunctionPropertyFast(const ListLayout::Role &role, const QJSValue &f); |
314 | |
315 | void clearProperty(const ListLayout::Role &role); |
316 | |
317 | QVariant getProperty(const ListLayout::Role &role, const QQmlListModel *owner, QV4::ExecutionEngine *eng); |
318 | ListModel *getListProperty(const ListLayout::Role &role); |
319 | StringOrTranslation *getStringProperty(const ListLayout::Role &role); |
320 | QObject *getQObjectProperty(const ListLayout::Role &role); |
321 | QPointer<QObject> *getGuardProperty(const ListLayout::Role &role); |
322 | QVariantMap *getVariantMapProperty(const ListLayout::Role &role); |
323 | QDateTime *getDateTimeProperty(const ListLayout::Role &role); |
324 | QJSValue *getFunctionProperty(const ListLayout::Role &role); |
325 | |
326 | inline char *getPropertyMemory(const ListLayout::Role &role); |
327 | |
328 | int getUid() const { return uid; } |
329 | |
330 | ModelNodeMetaObject *objectCache(); |
331 | |
332 | char data[BLOCK_SIZE]; |
333 | ListElement *next; |
334 | |
335 | int uid; |
336 | QObject *m_objectCache; |
337 | |
338 | friend class ListModel; |
339 | }; |
340 | |
341 | /*! |
342 | \internal |
343 | */ |
344 | class ListModel |
345 | { |
346 | public: |
347 | |
348 | ListModel(ListLayout *layout, QQmlListModel *modelCache); |
349 | ~ListModel() {} |
350 | |
351 | void destroy(); |
352 | |
353 | int setOrCreateProperty(int elementIndex, const QString &key, const QVariant &data); |
354 | int setExistingProperty(int uid, const QString &key, const QV4::Value &data, QV4::ExecutionEngine *eng); |
355 | |
356 | QVariant getProperty(int elementIndex, int roleIndex, const QQmlListModel *owner, QV4::ExecutionEngine *eng); |
357 | ListModel *getListProperty(int elementIndex, const ListLayout::Role &role); |
358 | |
359 | int roleCount() const |
360 | { |
361 | return m_layout->roleCount(); |
362 | } |
363 | |
364 | const ListLayout::Role &getExistingRole(int index) const |
365 | { |
366 | return m_layout->getExistingRole(index); |
367 | } |
368 | |
369 | const ListLayout::Role *getExistingRole(QV4::String *key) const |
370 | { |
371 | return m_layout->getExistingRole(key); |
372 | } |
373 | |
374 | const ListLayout::Role &getOrCreateListRole(const QString &name) |
375 | { |
376 | return m_layout->getRoleOrCreate(key: name, type: ListLayout::Role::List); |
377 | } |
378 | |
379 | int elementCount() const |
380 | { |
381 | return elements.count(); |
382 | } |
383 | |
384 | enum class SetElement {WasJustInserted, IsCurrentlyUpdated}; |
385 | |
386 | void set(int elementIndex, QV4::Object *object, QVector<int> *roles); |
387 | void set(int elementIndex, QV4::Object *object, SetElement reason = SetElement::IsCurrentlyUpdated); |
388 | |
389 | int append(QV4::Object *object); |
390 | void insert(int elementIndex, QV4::Object *object); |
391 | |
392 | Q_REQUIRED_RESULT QVector<std::function<void()>> remove(int index, int count); |
393 | |
394 | int appendElement(); |
395 | void insertElement(int index); |
396 | |
397 | void move(int from, int to, int n); |
398 | |
399 | static bool sync(ListModel *src, ListModel *target); |
400 | |
401 | QObject *getOrCreateModelObject(QQmlListModel *model, int elementIndex); |
402 | |
403 | private: |
404 | QPODVector<ListElement *, 4> elements; |
405 | ListLayout *m_layout; |
406 | |
407 | QQmlListModel *m_modelCache; |
408 | |
409 | struct ElementSync |
410 | { |
411 | ListElement *src = nullptr; |
412 | ListElement *target = nullptr; |
413 | int srcIndex = -1; |
414 | int targetIndex = -1; |
415 | QVector<int> changedRoles; |
416 | }; |
417 | |
418 | void newElement(int index); |
419 | |
420 | void updateCacheIndices(int start = 0, int end = -1); |
421 | |
422 | friend class ListElement; |
423 | friend class QQmlListModelWorkerAgent; |
424 | friend class QQmlListModelParser; |
425 | }; |
426 | |
427 | QT_END_NAMESPACE |
428 | |
429 | Q_DECLARE_METATYPE(ListModel *); |
430 | |
431 | #endif // QQUICKLISTMODEL_P_P_H |
432 | |