merging branches/fu-debug with trunk :)
[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 <QLinkedList>
27 #include <QDebug>
28
29 MappedSelectionModel::MappedSelectionModel(QAbstractItemModel *model)
30   : QItemSelectionModel(model)
31 {
32 }
33
34 QModelIndex MappedSelectionModel::mapFromSource(const QModelIndex &sourceIndex) {
35   QModelIndex proxyIndex = sourceIndex;
36   QLinkedList<const QAbstractProxyModel *> proxies;
37   const QAbstractItemModel *baseModel = model();
38   const QAbstractProxyModel *proxyModel = 0;
39   while((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
40     proxies.push_back(proxyModel);
41     baseModel = proxyModel->sourceModel();
42     if(baseModel == sourceIndex.model())
43       break;
44   }
45
46   while(!proxies.isEmpty()) {
47     proxyModel = proxies.takeLast();
48     proxyIndex = proxyModel->mapFromSource(proxyIndex);
49   }
50   return proxyIndex;
51 }
52
53 QItemSelection MappedSelectionModel::mapSelectionFromSource(const QItemSelection &sourceSelection) {
54   if(sourceSelection.isEmpty())
55     return sourceSelection;
56
57   QItemSelection proxySelection = sourceSelection;
58   QLinkedList<const QAbstractProxyModel *> proxies;
59   const QAbstractItemModel *baseModel = model();
60   const QAbstractProxyModel *proxyModel = 0;
61   while((proxyModel = qobject_cast<const QAbstractProxyModel *>(baseModel)) != 0) {
62     proxies.push_back(proxyModel);
63     baseModel = proxyModel->sourceModel();
64     if(baseModel == sourceSelection.first().model())
65       break;
66   }
67
68   while(!proxies.isEmpty()) {
69     proxyModel = proxies.takeLast();
70     proxySelection = proxyModel->mapSelectionFromSource(proxySelection);
71   }
72   return proxySelection;
73 }
74                                     
75 void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
76   setCurrentIndex(mapFromSource(index), command);
77 }
78
79 void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) {
80   select(mapSelectionFromSource(selection), command);
81 }
82