File indexing completed on 2024-05-05 03:56:42

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: David Faure <david.faure@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef REARRANGECOLUMNSPROXYMODEL_H
0009 #define REARRANGECOLUMNSPROXYMODEL_H
0010 
0011 #include "kitemmodels_export.h"
0012 
0013 #include <QIdentityProxyModel>
0014 
0015 #include <memory>
0016 
0017 class KRearrangeColumnsProxyModelPrivate;
0018 
0019 /**
0020  * @class KRearrangeColumnsProxyModel krearrangecolumnsproxymodel.h KRearrangeColumnsProxyModel
0021  *
0022  * This proxy shows specific columns from the source model, in any order.
0023  * This allows to reorder columns, as well as not showing all of them.
0024  *
0025  * The proxy supports source models that have a tree structure.
0026  * It also supports editing, and propagating changes from the source model.
0027  *
0028  * Showing the same source column more than once is not supported.
0029  *
0030  * Author: David Faure, KDAB
0031  * @since 5.12
0032  */
0033 class KITEMMODELS_EXPORT KRearrangeColumnsProxyModel : public QIdentityProxyModel
0034 {
0035     Q_OBJECT
0036 public:
0037     /**
0038      * Creates a KRearrangeColumnsProxyModel proxy.
0039      * Remember to call setSourceModel afterwards.
0040      */
0041     explicit KRearrangeColumnsProxyModel(QObject *parent = nullptr);
0042     /**
0043      * Destructor.
0044      */
0045     ~KRearrangeColumnsProxyModel() override;
0046 
0047     // API
0048 
0049     /**
0050      * Set the chosen source columns, in the desired order for the proxy columns
0051      * columns[proxyColumn=0] is the source column to show in the first proxy column, etc.
0052      *
0053      * Example: QList<int>() << 2 << 1;
0054      * This examples configures the proxy to hide column 0, show column 2 from the source model,
0055      * then show column 1 from the source model.
0056      */
0057     void setSourceColumns(const QList<int> &columns);
0058 
0059     // Implementation
0060 
0061     /// @reimp
0062     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0063     /// @reimp
0064     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0065 
0066     /// @reimp
0067     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0068     /// @reimp
0069     QModelIndex parent(const QModelIndex &child) const override;
0070 
0071     /// @reimp
0072     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
0073     /// @reimp
0074     QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
0075 
0076     /// @reimp
0077     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0078 
0079     /// @reimp
0080     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
0081 
0082     /// @reimp
0083     QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
0084 
0085     /**
0086      * Returns the proxy column for the given source column
0087      * or -1 if the source column isn't shown in the proxy.
0088      * @since 5.56
0089      */
0090     int proxyColumnForSourceColumn(int sourceColumn) const;
0091 
0092     /**
0093      * Returns the source column for the given proxy column.
0094      * @since 5.56
0095      */
0096     int sourceColumnForProxyColumn(int proxyColumn) const;
0097 
0098 private:
0099     std::unique_ptr<KRearrangeColumnsProxyModelPrivate> const d_ptr;
0100 };
0101 
0102 #endif