- New SignalProxy which is kinda sorta based on the idea of the QxtRPCPeer, though...
[quassel.git] / src / client / selectionmodelsynchronizer.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 "selectionmodelsynchronizer.h"
22
23
24 #include <QAbstractItemModel>
25 #include "mappedselectionmodel.h"
26
27 #include <QDebug>
28
29 SelectionModelSynchronizer::SelectionModelSynchronizer(QAbstractItemModel *parent)
30   : QObject(parent),
31     _model(parent)
32 {
33 }
34
35 SelectionModelSynchronizer::~SelectionModelSynchronizer() {
36 }
37
38 void SelectionModelSynchronizer::addSelectionModel(MappedSelectionModel *selectionmodel) {
39   if(selectionmodel->baseModel() != model()) {
40     qWarning() << "cannot Syncronize SelectionModel" << selectionmodel << "which has a different baseModel()";
41     return;
42   }
43
44   connect(selectionmodel, SIGNAL(mappedCurrentChanged(QModelIndex)),
45           this, SLOT(_mappedCurrentChanged(QModelIndex)));
46   connect(selectionmodel, SIGNAL(mappedSelectionChanged(QItemSelection)),
47           this, SLOT(_mappedSelectionChanged(QItemSelection)));
48   
49   connect(this, SIGNAL(setCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)),
50           selectionmodel, SLOT(mappedSetCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)));
51   connect(this, SIGNAL(select(QItemSelection, QItemSelectionModel::SelectionFlags)),
52           selectionmodel, SLOT(mappedSelect(QItemSelection, QItemSelectionModel::SelectionFlags)));
53   
54 }
55
56 void SelectionModelSynchronizer::removeSelectionModel(MappedSelectionModel *model) {
57   disconnect(model, 0, this, 0);
58   disconnect(this, 0, model, 0);
59 }
60
61 void SelectionModelSynchronizer::_mappedCurrentChanged(const QModelIndex &current) {
62   emit setCurrentIndex(current, QItemSelectionModel::ClearAndSelect);
63 }
64
65 void SelectionModelSynchronizer::_mappedSelectionChanged(const QItemSelection &selected) {
66   emit select(selected, QItemSelectionModel::ClearAndSelect);
67 }