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
114 bool m_dynamicRoles;
115
116 ListLayout *m_layout;
117 ListModel *m_listModel;
118 std::unique_ptr<QPropertyNotifier> translationChangeHandler;
119
120 QVector<class DynamicRoleModelNode *> m_modelObjects;
121 QVector<QString> m_roles;
122
123 struct ElementSync
124 {
125 DynamicRoleModelNode *src = nullptr;
126 DynamicRoleModelNode *target = nullptr;
127 int srcIndex = -1;
128 int targetIndex = -1;
129 QVector<int> changedRoles;
130 };
131
132 static bool sync(QQmlListModel *src, QQmlListModel *target);
133 static QQmlListModel *createWithOwner(QQmlListModel *newOwner);
134
135 void emitItemsChanged(int index, int count, const QVector<int> &roles);
136 void emitItemsAboutToBeInserted(int index, int count);
137 void emitItemsInserted();
138
139 void removeElements(int index, int removeCount);
140
141 void updateTranslations();
142};
143
145{
146 Q_OBJECT
147 QML_NAMED_ELEMENT(ListElement)
149};
150
152{
153public:
161
162
164
165 void verifyBindings(
166 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit,
167 const QList<const QV4::CompiledData::Binding *> &bindings) override;
168 void applyBindings(
169 QObject *obj, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
170 const QList<const QV4::CompiledData::Binding *> &bindings) override;
171
172private:
173 bool verifyProperty(
174 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit,
175 const QV4::CompiledData::Binding *binding);
176 // returns true if a role was set
177 bool applyProperty(
178 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
179 const QV4::CompiledData::Binding *binding, ListModel *model, int outterElementIndex);
180
181 static bool definesEmptyList(const QString &);
182
183 QString listElementTypeName;
184};
185
186template<>
191
192QT_END_NAMESPACE
193
194#endif // QQMLLISTMODEL_H
DynamicRoleModelNodeMetaObject(DynamicRoleModelNode *object)
void propertyWritten(int index) override
void propertyWrite(int index) override
static QVector< int > sync(DynamicRoleModelNode *src, DynamicRoleModelNode *target)
QVariant getValue(const QString &name) const
bool setValue(const QByteArray &name, const QVariant &val)
void setNodeUpdatesEnabled(bool enable)
void updateValues(const QVariantMap &object, QVector< int > &roles)
static QVector< 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)
void set(int elementIndex, QV4::Object *object, QVector< int > *roles)
ListModel * getListProperty(int elementIndex, const ListLayout::Role &role)
int roleCount() const
int setOrCreateProperty(int elementIndex, const QString &key, const QVariant &data)
const ListLayout::Role & getOrCreateListRole(const QString &name)
const ListLayout::Role & getExistingRole(int index) const
QQmlListModel * m_model
void propertyWritten(int index) override
static ModelNodeMetaObject * get(QObject *obj)
ModelNodeMetaObject(QObject *object, QQmlListModel *model, int elementIndex)
void updateValues(const QVector< int > &roles)
\inmodule QtCore
Definition qobject.h:105
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:23
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)