bugfix #BR159 - context menu now opens/starts query the same way as double-clicking
[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 <QScrollBar>
23 #include <QDebug>
24 #include <QMenu>
25
26 #include "nickview.h"
27 #include "nickviewfilter.h"
28 #include "networkmodel.h"
29 #include "buffermodel.h"
30 #include "types.h"
31 #include "client.h"
32
33 struct LazySizeHint {
34   LazySizeHint() : size(QSize()), needsUpdate(true) {};
35   QSize size;
36   bool needsUpdate;
37 };
38
39 NickView::NickView(QWidget *parent)
40   : QTreeView(parent),
41     _sizeHint(new LazySizeHint())
42 {
43   setIndentation(10);
44   setAnimated(true);
45   header()->hide();
46   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47   setSortingEnabled(true);
48   sortByColumn(0, Qt::AscendingOrder);
49
50   setContextMenuPolicy(Qt::CustomContextMenu);
51
52   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
53           this, SLOT(showContextMenu(const QPoint&)));
54   connect(this, SIGNAL(activated( const QModelIndex& )),
55           this, SLOT(startQuery( const QModelIndex& )));
56 }
57
58 NickView::~NickView() {
59   delete _sizeHint;
60   _sizeHint = 0;
61 }
62
63 void NickView::init() {
64   if(!model())
65     return;
66
67   for(int i = 1; i < model()->columnCount(); i++)
68     setColumnHidden(i, true);
69
70   expandAll();
71   _sizeHint->needsUpdate = true;
72 }
73
74 void NickView::setModel(QAbstractItemModel *model) {
75   QTreeView::setModel(model);
76   init();
77 }
78
79 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
80   QTreeView::rowsInserted(parent, start, end);
81   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent))
82     expand(parent);
83   _sizeHint->needsUpdate = true;
84 }
85
86 void NickView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
87   QTreeView::rowsAboutToBeRemoved(parent, start, end);
88   _sizeHint->needsUpdate = true;
89 }
90
91 void NickView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
92   QTreeView::dataChanged(topLeft, bottomRight);
93   _sizeHint->needsUpdate = true;
94 }
95
96 QString NickView::nickFromModelIndex(const QModelIndex & index) {
97   QString nick = index.sibling(index.row(), 0).data().toString();
98   return nick;
99 }
100
101 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
102   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
103   return bufferInfo;
104 }
105
106 void NickView::showContextMenu(const QPoint & pos ) {
107   QModelIndex index = indexAt(pos);
108   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
109
110   QString nick = nickFromModelIndex(index);
111
112   QMenu nickContextMenu(this);
113
114   QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
115   QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
116   QAction *pingAction = nickContextMenu.addAction(tr("PING"));
117
118   nickContextMenu.addSeparator();
119
120   QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
121   QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
122   QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
123   QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
124   QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
125
126   QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
127   QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
128   QAction *banAction = kickBanMenu->addAction(tr("Ban %1").arg(nick));
129   QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
130   QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
131   ignoreAction->setEnabled(false);
132
133   nickContextMenu.addSeparator();
134
135   QAction *queryAction = nickContextMenu.addAction(tr("Query"));
136   QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
137   dccChatAction->setEnabled(false);
138   QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
139   sendFileAction->setEnabled(false);
140
141   QAction *action = nickContextMenu.exec(QCursor::pos());
142   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
143
144   if(action == whoisAction)         { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
145   else if(action == versionAction)  { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
146   else if(action == pingAction)     { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
147
148   else if(action == opAction)       { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
149   else if(action == deOpAction)     { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
150   else if(action == voiceAction)    { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
151   else if(action == deVoiceAction)  { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
152
153   else if(action == kickAction)     { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
154   else if(action == banAction)      { executeCommand(bufferInfo, QString("/BAN %1").arg(nick)); }
155   else if(action == kickBanAction)  { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); 
156                                       executeCommand(bufferInfo, QString("/BAN %1").arg(nick)); }
157   else if(action == queryAction)    { startQuery(index); }
158
159 }
160
161 void NickView::startQuery(const QModelIndex & index) {
162   QString nick = nickFromModelIndex(index);
163   bool activated = false;
164
165   if(QSortFilterProxyModel *nickviewFilter = qobject_cast<QSortFilterProxyModel *>(model())) {
166     // rootIndex() is the channel, parent() is the corresponding network
167     QModelIndex networkIndex = rootIndex().parent(); 
168     QModelIndex source_networkIndex = nickviewFilter->mapToSource(networkIndex);
169     for(int i = 0; i < Client::networkModel()->rowCount(source_networkIndex); i++) {
170       QModelIndex childIndex = source_networkIndex.child( i, 0);
171       if(nick.toLower() == childIndex.data().toString().toLower()) {
172         QModelIndex queryIndex = Client::bufferModel()->mapFromSource(childIndex);
173         Client::bufferModel()->setCurrentIndex(queryIndex);
174         activated = true;
175       }
176     }
177   }
178   if(!activated) {
179     BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
180     executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
181   }
182 }
183
184 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
185   Client::instance()->userInput(bufferInfo, command);
186 }
187
188 QSize NickView::sizeHint() const {
189   Q_CHECK_PTR(_sizeHint);
190   if(_sizeHint->needsUpdate && model()) {;
191     int columnSize = 0;
192     for(int i = 0; i < model()->columnCount(); i++) {
193       if(!isColumnHidden(i))
194         columnSize += sizeHintForColumn(i);
195     }
196     _sizeHint->size = QSize(columnSize, 50);
197     _sizeHint->needsUpdate = false;
198   }
199   return _sizeHint->size;
200 }