1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "selectionmodelsynchronizer.h"
24 #include <QAbstractItemModel>
25 #include "mappedselectionmodel.h"
29 SelectionModelSynchronizer::SelectionModelSynchronizer(QAbstractItemModel *parent)
35 SelectionModelSynchronizer::~SelectionModelSynchronizer() {
38 void SelectionModelSynchronizer::addSelectionModel(MappedSelectionModel *selectionmodel) {
39 if(selectionmodel->model() == model()) {
40 addRegularSelectionModel(selectionmodel);
44 if(selectionmodel->baseModel() != model()) {
45 qWarning() << "cannot Syncronize SelectionModel" << selectionmodel << "which has a different baseModel()";
49 connect(selectionmodel, SIGNAL(mappedCurrentChanged(QModelIndex)),
50 this, SLOT(_mappedCurrentChanged(QModelIndex)));
51 connect(selectionmodel, SIGNAL(mappedSelectionChanged(QItemSelection)),
52 this, SLOT(_mappedSelectionChanged(QItemSelection)));
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)));
60 void SelectionModelSynchronizer::addRegularSelectionModel(QItemSelectionModel *selectionmodel) {
61 if(selectionmodel->model() != model()) {
62 qWarning() << "cannot Syncronize QItemSelectionModel" << selectionmodel << "which has a different model()";
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)));
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)));
77 void SelectionModelSynchronizer::removeSelectionModel(MappedSelectionModel *model) {
78 disconnect(model, 0, this, 0);
79 disconnect(this, 0, model, 0);
82 void SelectionModelSynchronizer::_mappedCurrentChanged(const QModelIndex ¤t) {
83 emit setCurrentIndex(current, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
86 void SelectionModelSynchronizer::_mappedSelectionChanged(const QItemSelection &selected) {
87 emit select(selected, QItemSelectionModel::ClearAndSelect);
90 void SelectionModelSynchronizer::_regularCurrentChanged(const QModelIndex &newCurrent, const QModelIndex &oldCurrent) {
92 emit setCurrentIndex(newCurrent, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
95 void SelectionModelSynchronizer::_regularSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
98 QItemSelectionModel *selectionModel = qobject_cast<QItemSelectionModel *>(sender());
99 emit select(selectionModel->selection(), QItemSelectionModel::ClearAndSelect);