all repos — mgba @ 811ad23e6175142fabaaee031ad3e6d58e2bb910

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}
25
26void ShortcutView::setController(ShortcutController* controller) {
27	m_controller = controller;
28	m_ui.shortcutTable->setModel(controller);
29}
30
31void ShortcutView::setInputController(InputController* controller) {
32	m_inputController = controller;
33	connect(controller, SIGNAL(axisChanged(int, int32_t)), m_ui.keyEdit, SLOT(setValueAxis(int, int32_t)));
34}
35
36bool ShortcutView::event(QEvent* event) {
37	if (event->type() == GamepadButtonEvent::Down()) {
38		updateButton(static_cast<GamepadButtonEvent*>(event)->value());
39		event->accept();
40		return true;
41	}
42	return QWidget::event(event);
43}
44
45void ShortcutView::load(const QModelIndex& index) {
46	if (!m_controller) {
47		return;
48	}
49	const QAction* action = m_controller->actionAt(index);
50	if (!action) {
51		return;
52	}
53	if (m_ui.gamepadButton->isChecked()) {
54		loadButton();
55	} else {
56		loadKey(action);
57	}
58}
59
60void ShortcutView::updateKey() {
61	if (!m_controller) {
62		return;
63	}
64	m_ui.keySequenceEdit->clearFocus();
65	m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), m_ui.keySequenceEdit->keySequence());
66}
67
68void ShortcutView::updateButton(int button) {
69	if (!m_controller) {
70		return;
71	}
72	m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
73
74}
75void ShortcutView::loadKey(const QAction* action) {
76	m_ui.keySequenceEdit->setFocus();
77	m_ui.keySequenceEdit->setKeySequence(action->shortcut());
78}
79
80void ShortcutView::loadButton() {
81	m_ui.keyEdit->setFocus();
82	m_ui.keyEdit->setValueButton(-1); // TODO: Real value
83}