Kill JumpKeyHandler and replace it by proper action shortcuts
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 31 May 2010 22:53:55 +0000 (00:53 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 31 May 2010 22:53:55 +0000 (00:53 +0200)
The so-called "RTS Shortcuts" (now called "Quick Access") were until now implemented through
JumpKeyHandler, which installed event filters in appropriate widgets. Instead, we can just as
well use normal actions and simplify the code (which showed its age anyway).

As a bonus, the Quick Access shortcuts are now configurable!

src/qtui/CMakeLists.txt
src/qtui/inputwidget.cpp
src/qtui/jumpkeyhandler.cpp [deleted file]
src/qtui/jumpkeyhandler.h [deleted file]
src/qtui/mainwin.cpp
src/qtui/mainwin.h

index b806d66..fb74978 100644 (file)
@@ -37,7 +37,6 @@ set(SOURCES
     debugmessagemodelfilter.cpp
     inputwidget.cpp
     ircconnectionwizard.cpp
-    jumpkeyhandler.cpp
     legacysystemtray.cpp
     mainpage.cpp
     mainwin.cpp
@@ -84,7 +83,6 @@ set(MOC_HDRS
     debugmessagemodelfilter.h
     inputwidget.h
     ircconnectionwizard.h
-    jumpkeyhandler.h
     legacysystemtray.h
     mainpage.h
     mainwin.h
index f490a67..d3a7f74 100644 (file)
@@ -26,7 +26,6 @@
 #include "client.h"
 #include "iconloader.h"
 #include "ircuser.h"
-#include "jumpkeyhandler.h"
 #include "networkmodel.h"
 #include "qtui.h"
 #include "qtuisettings.h"
@@ -54,7 +53,6 @@ InputWidget::InputWidget(QWidget *parent)
 
   ui.ownNick->setSizeAdjustPolicy(QComboBox::AdjustToContents);
   ui.ownNick->installEventFilter(new MouseWheelFilter(this));
-  ui.inputEdit->installEventFilter(new JumpKeyHandler(this));
   ui.inputEdit->installEventFilter(this);
 
   ui.inputEdit->setMinHeight(1);
diff --git a/src/qtui/jumpkeyhandler.cpp b/src/qtui/jumpkeyhandler.cpp
deleted file mode 100644 (file)
index f571442..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-/***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
- *   devel@quassel-irc.org                                                 *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) version 3.                                           *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
- ***************************************************************************/
-
-#include "jumpkeyhandler.h"
-
-#include <QKeyEvent>
-
-#include "client.h"
-#include "networkmodel.h"
-#include "buffermodel.h"
-
-JumpKeyHandler::JumpKeyHandler(QObject *parent)
-  : QObject(parent),
-#ifdef Q_WS_MAC
-    bindModifier(Qt::ControlModifier | Qt::AltModifier),
-    jumpModifier(Qt::ControlModifier)
-#else
-    bindModifier(Qt::ControlModifier),
-    jumpModifier(Qt::AltModifier)
-#endif
-{
-}
-
-bool JumpKeyHandler::eventFilter(QObject *obj, QEvent *event) {
-  if(event->type() != QEvent::KeyPress)
-    return QObject::eventFilter(obj, event);
-
-  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
-
-  const int key = keyEvent->key();
-  
-  if(key < Qt::Key_1 || Qt::Key_9 < key)
-    return QObject::eventFilter(obj, event);
-
-  int mod = keyEvent->modifiers() & ~(Qt::KeypadModifier|Qt::ShiftModifier);
-  if(mod ==  bindModifier) {
-    bindKey(key);
-    return true;
-  }
-  
-  if(mod == jumpModifier) {
-    jumpKey(key);
-    return true;
-  }
-
-  return false;
-}
-  
-void JumpKeyHandler::bindKey(int key) {
-  QModelIndex bufferIdx = Client::bufferModel()->standardSelectionModel()->currentIndex();
-  NetworkId netId = bufferIdx.data(NetworkModel::NetworkIdRole).value<NetworkId>();
-  const Network *net = Client::network(netId);
-  if(!net)
-    return;
-  
-  QString bufferName = bufferIdx.sibling(bufferIdx.row(), 0).data().toString();
-  BufferId bufferId = bufferIdx.data(NetworkModel::BufferIdRole).value<BufferId>();
-  
-  _keyboardJump[key] = bufferId;
-  CoreAccountSettings().setJumpKeyMap(_keyboardJump);
-  //emit statusMessage(tr("Bound Buffer %1::%2 to Key %3").arg(net->networkName()).arg(bufferName).arg(key - Qt::Key_0), 10000);
-  //statusBar()->showMessage(tr("Bound Buffer %1::%2 to Key %3").arg(net->networkName()).arg(bufferName).arg(key - Qt::Key_0), 10000);
-}
-
-void JumpKeyHandler::jumpKey(int key) {
-  if(key < Qt::Key_0 || Qt::Key_9 < key)
-    return;
-
-  if(_keyboardJump.isEmpty())
-    _keyboardJump = CoreAccountSettings().jumpKeyMap();
-
-  if(!_keyboardJump.contains(key))
-    return;
-
-  QModelIndex source_bufferIdx = Client::networkModel()->bufferIndex(_keyboardJump[key]);
-  QModelIndex bufferIdx = Client::bufferModel()->mapFromSource(source_bufferIdx);
-
-  if(bufferIdx.isValid())
-    Client::bufferModel()->standardSelectionModel()->setCurrentIndex(bufferIdx, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
-  
-}
diff --git a/src/qtui/jumpkeyhandler.h b/src/qtui/jumpkeyhandler.h
deleted file mode 100644 (file)
index fc50ef1..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
- *   devel@quassel-irc.org                                                 *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) version 3.                                           *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
- ***************************************************************************/
-
-#ifndef JUMPKEYHANDLER_H
-#define JUMPKEYHANDLER_H
-
-#include <QObject>
-#include <QHash>
-#include "types.h"
-
-class JumpKeyHandler : public QObject {
-  Q_OBJECT
-
-public:
-  JumpKeyHandler(QObject *parent = 0);
-
-  void jumpKey(int key);
-  void bindKey(int key);
-
-  virtual bool eventFilter(QObject *obj, QEvent *event);
-
-private:
-  int bindModifier;
-  int jumpModifier;
-  QHash<int, BufferId> _keyboardJump;
-};
-
-#endif //JUMPKEYHANDLER_H
index cdfa65b..1362e85 100644 (file)
@@ -69,7 +69,6 @@
 #include "inputwidget.h"
 #include "irclistmodel.h"
 #include "ircconnectionwizard.h"
-#include "jumpkeyhandler.h"
 #include "legacysystemtray.h"
 #include "msgprocessorstatuswidget.h"
 #include "nicklistwidget.h"
@@ -150,8 +149,6 @@ MainWin::MainWin(QWidget *parent)
   setWindowTitle("Quassel IRC");
   setWindowIconText("Quassel IRC");
   updateIcon();
-
-  installEventFilter(new JumpKeyHandler(this));
 }
 
 void MainWin::init() {
@@ -371,6 +368,57 @@ void MainWin::setupActions() {
 
   coll->addAction("JumpHotBuffer", new Action(tr("Jump to hot chat"), coll,
                                               this, SLOT(on_jumpHotBuffer_triggered()), QKeySequence(Qt::META + Qt::Key_A)));
+
+  // Jump keys
+#ifdef Q_WS_MAC
+  const int bindModifier = Qt::ControlModifier | Qt::AltModifier;
+  const int jumpModifier = Qt::ControlModifier;
+#else
+  const int bindModifier = Qt::ControlModifier;
+  const int jumpModifier = Qt::AltModifier;
+#endif
+
+  coll->addAction("BindJumpKey0", new Action(tr("Set Quick Access #0"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_0)))->setProperty("Index", 0);
+  coll->addAction("BindJumpKey1", new Action(tr("Set Quick Access #1"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_1)))->setProperty("Index", 1);
+  coll->addAction("BindJumpKey2", new Action(tr("Set Quick Access #2"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_2)))->setProperty("Index", 2);
+  coll->addAction("BindJumpKey3", new Action(tr("Set Quick Access #3"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_3)))->setProperty("Index", 3);
+  coll->addAction("BindJumpKey4", new Action(tr("Set Quick Access #4"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_4)))->setProperty("Index", 4);
+  coll->addAction("BindJumpKey5", new Action(tr("Set Quick Access #5"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_5)))->setProperty("Index", 5);
+  coll->addAction("BindJumpKey6", new Action(tr("Set Quick Access #6"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_6)))->setProperty("Index", 6);
+  coll->addAction("BindJumpKey7", new Action(tr("Set Quick Access #7"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_7)))->setProperty("Index", 7);
+  coll->addAction("BindJumpKey8", new Action(tr("Set Quick Access #8"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_8)))->setProperty("Index", 8);
+  coll->addAction("BindJumpKey9", new Action(tr("Set Quick Access #9"), coll, this, SLOT(bindJumpKey()),
+                                             QKeySequence(bindModifier + Qt::Key_9)))->setProperty("Index", 9);
+
+  coll->addAction("JumpKey0", new Action(tr("Quick Access #0"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_0)))->setProperty("Index", 0);
+  coll->addAction("JumpKey1", new Action(tr("Quick Access #1"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_1)))->setProperty("Index", 1);
+  coll->addAction("JumpKey2", new Action(tr("Quick Access #2"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_2)))->setProperty("Index", 2);
+  coll->addAction("JumpKey3", new Action(tr("Quick Access #3"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_3)))->setProperty("Index", 3);
+  coll->addAction("JumpKey4", new Action(tr("Quick Access #4"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_4)))->setProperty("Index", 4);
+  coll->addAction("JumpKey5", new Action(tr("Quick Access #5"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_5)))->setProperty("Index", 5);
+  coll->addAction("JumpKey6", new Action(tr("Quick Access #6"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_6)))->setProperty("Index", 6);
+  coll->addAction("JumpKey7", new Action(tr("Quick Access #7"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_7)))->setProperty("Index", 7);
+  coll->addAction("JumpKey8", new Action(tr("Quick Access #8"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_8)))->setProperty("Index", 8);
+  coll->addAction("JumpKey9", new Action(tr("Quick Access #9"), coll, this, SLOT(onJumpKey()),
+                                         QKeySequence(jumpModifier + Qt::Key_9)))->setProperty("Index", 9);
 }
 
 void MainWin::setupMenus() {
@@ -1152,6 +1200,34 @@ void MainWin::on_jumpHotBuffer_triggered() {
   Client::bufferModel()->switchToBuffer(bufferId);
 }
 
+void MainWin::onJumpKey() {
+  QAction *action = qobject_cast<QAction *>(sender());
+  if(!action || !Client::bufferModel())
+    return;
+  int idx = action->property("Index").toInt();
+
+  if(_jumpKeyMap.isEmpty())
+    _jumpKeyMap = CoreAccountSettings().jumpKeyMap();
+
+  if(!_jumpKeyMap.contains(idx))
+    return;
+
+  BufferId buffer = _jumpKeyMap.value(idx);
+  if(buffer.isValid())
+    Client::bufferModel()->switchToBuffer(buffer);
+
+}
+
+void MainWin::bindJumpKey() {
+  QAction *action = qobject_cast<QAction *>(sender());
+  if(!action || !Client::bufferModel())
+    return;
+  int idx = action->property("Index").toInt();
+
+  _jumpKeyMap[idx] = Client::bufferModel()->currentBuffer();
+  CoreAccountSettings().setJumpKeyMap(_jumpKeyMap);
+}
+
 void MainWin::on_actionDebugNetworkModel_triggered() {
   QTreeView *view = new QTreeView;
   view->setAttribute(Qt::WA_DeleteOnClose);
index f9130bd..02b7ff5 100644 (file)
@@ -134,6 +134,9 @@ class MainWin
     void on_actionDebugHotList_triggered();
     void on_actionDebugLog_triggered();
 
+    void bindJumpKey();
+    void onJumpKey();
+
     void clientNetworkCreated(NetworkId);
     void clientNetworkRemoved(NetworkId);
     void clientNetworkUpdated();
@@ -197,6 +200,7 @@ class MainWin
     QPoint _normalPos; //!< Position of the non-maximized window
 
     BufferHotListFilter *_bufferHotList;
+    QHash<int, BufferId> _jumpKeyMap;
 
     friend class QtUi;
 };