6664758efe2477a87a7b004464e56b1e080646fd
[quassel.git] / gui / networkview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 "global.h"
22 #include "networkview.h"
23
24 NetworkViewWidget::NetworkViewWidget(QWidget *parent) : QWidget(parent) {
25   ui.setupUi(this);
26
27   //setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
28 }
29
30
31 QSize NetworkViewWidget::sizeHint() const {
32   return QSize(150,100);
33
34 }
35
36 /**************************************************************************/
37
38 NetworkView::NetworkView(QString n, int m, QStringList nets, QWidget *parent) : QDockWidget(parent) {
39   setObjectName(QString("View-"+n)); // should be unique for mainwindow state!
40   name = n; mode = m;
41   setWindowTitle(name);
42   networks = nets;
43   currentBuffer = 0;
44   setWidget(new NetworkViewWidget(this));
45   tree = qobject_cast<NetworkViewWidget*>(widget())->tree();
46   tree->header()->hide();
47   tree->setSortingEnabled(true);
48   tree->setRootIsDecorated(true);
49   tree->setIndentation(10);
50   //tree->setAnimated(true);
51   connect(tree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(itemClicked(QTreeWidgetItem*)));
52   connect(tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(itemDoubleClicked(QTreeWidgetItem*)));
53   connect(this, SIGNAL(fakeUserInput(BufferId, QString)), guiProxy, SLOT(gsUserInput(BufferId, QString)));
54   
55 }
56
57 void NetworkView::setBuffers(QList<Buffer *> buffers) {
58   tree->clear(); bufitems.clear(); netitems.clear();
59   foreach(Buffer *b, buffers) {
60     bufferUpdated(b);
61   }
62 }
63
64 void NetworkView::bufferUpdated(Buffer *b) {
65   if(bufitems.contains(b)) {
66     // FIXME this looks ugly
67     /*
68       this is actually fugly! :) - EgS
69       if anyone else wonders what this does: it takes the TreeItem related to the buffer out of the parents child list
70       therefore we need to know the childs index
71     */
72     QTreeWidgetItem *item = bufitems[b]->parent()->takeChild(bufitems[b]->parent()->indexOfChild(bufitems[b]));
73     delete item;
74     bufitems.remove(b);
75   }
76   if(shouldShow(b)) {
77     QString net = b->networkName();
78     QString buf = b->bufferName();
79     QTreeWidgetItem *item;
80     QStringList label;
81     if(b->bufferType() == Buffer::ServerBuffer) label << tr("Status");
82     else label << buf;
83     if((mode & SomeNets) || ( mode & AllNets)) {
84       if(!netitems.contains(net)) {
85         netitems[net] = new QTreeWidgetItem(tree, QStringList(net));
86         netitems[net]->setFlags(Qt::ItemIsEnabled);
87       }
88       QTreeWidgetItem *ni = netitems[net];
89       ni->setExpanded(true);
90       ni->setFlags(Qt::ItemIsEnabled);
91       item = new QTreeWidgetItem(ni, label);
92     } else {
93       item = new QTreeWidgetItem(label);
94     }
95     //item->setFlags(Qt::ItemIsEnabled);
96     bufitems[b] = item;
97     // TODO Use style engine!
98     if(!b->isActive()) {
99       item->setForeground(0, QColor("grey"));
100     }
101     if(b == currentBuffer) {
102       // this fixes the multiple simultaneous selections
103       emit bufferSelected(b);
104       //item->setSelected(true);
105     }
106   }
107   foreach(QString key, netitems.keys()) {
108     if(!netitems[key]->childCount()) {
109       delete netitems[key];
110       QTreeWidgetItem *item = netitems[key]->parent()->takeChild(netitems[key]->parent()->indexOfChild(netitems[key]));
111       delete item;
112       netitems.remove(key);
113     }
114   }
115 }
116
117 void NetworkView::bufferActivity(uint level, Buffer *b) {
118   QColor c;
119   if(bufitems.contains(b) and b != currentBuffer) {
120     if(level & Highlight) {
121       c = QColor(Qt::red);
122     } else if(level & NewMessage) {
123       c = QColor(Qt::darkYellow);
124     } else if(level & OtherActivity) {
125       c = QColor(Qt::darkGreen);
126     }
127     bufitems[b]->setForeground(0, c);
128   }
129 }
130
131 void NetworkView::clearActivity(Buffer *b) {
132   QColor c;
133   // it should be sane not to check if b is in bufitems since we just checked before calling this function
134   if(b->isActive()) {
135       c = QColor(Qt::black);
136   } else {
137     c = QColor(Qt::gray);
138   }
139   bufitems[b]->setForeground(0, c);
140 }
141
142 bool NetworkView::shouldShow(Buffer *b) {
143   // bool f = false;
144   if((mode & NoActive) && b->isActive()) return false;
145   if((mode & NoInactive) && !b->isActive()) return false;
146   if((mode & NoChannels) && b->bufferType() == Buffer::ChannelBuffer) return false;
147   if((mode & NoQueries) && b->bufferType() == Buffer::QueryBuffer) return false;
148   if((mode & NoServers) && b->bufferType() == Buffer::ServerBuffer) return false;
149   if((mode & SomeNets) && !networks.contains(b->networkName())) return false;
150   return true;
151 }
152
153 void NetworkView::bufferDestroyed(Buffer *b) {
154
155 }
156
157 void NetworkView::itemClicked(QTreeWidgetItem *item) {
158   Buffer *b = bufitems.key(item);
159   if(b) {
160     // there is a buffer associated with the item (aka: status/channel/query)
161     emit bufferSelected(b);
162   } else {
163     // network item
164     item->setExpanded(!item->isExpanded());
165   }
166 }
167
168 void NetworkView::itemDoubleClicked(QTreeWidgetItem *item) {
169   Buffer *b = bufitems.key(item);
170   if(b && Buffer::ChannelBuffer == b->bufferType()) {
171     emit fakeUserInput(b->bufferId(), QString("/join " + b->bufferName()));
172     emit bufferSelected(b);
173   }
174 }
175
176 void NetworkView::selectBuffer(Buffer *b) {
177   QTreeWidgetItem *item = 0;
178   if(bufitems.contains(b)) item = bufitems[b];
179   QList<QTreeWidgetItem *> sel = tree->selectedItems();
180   foreach(QTreeWidgetItem *i, sel) { if(i != item) i->setSelected(false); }
181   if(item) {
182     item->setSelected(true);
183     clearActivity(b);
184     currentBuffer = b;
185   } else {
186     currentBuffer = 0;
187   }
188 }