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
qqmldelegatemodel_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 QQMLDATAMODEL_P_H
6#define QQMLDATAMODEL_P_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/qqmllistcompositor_p.h>
21#include <private/qqmlobjectmodel_p.h>
22#include <private/qqmlincubator_p.h>
23
24#include <QtCore/qabstractitemmodel.h>
25#include <QtCore/qstringlist.h>
26
28
29QT_BEGIN_NAMESPACE
30
31class QQmlChangeSet;
32class QQuickPackage;
33class QQmlDelegateModelGroup;
36
37
38class Q_QMLMODELS_EXPORT QQmlDelegateModel : public QQmlInstanceModel, public QQmlParserStatus
39{
40 Q_OBJECT
41 Q_DECLARE_PRIVATE(QQmlDelegateModel)
42
43 Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
44 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
45 Q_PROPERTY(QString filterOnGroup READ filterGroup WRITE setFilterGroup NOTIFY filterGroupChanged RESET resetFilterGroup)
46 Q_PROPERTY(QQmlDelegateModelGroup *items READ items CONSTANT) //TODO : worth renaming?
47 Q_PROPERTY(QQmlDelegateModelGroup *persistedItems READ persistedItems CONSTANT)
48 Q_PROPERTY(QQmlListProperty<QQmlDelegateModelGroup> groups READ groups CONSTANT)
49 Q_PROPERTY(QObject *parts READ parts CONSTANT)
50 Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
51 Q_PROPERTY(DelegateModelAccess delegateModelAccess READ delegateModelAccess
52 WRITE setDelegateModelAccess NOTIFY delegateModelAccessChanged REVISION(6, 10) FINAL)
53 Q_CLASSINFO("DefaultProperty", "delegate")
54 QML_NAMED_ELEMENT(DelegateModel)
55 QML_ADDED_IN_VERSION(2, 1)
56 QML_ATTACHED(QQmlDelegateModelAttached)
57 Q_INTERFACES(QQmlParserStatus)
58
59public:
60 enum DelegateModelAccess : quint8 {
61 Qt5ReadWrite,
62 ReadOnly,
63 ReadWrite
64 };
65 Q_ENUM(DelegateModelAccess)
66
67 QQmlDelegateModel();
68 QQmlDelegateModel(QQmlContext *, QObject *parent=nullptr);
69 ~QQmlDelegateModel();
70
71 void classBegin() override;
72 void componentComplete() override;
73
74 QVariant model() const;
75 void setModel(const QVariant &);
76
77 QQmlComponent *delegate() const;
78 void setDelegate(QQmlComponent *);
79
80 QVariant rootIndex() const;
81 void setRootIndex(const QVariant &root);
82
83 DelegateModelAccess delegateModelAccess() const;
84 void setDelegateModelAccess(DelegateModelAccess delegateModelAccess);
85
86 Q_INVOKABLE QVariant modelIndex(int idx) const;
87 Q_INVOKABLE QVariant parentModelIndex() const;
88
89 int count() const override;
90 bool isValid() const override { return delegate() != nullptr; }
91 QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override;
92 ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) override;
93 void cancel(int index) override;
94 QVariant variantValue(int index, const QString &role) override;
95 void setWatchedRoles(const QList<QByteArray> &roles) override;
96 QQmlIncubator::Status incubationStatus(int index) override;
97
98 void drainReusableItemsPool(int maxPoolTime) override;
99 int poolSize() override;
100
101 int indexOf(QObject *object, QObject *objectContext) const override;
102
103 QString filterGroup() const;
104 void setFilterGroup(const QString &group);
105 void resetFilterGroup();
106
107 QQmlDelegateModelGroup *items();
108 QQmlDelegateModelGroup *persistedItems();
109 QQmlListProperty<QQmlDelegateModelGroup> groups();
110 QObject *parts();
111
112 const QAbstractItemModel *abstractItemModel() const override;
113
114 bool event(QEvent *) override;
115
116 static QQmlDelegateModelAttached *qmlAttachedProperties(QObject *obj);
117
118 template<typename View, typename ViewPrivate>
119 static QQmlDelegateModel *createForView(View *q, ViewPrivate *d)
120 {
121 Q_ASSERT(d->model.isNull());
122 QQmlDelegateModel *delegateModel = new QQmlDelegateModel(qmlContext(q), q);
123 d->model = delegateModel;
124 d->ownModel = true;
125 if (d->componentComplete)
126 delegateModel->componentComplete();
127 return delegateModel;
128 }
129
130 template<typename View, typename ViewPrivate>
131 static void applyDelegateModelAccessChangeOnView(View *q, ViewPrivate *d)
132 {
133 if (d->explicitDelegateModelAccess) {
134 qmlWarning(q) << "Explicitly set delegateModelAccess is externally overridden";
135 d->explicitDelegateModelAccess = false;
136 }
137
138 Q_EMIT q->delegateModelAccessChanged();
139 }
140
141 template<typename View, typename ViewPrivate>
142 static void applyDelegateChangeOnView(View *q, ViewPrivate *d)
143 {
144 if (d->explicitDelegate) {
145 qmlWarning(q) << "Explicitly set delegate is externally overridden";
146 d->explicitDelegate = false;
147 }
148
149 Q_EMIT q->delegateChanged();
150 }
151
152Q_SIGNALS:
153 void filterGroupChanged();
154 void defaultGroupsChanged();
155 void rootIndexChanged();
156 void delegateChanged();
157 Q_REVISION(6, 10) void delegateModelAccessChanged();
158 Q_REVISION(6, 10) void modelChanged();
159
160private Q_SLOTS:
161 void _q_itemsChanged(int index, int count, const QVector<int> &roles);
162 void _q_itemsInserted(int index, int count);
163 void _q_itemsRemoved(int index, int count);
164 void _q_itemsMoved(int from, int to, int count);
165 void _q_modelAboutToBeReset();
166 void _q_rowsInserted(const QModelIndex &,int,int);
167 void _q_columnsInserted(const QModelIndex &, int, int);
168 void _q_columnsRemoved(const QModelIndex &, int, int);
169 void _q_columnsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
170 void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end);
171 void _q_rowsRemoved(const QModelIndex &,int,int);
172 void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
173 void _q_dataChanged(const QModelIndex&,const QModelIndex&,const QVector<int> &);
174 void _q_layoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint);
175
176private:
177 void handleModelReset();
178 bool isDescendantOf(const QPersistentModelIndex &desc, const QList<QPersistentModelIndex> &parents) const;
179
180 Q_DISABLE_COPY(QQmlDelegateModel)
181};
182
184class Q_QMLMODELS_EXPORT QQmlDelegateModelGroup : public QObject
185{
186 Q_OBJECT
187 Q_PROPERTY(int count READ count NOTIFY countChanged)
188 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
189 Q_PROPERTY(bool includeByDefault READ defaultInclude WRITE setDefaultInclude NOTIFY defaultIncludeChanged)
190 QML_NAMED_ELEMENT(DelegateModelGroup)
191 QML_ADDED_IN_VERSION(2, 1)
192public:
193 QQmlDelegateModelGroup(QObject *parent = nullptr);
194 QQmlDelegateModelGroup(const QString &name, QQmlDelegateModel *model, int compositorType, QObject *parent = nullptr);
195 ~QQmlDelegateModelGroup();
196
197 QString name() const;
198 void setName(const QString &name);
199
200 int count() const;
201
202 bool defaultInclude() const;
203 void setDefaultInclude(bool include);
204
205 Q_INVOKABLE QJSValue get(int index);
206
207public Q_SLOTS:
208 void insert(QQmlV4FunctionPtr);
209 void create(QQmlV4FunctionPtr);
210 void resolve(QQmlV4FunctionPtr);
211 void remove(QQmlV4FunctionPtr);
212 void addGroups(QQmlV4FunctionPtr);
213 void removeGroups(QQmlV4FunctionPtr);
214 void setGroups(QQmlV4FunctionPtr);
215 void move(QQmlV4FunctionPtr);
216
217Q_SIGNALS:
218 void countChanged();
219 void nameChanged();
220 void defaultIncludeChanged();
221 void changed(const QJSValue &removed, const QJSValue &inserted);
222private:
223 Q_DECLARE_PRIVATE(QQmlDelegateModelGroup)
224};
225
227class QQmlDelegateModelAttachedMetaObject;
229{
230 Q_OBJECT
231 Q_PROPERTY(QQmlDelegateModel *model READ model CONSTANT FINAL)
238
239public:
241 QQmlDelegateModelAttached(QQmlDelegateModelItem *cacheItem, QObject *parent);
243
244 void resetCurrentIndex();
246
247 void setInPersistedItems(bool inPersisted);
248 bool inPersistedItems() const;
249 int persistedItemsIndex() const;
250
251 void setInItems(bool inItems);
252 bool inItems() const;
253 int itemsIndex() const;
254
255 QQmlDelegateModel *model() const;
256
257 QStringList groups() const;
258 void setGroups(const QStringList &groups);
259
260 bool isUnresolved() const;
261
262 void emitChanges();
263
264 void emitUnresolvedChanged() { Q_EMIT unresolvedChanged(); }
265
269
270private:
271 void setInGroup(QQmlListCompositor::Group group, bool inGroup);
272
273public:
278
279 friend class QQmlDelegateModelAttachedMetaObject;
280};
281
283{
289
290 QQmlDelegateModelPointer(QQmlInstanceModel *model)
291 : model(model)
292 , concrete(model ? Unknown : InstanceModel)
293 {}
294
295 QQmlDelegateModelPointer(QQmlDelegateModel *model)
296 : model(model)
297 , concrete(DelegateModel)
298 {}
299
300 QQmlDelegateModelPointer &operator=(QQmlInstanceModel *instanceModel)
301 {
302 model = instanceModel;
303 concrete = model ? Unknown : InstanceModel;
304 return *this;
305 }
306
307 QQmlDelegateModelPointer &operator=(QQmlDelegateModel *delegateModel)
308 {
309 model = delegateModel;
310 concrete = DelegateModel;
311 return *this;
312 }
313
315 {
316 switch (concrete) {
317 case DelegateModel:
318 return static_cast<QQmlDelegateModel *>(model);
319 case InstanceModel:
320 return nullptr;
321 case Unknown:
322 break;
323 }
324
325 QQmlDelegateModel *result = qobject_cast<QQmlDelegateModel *>(model);
326 concrete = result ? DelegateModel : InstanceModel;
327 return result;
328 }
329
330 QQmlInstanceModel *instanceModel() { return model; }
331
332 operator bool() const { return model != nullptr; }
333
334private:
335 enum ConcreteType {
336 Unknown,
337 InstanceModel,
338 DelegateModel
339 };
340
341 QQmlInstanceModel *model = nullptr;
342 ConcreteType concrete = InstanceModel;
343};
344
345QT_END_NAMESPACE
346
347#endif // QQMLDATAMODEL_P_H
void initializeRequiredProperties(QQmlDelegateModelItem *modelItemToIncubate, QObject *object, QQmlDelegateModel::DelegateModelAccess access)
QPointer< QObject > proxiedObject
QQmlRefPointer< QQmlContextData > proxyContext
void statusChanged(Status) override
Called when the status of the incubator changes.
void setInitialState(QObject *) override
Called after the object is first created, but before complex property bindings are evaluated and,...
int index[QQmlListCompositor::MaximumGroupCount]
QQmlDelegateModelItem * incubating
QQDMIncubationTask(QQmlDelegateModelPrivate *l, IncubationMode mode)
QQmlDelegateModelPrivate * vdm
QQmlDelegateModelAttachedMetaObject(QQmlDelegateModelItemMetaType *metaType, QMetaObject *metaObject)
int metaCall(QObject *, QMetaObject::Call, int _id, void **) override
QQmlDelegateModelAttached(QQmlDelegateModelItem *cacheItem, QObject *parent)
void setCacheItem(QQmlDelegateModelItem *item)
void setGroups(const QStringList &groups)
bool isUnresolved() const
\qmlattachedproperty bool QtQml.Models::DelegateModel::isUnresolved
void emitChanges()
\qmlattachedproperty bool QtQml.Models::DelegateModel::inItems
int m_previousIndex[QQmlListCompositor::MaximumGroupCount]
int m_currentIndex[QQmlListCompositor::MaximumGroupCount]
void setInPersistedItems(bool inPersisted)
QQmlDelegateModel * model() const
\qmlattachedproperty model QtQml.Models::DelegateModel::model
QStringList groups() const
\qmlattachedproperty stringlist QtQml.Models::DelegateModel::groups
QQmlDelegateModelItem * m_cacheItem
QV4::ReturnedValue array(QV4::ExecutionEngine *engine, const QVector< QQmlChangeSet::Change > &changes)
QQmlDelegateModelEngineData(QV4::ExecutionEngine *v4)
virtual void initPackage(int, QQuickPackage *)
virtual void destroyingPackage(QQuickPackage *)
virtual void emitModelUpdated(const QQmlChangeSet &changeSet, bool reset)=0
virtual void createdPackage(int, QQuickPackage *)
bool parseIndex(const QV4::Value &value, int *index, Compositor::Group *group) const
void createdPackage(int index, QQuickPackage *package)
QPointer< QQmlDelegateModel > model
bool parseGroupArgs(QQmlV4FunctionPtr args, Compositor::Group *group, int *index, int *count, int *groups) const
void initPackage(int index, QQuickPackage *package)
QQmlDelegateModelGroupEmitterList emitters
void destroyingPackage(QQuickPackage *package)
void emitChanges(QV4::ExecutionEngine *engine)
QQDMIncubationTask * incubationTask
static QV4::ReturnedValue set_member(QQmlDelegateModelItem *thisItem, uint flag, const QV4::Value &arg)
void childContextObjectDestroyed(QObject *childContextObject)
virtual void setValue(const QString &role, const QVariant &value)
virtual QV4::ReturnedValue get()
static QV4::ReturnedValue get_member(QQmlDelegateModelItem *thisItem, uint flag, const QV4::Value &)
static QV4::ReturnedValue get_index(QQmlDelegateModelItem *thisItem, uint flag, const QV4::Value &arg)
static QV4::ReturnedValue get_groups(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
int groupIndex(Compositor::Group group)
QDynamicMetaObjectData * exchangeMetaObject(QDynamicMetaObjectData *metaObject)
virtual bool resolveIndex(const QQmlAdaptorModel &, int)
QQmlRefPointer< QQmlDelegateModelItemMetaType > metaType
static QQmlDelegateModelItem * dataForObject(QObject *object)
static QV4::ReturnedValue get_model(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
virtual void setModelIndex(int idx, int newRow, int newColumn, bool alwaysEmit=false)
virtual QQmlRefPointer< QQmlContextData > initProxy()
QQmlRefPointer< QQmlContextData > contextData
QQmlDelegateModelAttached * attached() const
static QV4::ReturnedValue set_groups(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc)
Q_REVISION(2, 12) void rowChanged()
void objectDestroyed(QObject *)
void propertyCreated(int, QMetaPropertyBuilder &) override
QList< QQmlPartsModel * > models
void setInitialState(QQDMIncubationTask *incubationTask, QObject *o)
void removeCacheItem(QQmlDelegateModelItem *cacheItem)
QVariant variantValue(Compositor::Group group, int index, const QString &name)
void emitCreatedPackage(QQDMIncubationTask *incubationTask, QQuickPackage *package)
QList< QQDMIncubationTask * > m_finishedIncubating
void connectModel(QQmlAdaptorModel *model)
QList< QQmlDelegateModelItem * > m_cache
QQmlDelegateModelParts * m_parts
static qsizetype group_count(QQmlListProperty< QQmlDelegateModelGroup > *property)
QList< QByteArray > m_watchedRoles
QQmlAbstractDelegateComponent * m_delegateChooser
void itemsRemoved(const QVector< Compositor::Remove > &removes)
QPointer< QQmlContext > m_context
void emitCreatedItem(QQDMIncubationTask *incubationTask, QObject *item)
void emitInitItem(QQDMIncubationTask *incubationTask, QObject *item)
static void group_append(QQmlListProperty< QQmlDelegateModelGroup > *property, QQmlDelegateModelGroup *group)
QQmlStrongJSQObjectReference< QQmlComponent > m_delegate
void emitDestroyingItem(QObject *item)
void addCacheItem(QQmlDelegateModelItem *item, Compositor::iterator it)
QQmlRefPointer< QQmlDelegateModelItemMetaType > m_cacheMetaType
void reuseItem(QQmlDelegateModelItem *item, int newModelIndex, int newGroups)
void drainReusableItemsPool(int maxPoolTime)
QQmlDelegateModelGroup * m_persistedItems
static QQmlDelegateModelGroup * group_at(QQmlListProperty< QQmlDelegateModelGroup > *property, qsizetype index)
QQmlDelegateModelGroupEmitterList m_pendingParts
void emitModelUpdated(const QQmlChangeSet &changeSet, bool reset) override
QQmlDelegateModelGroup * m_cacheItems
QObject * object(Compositor::Group group, int index, QQmlIncubator::IncubationMode incubationMode)
void destroyCacheItem(QQmlDelegateModelItem *cacheItem)
InsertionResult insert(Compositor::insert_iterator &before, const QV4::Value &object, int groups)
void itemsRemoved(const QVector< Compositor::Remove > &removes, QVarLengthArray< QVector< QQmlChangeSet::Change >, Compositor::MaximumGroupCount > *translatedRemoves, QHash< int, QList< QQmlDelegateModelItem * > > *movedItems=nullptr)
void setGroups(Compositor::iterator from, int count, Compositor::Group group, int groupFlags)
void itemsInserted(const QVector< Compositor::Insert > &inserts, QVarLengthArray< QVector< QQmlChangeSet::Change >, Compositor::MaximumGroupCount > *translatedInserts, QHash< int, QList< QQmlDelegateModelItem * > > *movedItems=nullptr)
void delegateChanged(bool add=true, bool remove=true)
QQmlDelegateModelGroup * m_items
void releaseIncubator(QQDMIncubationTask *incubationTask)
void incubatorStatusChanged(QQDMIncubationTask *incubationTask, QQmlIncubator::Status status)
void emitInitPackage(QQDMIncubationTask *incubationTask, QQuickPackage *package)
void addGroups(Compositor::iterator from, int count, Compositor::Group group, int groupFlags)
void emitDestroyingPackage(QQuickPackage *package)
QQmlComponent * resolveDelegate(int index)
QMetaObject::Connection m_delegateChooserChanged
QQmlDelegateModelGroup * m_groups[Compositor::MaximumGroupCount]
void itemsChanged(const QVector< Compositor::Change > &changes)
void removeGroups(Compositor::iterator from, int count, Compositor::Group group, int groupFlags)
void itemsInserted(const QVector< Compositor::Insert > &inserts)
QQmlReusableDelegateModelItemsPool m_reusableItemsPool
void itemsMoved(const QVector< Compositor::Remove > &removes, const QVector< Compositor::Insert > &inserts)
QQmlListCompositor::Group m_compositorGroup
static QQmlDelegateModelPrivate * get(QQmlDelegateModel *m)
void emitModelUpdated(const QQmlChangeSet &changeSet, bool reset) override
QVariant variantValue(int index, const QString &role) override
int count() const override
QObject * object(int index, QQmlIncubator::IncubationMode incubationMode=QQmlIncubator::AsynchronousIfNested) override
QList< QByteArray > watchedRoles() const
void updateFilterGroup(Compositor::Group group, const QQmlChangeSet &changeSet)
void setFilterGroup(const QString &group)
QQmlIncubator::Status incubationStatus(int index) override
int indexOf(QObject *item, QObject *objectContext) const override
void destroyingPackage(QQuickPackage *package) override
void setWatchedRoles(const QList< QByteArray > &roles) override
ReleaseFlags release(QObject *item, ReusableFlag reusable=NotReusable) override
void createdPackage(int index, QQuickPackage *package) override
void initPackage(int index, QQuickPackage *package) override
QString filterGroup() const
bool isValid() const override
bool insertItem(QQmlDelegateModelItem *modelItem)
void reuseItem(QQmlDelegateModelItem *item, int newModelIndex)
void drain(int maxPoolTime, std::function< void(QQmlDelegateModelItem *cacheItem)> releaseItem)
QQmlDelegateModelItem * takeItem(const QQmlComponent *delegate, int newIndexHint)
Definition qjsvalue.h:23
QT_REQUIRE_CONFIG(animation)
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcEventDispatcher)
QT_REQUIRE_CONFIG(qml_delegate_model)
static bool isDoneIncubating(QQmlIncubator::Status status)
DEFINE_OBJECT_VTABLE(QQmlDelegateModelGroupChange)
DEFINE_OBJECT_VTABLE(QV4::DelegateModelGroupFunction)
DEFINE_OBJECT_VTABLE(QQmlDelegateModelItemObject)
static void incrementIndexes(QQmlDelegateModelItem *cacheItem, int count, const int *deltas)
DEFINE_OBJECT_VTABLE(QQmlDelegateModelGroupChangeArray)
QQmlDelegateModelGroupEmitterList::iterator GroupEmitterListIt
QIntrusiveList< QQmlDelegateModelGroupEmitter, &QQmlDelegateModelGroupEmitter::emitterNode > QQmlDelegateModelGroupEmitterList
QT_BEGIN_NAMESPACE typedef QQmlListCompositor Compositor
QQmlDelegateModelPointer & operator=(QQmlDelegateModelPointer &&)=default
QQmlDelegateModelPointer(QQmlInstanceModel *model)
QQmlDelegateModelPointer & operator=(QQmlInstanceModel *instanceModel)
QQmlDelegateModelPointer(const QQmlDelegateModelPointer &)=default
QQmlDelegateModelPointer()=default
QQmlDelegateModelPointer & operator=(const QQmlDelegateModelPointer &)=default
QQmlInstanceModel * instanceModel()
QQmlDelegateModel * delegateModel()
QQmlDelegateModelPointer(QQmlDelegateModelPointer &&)=default
void init(ExecutionEngine *engine, uint flag, QV4::ReturnedValue(*code)(QQmlDelegateModelItem *item, uint flag, const QV4::Value &arg))
QV4::ReturnedValue(* code)(QQmlDelegateModelItem *item, uint flag, const QV4::Value &arg)
void init(const QVector< QQmlChangeSet::Change > &changes)
QVector< QQmlChangeSet::Change > * changes
void init(QQmlDelegateModelItem *item)