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"
29 #include "qtuisettings.h"
34 #include <QAbstractButton>
36 NickListWidget::NickListWidget(QWidget *parent)
37 : AbstractItemView(parent)
42 QDockWidget *NickListWidget::dock() const {
43 QDockWidget *dock = qobject_cast<QDockWidget *>(parent());
50 void NickListWidget::showWidget(bool visible) {
54 QModelIndex currentIndex = selectionModel()->currentIndex();
55 if(currentIndex.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
56 QDockWidget *dock_ = dock();
67 void NickListWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
68 BufferInfo::Type bufferType = (BufferInfo::Type)current.data(NetworkModel::BufferTypeRole).toInt();
69 BufferId newBufferId = current.data(NetworkModel::BufferIdRole).value<BufferId>();
70 BufferId oldBufferId = previous.data(NetworkModel::BufferIdRole).value<BufferId>();
72 if(bufferType != BufferInfo::ChannelBuffer) {
73 ui.stackedWidget->setCurrentWidget(ui.emptyPage);
77 // See NickListDock::NickListDock() below
78 // if(bufferType != BufferInfo::ChannelBuffer) {
79 // ui.stackedWidget->setCurrentWidget(ui.emptyPage);
80 // QDockWidget *dock_ = dock();
86 // QDockWidget *dock_ = dock();
87 // if(dock_ && dock_->toggleViewAction()->isChecked()) {
92 if(newBufferId == oldBufferId)
95 if(nickViews.contains(newBufferId)) {
96 ui.stackedWidget->setCurrentWidget(nickViews.value(newBufferId));
98 NickView *view = new NickView(this);
99 NickViewFilter *filter = new NickViewFilter(newBufferId, Client::networkModel());
100 view->setModel(filter);
101 QModelIndex source_current = Client::bufferModel()->mapToSource(current);
102 view->setRootIndex(filter->mapFromSource(source_current));
104 nickViews[newBufferId] = view;
105 ui.stackedWidget->addWidget(view);
106 ui.stackedWidget->setCurrentWidget(view);
110 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
112 if(!parent.isValid()) {
113 // ok this means that whole networks are about to be removed
114 // we can't determine which buffers are affect, so we hope that all nets are removed
115 // this is the most common case (for example disconnecting from the core or terminating the clint)
117 QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
118 while(iter != nickViews.end()) {
120 iter = nickViews.erase(iter);
121 ui.stackedWidget->removeWidget(nickView);
122 QAbstractItemModel *model = nickView->model();
123 nickView->setModel(0);
124 if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
125 filter->setSourceModel(0);
126 model->deleteLater();
127 nickView->deleteLater();
130 // check if there are explicitly buffers removed
131 for(int i = start; i <= end; i++) {
132 QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
133 if(!variant.isValid())
136 BufferId bufferId = qVariantValue<BufferId>(variant);
137 removeBuffer(bufferId);
142 void NickListWidget::removeBuffer(BufferId bufferId) {
143 if(!nickViews.contains(bufferId))
146 NickView *view = nickViews.take(bufferId);
147 ui.stackedWidget->removeWidget(view);
148 QAbstractItemModel *model = view->model();
150 if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
151 filter->setSourceModel(0);
152 model->deleteLater();
156 QSize NickListWidget::sizeHint() const {
157 QWidget *currentWidget = ui.stackedWidget->currentWidget();
158 if(!currentWidget || currentWidget == ui.emptyPage)
159 return QSize(100, height());
161 return currentWidget->sizeHint();
165 // ==============================
167 // ==============================
168 NickListDock::NickListDock(const QString &title, QWidget *parent)
169 : QDockWidget(title, parent)
171 // THIS STUFF IS NEEDED FOR NICKLIST AUTOHIDE...
172 // AS THIS BRINGS LOTS OF FUCKUPS WITH IT IT'S DEACTIVATED FOR NOW...
174 // QAction *toggleView = toggleViewAction();
175 // disconnect(toggleView, SIGNAL(triggered(bool)), this, 0);
176 // toggleView->setChecked(QtUiSettings().value("ShowNickList", QVariant(true)).toBool());
178 // // reconnecting the closebuttons clicked signal to the action
179 // foreach(QAbstractButton *button, findChildren<QAbstractButton *>()) {
180 // if(disconnect(button, SIGNAL(clicked()), this, SLOT(close())))
181 // connect(button, SIGNAL(clicked()), toggleView, SLOT(trigger()));
185 // NickListDock::~NickListDock() {
186 // QtUiSettings().setValue("ShowNickList", toggleViewAction()->isChecked());
189 // bool NickListDock::event(QEvent *event) {
190 // switch (event->type()) {
191 // case QEvent::Hide:
192 // case QEvent::Show:
193 // emit visibilityChanged(event->type() == QEvent::Show);
194 // return QWidget::event(event);
196 // return QDockWidget::event(event);