| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QQMLTABLEINSTANCEMODEL_P_H |
| 41 | #define QQMLTABLEINSTANCEMODEL_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtQmlModels/private/qqmldelegatemodel_p.h> |
| 55 | #include <QtQmlModels/private/qqmldelegatemodel_p_p.h> |
| 56 | |
| 57 | QT_REQUIRE_CONFIG(qml_table_model); |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | class QQmlTableInstanceModel; |
| 62 | class QQmlAbstractDelegateComponent; |
| 63 | |
| 64 | class QQmlTableInstanceModelIncubationTask : public QQDMIncubationTask |
| 65 | { |
| 66 | public: |
| 67 | QQmlTableInstanceModelIncubationTask( |
| 68 | QQmlTableInstanceModel *tableInstanceModel |
| 69 | , QQmlDelegateModelItem* modelItemToIncubate |
| 70 | , IncubationMode mode) |
| 71 | : QQDMIncubationTask(nullptr, mode) |
| 72 | , modelItemToIncubate(modelItemToIncubate) |
| 73 | , tableInstanceModel(tableInstanceModel) { |
| 74 | clear(); |
| 75 | } |
| 76 | |
| 77 | void statusChanged(Status status) override; |
| 78 | void setInitialState(QObject *object) override; |
| 79 | |
| 80 | QQmlDelegateModelItem *modelItemToIncubate = nullptr; |
| 81 | QQmlTableInstanceModel *tableInstanceModel = nullptr; |
| 82 | }; |
| 83 | |
| 84 | class Q_QMLMODELS_PRIVATE_EXPORT QQmlTableInstanceModel : public QQmlInstanceModel |
| 85 | { |
| 86 | Q_OBJECT |
| 87 | |
| 88 | public: |
| 89 | QQmlTableInstanceModel(QQmlContext *qmlContext, QObject *parent = nullptr); |
| 90 | ~QQmlTableInstanceModel() override; |
| 91 | |
| 92 | void useImportVersion(int minorVersion); |
| 93 | |
| 94 | int count() const override { return m_adaptorModel.count(); } |
| 95 | int rows() const { return m_adaptorModel.rowCount(); } |
| 96 | int columns() const { return m_adaptorModel.columnCount(); } |
| 97 | |
| 98 | bool isValid() const override { return true; } |
| 99 | |
| 100 | bool canFetchMore() const { return m_adaptorModel.canFetchMore(); } |
| 101 | void fetchMore() { m_adaptorModel.fetchMore(); } |
| 102 | |
| 103 | QVariant model() const; |
| 104 | void setModel(const QVariant &model); |
| 105 | |
| 106 | QQmlComponent *delegate() const; |
| 107 | void setDelegate(QQmlComponent *); |
| 108 | |
| 109 | const QAbstractItemModel *abstractItemModel() const override; |
| 110 | |
| 111 | QObject *object(int index, QQmlIncubator::IncubationMode incubationMode = QQmlIncubator::AsynchronousIfNested) override; |
| 112 | ReleaseFlags release(QObject *object, ReusableFlag reusable = NotReusable) override; |
| 113 | void dispose(QObject *object); |
| 114 | void cancel(int) override; |
| 115 | |
| 116 | void drainReusableItemsPool(int maxPoolTime) override; |
| 117 | int poolSize() override { return m_reusableItemsPool.size(); } |
| 118 | void reuseItem(QQmlDelegateModelItem *item, int newModelIndex); |
| 119 | |
| 120 | QQmlIncubator::Status incubationStatus(int index) override; |
| 121 | |
| 122 | QVariant variantValue(int, const QString &) override { Q_UNREACHABLE(); return QVariant(); } |
| 123 | void setWatchedRoles(const QList<QByteArray> &) override { Q_UNREACHABLE(); } |
| 124 | int indexOf(QObject *, QObject *) const override { Q_UNREACHABLE(); return 0; } |
| 125 | |
| 126 | private: |
| 127 | enum DestructionMode { |
| 128 | Deferred, |
| 129 | Immediate |
| 130 | }; |
| 131 | |
| 132 | QQmlComponent *resolveDelegate(int index); |
| 133 | |
| 134 | QQmlAdaptorModel m_adaptorModel; |
| 135 | QQmlAbstractDelegateComponent *m_delegateChooser = nullptr; |
| 136 | QQmlComponent *m_delegate = nullptr; |
| 137 | QPointer<QQmlContext> m_qmlContext; |
| 138 | QQmlRefPointer<QQmlDelegateModelItemMetaType> m_metaType; |
| 139 | |
| 140 | QHash<int, QQmlDelegateModelItem *> m_modelItems; |
| 141 | QQmlReusableDelegateModelItemsPool m_reusableItemsPool; |
| 142 | QList<QQmlIncubator *> m_finishedIncubationTasks; |
| 143 | |
| 144 | void incubateModelItem(QQmlDelegateModelItem *modelItem, QQmlIncubator::IncubationMode incubationMode); |
| 145 | void incubatorStatusChanged(QQmlTableInstanceModelIncubationTask *dmIncubationTask, QQmlIncubator::Status status); |
| 146 | void deleteIncubationTaskLater(QQmlIncubator *incubationTask); |
| 147 | void deleteAllFinishedIncubationTasks(); |
| 148 | QQmlDelegateModelItem *resolveModelItem(int index); |
| 149 | void destroyModelItem(QQmlDelegateModelItem *modelItem, DestructionMode mode); |
| 150 | |
| 151 | void dataChangedCallback(const QModelIndex &begin, const QModelIndex &end, const QVector<int> &roles); |
| 152 | |
| 153 | static bool isDoneIncubating(QQmlDelegateModelItem *modelItem); |
| 154 | static void deleteModelItemLater(QQmlDelegateModelItem *modelItem); |
| 155 | |
| 156 | friend class QQmlTableInstanceModelIncubationTask; |
| 157 | }; |
| 158 | |
| 159 | QT_END_NAMESPACE |
| 160 | |
| 161 | #endif // QQMLTABLEINSTANCEMODEL_P_H |
| 162 | |