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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • itemviews
klinkitemselectionmodel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3  a KDAB Group company, info@kdab.net,
4  author Stephen Kelly <stephen@kdab.com>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 
23 #include "klinkitemselectionmodel.h"
24 
25 #include "kmodelindexproxymapper.h"
26 
27 #include "kdebug.h"
28 
29 #include <QItemSelection>
30 
31 class KLinkItemSelectionModelPrivate
32 {
33 public:
34  KLinkItemSelectionModelPrivate(KLinkItemSelectionModel *proxySelectionModel, QAbstractItemModel *model,
35  QItemSelectionModel *linkedItemSelectionModel)
36  : q_ptr(proxySelectionModel),
37  m_model(model),
38  m_linkedItemSelectionModel(linkedItemSelectionModel),
39  m_ignoreCurrentChanged(false),
40  m_indexMapper(new KModelIndexProxyMapper(model, linkedItemSelectionModel->model(), proxySelectionModel))
41  {
42  }
43 
44  Q_DECLARE_PUBLIC(KLinkItemSelectionModel)
45  KLinkItemSelectionModel * const q_ptr;
46 
47 
48  bool assertSelectionValid(const QItemSelection &selection) const {
49  foreach(const QItemSelectionRange &range, selection) {
50  if (!range.isValid()) {
51  kDebug() << selection;
52  }
53  Q_ASSERT(range.isValid());
54  }
55  return true;
56  }
57 
58  void sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
59  void sourceCurrentChanged(const QModelIndex& current);
60  void slotCurrentChanged(const QModelIndex& current);
61 
62  QAbstractItemModel * const m_model;
63  QItemSelectionModel * const m_linkedItemSelectionModel;
64  bool m_ignoreCurrentChanged;
65  KModelIndexProxyMapper * const m_indexMapper;
66 };
67 
68 KLinkItemSelectionModel::KLinkItemSelectionModel(QAbstractItemModel *model, QItemSelectionModel *proxySelector, QObject *parent)
69  : QItemSelectionModel(model, parent),
70  d_ptr(new KLinkItemSelectionModelPrivate(this, model, proxySelector))
71 {
72  connect(proxySelector, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(sourceSelectionChanged(QItemSelection,QItemSelection)));
73  connect(proxySelector, SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(sourceCurrentChanged(QModelIndex)));
74  connect(this, SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(slotCurrentChanged(QModelIndex)));
75 }
76 
77 KLinkItemSelectionModel::~KLinkItemSelectionModel()
78 {
79  delete d_ptr;
80 }
81 
82 void KLinkItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
83 {
84  Q_D(KLinkItemSelectionModel);
85  // When an item is removed, the current index is set to the top index in the model.
86  // That causes a selectionChanged signal with a selection which we do not want.
87  if (d->m_ignoreCurrentChanged) {
88  return;
89  }
90  // Do *not* replace next line with: QItemSelectionModel::select(index, command)
91  //
92  // Doing so would end up calling KLinkItemSelectionModel::select(QItemSelection, QItemSelectionModel::SelectionFlags)
93  //
94  // This is because the code for QItemSelectionModel::select(QModelIndex, QItemSelectionModel::SelectionFlags) looks like this:
95  // {
96  // QItemSelection selection(index, index);
97  // select(selection, command);
98  // }
99  // So it calls KLinkItemSelectionModel overload of
100  // select(QItemSelection, QItemSelectionModel::SelectionFlags)
101  //
102  // When this happens and the selection flags include Toggle, it causes the
103  // selection to be toggled twice.
104  QItemSelectionModel::select(QItemSelection(index, index), command);
105  if (index.isValid())
106  d->m_linkedItemSelectionModel->select(d->m_indexMapper->mapSelectionLeftToRight(QItemSelection(index, index)), command);
107  else {
108  d->m_linkedItemSelectionModel->clearSelection();
109  }
110 }
111 
112 // QAbstractProxyModel::mapSelectionFromSource creates invalid ranges to we filter
113 // those out manually in a loop. Hopefully fixed in Qt 4.7.2, so we ifdef it out.
114 // http://qt.gitorious.org/qt/qt/merge_requests/2474
115 // http://qt.gitorious.org/qt/qt/merge_requests/831
116 #if QT_VERSION < 0x040702
117 #define RANGE_FIX_HACK
118 #endif
119 
120 #ifdef RANGE_FIX_HACK
121 static QItemSelection klink_removeInvalidRanges(const QItemSelection &selection)
122 {
123  QItemSelection result;
124  Q_FOREACH(const QItemSelectionRange &range, selection)
125  {
126  if (!range.isValid())
127  continue;
128  result << range;
129  }
130  return result;
131 }
132 #endif
133 
134 void KLinkItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
135 {
136  Q_D(KLinkItemSelectionModel);
137  d->m_ignoreCurrentChanged = true;
138 #ifdef RANGE_FIX_HACK
139  QItemSelection _selection = klink_removeInvalidRanges(selection);
140 #else
141  QItemSelection _selection = selection;
142 #endif
143  QItemSelectionModel::select(_selection, command);
144  Q_ASSERT(d->assertSelectionValid(_selection));
145  QItemSelection mappedSelection = d->m_indexMapper->mapSelectionLeftToRight(_selection);
146  Q_ASSERT(d->assertSelectionValid(mappedSelection));
147  d->m_linkedItemSelectionModel->select(mappedSelection, command);
148  d->m_ignoreCurrentChanged = false;
149 }
150 
151 void KLinkItemSelectionModelPrivate::slotCurrentChanged(const QModelIndex& current)
152 {
153  const QModelIndex mappedCurrent = m_indexMapper->mapLeftToRight(current);
154  if (!mappedCurrent.isValid()) {
155  return;
156  }
157  m_linkedItemSelectionModel->setCurrentIndex(mappedCurrent, QItemSelectionModel::NoUpdate);
158 }
159 
160 void KLinkItemSelectionModelPrivate::sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
161 {
162  Q_Q(KLinkItemSelectionModel);
163 #ifdef RANGE_FIX_HACK
164  QItemSelection _selected = klink_removeInvalidRanges(selected);
165  QItemSelection _deselected = klink_removeInvalidRanges(deselected);
166 #else
167  QItemSelection _selected = selected;
168  QItemSelection _deselected = deselected;
169 #endif
170  Q_ASSERT(assertSelectionValid(_selected));
171  Q_ASSERT(assertSelectionValid(_deselected));
172  const QItemSelection mappedDeselection = m_indexMapper->mapSelectionRightToLeft(_deselected);
173  const QItemSelection mappedSelection = m_indexMapper->mapSelectionRightToLeft(_selected);
174 
175  q->QItemSelectionModel::select(mappedDeselection, QItemSelectionModel::Deselect);
176  q->QItemSelectionModel::select(mappedSelection, QItemSelectionModel::Select);
177 }
178 
179 void KLinkItemSelectionModelPrivate::sourceCurrentChanged(const QModelIndex& current)
180 {
181  Q_Q(KLinkItemSelectionModel);
182  const QModelIndex mappedCurrent = m_indexMapper->mapRightToLeft(current);
183  if (!mappedCurrent.isValid()) {
184  return;
185  }
186  q->setCurrentIndex(mappedCurrent, QItemSelectionModel::NoUpdate);
187 }
188 
189 #include "klinkitemselectionmodel.moc"
QModelIndex
QItemSelectionModel::selectionChanged
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
klink_removeInvalidRanges
static QItemSelection klink_removeInvalidRanges(const QItemSelection &selection)
Definition: klinkitemselectionmodel.cpp:121
QItemSelectionRange
kdebug.h
KLinkItemSelectionModel::KLinkItemSelectionModel
KLinkItemSelectionModel(QAbstractItemModel *targetModel, QItemSelectionModel *linkedItemSelectionModel, QObject *parent=0)
Constructor.
Definition: klinkitemselectionmodel.cpp:68
KLinkItemSelectionModel::~KLinkItemSelectionModel
~KLinkItemSelectionModel()
Definition: klinkitemselectionmodel.cpp:77
QItemSelection::select
void select(const QModelIndex &topLeft, const QModelIndex &bottomRight)
KLinkItemSelectionModel
Makes it possible to share a selection in multiple views which do not have the same source model...
Definition: klinkitemselectionmodel.h:95
kmodelindexproxymapper.h
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KStandardAction::Deselect
Definition: kstandardaction.h:133
QModelIndex::isValid
bool isValid() const
KLinkItemSelectionModel::d_ptr
KLinkItemSelectionModelPrivate *const d_ptr
Definition: klinkitemselectionmodel.h:108
klinkitemselectionmodel.h
QObject
QItemSelectionModel::selection
const QItemSelection selection() const
QItemSelectionModel::select
virtual void select(const QModelIndex &index, QFlags< QItemSelectionModel::SelectionFlag > command)
QItemSelection
QItemSelectionModel::currentChanged
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
KModelIndexProxyMapper
This class facilitates easy mapping of indexes and selections through proxy models.
Definition: kmodelindexproxymapper.h:79
KLinkItemSelectionModel::select
void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Definition: klinkitemselectionmodel.cpp:82
QAbstractItemModel
QItemSelectionRange::isValid
bool isValid() const
QItemSelectionModel
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QItemSelectionModel::SelectionFlags
typedef SelectionFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • 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