all repos — mgba @ c90d105e3490d0b94a4dc5de182c6cf18b5616ba

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
11using namespace QGBA;
12
13ShortcutView::ShortcutView(QWidget* parent)
14	: QWidget(parent)
15	, m_controller(nullptr)
16	, m_inputController(nullptr)
17{
18	m_ui.setupUi(this);
19	m_ui.keyEdit->setValueButton(-1);
20
21	connect(m_ui.keySequenceEdit, SIGNAL(editingFinished()), this, SLOT(updateKey()));
22	connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
23	connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
24	connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
25}
26
27void ShortcutView::setController(ShortcutController* controller) {
28	m_controller = controller;
29	m_ui.shortcutTable->setModel(controller);
30}
31
32void ShortcutView::setInputController(InputController* controller) {
33	m_inputController = controller;
34	connect(controller, SIGNAL(axisChanged(int, int32_t)), m_ui.keyEdit, SLOT(setValueAxis(int, int32_t)));
35}
36
37bool ShortcutView::event(QEvent* event) {
38	if (event->type() == GamepadButtonEvent::Down()) {
39		updateButton(static_cast<GamepadButtonEvent*>(event)->value());
40		event->accept();
41		return true;
42	}
43	return QWidget::event(event);
44}
45
46void ShortcutView::load(const QModelIndex& index) {
47	if (!m_controller) {
48		return;
49	}
50	const QAction* action = m_controller->actionAt(index);
51	if (!action || m_controller->isMenuAt(index)) {
52		return;
53	}
54	if (m_ui.gamepadButton->isChecked()) {
55		m_ui.keyEdit->setFocus();
56		m_ui.keyEdit->setValueButton(-1); // There are no default bindings
57	} else {
58		m_ui.keySequenceEdit->setFocus();
59		m_ui.keySequenceEdit->setKeySequence(action->shortcut());
60	}
61}
62
63void ShortcutView::clear() {
64	if (!m_controller) {
65		return;
66	}
67	QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
68	const QAction* action = m_controller->actionAt(index);
69	if (!action || m_controller->isMenuAt(index)) {
70		return;
71	}
72	if (m_ui.gamepadButton->isChecked()) {
73		m_controller->clearButton(index);
74		m_ui.keyEdit->setValueButton(-1);
75	} else {
76		m_controller->clearKey(index);
77		m_ui.keySequenceEdit->setKeySequence(QKeySequence());
78	}
79}
80
81void ShortcutView::updateKey() {
82	if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
83		return;
84	}
85	m_ui.keySequenceEdit->clearFocus();
86	m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), m_ui.keySequenceEdit->keySequence());
87}
88
89void ShortcutView::updateButton(int button) {
90	if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
91		return;
92	}
93	m_ui.keyEdit->clearFocus();
94	m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
95
96}