all repos — mgba @ bd312238319c03151d5f9c9da199318f65ea90d5

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