| 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 | |
| 23 | QT_REQUIRE_CONFIG(qml_table_model); |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QQmlTableInstanceModel; |
| 28 | class QQmlAbstractDelegateComponent; |
| 29 | |
| 30 | class QQmlTableInstanceModelIncubationTask : public QQDMIncubationTask |
| 31 | { |
| 32 | public: |
| 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 | |
| 50 | class Q_QMLMODELS_EXPORT QQmlTableInstanceModel : public QQmlInstanceModel |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | |
| 54 | public: |
| 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 | const QAbstractItemModel *abstractItemModel() const override; |
| 76 | |
| 77 | QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override; |
| 78 | ReleaseFlags release(QObject *object, ReusableFlag reusable = NotReusable) override; |
| 79 | void dispose(QObject *object); |
| 80 | void cancel(int) override; |
| 81 | |
| 82 | void drainReusableItemsPool(int maxPoolTime) override; |
| 83 | int poolSize() override { return m_reusableItemsPool.size(); } |
| 84 | void reuseItem(QQmlDelegateModelItem *item, int newModelIndex); |
| 85 | |
| 86 | QQmlIncubator::Status incubationStatus(int index) override; |
| 87 | |
| 88 | bool setRequiredProperty(int index, const QString &name, const QVariant &value) final; |
| 89 | |
| 90 | QVariant variantValue(int, const QString &) override { Q_UNREACHABLE_RETURN(QVariant()); } |
| 91 | void setWatchedRoles(const QList<QByteArray> &) override { Q_UNREACHABLE(); } |
| 92 | int indexOf(QObject *, QObject *) const override { Q_UNREACHABLE_RETURN(0); } |
| 93 | |
| 94 | private: |
| 95 | enum DestructionMode { |
| 96 | Deferred, |
| 97 | Immediate |
| 98 | }; |
| 99 | |
| 100 | QQmlComponent *resolveDelegate(int index); |
| 101 | |
| 102 | QQmlAdaptorModel m_adaptorModel; |
| 103 | QQmlAbstractDelegateComponent *m_delegateChooser = nullptr; |
| 104 | QQmlComponent *m_delegate = nullptr; |
| 105 | QPointer<QQmlContext> m_qmlContext; |
| 106 | QQmlRefPointer<QQmlDelegateModelItemMetaType> m_metaType; |
| 107 | |
| 108 | QHash<int, QQmlDelegateModelItem *> m_modelItems; |
| 109 | QQmlReusableDelegateModelItemsPool m_reusableItemsPool; |
| 110 | QList<QQmlIncubator *> m_finishedIncubationTasks; |
| 111 | |
| 112 | void incubateModelItem(QQmlDelegateModelItem *modelItem, QQmlIncubator::IncubationMode incubationMode); |
| 113 | void incubatorStatusChanged(QQmlTableInstanceModelIncubationTask *dmIncubationTask, QQmlIncubator::Status status); |
| 114 | void deleteIncubationTaskLater(QQmlIncubator *incubationTask); |
| 115 | void deleteAllFinishedIncubationTasks(); |
| 116 | QQmlDelegateModelItem *resolveModelItem(int index); |
| 117 | void destroyModelItem(QQmlDelegateModelItem *modelItem, DestructionMode mode); |
| 118 | |
| 119 | void dataChangedCallback(const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles); |
| 120 | void modelAboutToBeResetCallback(); |
| 121 | |
| 122 | static bool isDoneIncubating(QQmlDelegateModelItem *modelItem); |
| 123 | static void deleteModelItemLater(QQmlDelegateModelItem *modelItem); |
| 124 | |
| 125 | friend class QQmlTableInstanceModelIncubationTask; |
| 126 | }; |
| 127 | |
| 128 | QT_END_NAMESPACE |
| 129 | |
| 130 | #endif // QQMLTABLEINSTANCEMODEL_P_H |
| 131 | |