KDEUI
kcheckableproxymodel.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of KDE. 00003 00004 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00019 USA. 00020 */ 00021 00022 00023 #include "kcheckableproxymodel.h" 00024 00025 #include <QItemSelectionModel> 00026 00027 class KCheckableProxyModelPrivate 00028 { 00029 Q_DECLARE_PUBLIC(KCheckableProxyModel) 00030 KCheckableProxyModel *q_ptr; 00031 00032 KCheckableProxyModelPrivate(KCheckableProxyModel *checkableModel) 00033 : q_ptr(checkableModel), 00034 m_itemSelectionModel(0) 00035 { 00036 00037 } 00038 00039 QItemSelectionModel *m_itemSelectionModel; 00040 00041 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); 00042 00043 }; 00044 00045 KCheckableProxyModel::KCheckableProxyModel(QObject* parent) 00046 : KIdentityProxyModel(parent), d_ptr(new KCheckableProxyModelPrivate(this)) 00047 { 00048 00049 } 00050 00051 KCheckableProxyModel::~KCheckableProxyModel() 00052 { 00053 delete d_ptr; 00054 } 00055 00056 void KCheckableProxyModel::setSelectionModel(QItemSelectionModel* itemSelectionModel) 00057 { 00058 Q_D(KCheckableProxyModel); 00059 d->m_itemSelectionModel = itemSelectionModel; 00060 Q_ASSERT(sourceModel() ? d->m_itemSelectionModel->model() == sourceModel() : true); 00061 connect(itemSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(selectionChanged(QItemSelection,QItemSelection))); 00062 } 00063 00064 QItemSelectionModel *KCheckableProxyModel::selectionModel() const 00065 { 00066 Q_D(const KCheckableProxyModel); 00067 return d->m_itemSelectionModel; 00068 } 00069 00070 Qt::ItemFlags KCheckableProxyModel::flags(const QModelIndex& index) const 00071 { 00072 if (!index.isValid()) 00073 return KIdentityProxyModel::flags(index); 00074 return KIdentityProxyModel::flags(index) | Qt::ItemIsUserCheckable; 00075 } 00076 00077 QVariant KCheckableProxyModel::data(const QModelIndex& index, int role) const 00078 { 00079 Q_D(const KCheckableProxyModel); 00080 00081 if (role == Qt::CheckStateRole) 00082 { 00083 if (!d->m_itemSelectionModel) 00084 return Qt::Unchecked; 00085 00086 return d->m_itemSelectionModel->selection().contains(mapToSource(index)) ? Qt::Checked : Qt::Unchecked; 00087 } 00088 return KIdentityProxyModel::data(index, role); 00089 } 00090 00091 bool KCheckableProxyModel::setData(const QModelIndex& index, const QVariant& value, int role) 00092 { 00093 Q_D(KCheckableProxyModel); 00094 if (role == Qt::CheckStateRole) 00095 { 00096 if (!d->m_itemSelectionModel) 00097 return false; 00098 00099 Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); 00100 const QModelIndex srcIndex = mapToSource(index); 00101 bool result = select(QItemSelection(srcIndex, srcIndex), state == Qt::Checked ? QItemSelectionModel::Select : QItemSelectionModel::Deselect); 00102 emit dataChanged(index, index); 00103 return result; 00104 } 00105 return KIdentityProxyModel::setData(index, value, role); 00106 } 00107 00108 void KCheckableProxyModel::setSourceModel(QAbstractItemModel* sourceModel) 00109 { 00110 KIdentityProxyModel::setSourceModel(sourceModel); 00111 Q_ASSERT(d_ptr->m_itemSelectionModel ? d_ptr->m_itemSelectionModel->model() == sourceModel : true); 00112 } 00113 00114 void KCheckableProxyModelPrivate::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) 00115 { 00116 Q_Q(KCheckableProxyModel); 00117 foreach (const QItemSelectionRange &range, q->mapSelectionFromSource(selected)) 00118 q->dataChanged(range.topLeft(), range.bottomRight()); 00119 foreach (const QItemSelectionRange &range, q->mapSelectionFromSource(deselected)) 00120 q->dataChanged(range.topLeft(), range.bottomRight()); 00121 } 00122 00123 bool KCheckableProxyModel::select(const QItemSelection& selection, QItemSelectionModel::SelectionFlags command) 00124 { 00125 Q_D(KCheckableProxyModel); 00126 d->m_itemSelectionModel->select(selection, command); 00127 return true; 00128 } 00129 00130 00131 #include "kcheckableproxymodel.moc" 00132
KDE 4.6 API Reference