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/qqmldelegatemodel_p.h>
21#include <private/qqmlguard_p.h>
22#include <private/qqmllistaccessor_p.h>
23#include <private/qqmlnullablevalue_p.h>
24#include <private/qqmlpropertycache_p.h>
25#include <private/qtqmlglobal_p.h>
26#include <private/qtqmlmodelsglobal_p.h>
27
28QT_REQUIRE_CONFIG(qml_delegate_model);
29
30QT_BEGIN_NAMESPACE
31
32class QQmlEngine;
33
34class QQmlDelegateModelItem;
35class QQmlDelegateModelItemMetaType;
36
37class Q_QMLMODELS_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 QQmlDelegateModel::DelegateModelAccess delegateModelAccess = QQmlDelegateModel::Qt5ReadWrite;
88
89 QQmlAdaptorModel();
90 ~QQmlAdaptorModel();
91
92 inline QVariant model() const { return list.list(); }
93 void setModel(const QVariant &variant);
94 void invalidateModel();
95
96 bool isValid() const;
97 int count() const;
98 int rowCount() const;
99 int columnCount() const;
100 int rowAt(int index) const;
101 int columnAt(int index) const;
102 int indexAt(int row, int column) const;
103
104 void useImportVersion(QTypeRevision revision);
105
106 inline bool adaptsAim() const { return qobject_cast<QAbstractItemModel *>(object: object()); }
107 inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); }
108 inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); }
109
110 inline QVariant value(int index, const QString &role) const {
111 return accessors->value(*this, index, role); }
112 inline QQmlDelegateModelItem *createItem(
113 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType, int index)
114 {
115 return accessors->createItem(*this, metaType, index, rowAt(index), columnAt(index));
116 }
117 inline bool hasProxyObject() const {
118 switch (list.type()) {
119 case QQmlListAccessor::Instance:
120 case QQmlListAccessor::ListProperty:
121 case QQmlListAccessor::ObjectList:
122 case QQmlListAccessor::ObjectSequence:
123 return true;
124 default:
125 break;
126 }
127 return false;
128 }
129
130 inline bool notify(
131 const QList<QQmlDelegateModelItem *> &items,
132 int index,
133 int count,
134 const QVector<int> &roles) const {
135 return accessors->notify(*this, items, index, count, roles); }
136 inline void replaceWatchedRoles(
137 const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) {
138 accessors->replaceWatchedRoles(*this, oldRoles, newRoles); }
139
140 inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); }
141 inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); }
142 inline bool canFetchMore() const { return accessors->canFetchMore(*this); }
143 inline void fetchMore() { return accessors->fetchMore(*this); }
144
145private:
146 static void objectDestroyedImpl(QQmlGuardImpl *);
147
148 Accessors m_nullAccessors;
149};
150
151QT_END_NAMESPACE
152
153#endif
154

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