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 "buffermodel.h"
28 #include "nickviewfilter.h"
30 NickListWidget::NickListWidget(QWidget *parent)
31 : AbstractItemView(parent)
36 void NickListWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
37 BufferInfo::Type bufferType = (BufferInfo::Type)current.data(NetworkModel::BufferTypeRole).toInt();
38 BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
39 BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
41 if(bufferType != BufferInfo::ChannelBuffer) {
42 ui.stackedWidget->setCurrentWidget(ui.emptyPage);
46 if(newBufferId == oldBufferId)
49 if(nickViews.contains(newBufferId)) {
50 ui.stackedWidget->setCurrentWidget(nickViews.value(newBufferId));
52 NickView *view = new NickView(this);
53 NickViewFilter *filter = new NickViewFilter(newBufferId, Client::networkModel());
54 view->setModel(filter);
55 QModelIndex source_current = Client::bufferModel()->mapToSource(current);
56 view->setRootIndex(filter->mapFromSource(source_current));
58 nickViews[newBufferId] = view;
59 ui.stackedWidget->addWidget(view);
60 ui.stackedWidget->setCurrentWidget(view);
64 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
66 if(!parent.isValid()) {
67 // ok this means that whole networks are about to be removed
68 // we can't determine which buffers are affect, so we hope that all nets are removed
69 // this is the most common case (for example disconnecting from the core or terminating the clint)
71 QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
72 while(iter != nickViews.end()) {
74 iter = nickViews.erase(iter);
75 ui.stackedWidget->removeWidget(nickView);
76 QAbstractItemModel *model = nickView->model();
77 nickView->setModel(0);
78 if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
79 filter->setSourceModel(0);
81 nickView->deleteLater();
84 // check if there are explicitly buffers removed
85 for(int i = start; i <= end; i++) {
86 QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
87 if(!variant.isValid())
90 BufferId bufferId = qVariantValue<BufferId>(variant);
91 removeBuffer(bufferId);
96 void NickListWidget::removeBuffer(BufferId bufferId) {
97 if(!nickViews.contains(bufferId))
100 NickView *view = nickViews.take(bufferId);
101 ui.stackedWidget->removeWidget(view);
102 QAbstractItemModel *model = view->model();
104 if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
105 filter->setSourceModel(0);
106 model->deleteLater();
110 QSize NickListWidget::sizeHint() const {
111 QWidget *currentWidget = ui.stackedWidget->currentWidget();
112 if(!currentWidget || currentWidget == ui.emptyPage)
113 return QSize(100, height());
115 return currentWidget->sizeHint();