Qt: Allow tab and backtab to be mapped for key sequences

This commit is contained in:
Jeffrey Pfau 2015-01-05 02:27:50 -08:00
parent 851d942cdd
commit ec5445d5ad
3 changed files with 25 additions and 11 deletions

View File

@ -95,12 +95,12 @@ public:
void clearKey(const QModelIndex& index); void clearKey(const QModelIndex& index);
void clearButton(const QModelIndex& index); void clearButton(const QModelIndex& index);
static QKeySequence keyEventToSequence(const QKeyEvent*);
protected: protected:
bool eventFilter(QObject*, QEvent*) override; bool eventFilter(QObject*, QEvent*) override;
private: private:
static QKeySequence keyEventToSequence(const QKeyEvent*);
ShortcutItem* itemAt(const QModelIndex& index); ShortcutItem* itemAt(const QModelIndex& index);
const ShortcutItem* itemAt(const QModelIndex& index) const; const ShortcutItem* itemAt(const QModelIndex& index) const;
void loadShortcuts(ShortcutItem*); void loadShortcuts(ShortcutItem*);

View File

@ -8,6 +8,8 @@
#include "GamepadButtonEvent.h" #include "GamepadButtonEvent.h"
#include "ShortcutController.h" #include "ShortcutController.h"
#include <QKeyEvent>
using namespace QGBA; using namespace QGBA;
ShortcutView::ShortcutView(QWidget* parent) ShortcutView::ShortcutView(QWidget* parent)
@ -17,8 +19,9 @@ ShortcutView::ShortcutView(QWidget* parent)
{ {
m_ui.setupUi(this); m_ui.setupUi(this);
m_ui.keyEdit->setValueButton(-1); m_ui.keyEdit->setValueButton(-1);
m_ui.keySequenceEdit->installEventFilter(this);
connect(m_ui.keySequenceEdit, SIGNAL(editingFinished()), this, SLOT(updateKey())); connect(m_ui.keySequenceEdit, SIGNAL(keySequenceChanged(const QKeySequence&)), this, SLOT(updateKey(const QKeySequence&)));
connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int))); connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&))); connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear())); connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
@ -43,6 +46,21 @@ bool ShortcutView::event(QEvent* event) {
return QWidget::event(event); return QWidget::event(event);
} }
bool ShortcutView::eventFilter(QObject*, QEvent* event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
return false;
}
if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
m_ui.keySequenceEdit->setKeySequence(ShortcutController::keyEventToSequence(keyEvent));
keyEvent->accept();
return true;
}
}
return false;
}
void ShortcutView::load(const QModelIndex& index) { void ShortcutView::load(const QModelIndex& index) {
if (!m_controller) { if (!m_controller) {
return; return;
@ -82,19 +100,17 @@ void ShortcutView::clear() {
} }
} }
void ShortcutView::updateKey() { void ShortcutView::updateKey(const QKeySequence& shortcut) {
if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) { if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
return; return;
} }
m_ui.keySequenceEdit->clearFocus(); m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), shortcut);
m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), m_ui.keySequenceEdit->keySequence());
} }
void ShortcutView::updateButton(int button) { void ShortcutView::updateButton(int button) {
if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) { if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
return; return;
} }
m_ui.keyEdit->clearFocus();
m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button); m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
} }

View File

@ -26,17 +26,15 @@ public:
protected: protected:
virtual bool event(QEvent* event) override; virtual bool event(QEvent* event) override;
virtual bool eventFilter(QObject* obj, QEvent* event) override;
private slots: private slots:
void load(const QModelIndex&); void load(const QModelIndex&);
void clear(); void clear();
void updateKey(); void updateKey(const QKeySequence&);
void updateButton(int button); void updateButton(int button);
private: private:
void loadKey(const QAction*);
void loadButton();
Ui::ShortcutView m_ui; Ui::ShortcutView m_ui;
ShortcutController* m_controller; ShortcutController* m_controller;