| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef KCHECKABLEPROXYMODEL_H |
| 8 | #define KCHECKABLEPROXYMODEL_H |
| 9 | |
| 10 | #include "kitemmodels_export.h" |
| 11 | |
| 12 | #include <QIdentityProxyModel> |
| 13 | #include <QItemSelection> |
| 14 | |
| 15 | #include <memory> |
| 16 | |
| 17 | class KCheckableProxyModelPrivate; |
| 18 | |
| 19 | /*! |
| 20 | * \class KCheckableProxyModel |
| 21 | * \inmodule KItemModels |
| 22 | * \brief Adds a checkable capability to a source model |
| 23 | * |
| 24 | * Items is standard Qt views such as QTreeView and QListView can have a |
| 25 | * checkable capability and draw checkboxes. Adding such a capability |
| 26 | * requires specific implementations of the data() and flags() virtual methods. |
| 27 | * This class implements those methods generically so that it is not necessary to |
| 28 | * implement them in your model. |
| 29 | * |
| 30 | * This can be combined with a KSelectionProxyModel showing the items currently selected |
| 31 | * |
| 32 | * \code |
| 33 | * |
| 34 | * QItemSelectionModel *checkModel = new QItemSelectionModel(rootModel, this); |
| 35 | * KCheckableProxyModel *checkable = new KCheckableProxyModel(this); |
| 36 | * checkable->setSourceModel(rootModel); |
| 37 | * checkable->setSelectionModel(checkModel); |
| 38 | * |
| 39 | * QTreeView *tree1 = new QTreeView(vSplitter); |
| 40 | * tree1->setModel(checkable); |
| 41 | * tree1->expandAll(); |
| 42 | * |
| 43 | * KSelectionProxyModel *selectionProxy = new KSelectionProxyModel(checkModel, this); |
| 44 | * selectionProxy->setFilterBehavior(KSelectionProxyModel::ExactSelection); |
| 45 | * selectionProxy->setSourceModel(rootModel); |
| 46 | * |
| 47 | * QTreeView *tree2 = new QTreeView(vSplitter); |
| 48 | * tree2->setModel(selectionProxy); |
| 49 | * \endcode |
| 50 | * |
| 51 | * \image kcheckableproxymodel.png "A KCheckableProxyModel and KSelectionProxyModel showing checked items" |
| 52 | * |
| 53 | * \since 4.6 |
| 54 | */ |
| 55 | class KITEMMODELS_EXPORT KCheckableProxyModel : public QIdentityProxyModel |
| 56 | { |
| 57 | Q_OBJECT |
| 58 | public: |
| 59 | /*! |
| 60 | * |
| 61 | */ |
| 62 | explicit KCheckableProxyModel(QObject *parent = nullptr); |
| 63 | ~KCheckableProxyModel() override; |
| 64 | |
| 65 | /*! |
| 66 | * |
| 67 | */ |
| 68 | void setSelectionModel(QItemSelectionModel *itemSelectionModel); |
| 69 | |
| 70 | /*! |
| 71 | * |
| 72 | */ |
| 73 | QItemSelectionModel *selectionModel() const; |
| 74 | |
| 75 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 76 | |
| 77 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 78 | |
| 79 | bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; |
| 80 | |
| 81 | void setSourceModel(QAbstractItemModel *sourceModel) override; |
| 82 | |
| 83 | /// Expose following role: "checkState" => Qt::CheckStateRole |
| 84 | QHash<int, QByteArray> roleNames() const override; |
| 85 | |
| 86 | protected: |
| 87 | /*! |
| 88 | * |
| 89 | */ |
| 90 | virtual bool select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command); |
| 91 | |
| 92 | private: |
| 93 | Q_DECLARE_PRIVATE(KCheckableProxyModel) |
| 94 | std::unique_ptr<KCheckableProxyModelPrivate> const d_ptr; |
| 95 | |
| 96 | Q_PRIVATE_SLOT(d_func(), void selectionChanged(const QItemSelection &, const QItemSelection &)) |
| 97 | }; |
| 98 | |
| 99 | #endif |
| 100 | |