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

KDEUI

  • 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 
60  QAbstractItemModel * const m_model;
61  QItemSelectionModel * const m_linkedItemSelectionModel;
62  bool m_ignoreCurrentChanged;
63  KModelIndexProxyMapper * const m_indexMapper;
64 };
65 
66 KLinkItemSelectionModel::KLinkItemSelectionModel(QAbstractItemModel *model, QItemSelectionModel *proxySelector, QObject *parent)
67  : QItemSelectionModel(model, parent),
68  d_ptr(new KLinkItemSelectionModelPrivate(this, model, proxySelector))
69 {
70  connect(proxySelector, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(sourceSelectionChanged(QItemSelection,QItemSelection)));
71 }
72 
73 KLinkItemSelectionModel::~KLinkItemSelectionModel()
74 {
75  delete d_ptr;
76 }
77 
78 void KLinkItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
79 {
80  Q_D(KLinkItemSelectionModel);
81  // When an item is removed, the current index is set to the top index in the model.
82  // That causes a selectionChanged signal with a selection which we do not want.
83  if (d->m_ignoreCurrentChanged) {
84  return;
85  }
86  QItemSelectionModel::select(index, command);
87  if (index.isValid())
88  d->m_linkedItemSelectionModel->select(d->m_indexMapper->mapSelectionLeftToRight(QItemSelection(index, index)), command);
89  else {
90  d->m_linkedItemSelectionModel->clearSelection();
91  }
92 }
93 
94 // QAbstractProxyModel::mapSelectionFromSource creates invalid ranges to we filter
95 // those out manually in a loop. Hopefully fixed in Qt 4.7.2, so we ifdef it out.
96 // http://qt.gitorious.org/qt/qt/merge_requests/2474
97 // http://qt.gitorious.org/qt/qt/merge_requests/831
98 #if QT_VERSION < 0x040702
99 #define RANGE_FIX_HACK
100 #endif
101 
102 #ifdef RANGE_FIX_HACK
103 static QItemSelection klink_removeInvalidRanges(const QItemSelection &selection)
104 {
105  QItemSelection result;
106  Q_FOREACH(const QItemSelectionRange &range, selection)
107  {
108  if (!range.isValid())
109  continue;
110  result << range;
111  }
112  return result;
113 }
114 #endif
115 
116 void KLinkItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
117 {
118  Q_D(KLinkItemSelectionModel);
119  d->m_ignoreCurrentChanged = true;
120 #ifdef RANGE_FIX_HACK
121  QItemSelection _selection = klink_removeInvalidRanges(selection);
122 #else
123  QItemSelection _selection = selection;
124 #endif
125  QItemSelectionModel::select(_selection, command);
126  Q_ASSERT(d->assertSelectionValid(_selection));
127  QItemSelection mappedSelection = d->m_indexMapper->mapSelectionLeftToRight(_selection);
128  Q_ASSERT(d->assertSelectionValid(mappedSelection));
129  d->m_linkedItemSelectionModel->select(mappedSelection, command);
130  d->m_ignoreCurrentChanged = false;
131 }
132 
133 void KLinkItemSelectionModelPrivate::sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
134 {
135  Q_Q(KLinkItemSelectionModel);
136 #ifdef RANGE_FIX_HACK
137  QItemSelection _selected = klink_removeInvalidRanges(selected);
138  QItemSelection _deselected = klink_removeInvalidRanges(deselected);
139 #else
140  QItemSelection _selected = selected;
141  QItemSelection _deselected = deselected;
142 #endif
143  Q_ASSERT(assertSelectionValid(_selected));
144  Q_ASSERT(assertSelectionValid(_deselected));
145  const QItemSelection mappedDeselection = m_indexMapper->mapSelectionRightToLeft(_deselected);
146  const QItemSelection mappedSelection = m_indexMapper->mapSelectionRightToLeft(_selected);
147 
148  q->QItemSelectionModel::select(mappedDeselection, QItemSelectionModel::Deselect);
149  q->QItemSelectionModel::select(mappedSelection, QItemSelectionModel::Select);
150 }
151 
152 #include "klinkitemselectionmodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Jul 16 2013 17:49:39 by doxygen 1.8.1.1 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.10.5 API Reference

Skip menu "kdelibs-4.10.5 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