fixed slow buffer switches
[quassel.git] / src / qtui / nicklistwidget.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 "nicklistwidget.h"
22
23 #include "buffer.h"
24 #include "nickview.h"
25 #include "client.h"
26 #include "networkmodel.h"
27 #include "nickviewfilter.h"
28
29 NickListWidget::NickListWidget(QWidget *parent)
30   : QWidget(parent),
31     _bufferModel(0),
32     _selectionModel(0)
33 {
34   ui.setupUi(this);
35 }
36
37 void NickListWidget::setModel(BufferModel *bufferModel) {
38   if(_bufferModel) {
39     disconnect(_bufferModel, 0, this, 0);
40   }
41   
42   _bufferModel = bufferModel;
43
44   if(bufferModel) {
45     connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
46             this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
47   }
48 }
49
50 void NickListWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
51   if(_selectionModel) {
52     disconnect(_selectionModel, 0, this, 0);
53   }
54
55   _selectionModel = selectionModel;
56
57   if(selectionModel) {
58     connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
59             this, SLOT(currentChanged(QModelIndex, QModelIndex)));
60   }
61 }
62
63
64 void NickListWidget::reset() {
65   NickView *nickView;
66   QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
67   while(iter != nickViews.end()) {
68     nickView = *iter;
69     iter = nickViews.erase(iter);
70     ui.stackedWidget->removeWidget(nickView);
71     nickView->deleteLater();
72   }
73 }
74
75
76 void NickListWidget::currentChanged(const QModelIndex &current, const QModelIndex &previous) {
77   Q_UNUSED(previous);
78   QVariant variant;
79
80   variant = current.data(NetworkModel::BufferIdRole);
81   if(!variant.isValid())
82     return;
83   setCurrentBuffer(variant.value<BufferId>());
84 }
85
86
87 void NickListWidget::setCurrentBuffer(BufferId bufferId) {
88   QModelIndex bufferIdx = Client::networkModel()->bufferIndex(bufferId);
89   
90   if(bufferIdx.data(NetworkModel::BufferTypeRole) != BufferInfo::ChannelBuffer) {
91     ui.stackedWidget->setCurrentWidget(ui.emptyPage);
92     return;
93   }
94
95   if(nickViews.contains(bufferId)) {
96     ui.stackedWidget->setCurrentWidget(nickViews.value(bufferId));
97   } else {
98     NickView *view = new NickView(this);
99     NickViewFilter *filter = new NickViewFilter(bufferId, Client::networkModel());
100     filter->setObjectName("Buffer " + QString::number(bufferId.toInt()));
101     view->setModel(filter);
102     view->setRootIndex(filter->mapFromSource(bufferIdx));
103     view->expandAll();
104     nickViews[bufferId] = view;
105     ui.stackedWidget->addWidget(view);
106     ui.stackedWidget->setCurrentWidget(view);
107   }
108 }
109
110
111 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
112   Q_ASSERT(model());
113   if(!parent.isValid()) {
114     // ok this means that whole networks are about to be removed
115     // we can't determine which buffers are affect, so we hope that all nets are removed
116     // this is the most common case (for example disconnecting from the core or terminating the clint)
117     reset();
118   } else {
119     // check if there are explicitly buffers removed
120     for(int i = start; i <= end; i++) {
121       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
122       if(!variant.isValid())
123         continue;
124
125       BufferId bufferId = qVariantValue<BufferId>(variant);
126       removeBuffer(bufferId);
127     }
128   }
129 }
130
131 void NickListWidget::removeBuffer(BufferId bufferId) {
132   if(!nickViews.contains(bufferId))
133     return;
134   
135   NickView *view = nickViews.take(bufferId);
136   ui.stackedWidget->removeWidget(view);
137   view->deleteLater();
138 }
139
140 QSize NickListWidget::sizeHint() const {
141   QWidget *currentWidget = ui.stackedWidget->currentWidget();
142   if(!currentWidget || currentWidget == ui.emptyPage)
143     return QSize(100, height());
144   else
145     return currentWidget->sizeHint();
146 }