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 
63 QT_REQUIRE_CONFIG(qml_delegate_model);
64 
65 QT_BEGIN_NAMESPACE
66 
67 class QQmlEngine;
68 
69 class QQmlDelegateModel;
70 class QQmlDelegateModelItem;
71 class QQmlDelegateModelItemMetaType;
72 
73 class Q_QMLMODELS_PRIVATE_EXPORT QQmlAdaptorModel : public QQmlStrongJSQObjectReference<QObject>
74 {
75 public:
76     class Accessors
77     {
78     public:
Accessors()79         inline Accessors() {}
80         virtual ~Accessors();
rowCount(const QQmlAdaptorModel &)81         virtual int rowCount(const QQmlAdaptorModel &) const { return 0; }
columnCount(const QQmlAdaptorModel &)82         virtual int columnCount(const QQmlAdaptorModel &) const { return 0; }
cleanup(QQmlAdaptorModel &)83         virtual void cleanup(QQmlAdaptorModel &) const {}
84 
value(const QQmlAdaptorModel &,int,const QString &)85         virtual QVariant value(const QQmlAdaptorModel &, int, const QString &) const {
86             return QVariant(); }
87 
createItem(QQmlAdaptorModel &,const QQmlRefPointer<QQmlDelegateModelItemMetaType> &,int,int,int)88         virtual QQmlDelegateModelItem *createItem(
89                 QQmlAdaptorModel &,
90                 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &,
91                 int, int, int) const { return nullptr; }
92 
notify(const QQmlAdaptorModel &,const QList<QQmlDelegateModelItem * > &,int,int,const QVector<int> &)93         virtual bool notify(
94                 const QQmlAdaptorModel &,
95                 const QList<QQmlDelegateModelItem *> &,
96                 int,
97                 int,
98                 const QVector<int> &) const { return false; }
replaceWatchedRoles(QQmlAdaptorModel &,const QList<QByteArray> &,const QList<QByteArray> &)99         virtual void replaceWatchedRoles(
100                 QQmlAdaptorModel &,
101                 const QList<QByteArray> &,
102                 const QList<QByteArray> &) const {}
parentModelIndex(const QQmlAdaptorModel &)103         virtual QVariant parentModelIndex(const QQmlAdaptorModel &) const {
104             return QVariant(); }
modelIndex(const QQmlAdaptorModel &,int)105         virtual QVariant modelIndex(const QQmlAdaptorModel &, int) const {
106             return QVariant(); }
canFetchMore(const QQmlAdaptorModel &)107         virtual bool canFetchMore(const QQmlAdaptorModel &) const { return false; }
fetchMore(QQmlAdaptorModel &)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 
118     int modelItemRevision = 0;
119 
120     QQmlAdaptorModel();
121     ~QQmlAdaptorModel();
122 
model()123     inline QVariant model() const { return list.list(); }
124     void setModel(const QVariant &variant, QObject *parent, QQmlEngine *engine);
125     void invalidateModel();
126 
127     bool isValid() const;
128     int count() const;
129     int rowCount() const;
130     int columnCount() const;
131     int rowAt(int index) const;
132     int columnAt(int index) const;
133     int indexAt(int row, int column) const;
134 
135     void useImportVersion(int minorVersion);
136 
adaptsAim()137     inline bool adaptsAim() const { return qobject_cast<QAbstractItemModel *>(object()); }
aim()138     inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); }
aim()139     inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); }
140 
value(int index,const QString & role)141     inline QVariant value(int index, const QString &role) const {
142         return accessors->value(*this, index, role); }
createItem(const QQmlRefPointer<QQmlDelegateModelItemMetaType> & metaType,int index)143     inline QQmlDelegateModelItem *createItem(
144             const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType, int index)
145     {
146         return accessors->createItem(*this, metaType, index, rowAt(index), columnAt(index));
147     }
hasProxyObject()148     inline bool hasProxyObject() const {
149         return list.type() == QQmlListAccessor::Instance
150                 || list.type() == QQmlListAccessor::ListProperty
151                 || list.type() == QQmlListAccessor::ObjectList;
152     }
153 
notify(const QList<QQmlDelegateModelItem * > & items,int index,int count,const QVector<int> & roles)154     inline bool notify(
155             const QList<QQmlDelegateModelItem *> &items,
156             int index,
157             int count,
158             const QVector<int> &roles) const {
159         return accessors->notify(*this, items, index, count, roles); }
replaceWatchedRoles(const QList<QByteArray> & oldRoles,const QList<QByteArray> & newRoles)160     inline void replaceWatchedRoles(
161             const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) {
162         accessors->replaceWatchedRoles(*this, oldRoles, newRoles); }
163 
modelIndex(int index)164     inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); }
parentModelIndex()165     inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); }
canFetchMore()166     inline bool canFetchMore() const { return accessors->canFetchMore(*this); }
fetchMore()167     inline void fetchMore() { return accessors->fetchMore(*this); }
168 
169 protected:
170     void objectDestroyed(QObject *) override;
171 };
172 
173 class QQmlAdaptorModelProxyInterface
174 {
175 public:
~QQmlAdaptorModelProxyInterface()176     virtual ~QQmlAdaptorModelProxyInterface() {}
177 
178     virtual QObject *proxiedObject() = 0;
179 };
180 
181 #define QQmlAdaptorModelProxyInterface_iid "org.qt-project.Qt.QQmlAdaptorModelProxyInterface"
182 
183 Q_DECLARE_INTERFACE(QQmlAdaptorModelProxyInterface, QQmlAdaptorModelProxyInterface_iid)
184 
185 QT_END_NAMESPACE
186 
187 #endif
188