YAY! Colors are back!
[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 #include "qtuisettings.h"
30
31 #include <QAction>
32 #include <QDebug>
33 #include <QEvent>
34 #include <QAbstractButton>
35
36 NickListWidget::NickListWidget(QWidget *parent)
37   : AbstractItemView(parent)
38 {
39   ui.setupUi(this);
40 }
41
42 QDockWidget *NickListWidget::dock() const {
43   QDockWidget *dock = qobject_cast<QDockWidget *>(parent());
44   if(dock)
45     return dock;
46   else
47     return 0;
48 }
49
50 void NickListWidget::showWidget(bool visible) {
51   if(!selectionModel())
52     return;
53
54   QModelIndex currentIndex = selectionModel()->currentIndex();
55   if(currentIndex.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer) {
56     QDockWidget *dock_ = dock();
57     if(!dock_)
58       return;
59
60     if(visible)
61       dock_->show();
62     else
63       dock_->close();
64   }
65 }
66
67 void NickListWidget::currentChanged(const QModelIndex &current, 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>();
71
72   if(bufferType != BufferInfo::ChannelBuffer) {
73     ui.stackedWidget->setCurrentWidget(ui.emptyPage);
74     return;
75   }
76
77   // See NickListDock::NickListDock() below
78 //   if(bufferType != BufferInfo::ChannelBuffer) {
79 //     ui.stackedWidget->setCurrentWidget(ui.emptyPage);
80 //     QDockWidget *dock_ = dock();
81 //     if(dock_) {
82 //       dock_->close();
83 //     }
84 //     return;
85 //   } else {
86 //     QDockWidget *dock_ = dock();
87 //     if(dock_ && dock_->toggleViewAction()->isChecked()) {
88 //       dock_->show();
89 //     }
90 //   }
91
92   if(newBufferId == oldBufferId)
93     return;
94
95   if(nickViews.contains(newBufferId)) {
96     ui.stackedWidget->setCurrentWidget(nickViews.value(newBufferId));
97   } else {
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));
103     nickViews[newBufferId] = view;
104     ui.stackedWidget->addWidget(view);
105     ui.stackedWidget->setCurrentWidget(view);
106   }
107 }
108
109 void NickListWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
110   Q_ASSERT(model());
111   if(!parent.isValid()) {
112     // ok this means that whole networks are about to be removed
113     // we can't determine which buffers are affect, so we hope that all nets are removed
114     // this is the most common case (for example disconnecting from the core or terminating the clint)
115     NickView *nickView;
116     QHash<BufferId, NickView *>::iterator iter = nickViews.begin();
117     while(iter != nickViews.end()) {
118       nickView = *iter;
119       iter = nickViews.erase(iter);
120       ui.stackedWidget->removeWidget(nickView);
121       QAbstractItemModel *model = nickView->model();
122       nickView->setModel(0);
123       if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
124         filter->setSourceModel(0);
125       model->deleteLater();
126       nickView->deleteLater();
127     }
128   } else {
129     // check if there are explicitly buffers removed
130     for(int i = start; i <= end; i++) {
131       QVariant variant = parent.child(i,0).data(NetworkModel::BufferIdRole);
132       if(!variant.isValid())
133         continue;
134
135       BufferId bufferId = qVariantValue<BufferId>(variant);
136       removeBuffer(bufferId);
137     }
138   }
139 }
140
141 void NickListWidget::removeBuffer(BufferId bufferId) {
142   if(!nickViews.contains(bufferId))
143     return;
144
145   NickView *view = nickViews.take(bufferId);
146   ui.stackedWidget->removeWidget(view);
147   QAbstractItemModel *model = view->model();
148   view->setModel(0);
149   if(QSortFilterProxyModel *filter = qobject_cast<QSortFilterProxyModel *>(model))
150     filter->setSourceModel(0);
151   model->deleteLater();
152   view->deleteLater();
153 }
154
155 QSize NickListWidget::sizeHint() const {
156   QWidget *currentWidget = ui.stackedWidget->currentWidget();
157   if(!currentWidget || currentWidget == ui.emptyPage)
158     return QSize(100, height());
159   else
160     return currentWidget->sizeHint();
161 }
162
163
164 // ==============================
165 //  NickList Dock
166 // ==============================
167 NickListDock::NickListDock(const QString &title, QWidget *parent)
168   : QDockWidget(title, parent)
169 {
170   // THIS STUFF IS NEEDED FOR NICKLIST AUTOHIDE... 
171   // AS THIS BRINGS LOTS OF FUCKUPS WITH IT IT'S DEACTIVATED FOR NOW...
172   
173 //   QAction *toggleView = toggleViewAction();
174 //   disconnect(toggleView, SIGNAL(triggered(bool)), this, 0);
175 //   toggleView->setChecked(QtUiSettings().value("ShowNickList", QVariant(true)).toBool());
176
177 //   // reconnecting the closebuttons clicked signal to the action
178 //   foreach(QAbstractButton *button, findChildren<QAbstractButton *>()) {
179 //     if(disconnect(button, SIGNAL(clicked()), this, SLOT(close())))
180 //       connect(button, SIGNAL(clicked()), toggleView, SLOT(trigger()));
181 //   }
182 }
183
184 // NickListDock::~NickListDock() {
185 //   QtUiSettings().setValue("ShowNickList", toggleViewAction()->isChecked());
186 // }
187
188 // bool NickListDock::event(QEvent *event) {
189 //   switch (event->type()) {
190 //   case QEvent::Hide:
191 //   case QEvent::Show:
192 //     emit visibilityChanged(event->type() == QEvent::Show);
193 //     return QWidget::event(event);
194 //   default:
195 //     return QDockWidget::event(event);
196 //   }
197 // }