KItemModels

krearrangecolumnsproxymodel.h
1 /*
2  SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3  SPDX-FileContributor: David Faure <[email protected]>
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 krearrangecolumnsproxymodel.h KRearrangeColumnsProxyModel
21  *
22  * This proxy shows specific columns from the source model, in any order.
23  * This allows to reorder columns, as well as not showing all of them.
24  *
25  * The proxy supports source models that have a tree structure.
26  * It also supports editing, and propagating changes from the source model.
27  *
28  * Showing the same source column more than once is not supported.
29  *
30  * Author: David Faure, KDAB
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  /**
43  * Destructor.
44  */
45  ~KRearrangeColumnsProxyModel() override;
46 
47  // API
48 
49  /**
50  * Set the chosen source columns, in the desired order for the proxy columns
51  * columns[proxyColumn=0] is the source column to show in the first proxy column, etc.
52  *
53  * Example: QVector<int>() << 2 << 1;
54  * This examples configures the proxy to hide column 0, show column 2 from the source model,
55  * then show column 1 from the source model.
56  */
57  void setSourceColumns(const QVector<int> &columns);
58 
59  // Implementation
60 
61  /// @reimp
62  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
63  /// @reimp
64  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
65 
66  /// @reimp
67  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
68  /// @reimp
69  QModelIndex parent(const QModelIndex &child) const override;
70 
71  /// @reimp
72  QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
73  /// @reimp
74  QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
75 
76  /// @reimp
77  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
78 
79  /// @reimp
80  bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
81 
82  /// @reimp
83  QModelIndex sibling(int row, int column, const QModelIndex &idx) const override;
84 
85  /**
86  * Returns the proxy column for the given source column
87  * or -1 if the source column isn't shown in the proxy.
88  * @since 5.56
89  */
90  int proxyColumnForSourceColumn(int sourceColumn) const;
91 
92  /**
93  * Returns the source column for the given proxy column.
94  * @since 5.56
95  */
96  int sourceColumnForProxyColumn(int proxyColumn) const;
97 
98 private:
99  std::unique_ptr<KRearrangeColumnsProxyModelPrivate> const d_ptr;
100 };
101 
102 #endif
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
virtual bool hasChildren(const QModelIndex &parent) const const override
DisplayRole
This proxy shows specific columns from the source model, in any order.
virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const const override
virtual int columnCount(const QModelIndex &parent) const const override
Orientation
QObject * parent() const const
virtual int rowCount(const QModelIndex &parent) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:51:32 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.