1// Copyright (C) 2018 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 QQMLTABLEINSTANCEMODEL_P_H
5#define QQMLTABLEINSTANCEMODEL_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 <QtQmlModels/private/qqmldelegatemodel_p.h>
19#include <QtQmlModels/private/qqmldelegatemodel_p_p.h>
20
21#include <QtCore/qpointer.h>
22
23QT_REQUIRE_CONFIG(qml_table_model);
24
25QT_BEGIN_NAMESPACE
26
27class QQmlTableInstanceModel;
28class QQmlAbstractDelegateComponent;
29
30class QQmlTableInstanceModelIncubationTask : public QQDMIncubationTask
31{
32public:
33 QQmlTableInstanceModelIncubationTask(
34 QQmlTableInstanceModel *tableInstanceModel
35 , QQmlDelegateModelItem* modelItemToIncubate
36 , IncubationMode mode)
37 : QQDMIncubationTask(nullptr, mode)
38 , modelItemToIncubate(modelItemToIncubate)
39 , tableInstanceModel(tableInstanceModel) {
40 clear();
41 }
42
43 void statusChanged(Status status) override;
44 void setInitialState(QObject *object) override;
45
46 QQmlDelegateModelItem *modelItemToIncubate = nullptr;
47 QQmlTableInstanceModel *tableInstanceModel = nullptr;
48};
49
50class Q_QMLMODELS_EXPORT QQmlTableInstanceModel : public QQmlInstanceModel
51{
52 Q_OBJECT
53
54public:
55 QQmlTableInstanceModel(QQmlContext *qmlContext, QObject *parent = nullptr);
56 ~QQmlTableInstanceModel() override;
57
58 void useImportVersion(QTypeRevision version);
59
60 int count() const override { return m_adaptorModel.count(); }
61 int rows() const { return m_adaptorModel.rowCount(); }
62 int columns() const { return m_adaptorModel.columnCount(); }
63
64 bool isValid() const override { return true; }
65
66 bool canFetchMore() const { return m_adaptorModel.canFetchMore(); }
67 void fetchMore() { m_adaptorModel.fetchMore(); }
68
69 QVariant model() const;
70 void setModel(const QVariant &model);
71
72 QQmlComponent *delegate() const;
73 void setDelegate(QQmlComponent *);
74
75 QQmlDelegateModel::DelegateModelAccess delegateModelAccess() const
76 {
77 return m_adaptorModel.delegateModelAccess;
78 }
79 void setDelegateModelAccess(QQmlDelegateModel::DelegateModelAccess delegateModelAccess)
80 {
81 m_adaptorModel.delegateModelAccess = delegateModelAccess;
82 }
83
84 const QAbstractItemModel *abstractItemModel() const override;
85
86 QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override;
87 ReleaseFlags release(QObject *object, ReusableFlag reusable = NotReusable) override;
88 void dispose(QObject *object);
89 void cancel(int) override;
90
91 void drainReusableItemsPool(int maxPoolTime) override;
92 int poolSize() override { return m_reusableItemsPool.size(); }
93 void reuseItem(QQmlDelegateModelItem *item, int newModelIndex);
94
95 QQmlIncubator::Status incubationStatus(int index) override;
96
97 bool setRequiredProperty(int index, const QString &name, const QVariant &value) final;
98
99 QVariant variantValue(int, const QString &) override { Q_UNREACHABLE_RETURN(QVariant()); }
100 void setWatchedRoles(const QList<QByteArray> &) override { Q_UNREACHABLE(); }
101 int indexOf(QObject *, QObject *) const override { Q_UNREACHABLE_RETURN(0); }
102
103 QQmlDelegateModelItem *getModelItem(int index);
104
105private:
106 enum DestructionMode {
107 Deferred,
108 Immediate
109 };
110
111 QQmlComponent *resolveDelegate(int index);
112
113 QQmlAdaptorModel m_adaptorModel;
114 QQmlAbstractDelegateComponent *m_delegateChooser = nullptr;
115 QQmlComponent *m_delegate = nullptr;
116 QPointer<QQmlContext> m_qmlContext;
117 QQmlRefPointer<QQmlDelegateModelItemMetaType> m_metaType;
118
119 QHash<int, QQmlDelegateModelItem *> m_modelItems;
120 QQmlReusableDelegateModelItemsPool m_reusableItemsPool;
121 QList<QQmlIncubator *> m_finishedIncubationTasks;
122
123 void incubateModelItem(QQmlDelegateModelItem *modelItem, QQmlIncubator::IncubationMode incubationMode);
124 void incubatorStatusChanged(QQmlTableInstanceModelIncubationTask *dmIncubationTask, QQmlIncubator::Status status);
125 void deleteIncubationTaskLater(QQmlIncubator *incubationTask);
126 void deleteAllFinishedIncubationTasks();
127 QQmlDelegateModelItem *resolveModelItem(int index);
128 void destroyModelItem(QQmlDelegateModelItem *modelItem, DestructionMode mode);
129
130 void dataChangedCallback(const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles);
131 void modelAboutToBeResetCallback();
132
133 static bool isDoneIncubating(QQmlDelegateModelItem *modelItem);
134 static void deleteModelItemLater(QQmlDelegateModelItem *modelItem);
135
136 friend class QQmlTableInstanceModelIncubationTask;
137};
138
139QT_END_NAMESPACE
140
141#endif // QQMLTABLEINSTANCEMODEL_P_H
142

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