1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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::mapSelectionFromSource(const QItemSelection &sourceSelection) {
61 return proxyModel()->mapSelectionFromSource(sourceSelection);
63 return sourceSelection;
66 QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) {
68 return proxyModel()->mapToSource(proxyIndex);
73 QItemSelection MappedSelectionModel::mapSelectionToSource(const QItemSelection &proxySelection) {
75 return proxyModel()->mapSelectionToSource(proxySelection);
77 return proxySelection;
80 void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
81 QModelIndex mappedIndex = mapFromSource(index);
82 if(!isSelected(mappedIndex))
83 select(mappedIndex, command);
86 void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) {
87 QItemSelection mappedSelection = mapSelectionFromSource(selection);
88 if(mappedSelection != QItemSelectionModel::selection())
89 select(mappedSelection, command);
92 void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
93 QModelIndex mappedIndex = mapFromSource(index);
94 if(mappedIndex == currentIndex())
96 setCurrentIndex(mappedIndex, command);
100 void MappedSelectionModel::_currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
102 if(current.isValid())
103 emit mappedCurrentChanged(mapToSource(current));
106 void MappedSelectionModel::_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
109 emit mappedSelectionChanged(mapSelectionToSource(QItemSelectionModel::selection()));