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 QQMLADAPTORMODEL_P_H
5#define QQMLADAPTORMODEL_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 <QtCore/qabstractitemmodel.h>
19
20#include <private/qtqmlglobal_p.h>
21#include <private/qqmllistaccessor_p.h>
22#include <private/qtqmlmodelsglobal_p.h>
23#include <private/qqmlguard_p.h>
24#include <private/qqmlnullablevalue_p.h>
25#include <private/qqmlpropertycache_p.h>
26
27QT_REQUIRE_CONFIG(qml_delegate_model);
28
29QT_BEGIN_NAMESPACE
30
31class QQmlEngine;
32
33class QQmlDelegateModel;
34class QQmlDelegateModelItem;
35class QQmlDelegateModelItemMetaType;
36
37class Q_QMLMODELS_PRIVATE_EXPORT QQmlAdaptorModel : public QQmlGuard<QObject>
38{
39public:
40 class Accessors
41 {
42 public:
43 inline Accessors() {}
44 virtual ~Accessors();
45 virtual int rowCount(const QQmlAdaptorModel &) const { return 0; }
46 virtual int columnCount(const QQmlAdaptorModel &) const { return 0; }
47 virtual void cleanup(QQmlAdaptorModel &) const {}
48
49 virtual QVariant value(const QQmlAdaptorModel &, int, const QString &) const {
50 return QVariant(); }
51
52 virtual QQmlDelegateModelItem *createItem(
53 QQmlAdaptorModel &,
54 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &,
55 int, int, int) { return nullptr; }
56
57 virtual bool notify(
58 const QQmlAdaptorModel &,
59 const QList<QQmlDelegateModelItem *> &,
60 int,
61 int,
62 const QVector<int> &) const { return false; }
63 virtual void replaceWatchedRoles(
64 QQmlAdaptorModel &,
65 const QList<QByteArray> &,
66 const QList<QByteArray> &) const {}
67 virtual QVariant parentModelIndex(const QQmlAdaptorModel &) const {
68 return QVariant(); }
69 virtual QVariant modelIndex(const QQmlAdaptorModel &, int) const {
70 return QVariant(); }
71 virtual bool canFetchMore(const QQmlAdaptorModel &) const { return false; }
72 virtual void fetchMore(QQmlAdaptorModel &) const {}
73
74 QScopedPointer<QMetaObject, QScopedPointerPodDeleter> metaObject;
75 QQmlPropertyCache::ConstPtr propertyCache;
76 };
77
78 Accessors *accessors;
79 QPersistentModelIndex rootIndex;
80 QQmlListAccessor list;
81 // we need to ensure that a JS created model does not get gced, but cannot
82 // arbitrarily set the parent (using QQmlStrongJSQObjectReference) of QObject based models,
83 // as that causes issues with singletons
84 QV4::PersistentValue modelStrongReference;
85
86 QTypeRevision modelItemRevision = QTypeRevision::zero();
87
88 QQmlAdaptorModel();
89 ~QQmlAdaptorModel();
90
91 inline QVariant model() const { return list.list(); }
92 void setModel(const QVariant &variant);
93 void invalidateModel();
94
95 bool isValid() const;
96 int count() const;
97 int rowCount() const;
98 int columnCount() const;
99 int rowAt(int index) const;
100 int columnAt(int index) const;
101 int indexAt(int row, int column) const;
102
103 void useImportVersion(QTypeRevision revision);
104
105 inline bool adaptsAim() const { return qobject_cast<QAbstractItemModel *>(object: object()); }
106 inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); }
107 inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); }
108
109 inline QVariant value(int index, const QString &role) const {
110 return accessors->value(*this, index, role); }
111 inline QQmlDelegateModelItem *createItem(
112 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType, int index)
113 {
114 return accessors->createItem(*this, metaType, index, rowAt(index), columnAt(index));
115 }
116 inline bool hasProxyObject() const {
117 return list.type() == QQmlListAccessor::Instance
118 || list.type() == QQmlListAccessor::ListProperty
119 || list.type() == QQmlListAccessor::ObjectList;
120 }
121
122 inline bool notify(
123 const QList<QQmlDelegateModelItem *> &items,
124 int index,
125 int count,
126 const QVector<int> &roles) const {
127 return accessors->notify(*this, items, index, count, roles); }
128 inline void replaceWatchedRoles(
129 const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) {
130 accessors->replaceWatchedRoles(*this, oldRoles, newRoles); }
131
132 inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); }
133 inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); }
134 inline bool canFetchMore() const { return accessors->canFetchMore(*this); }
135 inline void fetchMore() { return accessors->fetchMore(*this); }
136
137private:
138 static void objectDestroyedImpl(QQmlGuardImpl *);
139
140 Accessors m_nullAccessors;
141};
142
143class QQmlAdaptorModelProxyInterface
144{
145public:
146 virtual ~QQmlAdaptorModelProxyInterface() {}
147
148 virtual QObject *proxiedObject() = 0;
149};
150
151#define QQmlAdaptorModelProxyInterface_iid "org.qt-project.Qt.QQmlAdaptorModelProxyInterface"
152
153Q_DECLARE_INTERFACE(QQmlAdaptorModelProxyInterface, QQmlAdaptorModelProxyInterface_iid)
154
155QT_END_NAMESPACE
156
157#endif
158

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