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 "nicklistwidget.h"
26 #include "networkmodel.h"
27 #include "nickviewfilter.h"
29 NickListWidget::NickListWidget(QWidget *parent)
37 void NickListWidget::setModel(BufferModel *bufferModel) {
39 disconnect(_bufferModel, 0, this, 0);
42 _bufferModel = bufferModel;
45 connect(bufferModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
46 this, SLOT(rowsAboutToBeRemoved(QModelIndex, int, int)));
50 void NickListWidget::setSelectionModel(QItemSelectionModel *selectionModel) {
52 disconnect(_selectionModel, 0, this, 0);
55 _selectionModel = selectionModel;
58 connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
59 this, SLOT(currentChanged(QModelIndex, QModelIndex)));
64 void NickListWidget::reset() {
66 QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
67 while(iter != nickViews.end()) {
69 iter = nickViews.erase(iter);
70 ui.stackedWidget->removeWidget(nickView);
71 nickView->deleteLater();
76 void NickListWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
80 variant = current.data(NetworkModel::BufferIdRole);
81 if(!variant.isValid())
83 setCurrentBuffer(variant.value<BufferId>());
87 void NickListWidget::setCurrentBuffer(BufferId bufferId) {
88 QModelIndex bufferIdx = Client::networkModel()->bufferIndex(bufferId);
90 if(bufferIdx.data(NetworkModel::BufferTypeRole) != BufferInfo::ChannelBuffer) {
91 ui.stackedWidget->setCurrentWidget(ui.emptyPage);
95 if(nickViews.contains(bufferId)) {
96 ui.stackedWidget->setCurrentWidget(nickViews.value(bufferId));
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));
104 nickViews[bufferId] = view;
105 ui.stackedWidget->addWidget(view);
106 ui.stackedWidget->setCurrentWidget(view);
111 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
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)
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())
125 BufferId bufferId = qVariantValue<BufferId>(variant);
126 removeBuffer(bufferId);
131 void NickListWidget::removeBuffer(BufferId bufferId) {
132 if(!nickViews.contains(bufferId))
135 NickView *view = nickViews.take(bufferId);
136 ui.stackedWidget->removeWidget(view);
140 QSize NickListWidget::sizeHint() const {
141 QWidget *currentWidget = ui.stackedWidget->currentWidget();
142 if(!currentWidget || currentWidget == ui.emptyPage)
143 return QSize(100, height());
145 return currentWidget->sizeHint();