| 1 | // Copyright (C) 2019 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 QQMLTABLEMODEL_P_H |
| 5 | #define QQMLTABLEMODEL_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 "qqmlabstractcolumnmodel_p.h" |
| 19 | |
| 20 | #include <QtCore/QObject> |
| 21 | #include <QtCore/QHash> |
| 22 | #include <QtCore/QAbstractTableModel> |
| 23 | #include <QtQml/qqml.h> |
| 24 | #include <QtQmlModels/private/qtqmlmodelsglobal_p.h> |
| 25 | #include <QtQml/QJSValue> |
| 26 | #include <QtQml/QQmlListProperty> |
| 27 | |
| 28 | QT_REQUIRE_CONFIG(qml_table_model); |
| 29 | |
| 30 | QT_BEGIN_NAMESPACE |
| 31 | |
| 32 | class Q_LABSQMLMODELS_EXPORT QQmlTableModel : public QQmlAbstractColumnModel |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged FINAL) |
| 36 | Q_PROPERTY(QVariant rows READ rows WRITE setRows NOTIFY rowsChanged FINAL) |
| 37 | QML_NAMED_ELEMENT(TableModel) |
| 38 | QML_ADDED_IN_VERSION(1, 0) |
| 39 | |
| 40 | public: |
| 41 | Q_DISABLE_COPY_MOVE(QQmlTableModel) |
| 42 | |
| 43 | explicit QQmlTableModel(QObject *parent = nullptr); |
| 44 | ~QQmlTableModel() override; |
| 45 | |
| 46 | QVariant rows() const; |
| 47 | void setRows(const QVariant &rows); |
| 48 | |
| 49 | Q_INVOKABLE void appendRow(const QVariant &row); |
| 50 | Q_INVOKABLE void clear(); |
| 51 | Q_INVOKABLE QVariant getRow(int rowIndex); |
| 52 | Q_INVOKABLE void insertRow(int rowIndex, const QVariant &row); |
| 53 | Q_INVOKABLE void moveRow(int fromRowIndex, int toRowIndex, int rows = 1); |
| 54 | Q_INVOKABLE void removeRow(int rowIndex, int rows = 1); |
| 55 | Q_INVOKABLE void setRow(int rowIndex, const QVariant &row); |
| 56 | |
| 57 | //AbstractItemModel interface |
| 58 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
| 59 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 60 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
| 61 | QModelIndex parent(const QModelIndex &index) const override; |
| 62 | |
| 63 | Q_SIGNALS: |
| 64 | void rowCountChanged(); |
| 65 | |
| 66 | protected: |
| 67 | QVariant firstRow() const override; |
| 68 | void setInitialRows() override; |
| 69 | |
| 70 | private: |
| 71 | |
| 72 | void setRowsPrivate(const QVariantList &rowsAsVariantList); |
| 73 | QVariant dataPrivate(const QModelIndex &index, const QString &roleName) const override; |
| 74 | void setDataPrivate(const QModelIndex &index, const QString &roleName, QVariant value) override; |
| 75 | |
| 76 | enum RowOption { |
| 77 | NeedsExisting, |
| 78 | CanAppend |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | bool validateRowIndex(QLatin1StringView functionName, QLatin1StringView argumentName, |
| 83 | int rowIndex, RowOption operation) const; |
| 84 | |
| 85 | void doInsert(int rowIndex, const QVariant &row); |
| 86 | |
| 87 | QVariantList mRows; |
| 88 | int mRowCount = 0; |
| 89 | }; |
| 90 | |
| 91 | QT_END_NAMESPACE |
| 92 | |
| 93 | #endif // QQMLTABLEMODEL_P_H |
| 94 | |