Categories in the settings dialog are now clickable
[quassel.git] / src / qtui / jumpkeyhandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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   int mod = keyEvent->modifiers() & ~(Qt::KeypadModifier|Qt::ShiftModifier);
53   if(mod ==  bindModifier) {
54     bindKey(key);
55     return true;
56   }
57   
58   if(mod == jumpModifier) {
59     jumpKey(key);
60     return true;
61   }
62
63   return false;
64 }
65   
66 void JumpKeyHandler::bindKey(int key) {
67   QModelIndex bufferIdx = Client::bufferModel()->standardSelectionModel()->currentIndex();
68   NetworkId netId = bufferIdx.data(NetworkModel::NetworkIdRole).value<NetworkId>();
69   const Network *net = Client::network(netId);
70   if(!net)
71     return;
72   
73   QString bufferName = bufferIdx.sibling(bufferIdx.row(), 0).data().toString();
74   BufferId bufferId = bufferIdx.data(NetworkModel::BufferIdRole).value<BufferId>();
75   
76   _keyboardJump[key] = bufferId;
77   CoreAccountSettings().setJumpKeyMap(_keyboardJump);
78   //emit statusMessage(tr("Bound Buffer %1::%2 to Key %3").arg(net->networkName()).arg(bufferName).arg(key - Qt::Key_0), 10000);
79   //statusBar()->showMessage(tr("Bound Buffer %1::%2 to Key %3").arg(net->networkName()).arg(bufferName).arg(key - Qt::Key_0), 10000);
80 }
81
82 void JumpKeyHandler::jumpKey(int key) {
83   if(key < Qt::Key_0 || Qt::Key_9 < key)
84     return;
85
86   if(_keyboardJump.isEmpty())
87     _keyboardJump = CoreAccountSettings().jumpKeyMap();
88
89   if(!_keyboardJump.contains(key))
90     return;
91
92   QModelIndex source_bufferIdx = Client::networkModel()->bufferIndex(_keyboardJump[key]);
93   QModelIndex bufferIdx = Client::bufferModel()->mapFromSource(source_bufferIdx);
94
95   if(bufferIdx.isValid())
96     Client::bufferModel()->standardSelectionModel()->setCurrentIndex(bufferIdx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
97   
98 }