Finally sanitizificat0red the mess and #ifdef hell with main.cpp, Global:: and friends
[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 <QAbstractItemView>
24
25 #include "client.h"
26 #include "mappedselectionmodel.h"
27 #include "networkmodel.h"
28 #include "quassel.h"
29
30 BufferModel::BufferModel(NetworkModel *parent)
31   : QSortFilterProxyModel(parent),
32     _selectionModelSynchronizer(this)
33 {
34   setSourceModel(parent);
35   if(Quassel::isOptionSet("debugbufferswitches")) {
36     connect(_selectionModelSynchronizer.selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
37             this, SLOT(debug_currentChanged(const QModelIndex &, const QModelIndex &)));
38   }
39 }
40
41 bool BufferModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const {
42   Q_UNUSED(sourceRow);
43   // only networks and buffers are allowed
44   if(!parent.isValid())
45     return true;
46   if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
47     return true;
48
49   return false;
50 }
51
52 void BufferModel::synchronizeSelectionModel(MappedSelectionModel *selectionModel) {
53   _selectionModelSynchronizer.addSelectionModel(selectionModel);
54 }
55
56 void BufferModel::synchronizeView(QAbstractItemView *view) {
57   MappedSelectionModel *mappedSelectionModel = new MappedSelectionModel(view->model());
58   _selectionModelSynchronizer.addSelectionModel(mappedSelectionModel);
59   Q_ASSERT(mappedSelectionModel);
60   delete view->selectionModel();
61   view->setSelectionModel(mappedSelectionModel);
62 }
63
64 void BufferModel::setCurrentIndex(const QModelIndex &newCurrent) {
65   _selectionModelSynchronizer.selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::Current);
66   _selectionModelSynchronizer.selectionModel()->select(newCurrent, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
67 }
68
69 void BufferModel::switchToBuffer(const BufferId &bufferId) {
70   QModelIndex source_index = Client::networkModel()->bufferIndex(bufferId);
71   setCurrentIndex(mapFromSource(source_index));
72 }
73
74 void BufferModel::switchToBufferIndex(const QModelIndex &bufferIdx) {
75   // we accept indexes that directly belong to us or our parent - nothing else
76   if(bufferIdx.model() == this) {
77     setCurrentIndex(bufferIdx);
78     return;
79   }
80
81   if(bufferIdx.model() == sourceModel()) {
82     setCurrentIndex(mapFromSource(bufferIdx));
83     return;
84   }
85
86   qWarning() << "BufferModel::switchToBufferIndex(const QModelIndex &):" << bufferIdx << "does not belong to BufferModel or NetworkModel";
87 }
88
89 void BufferModel::debug_currentChanged(QModelIndex current, QModelIndex previous) {
90   Q_UNUSED(previous);
91   qDebug() << "Switched current Buffer: " << current << current.data().toString() << "Buffer:" << current.data(NetworkModel::BufferIdRole).value<BufferId>();
92 }