Aaaand renaming NickModel again. Oh well.
[quassel.git] / src / client / mappedselectionmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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::mapFromSource(const QItemSelection &sourceSelection) {
60   if(isProxyModel()) {
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);
67       else 
68         Q_ASSERT(!topleft.isValid() && !bottomright.isValid());
69     }
70     return mappedSelection;
71   } else {
72     return sourceSelection;
73   }
74 }
75                                     
76 QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) {
77   if(isProxyModel())
78     return proxyModel()->mapToSource(proxyIndex);
79   else
80     return proxyIndex;
81 }
82
83 QItemSelection MappedSelectionModel::mapToSource(const QItemSelection &proxySelection) {
84   if(isProxyModel()) {
85     QItemSelection mappedSelection;
86     foreach(QItemSelectionRange range, proxySelection) {
87       mappedSelection << QItemSelectionRange(mapToSource(range.topLeft()), mapToSource(range.bottomRight()));
88     }
89     return mappedSelection;
90   } else {
91     return proxySelection;
92   }
93 }
94                                                                         
95 void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
96   QModelIndex mappedIndex = mapFromSource(index);
97   if(!isSelected(mappedIndex))
98     select(mappedIndex, command);
99 }
100
101 void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) {
102   QItemSelection mappedSelection = mapFromSource(selection);
103   if(mappedSelection != QItemSelectionModel::selection())
104     select(mappedSelection, command);  
105 }
106
107 void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
108   QModelIndex mappedIndex = mapFromSource(index);
109   if(mappedIndex == currentIndex())
110     return;
111
112   if(mappedIndex.isValid())
113     setCurrentIndex(mappedIndex, command);
114   else if(hasSelection())
115     setCurrentIndex(currentIndex(), QItemSelectionModel::Clear);
116 }
117
118
119 void MappedSelectionModel::_currentChanged(const QModelIndex &current, const QModelIndex &previous) {
120   Q_UNUSED(previous)
121   emit mappedCurrentChanged(mapToSource(current));
122 }
123
124 void MappedSelectionModel::_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
125   Q_UNUSED(selected)
126   Q_UNUSED(deselected)
127   emit mappedSelectionChanged(mapToSource(QItemSelectionModel::selection()));
128 }
129