all repos — mgba @ 5966f46355d18ba472404a7f7ed3cbeebf846635

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 "InputController.h"
 10#include "ShortcutController.h"
 11
 12#include <QKeyEvent>
 13
 14using namespace QGBA;
 15
 16ShortcutView::ShortcutView(QWidget* parent)
 17	: QWidget(parent)
 18	, m_controller(nullptr)
 19	, m_input(nullptr)
 20{
 21	m_ui.setupUi(this);
 22	m_ui.keyEdit->setValueKey(0);
 23
 24	connect(m_ui.gamepadButton, &QAbstractButton::pressed, [this]() {
 25		m_ui.keyEdit->setValueButton(-1);
 26	});
 27	connect(m_ui.keyboardButton, &QAbstractButton::pressed, [this]() {
 28		m_ui.keyEdit->setValueKey(0);
 29	});
 30	connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
 31	connect(m_ui.keyEdit, SIGNAL(axisChanged(int, int)), this, SLOT(updateAxis(int, int)));
 32	connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
 33	connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
 34}
 35
 36void ShortcutView::setController(ShortcutController* controller) {
 37	m_controller = controller;
 38	m_ui.shortcutTable->setModel(controller);
 39}
 40
 41void ShortcutView::setInputController(InputController* controller) {
 42	if (m_input) {
 43		m_input->releaseFocus(this);
 44	}
 45	m_input = controller;
 46	m_input->stealFocus(this);
 47}
 48
 49void ShortcutView::load(const QModelIndex& index) {
 50	if (!m_controller) {
 51		return;
 52	}
 53	if (m_controller->isMenuAt(index)) {
 54		return;
 55	}
 56	int shortcut = m_controller->shortcutAt(index);
 57	if (index.column() == 1) {
 58		m_ui.keyboardButton->click();
 59	} else if (index.column() == 2) {
 60		m_ui.gamepadButton->click();
 61	}
 62	bool blockSignals = m_ui.keyEdit->blockSignals(true);
 63	m_ui.keyEdit->setFocus(Qt::MouseFocusReason);
 64	if (m_ui.gamepadButton->isChecked()) {
 65		m_ui.keyEdit->setValueButton(-1); // There are no default bindings
 66	} else {
 67		m_ui.keyEdit->setValueKey(shortcut);
 68	}
 69	m_ui.keyEdit->blockSignals(blockSignals);
 70}
 71
 72void ShortcutView::clear() {
 73	if (!m_controller) {
 74		return;
 75	}
 76	QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
 77	if (m_controller->isMenuAt(index)) {
 78		return;
 79	}
 80	if (m_ui.gamepadButton->isChecked()) {
 81		m_controller->clearButton(index);
 82		m_ui.keyEdit->setValueButton(-1);
 83	} else {
 84		m_controller->clearKey(index);
 85		m_ui.keyEdit->setValueKey(-1);
 86	}
 87}
 88
 89void ShortcutView::updateButton(int button) {
 90	if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
 91		return;
 92	}
 93	if (m_ui.gamepadButton->isChecked()) {
 94		m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
 95	} else {
 96		m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
 97	}
 98}
 99
100void ShortcutView::updateAxis(int axis, int direction) {
101	if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
102		return;
103	}
104	m_controller->updateAxis(m_ui.shortcutTable->selectionModel()->currentIndex(), axis,
105	                         static_cast<GamepadAxisEvent::Direction>(direction));
106}
107
108void ShortcutView::closeEvent(QCloseEvent*) {
109	if (m_input) {
110		m_input->releaseFocus(this);
111	}
112}
113
114bool ShortcutView::event(QEvent* event) {
115	if (m_input) {
116		if (event->type() == QEvent::WindowActivate) {
117			m_input->stealFocus(this);
118		} else if (event->type() == QEvent::WindowDeactivate) {
119			m_input->releaseFocus(this);
120		}
121	}
122	return QWidget::event(event);
123}