KItemModels

kcheckableproxymodel.h
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
17class KCheckableProxyModelPrivate;
18
19/**
20 * @class KCheckableProxyModel kcheckableproxymodel.h KCheckableProxyModel
21 *
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 html kcheckableproxymodel.png "A KCheckableProxyModel and KSelectionProxyModel showing checked items"
52 *
53 * @since 4.6
54 * @author Stephen Kelly <steveire@gmail.com>
55 */
56class KITEMMODELS_EXPORT KCheckableProxyModel : public QIdentityProxyModel
57{
58 Q_OBJECT
59public:
60 explicit KCheckableProxyModel(QObject *parent = nullptr);
61 ~KCheckableProxyModel() override;
62
63 void setSelectionModel(QItemSelectionModel *itemSelectionModel);
64 QItemSelectionModel *selectionModel() const;
65
66 Qt::ItemFlags flags(const QModelIndex &index) const override;
67
68 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
69
70 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
71
72 void setSourceModel(QAbstractItemModel *sourceModel) override;
73
74 /// Expose following role: "checkState" => Qt::CheckStateRole
75 QHash<int, QByteArray> roleNames() const override;
76
77protected:
78 virtual bool select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
79
80private:
81 Q_DECLARE_PRIVATE(KCheckableProxyModel)
82 std::unique_ptr<KCheckableProxyModelPrivate> const d_ptr;
83
84 Q_PRIVATE_SLOT(d_func(), void selectionChanged(const QItemSelection &, const QItemSelection &))
85};
86
87#endif
Adds a checkable capability to a source model.
virtual QVariant data(const QModelIndex &proxyIndex, int role) const const override
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual QHash< int, QByteArray > roleNames() const const override
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override
virtual void setSourceModel(QAbstractItemModel *newSourceModel) override
DisplayRole
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.