| 1 | // Copyright (C) 2025 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 QQMLABSTRACTCOLUMNMODEL_P_H |
| 5 | #define QQMLABSTRACTCOLUMNMODEL_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 "qqmltablemodelcolumn_p.h" |
| 19 | |
| 20 | #include <QtCore/qabstractitemmodel.h> |
| 21 | #include <QtCore/qvariant.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class Q_LABSQMLMODELS_EXPORT QQmlAbstractColumnModel : public QAbstractItemModel, public QQmlParserStatus |
| 26 | { |
| 27 | Q_OBJECT |
| 28 | QML_ANONYMOUS |
| 29 | Q_PROPERTY(int columnCount READ columnCount NOTIFY columnCountChanged FINAL) |
| 30 | Q_PROPERTY(QQmlListProperty<QQmlTableModelColumn> columns READ columns CONSTANT FINAL) |
| 31 | Q_INTERFACES(QQmlParserStatus) |
| 32 | Q_CLASSINFO("DefaultProperty" , "columns" ) |
| 33 | |
| 34 | public: |
| 35 | Q_DISABLE_COPY_MOVE(QQmlAbstractColumnModel) |
| 36 | |
| 37 | explicit QQmlAbstractColumnModel(QObject *parent = nullptr); |
| 38 | ~QQmlAbstractColumnModel() override = default; |
| 39 | |
| 40 | QQmlListProperty<QQmlTableModelColumn> columns(); |
| 41 | |
| 42 | static void columns_append(QQmlListProperty<QQmlTableModelColumn> *property, QQmlTableModelColumn *value); |
| 43 | static qsizetype columns_count(QQmlListProperty<QQmlTableModelColumn> *property); |
| 44 | static QQmlTableModelColumn *columns_at(QQmlListProperty<QQmlTableModelColumn> *property, qsizetype index); |
| 45 | static void columns_clear(QQmlListProperty<QQmlTableModelColumn> *property); |
| 46 | static void columns_replace(QQmlListProperty<QQmlTableModelColumn> *property, qsizetype index, QQmlTableModelColumn *value); |
| 47 | static void columns_removeLast(QQmlListProperty<QQmlTableModelColumn> *property); |
| 48 | |
| 49 | Q_INVOKABLE QVariant data(const QModelIndex &index, const QString &role) const; |
| 50 | Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 51 | Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, const QString &role); |
| 52 | Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override; |
| 53 | |
| 54 | QHash<int, QByteArray> roleNames() const override; |
| 55 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 56 | |
| 57 | Q_SIGNALS: |
| 58 | void columnCountChanged(); |
| 59 | void rowsChanged(); |
| 60 | |
| 61 | protected: |
| 62 | void classBegin() override; |
| 63 | void componentComplete() override; |
| 64 | |
| 65 | virtual QVariant firstRow() const = 0; |
| 66 | virtual void setInitialRows() = 0; |
| 67 | |
| 68 | virtual QVariant dataPrivate(const QModelIndex &index, const QString &roleName) const = 0; |
| 69 | virtual void setDataPrivate(const QModelIndex &index, const QString &roleName, QVariant value) = 0; |
| 70 | |
| 71 | enum class ColumnRole : quint8 |
| 72 | { |
| 73 | StringRole, |
| 74 | FunctionRole |
| 75 | }; |
| 76 | |
| 77 | class ColumnRoleMetadata |
| 78 | { |
| 79 | public: |
| 80 | ColumnRoleMetadata(); |
| 81 | ColumnRoleMetadata(ColumnRole role, QString name, int type, QString typeName); |
| 82 | |
| 83 | bool isValid() const; |
| 84 | |
| 85 | ColumnRole columnRole = ColumnRole::FunctionRole; |
| 86 | QString name; |
| 87 | int type = QMetaType::UnknownType; |
| 88 | QString typeName; |
| 89 | }; |
| 90 | |
| 91 | struct ColumnMetadata |
| 92 | { |
| 93 | // Key = role name that will be made visible to the delegate |
| 94 | // Value = metadata about that role, including actual name in the model data, type, etc. |
| 95 | QHash<QString, ColumnRoleMetadata> roles; |
| 96 | }; |
| 97 | |
| 98 | ColumnRoleMetadata fetchColumnRoleData(const QString &roleNameKey, QQmlTableModelColumn *tableModelColumn, int columnIndex) const; |
| 99 | void fetchColumnMetadata(); |
| 100 | |
| 101 | enum NewRowOperationFlag { |
| 102 | OtherOperation, // insert(), set(), etc. |
| 103 | SetRowsOperation, |
| 104 | AppendOperation |
| 105 | }; |
| 106 | |
| 107 | bool validateRowType(QLatin1StringView functionName, const QVariant &row) const; |
| 108 | virtual bool validateNewRow(QLatin1StringView functionName, const QVariant &row, |
| 109 | NewRowOperationFlag operation = OtherOperation) const; |
| 110 | |
| 111 | QList<QQmlTableModelColumn *> mColumns; |
| 112 | |
| 113 | bool mComponentCompleted = false; |
| 114 | int mColumnCount = 0; |
| 115 | // Each entry contains information about the properties of the column at that index. |
| 116 | QVector<ColumnMetadata> mColumnMetadata; |
| 117 | // key = property index (0 to number of properties across all columns) |
| 118 | // value = role name |
| 119 | QHash<int, QByteArray> mRoleNames; |
| 120 | }; |
| 121 | |
| 122 | QT_END_NAMESPACE |
| 123 | |
| 124 | #endif // QQMLABSTRACTCOLUMNMODEL_P_H |
| 125 | |