714731a961c7336573285bfea11fd7f3993633fa
[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 "buffermodel.h"
28 #include "nickviewfilter.h"
29
30 NickListWidget::NickListWidget(QWidget *parent)
31   : AbstractItemView(parent)
32 {
33   ui.setupUi(this);
34 }
35
36 void NickListWidget::currentChanged(const QModelIndex &current, 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>();
40
41   if(bufferType != BufferInfo::ChannelBuffer) {
42     ui.stackedWidget->setCurrentWidget(ui.emptyPage);
43     return;
44   }
45
46   if(newBufferId == oldBufferId)
47     return;
48
49   if(nickViews.contains(newBufferId)) {
50     ui.stackedWidget->setCurrentWidget(nickViews.value(newBufferId));
51   } else {
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));
57     view->expandAll();
58     nickViews[newBufferId] = view;
59     ui.stackedWidget->addWidget(view);
60     ui.stackedWidget->setCurrentWidget(view);
61   }
62 }
63
64 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
65   Q_ASSERT(model());
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)
70     NickView *nickView;
71     QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
72     while(iter != nickViews.end()) {
73       nickView = *iter;
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);
80       model->deleteLater();
81       nickView->deleteLater();
82     }
83   } else {
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())
88         continue;
89
90       BufferId bufferId = qVariantValue<BufferId>(variant);
91       removeBuffer(bufferId);
92     }
93   }
94 }
95
96 void NickListWidget::removeBuffer(BufferId bufferId) {
97   if(!nickViews.contains(bufferId))
98     return;
99
100   NickView *view = nickViews.take(bufferId);
101   ui.stackedWidget->removeWidget(view);
102   QAbstractItemModel *model = view->model();
103   view->setModel(0);
104   if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
105     filter->setSourceModel(0);
106   model->deleteLater();
107   view->deleteLater();
108 }
109
110 QSize NickListWidget::sizeHint() const {
111   QWidget *currentWidget = ui.stackedWidget->currentWidget();
112   if(!currentWidget || currentWidget == ui.emptyPage)
113     return QSize(100, height());
114   else
115     return currentWidget->sizeHint();
116 }