Trying to workaroundinate a weird bug with connection states not always being sent
[quassel.git] / src / client / mappedselectionmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "mappedselectionmodel.h"
22
23 #include <QItemSelectionModel>
24 #include <QAbstractItemModel>
25 #include <QAbstractProxyModel>
26 #include <QDebug>
27
28 MappedSelectionModel::MappedSelectionModel(QAbstractItemModel *model)
29   : QItemSelectionModel(model)
30 {
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)));
36 }
37
38 MappedSelectionModel::~MappedSelectionModel() {
39 }
40
41 const QAbstractItemModel *MappedSelectionModel::baseModel() const {
42   if(isProxyModel())
43     return proxyModel()->sourceModel();
44   else
45     return model();
46 }
47
48 const QAbstractProxyModel *MappedSelectionModel::proxyModel() const {
49   return qobject_cast<const QAbstractProxyModel *>(model());
50 }
51
52 QModelIndex MappedSelectionModel::mapFromSource(const QModelIndex &sourceIndex) {
53   if(isProxyModel())
54     return proxyModel()->mapFromSource(sourceIndex);
55   else
56     return sourceIndex;
57 }
58
59 QItemSelection MappedSelectionModel::mapSelectionFromSource(const QItemSelection &sourceSelection) {
60   if(isProxyModel())
61     return proxyModel()->mapSelectionFromSource(sourceSelection);
62   else
63     return sourceSelection;
64 }
65                                     
66 QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) {
67   if(isProxyModel())
68     return proxyModel()->mapToSource(proxyIndex);
69   else
70     return proxyIndex;
71 }
72
73 QItemSelection MappedSelectionModel::mapSelectionToSource(const QItemSelection &proxySelection) {
74   if(isProxyModel())
75     return proxyModel()->mapSelectionToSource(proxySelection);
76   else
77     return proxySelection;
78 }
79                                                                         
80 void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
81   QModelIndex mappedIndex = mapFromSource(index);
82   if(!isSelected(mappedIndex))
83     select(mappedIndex, command);
84 }
85
86 void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) {
87   QItemSelection mappedSelection = mapSelectionFromSource(selection);
88   if(mappedSelection != QItemSelectionModel::selection())
89     select(mappedSelection, command);  
90 }
91
92 void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
93   QModelIndex mappedIndex = mapFromSource(index);
94   if(mappedIndex == currentIndex())
95     return;
96
97   if(mappedIndex.isValid())
98     setCurrentIndex(mappedIndex, command);
99   else if(hasSelection())
100     setCurrentIndex(currentIndex(), QItemSelectionModel::Clear);
101 }
102
103
104 void MappedSelectionModel::_currentChanged(const QModelIndex &current, const QModelIndex &previous) {
105   Q_UNUSED(previous)
106   emit mappedCurrentChanged(mapToSource(current));
107 }
108
109 void MappedSelectionModel::_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
110   Q_UNUSED(selected)
111   Q_UNUSED(deselected)
112   emit mappedSelectionChanged(mapSelectionToSource(QItemSelectionModel::selection()));
113 }
114