2 Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3 a KDAB Group company, info@kdab.net,
4 author Stephen Kelly <stephen@kdab.com>
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.
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.
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
22 #include <QtCore/QObject>
23 #include "klinkitemselectionmodel.hxx"
24 // #include "klinkitemselectionmodel_p.hxx"
26 #include "kmodelindexproxymapper.hxx"
28 // #include "kdebug.h"
30 #include <QItemSelection>
35 KLinkItemSelectionModel::KLinkItemSelectionModel(QAbstractItemModel *model, QItemSelectionModel *proxySelector, QObject *parent)
36 : QItemSelectionModel(model, parent),
37 d_ptr(new KLinkItemSelectionModelPrivate(this, model, proxySelector))
39 connect(proxySelector, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(sourceSelectionChanged(QItemSelection, QItemSelection)));
42 KLinkItemSelectionModel::~KLinkItemSelectionModel()
47 void KLinkItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
49 //std::cout<<"KLinkItemSelectionModel::select(const QModelIndex &index"<<std::endl;
50 Q_D(KLinkItemSelectionModel);
51 // When an item is removed, the current index is set to the top index in the model.
52 // That causes a selectionChanged signal with a selection which we do not want.
53 if (d->m_ignoreCurrentChanged) {
56 QItemSelectionModel::select(index, command);
58 d->m_linkedItemSelectionModel->select(d->m_indexMapper->mapSelectionLeftToRight(QItemSelection(index, index)), command);
60 d->m_linkedItemSelectionModel->clearSelection();
64 // // QAbstractProxyModel::mapSelectionFromSource creates invalid ranges to we filter
65 // // those out manually in a loop. Hopefully fixed in Qt 4.7.2, so we ifdef it out.
66 // // http://qt.gitorious.org/qt/qt/merge_requests/2474
67 // // http://qt.gitorious.org/qt/qt/merge_requests/831
68 // #if QT_VERSION < 0x040702
69 // #define RANGE_FIX_HACK
72 // #ifdef RANGE_FIX_HACK
73 // static QItemSelection klink_removeInvalidRanges(const QItemSelection &selection)
75 // QItemSelection result;
76 // Q_FOREACH(const QItemSelectionRange &range, selection)
78 // if (!range.isValid())
86 void KLinkItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
88 // std::cout<<"KLinkItemSelectionModel::select(const QItemSelection &selection"<<std::endl;
90 Q_FOREACH(const QItemSelectionRange &range, selection){
91 // if (range.isEmpty()){
92 // std::cout<<"range EMPTY"<<std::endl;
94 // std::cout<<"range NOT EMPTY"<<std::endl;
96 // if (range.isValid()){
97 // std::cout<<"range VALID"<<std::endl;
98 // std::cout<<"count =>"<<range.indexes().count()<<std::endl;
99 // Q_FOREACH(const QModelIndex &i, range.indexes())
100 // std::cout<< " KLinkItemSelectionModel::select =====> " << i.data().toString().toStdString() << std::endl;
102 // std::cout<<"range NOT VALID"<<std::endl;
107 Q_D(KLinkItemSelectionModel);
108 d->m_ignoreCurrentChanged = true;
109 #ifdef RANGE_FIX_HACK
110 QItemSelection _selection = klink_removeInvalidRanges(selection);
112 QItemSelection _selection = selection;
114 QItemSelectionModel::select(_selection, command);
115 Q_ASSERT(d->assertSelectionValid(_selection));
116 QItemSelection mappedSelection = d->m_indexMapper->mapSelectionLeftToRight(_selection);
117 Q_ASSERT(d->assertSelectionValid(mappedSelection));
118 d->m_linkedItemSelectionModel->select(mappedSelection, command);
119 d->m_ignoreCurrentChanged = false;
124 // #include "klinkitemselectionmodel_moc.cxx"
125 // #include "klinkitemselectionmodel.moc"