Quassel now handles even huge ChatScenes without slowing to a crawl.
[quassel.git] / src / client / buffermodel.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 "buffermodel.h"
22
23 #include "networkmodel.h"
24 #include "mappedselectionmodel.h"
25 #include "buffer.h"
26 #include <QAbstractItemView>
27
28 BufferModel::BufferModel(NetworkModel *parent)
29   : QSortFilterProxyModel(parent),
30     _selectionModelSynchronizer(this)
31 {
32   setSourceModel(parent);
33   if(QCoreApplication::instance()->arguments().contains("--debugbufferswitches")) {
34     connect(_selectionModelSynchronizer.selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
35             this, SLOT(debug_currentChanged(const QModelIndex &, const QModelIndex &)));
36   }
37 }
38
39 bool BufferModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const {
40   Q_UNUSED(sourceRow);
41   // only networks and buffers are allowed
42   if(!parent.isValid())
43     return true;
44   if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
45     return true;
46
47   return false;
48 }
49
50 void BufferModel::synchronizeSelectionModel(MappedSelectionModel *selectionModel) {
51   _selectionModelSynchronizer.addSelectionModel(selectionModel);
52 }
53
54 void BufferModel::synchronizeView(QAbstractItemView *view) {
55   MappedSelectionModel *mappedSelectionModel = new MappedSelectionModel(view->model());
56   _selectionModelSynchronizer.addSelectionModel(mappedSelectionModel);
57   Q_ASSERT(mappedSelectionModel);
58   delete view->selectionModel();
59   view->setSelectionModel(mappedSelectionModel);
60 }
61
62 QModelIndex BufferModel::currentIndex() {
63   return standardSelectionModel()->currentIndex();
64 }
65
66 void BufferModel::setCurrentIndex(const QModelIndex &newCurrent) {
67   _selectionModelSynchronizer.selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::Current);
68   _selectionModelSynchronizer.selectionModel()->select(newCurrent, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
69 }
70
71 void BufferModel::debug_currentChanged(QModelIndex current, QModelIndex previous) {
72   Q_UNUSED(previous);
73   qDebug() << "Switched current Buffer: " << current << current.data().toString() << "Buffer:" << current.data(NetworkModel::BufferIdRole).value<BufferId>();
74 }