users who are away are now greyed out in the nickview
[quassel.git] / src / client / selectionmodelsynchronizer.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 "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->model() == model()) {
40     addRegularSelectionModel(selectionmodel);
41     return;
42   }
43   
44   if(selectionmodel->baseModel() != model()) {
45     qWarning() << "cannot Syncronize SelectionModel" << selectionmodel << "which has a different baseModel()";
46     return;
47   }
48
49   connect(selectionmodel, SIGNAL(mappedCurrentChanged(QModelIndex)),
50           this, SLOT(_mappedCurrentChanged(QModelIndex)));
51   connect(selectionmodel, SIGNAL(mappedSelectionChanged(QItemSelection)),
52           this, SLOT(_mappedSelectionChanged(QItemSelection)));
53   
54   connect(this, SIGNAL(setCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)),
55           selectionmodel, SLOT(mappedSetCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)));
56   connect(this, SIGNAL(select(QItemSelection, QItemSelectionModel::SelectionFlags)),
57           selectionmodel, SLOT(mappedSelect(QItemSelection, QItemSelectionModel::SelectionFlags)));
58 }
59
60 void SelectionModelSynchronizer::addRegularSelectionModel(QItemSelectionModel *selectionmodel) {
61   if(selectionmodel->model() != model()) {
62     qWarning() << "cannot Syncronize QItemSelectionModel" << selectionmodel << "which has a different model()";    
63     return;
64   }
65   connect(selectionmodel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
66           this, SLOT(_regularCurrentChanged(QModelIndex, QModelIndex)));
67   connect(selectionmodel, SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
68           this, SLOT(_regularSelectionChanged(QItemSelection, QItemSelection)));
69   
70   connect(this, SIGNAL(setCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)),
71           selectionmodel, SLOT(setCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)));
72   connect(this, SIGNAL(select(QItemSelection, QItemSelectionModel::SelectionFlags)),
73           selectionmodel, SLOT(select(QItemSelection, QItemSelectionModel::SelectionFlags)));
74   
75 }
76
77 void SelectionModelSynchronizer::removeSelectionModel(MappedSelectionModel *model) {
78   disconnect(model, 0, this, 0);
79   disconnect(this, 0, model, 0);
80 }
81
82 void SelectionModelSynchronizer::_mappedCurrentChanged(const QModelIndex &current) {
83   emit setCurrentIndex(current, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
84 }
85
86 void SelectionModelSynchronizer::_mappedSelectionChanged(const QItemSelection &selected) {
87   emit select(selected, QItemSelectionModel::ClearAndSelect);
88 }
89
90 void SelectionModelSynchronizer::_regularCurrentChanged(const QModelIndex &newCurrent, const QModelIndex &oldCurrent) {
91   Q_UNUSED(oldCurrent)
92   emit setCurrentIndex(newCurrent, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
93 }
94
95 void SelectionModelSynchronizer::_regularSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
96   Q_UNUSED(selected)
97   Q_UNUSED(deselected)
98   QItemSelectionModel *selectionModel = qobject_cast<QItemSelectionModel *>(sender());
99   emit select(selectionModel->selection(), QItemSelectionModel::ClearAndSelect);
100 }