File indexing completed on 2024-07-21 06:38:21

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KCHECKABLEPROXYMODEL_H
0008 #define KCHECKABLEPROXYMODEL_H
0009 
0010 #include "kitemmodels_export.h"
0011 
0012 #include <QIdentityProxyModel>
0013 #include <QItemSelection>
0014 
0015 #include <memory>
0016 
0017 class KCheckableProxyModelPrivate;
0018 
0019 /**
0020  * @class KCheckableProxyModel kcheckableproxymodel.h KCheckableProxyModel
0021  *
0022  * @brief Adds a checkable capability to a source model
0023  *
0024  * Items is standard Qt views such as QTreeView and QListView can have a
0025  * checkable capability and draw checkboxes. Adding such a capability
0026  * requires specific implementations of the data() and flags() virtual methods.
0027  * This class implements those methods generically so that it is not necessary to
0028  * implement them in your model.
0029  *
0030  * This can be combined with a KSelectionProxyModel showing the items currently selected
0031  *
0032  * @code
0033  *
0034  *   QItemSelectionModel *checkModel = new QItemSelectionModel(rootModel, this);
0035  *   KCheckableProxyModel *checkable = new KCheckableProxyModel(this);
0036  *   checkable->setSourceModel(rootModel);
0037  *   checkable->setSelectionModel(checkModel);
0038  *
0039  *   QTreeView *tree1 = new QTreeView(vSplitter);
0040  *   tree1->setModel(checkable);
0041  *   tree1->expandAll();
0042  *
0043  *   KSelectionProxyModel *selectionProxy = new KSelectionProxyModel(checkModel, this);
0044  *   selectionProxy->setFilterBehavior(KSelectionProxyModel::ExactSelection);
0045  *   selectionProxy->setSourceModel(rootModel);
0046  *
0047  *   QTreeView *tree2 = new QTreeView(vSplitter);
0048  *   tree2->setModel(selectionProxy);
0049  * @endcode
0050  *
0051  * @image html kcheckableproxymodel.png "A KCheckableProxyModel and KSelectionProxyModel showing checked items"
0052  *
0053  * @since 4.6
0054  * @author Stephen Kelly <steveire@gmail.com>
0055  */
0056 class KITEMMODELS_EXPORT KCheckableProxyModel : public QIdentityProxyModel
0057 {
0058     Q_OBJECT
0059 public:
0060     explicit KCheckableProxyModel(QObject *parent = nullptr);
0061     ~KCheckableProxyModel() override;
0062 
0063     void setSelectionModel(QItemSelectionModel *itemSelectionModel);
0064     QItemSelectionModel *selectionModel() const;
0065 
0066     Qt::ItemFlags flags(const QModelIndex &index) const override;
0067 
0068     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0069 
0070     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0071 
0072     void setSourceModel(QAbstractItemModel *sourceModel) override;
0073 
0074     /// Expose following role: "checkState" => Qt::CheckStateRole
0075     QHash<int, QByteArray> roleNames() const override;
0076 
0077 protected:
0078     virtual bool select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
0079 
0080 private:
0081     Q_DECLARE_PRIVATE(KCheckableProxyModel)
0082     std::unique_ptr<KCheckableProxyModelPrivate> const d_ptr;
0083 
0084     Q_PRIVATE_SLOT(d_func(), void selectionChanged(const QItemSelection &, const QItemSelection &))
0085 };
0086 
0087 #endif