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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • 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 
27 class 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 
45 KCheckableProxyModel::KCheckableProxyModel(QObject* parent)
46  : KIdentityProxyModel(parent), d_ptr(new KCheckableProxyModelPrivate(this))
47 {
48 
49 }
50 
51 KCheckableProxyModel::~KCheckableProxyModel()
52 {
53  delete d_ptr;
54 }
55 
56 void 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 
64 QItemSelectionModel *KCheckableProxyModel::selectionModel() const
65 {
66  Q_D(const KCheckableProxyModel);
67  return d->m_itemSelectionModel;
68 }
69 
70 Qt::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 
77 QVariant 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 
93 bool 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 
112 void 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 
118 void 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 
127 bool 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 
QVariant
KCheckableProxyModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: kcheckableproxymodel.cpp:93
KIdentityProxyModel
The KIdentityProxyModel class proxies its source model unmodified.
Definition: kidentityproxymodel.h:31
QItemSelectionModel
KCheckableProxyModel
Adds a checkable capability to a source model.
Definition: kcheckableproxymodel.h:68
kcheckableproxymodel.h
KCheckableProxyModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: kcheckableproxymodel.cpp:77
KCheckableProxyModel::selectionModel
QItemSelectionModel * selectionModel() const
Definition: kcheckableproxymodel.cpp:64
QObject
KCheckableProxyModel::KCheckableProxyModel
KCheckableProxyModel(QObject *parent=0)
Definition: kcheckableproxymodel.cpp:45
KStandardAction::Deselect
Definition: kstandardaction.h:133
KCheckableProxyModel::~KCheckableProxyModel
~KCheckableProxyModel()
Definition: kcheckableproxymodel.cpp:51
KIdentityProxyModel::setSourceModel
void setSourceModel(QAbstractItemModel *sourceModel)
Definition: kidentityproxymodel.cpp:401
KIdentityProxyModel::mapToSource
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
Definition: kidentityproxymodel.cpp:299
KCheckableProxyModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: kcheckableproxymodel.cpp:70
KCheckableProxyModel::setSourceModel
void setSourceModel(QAbstractItemModel *sourceModel)
Definition: kcheckableproxymodel.cpp:112
KCheckableProxyModel::select
virtual bool select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
Definition: kcheckableproxymodel.cpp:127
QAbstractItemModel
KCheckableProxyModel::setSelectionModel
void setSelectionModel(QItemSelectionModel *itemSelectionModel)
Definition: kcheckableproxymodel.cpp:56
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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