1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "mappedselectionmodel.h"
23 #include <QItemSelectionModel>
24 #include <QAbstractItemModel>
25 #include <QAbstractProxyModel>
28 MappedSelectionModel::MappedSelectionModel(QAbstractItemModel *model)
29 : QItemSelectionModel(model)
31 _isProxyModel = (bool)proxyModel();
32 connect(this, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
33 this, SLOT(_currentChanged(QModelIndex, QModelIndex)));
34 connect(this, SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
35 this, SLOT(_selectionChanged(QItemSelection, QItemSelection)));
38 MappedSelectionModel::~MappedSelectionModel() {
41 const QAbstractItemModel *MappedSelectionModel::baseModel() const {
43 return proxyModel()->sourceModel();
48 const QAbstractProxyModel *MappedSelectionModel::proxyModel() const {
49 return qobject_cast<const QAbstractProxyModel *>(model());
52 QModelIndex MappedSelectionModel::mapFromSource(const QModelIndex &sourceIndex) {
54 return proxyModel()->mapFromSource(sourceIndex);
59 QItemSelection MappedSelectionModel::mapFromSource(const QItemSelection &sourceSelection) {
61 QItemSelection mappedSelection;
62 foreach(QItemSelectionRange range, sourceSelection) {
63 QModelIndex topleft = mapFromSource(range.topLeft());
64 QModelIndex bottomright = mapFromSource(range.bottomRight());
65 if(topleft.isValid() && bottomright.isValid())
66 mappedSelection << QItemSelectionRange(topleft, bottomright);
68 Q_ASSERT(!topleft.isValid() && !bottomright.isValid());
70 return mappedSelection;
72 return sourceSelection;
76 QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) {
78 return proxyModel()->mapToSource(proxyIndex);
83 QItemSelection MappedSelectionModel::mapToSource(const QItemSelection &proxySelection) {
85 QItemSelection mappedSelection;
86 foreach(QItemSelectionRange range, proxySelection) {
87 mappedSelection << QItemSelectionRange(mapToSource(range.topLeft()), mapToSource(range.bottomRight()));
89 return mappedSelection;
91 return proxySelection;
95 void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
96 QModelIndex mappedIndex = mapFromSource(index);
97 if(!isSelected(mappedIndex))
98 select(mappedIndex, command);
101 void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) {
102 QItemSelection mappedSelection = mapFromSource(selection);
103 if(mappedSelection != QItemSelectionModel::selection())
104 select(mappedSelection, command);
107 void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
108 QModelIndex mappedIndex = mapFromSource(index);
109 if(mappedIndex == currentIndex())
112 if(mappedIndex.isValid())
113 setCurrentIndex(mappedIndex, command);
114 else if(hasSelection())
115 setCurrentIndex(currentIndex(), QItemSelectionModel::Clear);
119 void MappedSelectionModel::_currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
121 emit mappedCurrentChanged(mapToSource(current));
124 void MappedSelectionModel::_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
127 emit mappedSelectionChanged(mapToSource(QItemSelectionModel::selection()));