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
4#ifndef QQMLDATAMODEL_P_H
5#define QQMLDATAMODEL_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 <private/qtqmlmodelsglobal_p.h>
19#include <private/qqmllistcompositor_p.h>
20#include <private/qqmlobjectmodel_p.h>
21#include <private/qqmlincubator_p.h>
22
23#include <QtCore/qabstractitemmodel.h>
24#include <QtCore/qstringlist.h>
25
26QT_REQUIRE_CONFIG(qml_delegate_model);
27
28QT_BEGIN_NAMESPACE
29
30class QQmlChangeSet;
31class QQuickPackage;
32class QQmlV4Function;
33class QQmlDelegateModelGroup;
34class QQmlDelegateModelAttached;
35class QQmlDelegateModelPrivate;
36
37
38class Q_QMLMODELS_PRIVATE_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 FINAL)
44 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL)
45 Q_PROPERTY(QString filterOnGroup READ filterGroup WRITE setFilterGroup NOTIFY filterGroupChanged RESET resetFilterGroup FINAL)
46 Q_PROPERTY(QQmlDelegateModelGroup *items READ items CONSTANT FINAL) //TODO : worth renaming?
47 Q_PROPERTY(QQmlDelegateModelGroup *persistedItems READ persistedItems CONSTANT FINAL)
48 Q_PROPERTY(QQmlListProperty<QQmlDelegateModelGroup> groups READ groups CONSTANT FINAL)
49 Q_PROPERTY(QObject *parts READ parts CONSTANT FINAL)
50 Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged FINAL)
51 Q_CLASSINFO("DefaultProperty", "delegate")
52 QML_NAMED_ELEMENT(DelegateModel)
53 QML_ADDED_IN_VERSION(2, 1)
54 QML_ATTACHED(QQmlDelegateModelAttached)
55 Q_INTERFACES(QQmlParserStatus)
56
57public:
58 QQmlDelegateModel();
59 QQmlDelegateModel(QQmlContext *, QObject *parent=nullptr);
60 ~QQmlDelegateModel();
61
62 void classBegin() override;
63 void componentComplete() override;
64
65 QVariant model() const;
66 void setModel(const QVariant &);
67
68 QQmlComponent *delegate() const;
69 void setDelegate(QQmlComponent *);
70
71 QVariant rootIndex() const;
72 void setRootIndex(const QVariant &root);
73
74 Q_INVOKABLE QVariant modelIndex(int idx) const;
75 Q_INVOKABLE QVariant parentModelIndex() const;
76
77 int count() const override;
78 bool isValid() const override { return delegate() != nullptr; }
79 QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override;
80 ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) override;
81 void cancel(int index) override;
82 QVariant variantValue(int index, const QString &role) override;
83 void setWatchedRoles(const QList<QByteArray> &roles) override;
84 QQmlIncubator::Status incubationStatus(int index) override;
85
86 void drainReusableItemsPool(int maxPoolTime) override;
87 int poolSize() override;
88
89 int indexOf(QObject *object, QObject *objectContext) const override;
90
91 QString filterGroup() const;
92 void setFilterGroup(const QString &group);
93 void resetFilterGroup();
94
95 QQmlDelegateModelGroup *items();
96 QQmlDelegateModelGroup *persistedItems();
97 QQmlListProperty<QQmlDelegateModelGroup> groups();
98 QObject *parts();
99
100 const QAbstractItemModel *abstractItemModel() const override;
101
102 bool event(QEvent *) override;
103
104 static QQmlDelegateModelAttached *qmlAttachedProperties(QObject *obj);
105
106Q_SIGNALS:
107 void filterGroupChanged();
108 void defaultGroupsChanged();
109 void rootIndexChanged();
110 void delegateChanged();
111
112private Q_SLOTS:
113 void _q_itemsChanged(int index, int count, const QVector<int> &roles);
114 void _q_itemsInserted(int index, int count);
115 void _q_itemsRemoved(int index, int count);
116 void _q_itemsMoved(int from, int to, int count);
117 void _q_modelAboutToBeReset();
118 void _q_rowsInserted(const QModelIndex &,int,int);
119 void _q_columnsInserted(const QModelIndex &, int, int);
120 void _q_columnsRemoved(const QModelIndex &, int, int);
121 void _q_columnsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
122 void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end);
123 void _q_rowsRemoved(const QModelIndex &,int,int);
124 void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
125 void _q_dataChanged(const QModelIndex&,const QModelIndex&,const QVector<int> &);
126 void _q_layoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint);
127
128private:
129 void handleModelReset();
130 bool isDescendantOf(const QPersistentModelIndex &desc, const QList<QPersistentModelIndex> &parents) const;
131
132 Q_DISABLE_COPY(QQmlDelegateModel)
133};
134
135class QQmlDelegateModelGroupPrivate;
136class Q_QMLMODELS_PRIVATE_EXPORT QQmlDelegateModelGroup : public QObject
137{
138 Q_OBJECT
139 Q_PROPERTY(int count READ count NOTIFY countChanged FINAL)
140 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
141 Q_PROPERTY(bool includeByDefault READ defaultInclude WRITE setDefaultInclude NOTIFY defaultIncludeChanged FINAL)
142 QML_NAMED_ELEMENT(DelegateModelGroup)
143 QML_ADDED_IN_VERSION(2, 1)
144public:
145 QQmlDelegateModelGroup(QObject *parent = nullptr);
146 QQmlDelegateModelGroup(const QString &name, QQmlDelegateModel *model, int compositorType, QObject *parent = nullptr);
147 ~QQmlDelegateModelGroup();
148
149 QString name() const;
150 void setName(const QString &name);
151
152 int count() const;
153
154 bool defaultInclude() const;
155 void setDefaultInclude(bool include);
156
157 Q_INVOKABLE QJSValue get(int index);
158
159public Q_SLOTS:
160 void insert(QQmlV4Function *);
161 void create(QQmlV4Function *);
162 void resolve(QQmlV4Function *);
163 void remove(QQmlV4Function *);
164 void addGroups(QQmlV4Function *);
165 void removeGroups(QQmlV4Function *);
166 void setGroups(QQmlV4Function *);
167 void move(QQmlV4Function *);
168
169Q_SIGNALS:
170 void countChanged();
171 void nameChanged();
172 void defaultIncludeChanged();
173 void changed(const QJSValue &removed, const QJSValue &inserted);
174private:
175 Q_DECLARE_PRIVATE(QQmlDelegateModelGroup)
176};
177
178class QQmlDelegateModelItem;
179class QQmlDelegateModelAttachedMetaObject;
180class QQmlDelegateModelAttached : public QObject
181{
182 Q_OBJECT
183 Q_PROPERTY(QQmlDelegateModel *model READ model CONSTANT FINAL)
184 Q_PROPERTY(QStringList groups READ groups WRITE setGroups NOTIFY groupsChanged FINAL)
185 Q_PROPERTY(bool isUnresolved READ isUnresolved NOTIFY unresolvedChanged FINAL)
186 Q_PROPERTY(bool inPersistedItems READ inPersistedItems WRITE setInPersistedItems NOTIFY groupsChanged FINAL)
187 Q_PROPERTY(bool inItems READ inItems WRITE setInItems NOTIFY groupsChanged FINAL)
188 Q_PROPERTY(int persistedItemsIndex READ persistedItemsIndex NOTIFY groupsChanged FINAL)
189 Q_PROPERTY(int itemsIndex READ itemsIndex NOTIFY groupsChanged FINAL)
190
191public:
192 QQmlDelegateModelAttached(QObject *parent);
193 QQmlDelegateModelAttached(QQmlDelegateModelItem *cacheItem, QObject *parent);
194 ~QQmlDelegateModelAttached() {}
195
196 void resetCurrentIndex();
197 void setCacheItem(QQmlDelegateModelItem *item);
198
199 void setInPersistedItems(bool inPersisted);
200 bool inPersistedItems() const;
201 int persistedItemsIndex() const;
202
203 void setInItems(bool inItems);
204 bool inItems() const;
205 int itemsIndex() const;
206
207 QQmlDelegateModel *model() const;
208
209 QStringList groups() const;
210 void setGroups(const QStringList &groups);
211
212 bool isUnresolved() const;
213
214 void emitChanges();
215
216 void emitUnresolvedChanged() { Q_EMIT unresolvedChanged(); }
217
218Q_SIGNALS:
219 void groupsChanged();
220 void unresolvedChanged();
221
222private:
223 void setInGroup(QQmlListCompositor::Group group, bool inGroup);
224
225public:
226 QQmlDelegateModelItem *m_cacheItem;
227 int m_previousGroups;
228 int m_currentIndex[QQmlListCompositor::MaximumGroupCount];
229 int m_previousIndex[QQmlListCompositor::MaximumGroupCount];
230
231 friend class QQmlDelegateModelAttachedMetaObject;
232};
233
234QT_END_NAMESPACE
235
236QML_DECLARE_TYPE(QQmlDelegateModel)
237QML_DECLARE_TYPE(QQmlDelegateModelGroup)
238
239#endif // QQMLDATAMODEL_P_H
240

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