Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmladaptormodel_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QQMLADAPTORMODEL_P_H
6#define QQMLADAPTORMODEL_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qabstractitemmodel.h>
20
21#include <private/qqmldelegatemodel_p.h>
22#include <private/qqmlguard_p.h>
23#include <private/qqmllistaccessor_p.h>
24#include <private/qqmlnullablevalue_p.h>
25#include <private/qqmlpropertycache_p.h>
26#include <private/qtqmlglobal_p.h>
27#include <private/qtqmlmodelsglobal_p.h>
28
30
31QT_BEGIN_NAMESPACE
32
33class QQmlEngine;
34
36class QQmlDelegateModelItemMetaType;
37
38class Q_QMLMODELS_EXPORT QQmlAdaptorModel : public QQmlGuard<QObject>
39{
40public:
41 class Accessors
42 {
43 public:
44 inline Accessors() {}
45 virtual ~Accessors();
46 virtual int rowCount(const QQmlAdaptorModel &) const { return 0; }
47 virtual int columnCount(const QQmlAdaptorModel &) const { return 0; }
48 virtual void cleanup(QQmlAdaptorModel &) const {}
49
50 virtual QVariant value(const QQmlAdaptorModel &, int, const QString &) const {
51 return QVariant(); }
52
53 virtual QQmlDelegateModelItem *createItem(
54 QQmlAdaptorModel &,
55 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &,
56 int, int, int) { return nullptr; }
57
58 virtual bool notify(
59 const QQmlAdaptorModel &,
60 const QList<QQmlDelegateModelItem *> &,
61 int,
62 int,
63 const QVector<int> &) const { return false; }
64 virtual void replaceWatchedRoles(
65 QQmlAdaptorModel &,
66 const QList<QByteArray> &,
67 const QList<QByteArray> &) const {}
68 virtual QVariant parentModelIndex(const QQmlAdaptorModel &) const {
69 return QVariant(); }
70 virtual QVariant modelIndex(const QQmlAdaptorModel &, int) const {
71 return QVariant(); }
72 virtual bool canFetchMore(const QQmlAdaptorModel &) const { return false; }
73 virtual void fetchMore(QQmlAdaptorModel &) const {}
74
75 QScopedPointer<QMetaObject, QScopedPointerPodDeleter> metaObject;
76 QQmlPropertyCache::ConstPtr propertyCache;
77 };
78
79 Accessors *accessors;
80 QPersistentModelIndex rootIndex;
81 QQmlListAccessor list;
82 // we need to ensure that a JS created model does not get gced, but cannot
83 // arbitrarily set the parent (using QQmlStrongJSQObjectReference) of QObject based models,
84 // as that causes issues with singletons
85 QV4::PersistentValue modelStrongReference;
86
87 QTypeRevision modelItemRevision = QTypeRevision::zero();
88 QQmlDelegateModel::DelegateModelAccess delegateModelAccess = QQmlDelegateModel::Qt5ReadWrite;
89
90 QQmlAdaptorModel();
91 ~QQmlAdaptorModel();
92
93 inline QVariant model() const { return list.list(); }
94 void setModel(const QVariant &variant);
95 void invalidateModel();
96
97 bool isValid() const;
98 int count() const;
99 int rowCount() const;
100 int columnCount() const;
101 int rowAt(int index) const;
102 int columnAt(int index) const;
103 int indexAt(int row, int column) const;
104
105 void useImportVersion(QTypeRevision revision);
106
107 inline bool adaptsAim() const { return qobject_cast<QAbstractItemModel *>(object()); }
108 inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); }
109 inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); }
110
111 inline QVariant value(int index, const QString &role) const {
112 return accessors->value(*this, index, role); }
113 inline QQmlDelegateModelItem *createItem(
114 const QQmlRefPointer<QQmlDelegateModelItemMetaType> &metaType, int index)
115 {
116 return accessors->createItem(*this, metaType, index, rowAt(index), columnAt(index));
117 }
118 inline bool hasProxyObject() const {
119 switch (list.type()) {
120 case QQmlListAccessor::Instance:
121 case QQmlListAccessor::ListProperty:
122 case QQmlListAccessor::ObjectList:
123 case QQmlListAccessor::ObjectSequence:
124 return true;
125 default:
126 break;
127 }
128 return false;
129 }
130
131 inline bool notify(
132 const QList<QQmlDelegateModelItem *> &items,
133 int index,
134 int count,
135 const QVector<int> &roles) const {
136 return accessors->notify(*this, items, index, count, roles); }
137 inline void replaceWatchedRoles(
138 const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) {
139 accessors->replaceWatchedRoles(*this, oldRoles, newRoles); }
140
141 inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); }
142 inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); }
143 inline bool canFetchMore() const { return accessors->canFetchMore(*this); }
144 inline void fetchMore() { return accessors->fetchMore(*this); }
145
146private:
147 static void objectDestroyedImpl(QQmlGuardImpl *);
148
149 Accessors m_nullAccessors;
150};
151
152QT_END_NAMESPACE
153
154#endif
QT_REQUIRE_CONFIG(qml_delegate_model)