Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmllistmodel_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QQMLLISTMODEL_H
6#define QQMLLISTMODEL_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qtqmlmodelsglobal_p.h>
20#include <private/qqmlcustomparser_p.h>
21
22#include <QtCore/QObject>
23#include <QtCore/QStringList>
24#include <QtCore/QHash>
25#include <QtCore/QList>
26#include <QtCore/QVariant>
27#include <QtCore/qabstractitemmodel.h>
28
29#include <private/qv4engine_p.h>
30#include <private/qpodvector_p.h>
31
33
34QT_BEGIN_NAMESPACE
35
36
37class QQmlListModelWorkerAgent;
38class ListModel;
39class ListLayout;
40
41namespace QV4 {
42struct ModelObject;
43}
44
45class Q_QMLMODELS_EXPORT QQmlListModel : public QAbstractListModel
46{
47 Q_OBJECT
48 Q_PROPERTY(int count READ count NOTIFY countChanged)
49 Q_PROPERTY(bool dynamicRoles READ dynamicRoles WRITE setDynamicRoles)
50 Q_PROPERTY(QObject *agent READ agent CONSTANT REVISION(2, 14))
51 QML_NAMED_ELEMENT(ListModel)
52 QML_ADDED_IN_VERSION(2, 0)
53 QML_CUSTOMPARSER
54
55public:
56 QQmlListModel(QObject *parent=nullptr);
57 ~QQmlListModel();
58
59 QModelIndex index(int row, int column, const QModelIndex &parent) const override;
60 int rowCount(const QModelIndex &parent) const override;
61 QVariant data(const QModelIndex &index, int role) const override;
62 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
63 QHash<int,QByteArray> roleNames() const override;
64
65 QVariant data(int index, int role) const;
66 int count() const;
67
68 Q_INVOKABLE void clear();
69 Q_INVOKABLE void remove(QQmlV4FunctionPtr args);
70 Q_INVOKABLE void append(QQmlV4FunctionPtr args);
71 Q_INVOKABLE void insert(QQmlV4FunctionPtr args);
72 Q_INVOKABLE QJSValue get(int index) const;
73 Q_INVOKABLE void set(int index, const QJSValue &value);
74 Q_INVOKABLE void setProperty(int index, const QString& property, const QVariant& value);
75 Q_INVOKABLE void move(int from, int to, int count);
76 Q_INVOKABLE void sync();
77
78 QQmlListModelWorkerAgent *agent();
79
80 bool dynamicRoles() const { return m_dynamicRoles; }
81 void setDynamicRoles(bool enableDynamicRoles);
82
83 ListModel *listModel() const { return m_listModel; }
84
85Q_SIGNALS:
86 void countChanged();
87
88private:
89 friend class QQmlListModelParser;
90 friend class QQmlListModelWorkerAgent;
91 friend class ModelObject;
92 friend struct QV4::ModelObject;
93 friend class ModelNodeMetaObject;
94 friend class ListModel;
95 friend class ListElement;
96 friend class DynamicRoleModelNode;
97 friend class DynamicRoleModelNodeMetaObject;
98 friend struct StringOrTranslation;
99
100 // Constructs a flat list model for a worker agent
101 QQmlListModel(QQmlListModel *orig, QQmlListModelWorkerAgent *agent);
102 QQmlListModel(const QQmlListModel *owner, ListModel *data, QV4::ExecutionEngine *engine, QObject *parent=nullptr);
103
104 QV4::ExecutionEngine *engine() const;
105
106 inline bool canMove(int from, int to, int n) const { return !(from+n > count() || to+n > count() || from < 0 || to < 0 || n < 0); }
107
108 mutable QQmlListModelWorkerAgent *m_agent;
109 mutable QV4::ExecutionEngine *m_engine;
110 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_compilationUnit;
111 bool m_mainThread;
112 bool m_primary;
113 bool m_ownAgent = false;
114
115 bool m_dynamicRoles;
116
117 ListLayout *m_layout;
118 ListModel *m_listModel;
119 std::unique_ptr<QPropertyNotifier> translationChangeHandler;
120
121 QList<class DynamicRoleModelNode *> m_modelObjects;
122 QList<QString> m_roles;
123
124 struct ElementSync
125 {
126 DynamicRoleModelNode *src = nullptr;
127 DynamicRoleModelNode *target = nullptr;
128 int srcIndex = -1;
129 int targetIndex = -1;
130 QList<int> changedRoles;
131 };
132
133 static bool sync(QQmlListModel *src, QQmlListModel *target);
134 static QQmlListModel *createWithOwner(QQmlListModel *newOwner);
135
136 void emitItemsChanged(int index, int count, const QList<int> &roles);
137 void emitItemsAboutToBeInserted(int index, int count);
138 void emitItemsInserted();
139
140 void removeElements(int index, int removeCount);
141
142 void updateTranslations();
143};
144
146{
147 Q_OBJECT
148 QML_NAMED_ELEMENT(ListElement)
150};
151
153{
154public:
162
163
165
166 void verifyBindings(
167 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit,
168 const QList<const QV4::CompiledData::Binding *> &bindings) override;
169 void applyBindings(
170 QObject *obj, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
171 const QList<const QV4::CompiledData::Binding *> &bindings) override;
172
173private:
174 bool verifyProperty(
175 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit,
176 const QV4::CompiledData::Binding *binding);
177 // returns true if a role was set
178 bool applyProperty(
179 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
180 const QV4::CompiledData::Binding *binding, ListModel *model, QQmlListModel *owner,
181 int outterElementIndex);
182
183 static bool definesEmptyList(QStringView s);
184
185 QString listElementTypeName;
186};
187
188template<>
193
194QT_END_NAMESPACE
195
196#endif // QQMLLISTMODEL_H
DynamicRoleModelNodeMetaObject(DynamicRoleModelNode *object)
void propertyWritten(int index) override
void propertyWrite(int index) override
void updateValues(const QVariantMap &object, QList< int > &roles)
static QList< int > sync(DynamicRoleModelNode *src, DynamicRoleModelNode *target)
QVariant getValue(const QString &name) const
bool setValue(const QByteArray &name, const QVariant &val)
void setNodeUpdatesEnabled(bool enable)
static QList< int > sync(ListElement *src, ListLayout *srcLayout, ListElement *target, ListLayout *targetLayout)
ListElement(int existingUid)
Role(const Role *other)
ListLayout(const ListLayout *other)
int roleCount() const
const Role * getRoleOrCreate(const QString &key, const QVariant &data)
const Role * getExistingRole(QV4::String *key) const
static void sync(ListLayout *src, ListLayout *target)
const Role & getExistingRole(int index) const
const Role & getRoleOrCreate(const QString &key, Role::DataType type)
const Role & getRoleOrCreate(QV4::String *key, Role::DataType type)
const Role * getExistingRole(const QString &key) const
void set(int elementIndex, QV4::Object *object, SetElement reason=SetElement::IsCurrentlyUpdated)
static bool sync(ListModel *src, ListModel *target)
int append(QV4::Object *object)
QVariant getProperty(int elementIndex, int roleIndex, const QQmlListModel *owner, QV4::ExecutionEngine *eng)
int elementCount() const
void move(int from, int to, int n)
const ListLayout::Role * getExistingRole(QV4::String *key) const
void insertElement(int index)
ListModel(ListLayout *layout, QQmlListModel *modelCache)
int appendElement()
void insert(int elementIndex, QV4::Object *object)
int setExistingProperty(int uid, const QString &key, const QV4::Value &data, QV4::ExecutionEngine *eng)
void updateTranslations()
QObject * getOrCreateModelObject(QQmlListModel *model, int elementIndex)
ListModel * getListProperty(int elementIndex, const ListLayout::Role &role)
int roleCount() const
int setOrCreateProperty(int elementIndex, const QString &key, const QVariant &data)
void set(int elementIndex, QV4::Object *object, QList< int > *roles)
const ListLayout::Role & getOrCreateListRole(const QString &name)
const ListLayout::Role & getExistingRole(int index) const
QQmlListModel * m_model
void propertyWritten(int index) override
void updateValues(const QList< int > &roles)
static ModelNodeMetaObject * get(QObject *obj)
ModelNodeMetaObject(QObject *object, QQmlListModel *model, int elementIndex)
\inmodule QtCore
Definition qobject.h:106
void verifyBindings(const QQmlRefPointer< QV4::CompiledData::CompilationUnit > &compilationUnit, const QList< const QV4::CompiledData::Binding * > &bindings) override
void applyBindings(QObject *obj, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit, const QList< const QV4::CompiledData::Binding * > &bindings) override
Definition qjsvalue.h:24
DEFINE_OBJECT_VTABLE(ModelObject)
QT_REQUIRE_CONFIG(animation)
QQmlCustomParser * qmlCreateCustomParser< QQmlConnections >()
static QAtomicInt uidCounter(MIN_LISTMODEL_UID)
Q_DECLARE_METATYPE(const QV4::CompiledData::Binding *)
static bool isMemoryUsed(const char *mem)
static QString roleTypeName(ListLayout::Role::DataType t)
QT_END_NAMESPACE Q_DECLARE_METATYPE(ListModel *)
QT_REQUIRE_CONFIG(qml_list_model)
QV4QPointer< QQmlListModel > m_model
PropertyKey next(const Object *o, Property *pd=nullptr, PropertyAttributes *attrs=nullptr) override
~ModelObjectOwnPropertyKeyIterator() override=default
static OwnPropertyKeyIterator * virtualOwnPropertyKeys(const Object *m, Value *target)
static ReturnedValue lookupGetter(Lookup *l, ExecutionEngine *engine, const Value &object)
static ReturnedValue virtualResolveLookupGetter(const Object *object, ExecutionEngine *engine, Lookup *lookup)
static ReturnedValue virtualGet(const Managed *m, PropertyKey id, const Value *receiver, bool *hasProperty)
const QV4::CompiledData::Binding * binding
void setString(const QString &s)
QString toString(const QQmlListModel *owner) const
QString asString() const
void setTranslation(const QV4::CompiledData::Binding *binding)