Add buffer-specific actions to ChatView's context menu
[quassel.git] / src / qtui / jumpkeyhandler.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 "jumpkeyhandler.h"
22
23 #include <QKeyEvent>
24
25 #include "client.h"
26 #include "networkmodel.h"
27 #include "buffermodel.h"
28
29 JumpKeyHandler::JumpKeyHandler(QObject *parent)
30   : QObject(parent),
31 #ifdef Q_WS_MAC
32     bindModifier(Qt::ControlModifier | Qt::AltModifier),
33     jumpModifier(Qt::ControlModifier)
34 #else
35     bindModifier(Qt::ControlModifier),
36     jumpModifier(Qt::AltModifier)
37 #endif
38 {
39 }
40
41 bool JumpKeyHandler::eventFilter(QObject *obj, QEvent *event) {
42   if(event->type() != QEvent::KeyPress)
43     return QObject::eventFilter(obj, event);
44
45   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
46
47   const int key = keyEvent->key();
48   
49   if(key < Qt::Key_1 || Qt::Key_9 < key)
50     return QObject::eventFilter(obj, event);
51   
52   if(keyEvent->modifiers() ==  bindModifier) {
53     bindKey(key);
54     return true;
55   }
56   
57   if(keyEvent->modifiers() == jumpModifier) {
58     jumpKey(key);
59     return true;
60   }
61
62   return false;
63 }
64   
65 void JumpKeyHandler::bindKey(int key) {
66   QModelIndex bufferIdx = Client::bufferModel()->standardSelectionModel()->currentIndex();
67   NetworkId netId = bufferIdx.data(NetworkModel::NetworkIdRole).value<NetworkId>();
68   const Network *net = Client::network(netId);
69   if(!net)
70     return;
71   
72   QString bufferName = bufferIdx.sibling(bufferIdx.row(), 0).data().toString();
73   BufferId bufferId = bufferIdx.data(NetworkModel::BufferIdRole).value<BufferId>();
74   
75   _keyboardJump[key] = bufferId;
76   CoreAccountSettings().setJumpKeyMap(_keyboardJump);
77   //emit statusMessage(tr("Bound Buffer %1::%2 to Key %3").arg(net->networkName()).arg(bufferName).arg(key - Qt::Key_0), 10000);
78   //statusBar()->showMessage(tr("Bound Buffer %1::%2 to Key %3").arg(net->networkName()).arg(bufferName).arg(key - Qt::Key_0), 10000);
79 }
80
81 void JumpKeyHandler::jumpKey(int key) {
82   if(key < Qt::Key_0 || Qt::Key_9 < key)
83     return;
84
85   if(_keyboardJump.isEmpty())
86     _keyboardJump = CoreAccountSettings().jumpKeyMap();
87
88   if(!_keyboardJump.contains(key))
89     return;
90
91   QModelIndex source_bufferIdx = Client::networkModel()->bufferIndex(_keyboardJump[key]);
92   QModelIndex bufferIdx = Client::bufferModel()->mapFromSource(source_bufferIdx);
93
94   if(bufferIdx.isValid())
95     Client::bufferModel()->standardSelectionModel()->setCurrentIndex(bufferIdx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
96   
97 }