Make the newly arrived topicbutton display background colors and font styles.
[quassel.git] / src / uisupport / nickview.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 <QHeaderView>
22 #include <QDebug>
23 #include <QMenu>
24
25 #include "nickview.h"
26 #include "nickviewfilter.h"
27 #include "networkmodel.h"
28 #include "types.h"
29 #include "client.h"
30
31
32 NickView::NickView(QWidget *parent)
33   : QTreeView(parent),
34     _sizeHint(QTreeView::sizeHint())
35 {
36   setIndentation(10);
37   setAnimated(true);
38   header()->hide();
39   setSortingEnabled(true);
40   sortByColumn(0, Qt::AscendingOrder);
41
42   setContextMenuPolicy(Qt::CustomContextMenu);
43
44   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
45           this, SLOT(showContextMenu(const QPoint&)));
46   connect(this, SIGNAL(activated( const QModelIndex& )),
47           this, SLOT(startQuery( const QModelIndex& )));
48 }
49
50 NickView::~NickView() {
51 }
52
53 void NickView::init() {
54   if(!model())
55     return;
56
57   for(int i = 1; i < model()->columnCount(); i++)
58     setColumnHidden(i, true);
59
60   expandAll();
61   updateSizeHint();
62 }
63
64 void NickView::setModel(QAbstractItemModel *model) {
65   QTreeView::setModel(model);
66   init();
67 }
68
69 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
70   QTreeView::rowsInserted(parent, start, end);
71   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent))
72     expand(parent);
73   updateSizeHint();
74 }
75
76 void NickView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
77   QTreeView::rowsAboutToBeRemoved(parent, start, end);
78   updateSizeHint();
79 }
80
81 void NickView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
82   QTreeView::dataChanged(topLeft, bottomRight);
83   updateSizeHint();
84 }
85
86 QString NickView::nickFromModelIndex(const QModelIndex & index) {
87   QString nick = index.sibling(index.row(), 0).data().toString();
88   return nick;
89 }
90
91 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
92   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
93   return bufferInfo;
94 }
95
96 void NickView::showContextMenu(const QPoint & pos ) {
97   QModelIndex index = indexAt(pos);
98   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
99
100   QString nick = nickFromModelIndex(index);
101
102   QMenu nickContextMenu(this);
103
104   QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
105   QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
106   QAction *pingAction = nickContextMenu.addAction(tr("PING"));
107
108   nickContextMenu.addSeparator();
109
110   QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
111   QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
112   QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
113   QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
114   QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
115
116   QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
117   //TODO: add kick message from network identity (kick reason)
118   QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
119   QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
120   kickBanMenu->setEnabled(false);
121   QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
122   ignoreAction->setEnabled(false);
123
124   nickContextMenu.addSeparator();
125
126   QAction *queryAction = nickContextMenu.addAction(tr("Query"));
127   QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
128   dccChatAction->setEnabled(false);
129   QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
130   sendFileAction->setEnabled(false);
131
132   QAction *action = nickContextMenu.exec(QCursor::pos());
133   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
134
135   if(action == whoisAction)         { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
136   else if(action == versionAction)  { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
137   else if(action == pingAction)     { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
138
139   else if(action == opAction)       { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
140   else if(action == deOpAction)     { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
141   else if(action == voiceAction)    { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
142   else if(action == deVoiceAction)  { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
143
144   else if(action == kickAction)     { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
145   else if(action == kickBanAction)  { executeCommand(bufferInfo, QString("/KICKBAN %1").arg(nick)); }
146   else if(action == queryAction)    { executeCommand(bufferInfo, QString("/QUERY %1").arg(nick)); }
147
148 }
149
150 void NickView::startQuery(const QModelIndex & index) {
151   QString nick = nickFromModelIndex(index);
152   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
153   executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
154 }
155
156 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
157   Client::instance()->userInput(bufferInfo, command);
158 }
159
160 void NickView::updateSizeHint() {
161   if(!model())
162     return;
163
164   int columnSize = 0;
165   for(int i = 0; i < model()->columnCount(); i++) {
166     if(!isColumnHidden(i))
167       columnSize += sizeHintForColumn(i);
168   }
169
170   _sizeHint = QSize(columnSize, 50);
171 }
172
173 QSize NickView::sizeHint() const {
174   return _sizeHint;
175 }