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 #ifndef KLINKITEMSELECTIONMODEL_H
23 #define KLINKITEMSELECTIONMODEL_H
25 #include "HEXABLOCKGUI_Export.hxx"
28 #include <QItemSelectionModel>
29 #include <QAbstractProxyModel>
33 // #include "kdeui_export.h"
34 // #include "klinkitemselectionmodel_p.hxx"
36 #include "hexa_base.hxx"
37 #include "kmodelindexproxymapper.hxx"
39 class KLinkItemSelectionModelPrivate;
41 class HEXABLOCK_EXPORT KLinkItemSelectionModel : public QItemSelectionModel
45 KLinkItemSelectionModel(QAbstractItemModel *targetModel, QItemSelectionModel *linkedItemSelectionModel, QObject *parent = 0);
46 ~KLinkItemSelectionModel();
47 /* reimp */ void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
48 /* reimp */ void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
51 KLinkItemSelectionModelPrivate * const d_ptr;
54 Q_DECLARE_PRIVATE(KLinkItemSelectionModel)
55 Q_PRIVATE_SLOT( d_func(), void sourceSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
59 class KLinkItemSelectionModelPrivate
62 KLinkItemSelectionModelPrivate(KLinkItemSelectionModel *proxySelectionModel, QAbstractItemModel *model,
63 QItemSelectionModel *linkedItemSelectionModel)
64 : q_ptr(proxySelectionModel),
66 m_linkedItemSelectionModel(linkedItemSelectionModel),
67 m_ignoreCurrentChanged(false),
68 m_indexMapper(new KModelIndexProxyMapper(model, linkedItemSelectionModel->model(), proxySelectionModel))
72 Q_DECLARE_PUBLIC(KLinkItemSelectionModel)
73 KLinkItemSelectionModel * const q_ptr;
76 bool assertSelectionValid(const QItemSelection &selection) const {
77 foreach(const QItemSelectionRange &range, selection) {
78 // if (!range.isValid()) {
79 // kDebug() << selection;
82 Q_ASSERT(range.isValid());
87 void sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
91 Q_Q(KLinkItemSelectionModel);
92 QItemSelection _selected = selected;
93 QItemSelection _deselected = deselected;
94 Q_ASSERT(assertSelectionValid(_selected));
95 Q_ASSERT(assertSelectionValid(_deselected));
97 // std::cout << "XXXXXXXXXXXXX _selected.count() " << _selected.count() << std::endl;
98 // std::cout << "XXXXXXXXXXXXX _deselected.count() " << _deselected.count() << std::endl;
100 const QItemSelection mappedDeselection = m_indexMapper->mapSelectionRightToLeft(_deselected);
101 const QItemSelection mappedSelection = m_indexMapper->mapSelectionRightToLeft(_selected);
103 // const QItemSelection mappedDeselection = _deselected;
104 // const QItemSelection mappedSelection = _selected;
106 // std::cout << "XXXXXXXXXXXXX mappedSelection.count() " << mappedSelection.count() << std::endl;
107 // std::cout << "XXXXXXXXXXXXX mappedDeselection.count() " << mappedDeselection.count() << std::endl;
109 q->QItemSelectionModel::select(mappedDeselection, QItemSelectionModel::Deselect);
110 q->QItemSelectionModel::select(mappedSelection, QItemSelectionModel::Select);
112 // q->select(mappedDeselection, QItemSelectionModel::Deselect);
113 // q->select(mappedSelection, QItemSelectionModel::Select);
118 QAbstractItemModel * const m_model;
119 QItemSelectionModel * const m_linkedItemSelectionModel;
120 bool m_ignoreCurrentChanged;
121 KModelIndexProxyMapper * const m_indexMapper;