all repos — mgba @ 22245617f434049f4646916d1b2930d376503b0d

mGBA Game Boy Advance Emulator

src/platform/qt/ShortcutView.cpp (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "ShortcutView.h"
  7
  8#include "GamepadButtonEvent.h"
  9#include "ShortcutController.h"
 10
 11#include <QKeyEvent>
 12
 13using namespace QGBA;
 14
 15ShortcutView::ShortcutView(QWidget* parent)
 16	: QWidget(parent)
 17	, m_controller(nullptr)
 18{
 19	m_ui.setupUi(this);
 20	m_ui.keyEdit->setValueButton(-1);
 21	m_ui.keySequenceEdit->installEventFilter(this);
 22
 23	connect(m_ui.keySequenceEdit, SIGNAL(keySequenceChanged(const QKeySequence&)), this, SLOT(updateKey(const QKeySequence&)));
 24	connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
 25	connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
 26	connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
 27}
 28
 29void ShortcutView::setController(ShortcutController* controller) {
 30	m_controller = controller;
 31	m_ui.shortcutTable->setModel(controller);
 32}
 33
 34bool ShortcutView::event(QEvent* event) {
 35	if (event->type() == GamepadButtonEvent::Down()) {
 36		updateButton(static_cast<GamepadButtonEvent*>(event)->value());
 37		event->accept();
 38		return true;
 39	}
 40	return QWidget::event(event);
 41}
 42
 43bool ShortcutView::eventFilter(QObject*, QEvent* event) {
 44	if (event->type() == QEvent::KeyPress) {
 45		QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
 46		if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
 47			return false;
 48		}
 49		if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
 50			m_ui.keySequenceEdit->setKeySequence(ShortcutController::keyEventToSequence(keyEvent));
 51			keyEvent->accept();
 52			return true;
 53		}
 54	}
 55	return false;
 56}
 57
 58void ShortcutView::load(const QModelIndex& index) {
 59	if (!m_controller) {
 60		return;
 61	}
 62	if (m_controller->isMenuAt(index)) {
 63		return;
 64	}
 65	QKeySequence sequence = m_controller->shortcutAt(index);
 66	if (index.column() == 1) {
 67		m_ui.keyboardButton->click();
 68	} else if (index.column() == 2) {
 69		m_ui.gamepadButton->click();
 70	}
 71	if (m_ui.gamepadButton->isChecked()) {
 72		m_ui.keyEdit->setFocus();
 73		m_ui.keyEdit->setValueButton(-1); // There are no default bindings
 74	} else {
 75		m_ui.keySequenceEdit->setFocus();
 76		m_ui.keySequenceEdit->setKeySequence(sequence);
 77	}
 78}
 79
 80void ShortcutView::clear() {
 81	if (!m_controller) {
 82		return;
 83	}
 84	QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
 85	if (m_controller->isMenuAt(index)) {
 86		return;
 87	}
 88	if (m_ui.gamepadButton->isChecked()) {
 89		m_controller->clearButton(index);
 90		m_ui.keyEdit->setValueButton(-1);
 91	} else {
 92		m_controller->clearKey(index);
 93		m_ui.keySequenceEdit->setKeySequence(QKeySequence());
 94	}
 95}
 96
 97void ShortcutView::updateKey(const QKeySequence& shortcut) {
 98	if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
 99		return;
100	}
101	m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), shortcut);
102}
103
104void ShortcutView::updateButton(int button) {
105	if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
106		return;
107	}
108	m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
109
110}