Introduce QtUiStyleSettings and make highlight color configurable again
[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 class ExpandAllEvent : public QEvent {
34 public:
35   ExpandAllEvent() : QEvent(QEvent::User) {}
36 };
37
38 NickView::NickView(QWidget *parent)
39   : QTreeView(parent)
40 {
41   setIndentation(10);
42   setAnimated(true);
43   header()->hide();
44   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45   setSortingEnabled(true);
46   sortByColumn(0, Qt::AscendingOrder);
47
48   setContextMenuPolicy(Qt::CustomContextMenu);
49
50   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
51           this, SLOT(showContextMenu(const QPoint&)));
52   connect(this, SIGNAL(activated( const QModelIndex& )),
53           this, SLOT(startQuery( const QModelIndex& )));
54 }
55
56 void NickView::init() {
57   if(!model())
58     return;
59
60   for(int i = 1; i < model()->columnCount(); i++)
61     setColumnHidden(i, true);
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     QCoreApplication::postEvent(this, new ExpandAllEvent);
73   }
74 }
75
76 void NickView::setRootIndex(const QModelIndex &index) {
77   QAbstractItemView::setRootIndex(index);
78   if(index.isValid())
79     QCoreApplication::postEvent(this, new ExpandAllEvent);
80 }
81
82 QString NickView::nickFromModelIndex(const QModelIndex & index) {
83   QString nick = index.sibling(index.row(), 0).data().toString();
84   return nick;
85 }
86
87 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
88   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
89   return bufferInfo;
90 }
91
92 void NickView::showContextMenu(const QPoint & pos ) {
93   QModelIndex index = indexAt(pos);
94   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
95
96   QString nick = nickFromModelIndex(index);
97
98   QMenu nickContextMenu(this);
99
100   QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
101   QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
102   QAction *pingAction = nickContextMenu.addAction(tr("PING"));
103
104   nickContextMenu.addSeparator();
105
106   QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
107   QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
108   QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
109   QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
110   QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
111
112   QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
113   QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
114   QAction *banAction = kickBanMenu->addAction(tr("Ban %1").arg(nick));
115   QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
116   QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
117   ignoreAction->setEnabled(false);
118
119   nickContextMenu.addSeparator();
120
121   QAction *queryAction = nickContextMenu.addAction(tr("Query"));
122   QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
123   dccChatAction->setEnabled(false);
124   QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
125   sendFileAction->setEnabled(false);
126
127   QAction *action = nickContextMenu.exec(QCursor::pos());
128   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
129
130   if(action == whoisAction)         { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
131   else if(action == versionAction)  { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
132   else if(action == pingAction)     { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
133
134   else if(action == opAction)       { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
135   else if(action == deOpAction)     { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
136   else if(action == voiceAction)    { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
137   else if(action == deVoiceAction)  { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
138
139   else if(action == kickAction)     { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
140   else if(action == banAction)      { executeCommand(bufferInfo, QString("/BAN %1").arg(nick)); }
141   else if(action == kickBanAction)  { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); 
142                                       executeCommand(bufferInfo, QString("/BAN %1").arg(nick)); }
143   else if(action == queryAction)    { startQuery(index); }
144
145 }
146
147 void NickView::startQuery(const QModelIndex & index) {
148   QString nick = nickFromModelIndex(index);
149   bool activated = false;
150
151   if(QSortFilterProxyModel *nickviewFilter = qobject_cast<QSortFilterProxyModel *>(model())) {
152     // rootIndex() is the channel, parent() is the corresponding network
153     QModelIndex networkIndex = rootIndex().parent(); 
154     QModelIndex source_networkIndex = nickviewFilter->mapToSource(networkIndex);
155     for(int i = 0; i < Client::networkModel()->rowCount(source_networkIndex); i++) {
156       QModelIndex childIndex = source_networkIndex.child( i, 0);
157       if(nick.toLower() == childIndex.data().toString().toLower()) {
158         QModelIndex queryIndex = Client::bufferModel()->mapFromSource(childIndex);
159         Client::bufferModel()->setCurrentIndex(queryIndex);
160         activated = true;
161       }
162     }
163   }
164   if(!activated) {
165     BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
166     executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
167   }
168 }
169
170 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
171   Client::instance()->userInput(bufferInfo, command);
172 }
173
174 void NickView::customEvent(QEvent *event) {
175   // THIS IS A REPLACEMENT FOR expandAll()
176   /* WARNING: do not call expandAll()!
177    * it fucks up big time in combination with sorting and changing the rootIndex
178    * the following sequence of commands leads to unexpected behavior when inserting new items
179    * setSortingEnabled(true);
180    * setModel();
181    * expandAll();
182    * setRootIndex();
183    */
184   if(event->type() != QEvent::User)
185     return;
186
187   QModelIndex topLevelIdx;
188   for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
189     topLevelIdx = model()->index(i, 0, rootIndex());
190     if(isExpanded(topLevelIdx))
191       continue;
192     else {
193       expand(topLevelIdx);
194       if(i < model()->rowCount(rootIndex()) - 1)
195         QCoreApplication::postEvent(this, new ExpandAllEvent);
196       break;
197     }
198   }
199   event->accept();
200 }