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 QQMLADAPTORMODEL_P_H
41#define QQMLADAPTORMODEL_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 <QtCore/qabstractitemmodel.h>
55
56#include <private/qqmlglobal_p.h>
57#include <private/qqmllistaccessor_p.h>
58#include <private/qtqmlmodelsglobal_p.h>
59#include <private/qqmlguard_p.h>
60#include <private/qqmlnullablevalue_p.h>
61#include <private/qqmlpropertycache_p.h>
62
63QT_REQUIRE_CONFIG(qml_delegate_model);
64
65QT_BEGIN_NAMESPACE
66
67class QQmlEngine;
68
69class QQmlDelegateModel;
70class QQmlDelegateModelItem;
71class QQmlDelegateModelItemMetaType;
72
73class Q_QMLMODELS_PRIVATE_EXPORT QQmlAdaptorModel : public QQmlGuard<QObject>
74{
75public:
76 class Accessors
77 {
78 public:
79 inline Accessors() {}
80 virtual ~Accessors();
81 virtual int rowCount(const QQmlAdaptorModel &) const { return 0; }
82 virtual int columnCount(const QQmlAdaptorModel &) const { return 0; }
83 virtual void cleanup(QQmlAdaptorModel &) const {}
84
85 virtual QVariant value(const QQmlAdaptorModel &, int, const QString &) const {
86 return QVariant(); }
87
88 virtual QQmlDelegateModelItem *createItem(
89 QQmlAdaptorModel &,
90 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &,
91 int, int, int) const { return nullptr; }
92
93 virtual bool notify(
94 const QQmlAdaptorModel &,
95 const QList<QQmlDelegateModelItem *> &,
96 int,
97 int,
98 const QVector<int> &) const { return false; }
99 virtual void replaceWatchedRoles(
100 QQmlAdaptorModel &,
101 const QList<QByteArray> &,
102 const QList<QByteArray> &) const {}
103 virtual QVariant parentModelIndex(const QQmlAdaptorModel &) const {
104 return QVariant(); }
105 virtual QVariant modelIndex(const QQmlAdaptorModel &, int) const {
106 return QVariant(); }
107 virtual bool canFetchMore(const QQmlAdaptorModel &) const { return false; }
108 virtual void fetchMore(QQmlAdaptorModel &) const {}
109
110 QScopedPointer<QMetaObject, QScopedPointerPodDeleter> metaObject;
111 QQmlRefPointer<QQmlPropertyCache> propertyCache;
112 };
113
114 const Accessors *accessors;
115 QPersistentModelIndex rootIndex;
116 QQmlListAccessor list;
117 // we need to ensure that a JS created model does not get gced, but cannot
118 // arbitrarily set the parent (using QQmlStrongJSQObjectReference) of QObject based models,
119 // as that causes issues with singletons
120 QV4::PersistentValue modelStrongReference;
121
122 int modelItemRevision = 0;
123
124 QQmlAdaptorModel();
125 ~QQmlAdaptorModel();
126
127 inline QVariant model() const { return list.list(); }
128 void setModel(const QVariant &variant, QObject *parent, QQmlEngine *engine);
129 void invalidateModel();
130
131 bool isValid() const;
132 int count() const;
133 int rowCount() const;
134 int columnCount() const;
135 int rowAt(int index) const;
136 int columnAt(int index) const;
137 int indexAt(int row, int column) const;
138
139 void useImportVersion(int minorVersion);
140
141 inline bool adaptsAim() const { return qobject_cast<QAbstractItemModel *>(object: object()); }
142 inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); }
143 inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); }
144
145 inline QVariant value(int index, const QString &role) const {
146 return accessors->value(*this, index, role); }
147 inline QQmlDelegateModelItem *createItem(
148 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType, int index)
149 {
150 return accessors->createItem(*this, metaType, index, rowAt(index), columnAt(index));
151 }
152 inline bool hasProxyObject() const {
153 return list.type() == QQmlListAccessor::Instance
154 || list.type() == QQmlListAccessor::ListProperty
155 || list.type() == QQmlListAccessor::ObjectList;
156 }
157
158 inline bool notify(
159 const QList<QQmlDelegateModelItem *> &items,
160 int index,
161 int count,
162 const QVector<int> &roles) const {
163 return accessors->notify(*this, items, index, count, roles); }
164 inline void replaceWatchedRoles(
165 const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) {
166 accessors->replaceWatchedRoles(*this, oldRoles, newRoles); }
167
168 inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); }
169 inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); }
170 inline bool canFetchMore() const { return accessors->canFetchMore(*this); }
171 inline void fetchMore() { return accessors->fetchMore(*this); }
172
173protected:
174 void objectDestroyed(QObject *) override;
175};
176
177class QQmlAdaptorModelProxyInterface
178{
179public:
180 virtual ~QQmlAdaptorModelProxyInterface() {}
181
182 virtual QObject *proxiedObject() = 0;
183};
184
185#define QQmlAdaptorModelProxyInterface_iid "org.qt-project.Qt.QQmlAdaptorModelProxyInterface"
186
187Q_DECLARE_INTERFACE(QQmlAdaptorModelProxyInterface, QQmlAdaptorModelProxyInterface_iid)
188
189QT_END_NAMESPACE
190
191#endif
192

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