2
This file is part of KDE.
4
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
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.
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.
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,
23
#include "kcheckableproxymodel.h"
25
#include <QItemSelectionModel>
27
class KCheckableProxyModelPrivate
29
Q_DECLARE_PUBLIC(KCheckableProxyModel)
30
KCheckableProxyModel *q_ptr;
32
KCheckableProxyModelPrivate(KCheckableProxyModel *checkableModel)
33
: q_ptr(checkableModel),
34
m_itemSelectionModel(0)
39
QItemSelectionModel *m_itemSelectionModel;
41
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
45
KCheckableProxyModel::KCheckableProxyModel(QObject* parent)
46
: KIdentityProxyModel(parent), d_ptr(new KCheckableProxyModelPrivate(this))
51
KCheckableProxyModel::~KCheckableProxyModel()
56
void KCheckableProxyModel::setSelectionModel(QItemSelectionModel* itemSelectionModel)
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)));
64
QItemSelectionModel *KCheckableProxyModel::selectionModel() const
66
Q_D(const KCheckableProxyModel);
67
return d->m_itemSelectionModel;
70
Qt::ItemFlags KCheckableProxyModel::flags(const QModelIndex& index) const
73
return KIdentityProxyModel::flags(index);
74
return KIdentityProxyModel::flags(index) | Qt::ItemIsUserCheckable;
77
QVariant KCheckableProxyModel::data(const QModelIndex& index, int role) const
79
Q_D(const KCheckableProxyModel);
81
if (role == Qt::CheckStateRole)
83
if (!d->m_itemSelectionModel)
86
return d->m_itemSelectionModel->selection().contains(mapToSource(index)) ? Qt::Checked : Qt::Unchecked;
88
return KIdentityProxyModel::data(index, role);
91
bool KCheckableProxyModel::setData(const QModelIndex& index, const QVariant& value, int role)
93
Q_D(KCheckableProxyModel);
94
if (role == Qt::CheckStateRole)
96
if (!d->m_itemSelectionModel)
99
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
100
const QModelIndex srcIndex = mapToSource(index);
101
bool result = select(QItemSelection(srcIndex, srcIndex), state == Qt::Checked ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
102
emit dataChanged(index, index);
105
return KIdentityProxyModel::setData(index, value, role);
108
void KCheckableProxyModel::setSourceModel(QAbstractItemModel* sourceModel)
110
KIdentityProxyModel::setSourceModel(sourceModel);
111
Q_ASSERT(d_ptr->m_itemSelectionModel ? d_ptr->m_itemSelectionModel->model() == sourceModel : true);
114
void KCheckableProxyModelPrivate::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
116
Q_Q(KCheckableProxyModel);
117
foreach (const QItemSelectionRange &range, q->mapSelectionFromSource(selected))
118
q->dataChanged(range.topLeft(), range.bottomRight());
119
foreach (const QItemSelectionRange &range, q->mapSelectionFromSource(deselected))
120
q->dataChanged(range.topLeft(), range.bottomRight());
123
bool KCheckableProxyModel::select(const QItemSelection& selection, QItemSelectionModel::SelectionFlags command)
125
Q_D(KCheckableProxyModel);
126
d->m_itemSelectionModel->select(selection, command);
131
#include "kcheckableproxymodel.moc"