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

source code of qtdeclarative/src/qmlmodels/qqmllistmodel_p_p.h