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"
25 #include "networkmodel.h"
26 #include "buffermodel.h"
27 #include "nickviewfilter.h"
28 #include "qtuisettings.h"
33 #include <QAbstractButton>
35 NickListWidget::NickListWidget(QWidget *parent)
36 : AbstractItemView(parent)
41 QDockWidget *NickListWidget::dock() const {
42 QDockWidget *dock = qobject_cast<QDockWidget *>(parent());
49 void NickListWidget::showWidget(bool visible) {
53 QModelIndex currentIndex = selectionModel()->currentIndex();
54 if(currentIndex.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
55 QDockWidget *dock_ = dock();
66 void NickListWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
67 BufferInfo::Type bufferType = (BufferInfo::Type)current.data(NetworkModel::BufferTypeRole).toInt();
68 BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
69 BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
71 if(bufferType != BufferInfo::ChannelBuffer) {
72 ui.stackedWidget->setCurrentWidget(ui.emptyPage);
76 // See NickListDock::NickListDock() below
77 // if(bufferType != BufferInfo::ChannelBuffer) {
78 // ui.stackedWidget->setCurrentWidget(ui.emptyPage);
79 // QDockWidget *dock_ = dock();
85 // QDockWidget *dock_ = dock();
86 // if(dock_ && dock_->toggleViewAction()->isChecked()) {
91 if(newBufferId == oldBufferId)
94 if(nickViews.contains(newBufferId)) {
95 ui.stackedWidget->setCurrentWidget(nickViews.value(newBufferId));
97 NickView *view = new NickView(this);
98 NickViewFilter *filter = new NickViewFilter(newBufferId, Client::networkModel());
99 view->setModel(filter);
100 QModelIndex source_current = Client::bufferModel()->mapToSource(current);
101 view->setRootIndex(filter->mapFromSource(source_current));
102 nickViews[newBufferId] = view;
103 ui.stackedWidget->addWidget(view);
104 ui.stackedWidget->setCurrentWidget(view);
108 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
110 if(!parent.isValid()) {
111 // ok this means that whole networks are about to be removed
112 // we can't determine which buffers are affect, so we hope that all nets are removed
113 // this is the most common case (for example disconnecting from the core or terminating the clint)
115 QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
116 while(iter != nickViews.end()) {
118 iter = nickViews.erase(iter);
119 ui.stackedWidget->removeWidget(nickView);
120 QAbstractItemModel *model = nickView->model();
121 nickView->setModel(0);
122 if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
123 filter->setSourceModel(0);
124 model->deleteLater();
125 nickView->deleteLater();
128 // check if there are explicitly buffers removed
129 for(int i = start; i <= end; i++) {
130 QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
131 if(!variant.isValid())
134 BufferId bufferId = qVariantValue<BufferId>(variant);
135 removeBuffer(bufferId);
140 void NickListWidget::removeBuffer(BufferId bufferId) {
141 if(!nickViews.contains(bufferId))
144 NickView *view = nickViews.take(bufferId);
145 ui.stackedWidget->removeWidget(view);
146 QAbstractItemModel *model = view->model();
148 if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
149 filter->setSourceModel(0);
150 model->deleteLater();
154 QSize NickListWidget::sizeHint() const {
155 QWidget *currentWidget = ui.stackedWidget->currentWidget();
156 if(!currentWidget || currentWidget == ui.emptyPage)
157 return QSize(100, height());
159 return currentWidget->sizeHint();
163 // ==============================
165 // ==============================
166 NickListDock::NickListDock(const QString &title, QWidget *parent)
167 : QDockWidget(title, parent)
169 // THIS STUFF IS NEEDED FOR NICKLIST AUTOHIDE...
170 // AS THIS BRINGS LOTS OF FUCKUPS WITH IT IT'S DEACTIVATED FOR NOW...
172 // QAction *toggleView = toggleViewAction();
173 // disconnect(toggleView, SIGNAL(triggered(bool)), this, 0);
174 // toggleView->setChecked(QtUiSettings().value("ShowNickList", QVariant(true)).toBool());
176 // // reconnecting the closebuttons clicked signal to the action
177 // foreach(QAbstractButton *button, findChildren<QAbstractButton *>()) {
178 // if(disconnect(button, SIGNAL(clicked()), this, SLOT(close())))
179 // connect(button, SIGNAL(clicked()), toggleView, SLOT(trigger()));
183 // NickListDock::~NickListDock() {
184 // QtUiSettings().setValue("ShowNickList", toggleViewAction()->isChecked());
187 // bool NickListDock::event(QEvent *event) {
188 // switch (event->type()) {
189 // case QEvent::Hide:
190 // case QEvent::Show:
191 // emit visibilityChanged(event->type() == QEvent::Show);
192 // return QWidget::event(event);
194 // return QDockWidget::event(event);