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

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