| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> |
| 3 | SPDX-FileContributor: David Faure <david.faure@kdab.com> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifndef REARRANGECOLUMNSPROXYMODEL_H |
| 9 | #define REARRANGECOLUMNSPROXYMODEL_H |
| 10 | |
| 11 | #include "kitemmodels_export.h" |
| 12 | |
| 13 | #include <QIdentityProxyModel> |
| 14 | |
| 15 | #include <memory> |
| 16 | |
| 17 | class KRearrangeColumnsProxyModelPrivate; |
| 18 | |
| 19 | /*! |
| 20 | * \class KRearrangeColumnsProxyModel |
| 21 | * \inmodule KItemModels |
| 22 | * \brief This proxy shows specific columns from the source model, in any order. |
| 23 | * |
| 24 | * This allows to reorder columns, as well as not showing all of them. |
| 25 | * |
| 26 | * The proxy supports source models that have a tree structure. |
| 27 | * It also supports editing, and propagating changes from the source model. |
| 28 | * |
| 29 | * Showing the same source column more than once is not supported. |
| 30 | * |
| 31 | * \since 5.12 |
| 32 | */ |
| 33 | class KITEMMODELS_EXPORT KRearrangeColumnsProxyModel : public QIdentityProxyModel |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | public: |
| 37 | /*! |
| 38 | * Creates a KRearrangeColumnsProxyModel proxy. |
| 39 | * Remember to call setSourceModel() afterwards. |
| 40 | */ |
| 41 | explicit KRearrangeColumnsProxyModel(QObject *parent = nullptr); |
| 42 | ~KRearrangeColumnsProxyModel() override; |
| 43 | |
| 44 | // API |
| 45 | |
| 46 | /*! |
| 47 | * Set the chosen source columns, in the desired order for the proxy columns |
| 48 | * columns[proxyColumn=0] is the source column to show in the first proxy column, etc. |
| 49 | * |
| 50 | * Example: QList<int>() << 2 << 1; |
| 51 | * This examples configures the proxy to hide column 0, show column 2 from the source model, |
| 52 | * then show column 1 from the source model. |
| 53 | */ |
| 54 | void setSourceColumns(const QList<int> &columns); |
| 55 | |
| 56 | // Implementation |
| 57 | |
| 58 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
| 59 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 60 | |
| 61 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; |
| 62 | QModelIndex parent(const QModelIndex &child) const override; |
| 63 | |
| 64 | QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override; |
| 65 | QModelIndex mapToSource(const QModelIndex &proxyIndex) const override; |
| 66 | |
| 67 | QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; |
| 68 | |
| 69 | bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; |
| 70 | |
| 71 | QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; |
| 72 | |
| 73 | /*! |
| 74 | * Returns the proxy column for the given source column |
| 75 | * or -1 if the source column isn't shown in the proxy. |
| 76 | * \since 5.56 |
| 77 | */ |
| 78 | int proxyColumnForSourceColumn(int sourceColumn) const; |
| 79 | |
| 80 | /*! |
| 81 | * Returns the source column for the given proxy column. |
| 82 | * \since 5.56 |
| 83 | */ |
| 84 | int sourceColumnForProxyColumn(int proxyColumn) const; |
| 85 | |
| 86 | private: |
| 87 | std::unique_ptr<KRearrangeColumnsProxyModelPrivate> const d_ptr; |
| 88 | }; |
| 89 | |
| 90 | #endif |
| 91 | |