1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QHeaderView>
26 #include "nickviewfilter.h"
27 #include "networkmodel.h"
28 #include "buffermodel.h"
33 LazySizeHint() : size(QSize()), needsUpdate(true) {};
38 NickView::NickView(QWidget *parent)
40 _sizeHint(new LazySizeHint())
45 setSortingEnabled(true);
46 sortByColumn(0, Qt::AscendingOrder);
48 setContextMenuPolicy(Qt::CustomContextMenu);
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& )));
56 NickView::~NickView() {
61 void NickView::init() {
65 for(int i = 1; i < model()->columnCount(); i++)
66 setColumnHidden(i, true);
69 _sizeHint->needsUpdate = true;
72 void NickView::setModel(QAbstractItemModel *model) {
73 QTreeView::setModel(model);
77 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
78 QTreeView::rowsInserted(parent, start, end);
79 if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent))
81 _sizeHint->needsUpdate = true;
84 void NickView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
85 QTreeView::rowsAboutToBeRemoved(parent, start, end);
86 _sizeHint->needsUpdate = true;
89 void NickView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
90 QTreeView::dataChanged(topLeft, bottomRight);
91 _sizeHint->needsUpdate = true;
94 QString NickView::nickFromModelIndex(const QModelIndex & index) {
95 QString nick = index.sibling(index.row(), 0).data().toString();
99 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
100 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
104 void NickView::showContextMenu(const QPoint & pos ) {
105 QModelIndex index = indexAt(pos);
106 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
108 QString nick = nickFromModelIndex(index);
110 QMenu nickContextMenu(this);
112 QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
113 QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
114 QAction *pingAction = nickContextMenu.addAction(tr("PING"));
116 nickContextMenu.addSeparator();
118 QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
119 QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
120 QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
121 QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
122 QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
124 QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
125 //TODO: add kick message from network identity (kick reason)
126 QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
127 QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
128 kickBanMenu->setEnabled(false);
129 QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
130 ignoreAction->setEnabled(false);
132 nickContextMenu.addSeparator();
134 QAction *queryAction = nickContextMenu.addAction(tr("Query"));
135 QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
136 dccChatAction->setEnabled(false);
137 QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
138 sendFileAction->setEnabled(false);
140 QAction *action = nickContextMenu.exec(QCursor::pos());
141 BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
143 if(action == whoisAction) { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
144 else if(action == versionAction) { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
145 else if(action == pingAction) { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
147 else if(action == opAction) { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
148 else if(action == deOpAction) { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
149 else if(action == voiceAction) { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
150 else if(action == deVoiceAction) { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
152 else if(action == kickAction) { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
153 else if(action == kickBanAction) { executeCommand(bufferInfo, QString("/KICKBAN %1").arg(nick)); }
154 else if(action == queryAction) { executeCommand(bufferInfo, QString("/QUERY %1").arg(nick)); }
158 void NickView::startQuery(const QModelIndex & index) {
159 QString nick = nickFromModelIndex(index);
160 bool activated = false;
162 if(QSortFilterProxyModel *nickviewFilter = qobject_cast<QSortFilterProxyModel *>(model())) {
163 // rootIndex() is the channel, parent() is the corresponding network
164 QModelIndex networkIndex = rootIndex().parent();
165 QModelIndex source_networkIndex = nickviewFilter->mapToSource(networkIndex);
166 for(int i = 0; i < Client::networkModel()->rowCount(source_networkIndex); i++) {
167 QModelIndex childIndex = source_networkIndex.child( i, 0);
168 if(nick.toLower() == childIndex.data().toString().toLower()) {
169 QModelIndex queryIndex = Client::bufferModel()->mapFromSource(childIndex);
170 Client::bufferModel()->setCurrentIndex(queryIndex);
176 BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
177 executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
181 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
182 Client::instance()->userInput(bufferInfo, command);
185 QSize NickView::sizeHint() const {
186 Q_CHECK_PTR(_sizeHint);
187 if(_sizeHint->needsUpdate && model()) {;
189 for(int i = 0; i < model()->columnCount(); i++) {
190 if(!isColumnHidden(i))
191 columnSize += sizeHintForColumn(i);
193 _sizeHint->size = QSize(columnSize, 50);
194 _sizeHint->needsUpdate = false;
196 return _sizeHint->size;