• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • itemviews
kcheckableproxymodel.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDE.
3
4 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 USA.
20*/
21
22
23#include "kcheckableproxymodel.h"
24
25#include <QItemSelectionModel>
26
27class KCheckableProxyModelPrivate
28{
29 Q_DECLARE_PUBLIC(KCheckableProxyModel)
30 KCheckableProxyModel *q_ptr;
31
32 KCheckableProxyModelPrivate(KCheckableProxyModel *checkableModel)
33 : q_ptr(checkableModel),
34 m_itemSelectionModel(0)
35 {
36
37 }
38
39 QItemSelectionModel *m_itemSelectionModel;
40
41 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
42
43};
44
45KCheckableProxyModel::KCheckableProxyModel(QObject* parent)
46 : KIdentityProxyModel(parent), d_ptr(new KCheckableProxyModelPrivate(this))
47{
48
49}
50
51KCheckableProxyModel::~KCheckableProxyModel()
52{
53 delete d_ptr;
54}
55
56void KCheckableProxyModel::setSelectionModel(QItemSelectionModel* itemSelectionModel)
57{
58 Q_D(KCheckableProxyModel);
59 d->m_itemSelectionModel = itemSelectionModel;
60 Q_ASSERT(sourceModel() ? d->m_itemSelectionModel->model() == sourceModel() : true);
61 connect(itemSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(selectionChanged(QItemSelection,QItemSelection)));
62}
63
64QItemSelectionModel *KCheckableProxyModel::selectionModel() const
65{
66 Q_D(const KCheckableProxyModel);
67 return d->m_itemSelectionModel;
68}
69
70Qt::ItemFlags KCheckableProxyModel::flags(const QModelIndex& index) const
71{
72 if (!index.isValid() || index.column() != 0)
73 return KIdentityProxyModel::flags(index);
74 return KIdentityProxyModel::flags(index) | Qt::ItemIsUserCheckable;
75}
76
77QVariant KCheckableProxyModel::data(const QModelIndex& index, int role) const
78{
79 Q_D(const KCheckableProxyModel);
80
81 if (role == Qt::CheckStateRole)
82 {
83 if (index.column() != 0)
84 return QVariant();
85 if (!d->m_itemSelectionModel)
86 return Qt::Unchecked;
87
88 return d->m_itemSelectionModel->selection().contains(mapToSource(index)) ? Qt::Checked : Qt::Unchecked;
89 }
90 return KIdentityProxyModel::data(index, role);
91}
92
93bool KCheckableProxyModel::setData(const QModelIndex& index, const QVariant& value, int role)
94{
95 Q_D(KCheckableProxyModel);
96 if (role == Qt::CheckStateRole)
97 {
98 if (index.column() != 0)
99 return false;
100 if (!d->m_itemSelectionModel)
101 return false;
102
103 Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
104 const QModelIndex srcIndex = mapToSource(index);
105 bool result = select(QItemSelection(srcIndex, srcIndex), state == Qt::Checked ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
106 emit dataChanged(index, index);
107 return result;
108 }
109 return KIdentityProxyModel::setData(index, value, role);
110}
111
112void KCheckableProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
113{
114 KIdentityProxyModel::setSourceModel(sourceModel);
115 Q_ASSERT(d_ptr->m_itemSelectionModel ? d_ptr->m_itemSelectionModel->model() == sourceModel : true);
116}
117
118void KCheckableProxyModelPrivate::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
119{
120 Q_Q(KCheckableProxyModel);
121 foreach (const QItemSelectionRange &range, q->mapSelectionFromSource(selected))
122 q->dataChanged(range.topLeft(), range.bottomRight());
123 foreach (const QItemSelectionRange &range, q->mapSelectionFromSource(deselected))
124 q->dataChanged(range.topLeft(), range.bottomRight());
125}
126
127bool KCheckableProxyModel::select(const QItemSelection& selection, QItemSelectionModel::SelectionFlags command)
128{
129 Q_D(KCheckableProxyModel);
130 d->m_itemSelectionModel->select(selection, command);
131 return true;
132}
133
134
135#include "kcheckableproxymodel.moc"
136
KCheckableProxyModel
Adds a checkable capability to a source model.
Definition: kcheckableproxymodel.h:69
KCheckableProxyModel::selectionModel
QItemSelectionModel * selectionModel() const
Definition: kcheckableproxymodel.cpp:64
KCheckableProxyModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: kcheckableproxymodel.cpp:93
KCheckableProxyModel::setSelectionModel
void setSelectionModel(QItemSelectionModel *itemSelectionModel)
Definition: kcheckableproxymodel.cpp:56
KCheckableProxyModel::~KCheckableProxyModel
~KCheckableProxyModel()
Definition: kcheckableproxymodel.cpp:51
KCheckableProxyModel::KCheckableProxyModel
KCheckableProxyModel(QObject *parent=0)
Definition: kcheckableproxymodel.cpp:45
KCheckableProxyModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: kcheckableproxymodel.cpp:77
KCheckableProxyModel::select
virtual bool select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
Definition: kcheckableproxymodel.cpp:127
KCheckableProxyModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: kcheckableproxymodel.cpp:70
KCheckableProxyModel::setSourceModel
void setSourceModel(QAbstractItemModel *sourceModel)
Definition: kcheckableproxymodel.cpp:112
KIdentityProxyModel
The KIdentityProxyModel class proxies its source model unmodified.
Definition: kidentityproxymodel.h:32
KIdentityProxyModel::mapToSource
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
Definition: kidentityproxymodel.cpp:307
KIdentityProxyModel::setSourceModel
void setSourceModel(QAbstractItemModel *sourceModel)
Definition: kidentityproxymodel.cpp:409
QAbstractItemModel
QItemSelectionModel
QObject
kcheckableproxymodel.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal