1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLDATAMODEL_P_H
41#define QQMLDATAMODEL_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <private/qtqmlmodelsglobal_p.h>
55#include <private/qqmllistcompositor_p.h>
56#include <private/qqmlobjectmodel_p.h>
57#include <private/qqmlincubator_p.h>
58
59#include <QtCore/qabstractitemmodel.h>
60#include <QtCore/qstringlist.h>
61
62QT_REQUIRE_CONFIG(qml_delegate_model);
63
64QT_BEGIN_NAMESPACE
65
66class QQmlChangeSet;
67class QQuickPackage;
68class QQmlV4Function;
69class QQmlDelegateModelGroup;
70class QQmlDelegateModelAttached;
71class QQmlDelegateModelPrivate;
72
73
74class Q_QMLMODELS_PRIVATE_EXPORT QQmlDelegateModel : public QQmlInstanceModel, public QQmlParserStatus
75{
76 Q_OBJECT
77 Q_DECLARE_PRIVATE(QQmlDelegateModel)
78
79 Q_PROPERTY(QVariant model READ model WRITE setModel)
80 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
81 Q_PROPERTY(QString filterOnGroup READ filterGroup WRITE setFilterGroup NOTIFY filterGroupChanged RESET resetFilterGroup)
82 Q_PROPERTY(QQmlDelegateModelGroup *items READ items CONSTANT) //TODO : worth renaming?
83 Q_PROPERTY(QQmlDelegateModelGroup *persistedItems READ persistedItems CONSTANT)
84 Q_PROPERTY(QQmlListProperty<QQmlDelegateModelGroup> groups READ groups CONSTANT)
85 Q_PROPERTY(QObject *parts READ parts CONSTANT)
86 Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
87 Q_CLASSINFO("DefaultProperty", "delegate")
88 QML_NAMED_ELEMENT(DelegateModel)
89 QML_ADDED_IN_MINOR_VERSION(1)
90 QML_ATTACHED(QQmlDelegateModelAttached)
91 Q_INTERFACES(QQmlParserStatus)
92
93public:
94 QQmlDelegateModel();
95 QQmlDelegateModel(QQmlContext *, QObject *parent=nullptr);
96 ~QQmlDelegateModel();
97
98 void classBegin() override;
99 void componentComplete() override;
100
101 QVariant model() const;
102 void setModel(const QVariant &);
103
104 QQmlComponent *delegate() const;
105 void setDelegate(QQmlComponent *);
106
107 QVariant rootIndex() const;
108 void setRootIndex(const QVariant &root);
109
110 Q_INVOKABLE QVariant modelIndex(int idx) const;
111 Q_INVOKABLE QVariant parentModelIndex() const;
112
113 int count() const override;
114 bool isValid() const override { return delegate() != nullptr; }
115 QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override;
116 ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) override;
117 void cancel(int index) override;
118 QVariant variantValue(int index, const QString &role) override;
119 void setWatchedRoles(const QList<QByteArray> &roles) override;
120 QQmlIncubator::Status incubationStatus(int index) override;
121
122 void drainReusableItemsPool(int maxPoolTime) override;
123 int poolSize() override;
124
125 int indexOf(QObject *object, QObject *objectContext) const override;
126
127 QString filterGroup() const;
128 void setFilterGroup(const QString &group);
129 void resetFilterGroup();
130
131 QQmlDelegateModelGroup *items();
132 QQmlDelegateModelGroup *persistedItems();
133 QQmlListProperty<QQmlDelegateModelGroup> groups();
134 QObject *parts();
135
136 const QAbstractItemModel *abstractItemModel() const override;
137
138 bool event(QEvent *) override;
139
140 static QQmlDelegateModelAttached *qmlAttachedProperties(QObject *obj);
141
142Q_SIGNALS:
143 void filterGroupChanged();
144 void defaultGroupsChanged();
145 void rootIndexChanged();
146 void delegateChanged();
147
148private Q_SLOTS:
149 void _q_itemsChanged(int index, int count, const QVector<int> &roles);
150 void _q_itemsInserted(int index, int count);
151 void _q_itemsRemoved(int index, int count);
152 void _q_itemsMoved(int from, int to, int count);
153 void _q_modelReset();
154 void _q_rowsInserted(const QModelIndex &,int,int);
155 void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int begin, int end);
156 void _q_rowsRemoved(const QModelIndex &,int,int);
157 void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
158 void _q_dataChanged(const QModelIndex&,const QModelIndex&,const QVector<int> &);
159 void _q_layoutChanged(const QList<QPersistentModelIndex>&, QAbstractItemModel::LayoutChangeHint);
160
161private:
162 bool isDescendantOf(const QPersistentModelIndex &desc, const QList<QPersistentModelIndex> &parents) const;
163
164 Q_DISABLE_COPY(QQmlDelegateModel)
165};
166
167class QQmlDelegateModelGroupPrivate;
168class Q_QMLMODELS_PRIVATE_EXPORT QQmlDelegateModelGroup : public QObject
169{
170 Q_OBJECT
171 Q_PROPERTY(int count READ count NOTIFY countChanged)
172 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
173 Q_PROPERTY(bool includeByDefault READ defaultInclude WRITE setDefaultInclude NOTIFY defaultIncludeChanged)
174 QML_NAMED_ELEMENT(DelegateModelGroup)
175 QML_ADDED_IN_MINOR_VERSION(1)
176public:
177 QQmlDelegateModelGroup(QObject *parent = nullptr);
178 QQmlDelegateModelGroup(const QString &name, QQmlDelegateModel *model, int compositorType, QObject *parent = nullptr);
179 ~QQmlDelegateModelGroup();
180
181 QString name() const;
182 void setName(const QString &name);
183
184 int count() const;
185
186 bool defaultInclude() const;
187 void setDefaultInclude(bool include);
188
189 Q_INVOKABLE QJSValue get(int index);
190
191public Q_SLOTS:
192 void insert(QQmlV4Function *);
193 void create(QQmlV4Function *);
194 void resolve(QQmlV4Function *);
195 void remove(QQmlV4Function *);
196 void addGroups(QQmlV4Function *);
197 void removeGroups(QQmlV4Function *);
198 void setGroups(QQmlV4Function *);
199 void move(QQmlV4Function *);
200
201Q_SIGNALS:
202 void countChanged();
203 void nameChanged();
204 void defaultIncludeChanged();
205 void changed(const QJSValue &removed, const QJSValue &inserted);
206private:
207 Q_DECLARE_PRIVATE(QQmlDelegateModelGroup)
208};
209
210class QQmlDelegateModelItem;
211class QQmlDelegateModelAttachedMetaObject;
212class QQmlDelegateModelAttached : public QObject
213{
214 Q_OBJECT
215 Q_PROPERTY(QQmlDelegateModel *model READ model CONSTANT)
216 Q_PROPERTY(QStringList groups READ groups WRITE setGroups NOTIFY groupsChanged)
217 Q_PROPERTY(bool isUnresolved READ isUnresolved NOTIFY unresolvedChanged)
218public:
219 QQmlDelegateModelAttached(QObject *parent);
220 QQmlDelegateModelAttached(QQmlDelegateModelItem *cacheItem, QObject *parent);
221 ~QQmlDelegateModelAttached() {}
222
223 void resetCurrentIndex();
224 void setCacheItem(QQmlDelegateModelItem *item);
225
226 QQmlDelegateModel *model() const;
227
228 QStringList groups() const;
229 void setGroups(const QStringList &groups);
230
231 bool isUnresolved() const;
232
233 void emitChanges();
234
235 void emitUnresolvedChanged() { Q_EMIT unresolvedChanged(); }
236
237Q_SIGNALS:
238 void groupsChanged();
239 void unresolvedChanged();
240
241public:
242 QQmlDelegateModelItem *m_cacheItem;
243 int m_previousGroups;
244 int m_currentIndex[QQmlListCompositor::MaximumGroupCount];
245 int m_previousIndex[QQmlListCompositor::MaximumGroupCount];
246
247 friend class QQmlDelegateModelAttachedMetaObject;
248};
249
250QT_END_NAMESPACE
251
252QML_DECLARE_TYPE(QQmlDelegateModel)
253QML_DECLARE_TYPE(QQmlDelegateModelGroup)
254
255#endif // QQMLDATAMODEL_P_H
256

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