| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2019 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 QQMLTABLEMODEL_P_H |
| 41 | #define QQMLTABLEMODEL_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 "qqmltablemodelcolumn_p.h" |
| 55 | |
| 56 | #include <QtCore/QObject> |
| 57 | #include <QtCore/QAbstractTableModel> |
| 58 | #include <QtQml/qqml.h> |
| 59 | #include <QtQmlModels/private/qtqmlmodelsglobal_p.h> |
| 60 | #include <QtQml/QJSValue> |
| 61 | #include <QtQml/QQmlListProperty> |
| 62 | |
| 63 | QT_REQUIRE_CONFIG(qml_table_model); |
| 64 | |
| 65 | QT_BEGIN_NAMESPACE |
| 66 | |
| 67 | class QQmlTableModel : public QAbstractTableModel, public QQmlParserStatus |
| 68 | { |
| 69 | Q_OBJECT |
| 70 | Q_PROPERTY(int columnCount READ columnCount NOTIFY columnCountChanged FINAL) |
| 71 | Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged FINAL) |
| 72 | Q_PROPERTY(QVariant rows READ rows WRITE setRows NOTIFY rowsChanged FINAL) |
| 73 | Q_PROPERTY(QQmlListProperty<QQmlTableModelColumn> columns READ columns CONSTANT FINAL) |
| 74 | Q_INTERFACES(QQmlParserStatus) |
| 75 | Q_CLASSINFO("DefaultProperty" , "columns" ) |
| 76 | QML_NAMED_ELEMENT(TableModel) |
| 77 | |
| 78 | public: |
| 79 | QQmlTableModel(QObject *parent = nullptr); |
| 80 | ~QQmlTableModel() override; |
| 81 | |
| 82 | QVariant rows() const; |
| 83 | void setRows(const QVariant &rows); |
| 84 | |
| 85 | Q_INVOKABLE void appendRow(const QVariant &row); |
| 86 | Q_INVOKABLE void clear(); |
| 87 | Q_INVOKABLE QVariant getRow(int rowIndex); |
| 88 | Q_INVOKABLE void insertRow(int rowIndex, const QVariant &row); |
| 89 | Q_INVOKABLE void moveRow(int fromRowIndex, int toRowIndex, int rows = 1); |
| 90 | Q_INVOKABLE void removeRow(int rowIndex, int rows = 1); |
| 91 | Q_INVOKABLE void setRow(int rowIndex, const QVariant &row); |
| 92 | |
| 93 | QQmlListProperty<QQmlTableModelColumn> columns(); |
| 94 | |
| 95 | static void columns_append(QQmlListProperty<QQmlTableModelColumn> *property, QQmlTableModelColumn *value); |
| 96 | static int columns_count(QQmlListProperty<QQmlTableModelColumn> *property); |
| 97 | static QQmlTableModelColumn *columns_at(QQmlListProperty<QQmlTableModelColumn> *property, int index); |
| 98 | static void columns_clear(QQmlListProperty<QQmlTableModelColumn> *property); |
| 99 | static void columns_replace(QQmlListProperty<QQmlTableModelColumn> *property, int index, QQmlTableModelColumn *value); |
| 100 | static void columns_removeLast(QQmlListProperty<QQmlTableModelColumn> *property); |
| 101 | |
| 102 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
| 103 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 104 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
| 105 | Q_INVOKABLE QVariant data(const QModelIndex &index, const QString &role) const; |
| 106 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 107 | Q_INVOKABLE bool setData(const QModelIndex &index, const QString &role, const QVariant &value); |
| 108 | bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override; |
| 109 | QHash<int, QByteArray> roleNames() const override; |
| 110 | |
| 111 | Q_SIGNALS: |
| 112 | void columnCountChanged(); |
| 113 | void rowCountChanged(); |
| 114 | void rowsChanged(); |
| 115 | |
| 116 | private: |
| 117 | class ColumnRoleMetadata |
| 118 | { |
| 119 | public: |
| 120 | ColumnRoleMetadata(); |
| 121 | ColumnRoleMetadata(bool isStringRole, const QString &name, int type, const QString &typeName); |
| 122 | |
| 123 | bool isValid() const; |
| 124 | |
| 125 | // If this is false, it's a function role. |
| 126 | bool isStringRole = false; |
| 127 | QString name; |
| 128 | int type = QMetaType::UnknownType; |
| 129 | QString typeName; |
| 130 | }; |
| 131 | |
| 132 | struct ColumnMetadata |
| 133 | { |
| 134 | // Key = role name that will be made visible to the delegate |
| 135 | // Value = metadata about that role, including actual name in the model data, type, etc. |
| 136 | QHash<QString, ColumnRoleMetadata> roles; |
| 137 | }; |
| 138 | |
| 139 | enum NewRowOperationFlag { |
| 140 | OtherOperation, // insert(), set(), etc. |
| 141 | SetRowsOperation, |
| 142 | AppendOperation |
| 143 | }; |
| 144 | |
| 145 | void doSetRows(const QVariantList &rowsAsVariantList); |
| 146 | ColumnRoleMetadata fetchColumnRoleData(const QString &roleNameKey, |
| 147 | QQmlTableModelColumn *tableModelColumn, int columnIndex) const; |
| 148 | void fetchColumnMetadata(); |
| 149 | |
| 150 | bool validateRowType(const char *functionName, const QVariant &row) const; |
| 151 | bool validateNewRow(const char *functionName, const QVariant &row, |
| 152 | int rowIndex, NewRowOperationFlag operation = OtherOperation) const; |
| 153 | bool validateRowIndex(const char *functionName, const char *argumentName, int rowIndex) const; |
| 154 | |
| 155 | void doInsert(int rowIndex, const QVariant &row); |
| 156 | |
| 157 | void classBegin() override; |
| 158 | void componentComplete() override; |
| 159 | |
| 160 | bool componentCompleted = false; |
| 161 | QVariantList mRows; |
| 162 | QList<QQmlTableModelColumn *> mColumns; |
| 163 | int mRowCount = 0; |
| 164 | int mColumnCount = 0; |
| 165 | // Each entry contains information about the properties of the column at that index. |
| 166 | QVector<ColumnMetadata> mColumnMetadata; |
| 167 | // key = property index (0 to number of properties across all columns) |
| 168 | // value = role name |
| 169 | QHash<int, QByteArray> mRoleNames; |
| 170 | }; |
| 171 | |
| 172 | QT_END_NAMESPACE |
| 173 | |
| 174 | QML_DECLARE_TYPE(QQmlTableModel) |
| 175 | |
| 176 | #endif // QQMLTABLEMODEL_P_H |
| 177 | |