KItemModels

kcheckableproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kcheckableproxymodel.h"
8
9#include <QItemSelectionModel>
10
11class KCheckableProxyModelPrivate
12{
13 Q_DECLARE_PUBLIC(KCheckableProxyModel)
15
16 KCheckableProxyModelPrivate(KCheckableProxyModel *checkableModel)
17 : q_ptr(checkableModel)
18 {
19 }
20
21 QItemSelectionModel *m_itemSelectionModel = nullptr;
22
23 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
24};
25
26KCheckableProxyModel::KCheckableProxyModel(QObject *parent)
27 : QIdentityProxyModel(parent)
28 , d_ptr(new KCheckableProxyModelPrivate(this))
29{
30}
31
32KCheckableProxyModel::~KCheckableProxyModel() = default;
33
34void KCheckableProxyModel::setSelectionModel(QItemSelectionModel *itemSelectionModel)
35{
37 d->m_itemSelectionModel = itemSelectionModel;
38 Q_ASSERT(sourceModel() ? d->m_itemSelectionModel->model() == sourceModel() : true);
39 connect(itemSelectionModel, &QItemSelectionModel::selectionChanged, this, [d](const QItemSelection &selected, const QItemSelection &deselected) {
40 d->selectionChanged(selected, deselected);
41 });
42}
43
44QItemSelectionModel *KCheckableProxyModel::selectionModel() const
45{
47 return d->m_itemSelectionModel;
48}
49
50Qt::ItemFlags KCheckableProxyModel::flags(const QModelIndex &index) const
51{
52 if (!index.isValid() || index.column() != 0) {
54 }
56}
57
58QVariant KCheckableProxyModel::data(const QModelIndex &index, int role) const
59{
61
62 if (role == Qt::CheckStateRole) {
63 if (index.column() != 0) {
64 return QVariant();
65 }
66 if (!d->m_itemSelectionModel) {
67 return Qt::Unchecked;
68 }
69
70 return d->m_itemSelectionModel->selection().contains(mapToSource(index)) ? Qt::Checked : Qt::Unchecked;
71 }
72 return QIdentityProxyModel::data(index, role);
73}
74
75bool KCheckableProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
76{
78 if (role == Qt::CheckStateRole) {
79 if (index.column() != 0) {
80 return false;
81 }
82 if (!d->m_itemSelectionModel) {
83 return false;
84 }
85
86 Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
87 const QModelIndex srcIndex = mapToSource(index);
88 bool result = select(QItemSelection(srcIndex, srcIndex), state == Qt::Checked ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
90 return result;
91 }
92 return QIdentityProxyModel::setData(index, value, role);
93}
94
95void KCheckableProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
96{
98 Q_ASSERT(d_ptr->m_itemSelectionModel ? d_ptr->m_itemSelectionModel->model() == sourceModel : true);
99}
100
101void KCheckableProxyModelPrivate::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
102{
104 const auto lstSelected = q->mapSelectionFromSource(selected);
105 for (const QItemSelectionRange &range : lstSelected) {
106 Q_EMIT q->dataChanged(range.topLeft(), range.bottomRight());
107 }
108 const auto lstDeselected = q->mapSelectionFromSource(deselected);
109 for (const QItemSelectionRange &range : lstDeselected) {
110 Q_EMIT q->dataChanged(range.topLeft(), range.bottomRight());
111 }
112}
113
114bool KCheckableProxyModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
115{
117 d->m_itemSelectionModel->select(selection, command);
118 return true;
119}
120
122{
123 auto roles = QIdentityProxyModel::roleNames();
124 roles[Qt::CheckStateRole] = QByteArrayLiteral("checkState");
125 return roles;
126}
127
128#include "moc_kcheckableproxymodel.cpp"
Adds a checkable capability to a source model.
QHash< int, QByteArray > roleNames() const override
Expose following role: "checkState" => Qt::CheckStateRole.
virtual QVariant data(const QModelIndex &index, int role) const const=0
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QHash< int, QByteArray > roleNames() const const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual void setSourceModel(QAbstractItemModel *newSourceModel) override
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
int column() const const
bool isValid() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Unchecked
CheckStateRole
typedef ItemFlags
int toInt(bool *ok) const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.