diff --git a/src/platform/qt/GBAKeyEditor.cpp b/src/platform/qt/GBAKeyEditor.cpp index 4a8577979..6adad32f7 100644 --- a/src/platform/qt/GBAKeyEditor.cpp +++ b/src/platform/qt/GBAKeyEditor.cpp @@ -32,6 +32,7 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString& setMinimumSize(300, 300); const GBAInputMap* map = controller->map(); + controller->stealFocus(this); m_keyDU = new KeyEditor(this); m_keyDD = new KeyEditor(this); @@ -151,6 +152,19 @@ void GBAKeyEditor::paintEvent(QPaintEvent* event) { painter.drawPicture(0, 0, m_background); } +void GBAKeyEditor::closeEvent(QCloseEvent*) { + m_controller->releaseFocus(this); +} + +bool GBAKeyEditor::event(QEvent* event) { + if (event->type() == QEvent::WindowActivate) { + m_controller->stealFocus(this); + } else if (event->type() == QEvent::WindowDeactivate) { + m_controller->releaseFocus(this); + } + return QWidget::event(event); +} + void GBAKeyEditor::setNext() { findFocus(); diff --git a/src/platform/qt/GBAKeyEditor.h b/src/platform/qt/GBAKeyEditor.h index 9496ffffe..65707d4eb 100644 --- a/src/platform/qt/GBAKeyEditor.h +++ b/src/platform/qt/GBAKeyEditor.h @@ -35,6 +35,8 @@ public slots: protected: virtual void resizeEvent(QResizeEvent*) override; virtual void paintEvent(QPaintEvent*) override; + virtual bool event(QEvent*) override; + virtual void closeEvent(QCloseEvent*) override; private slots: void setNext(); diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index 93a8d58ef..c0d19d9fa 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -35,6 +35,7 @@ InputController::InputController(int playerId, QWidget* topLevel, QObject* paren #endif , m_allowOpposing(false) , m_topLevel(topLevel) + , m_focusParent(topLevel) { GBAInputMapInit(&m_inputMap); @@ -469,10 +470,10 @@ void InputController::testGamepad(int type) { void InputController::sendGamepadEvent(QEvent* event) { QWidget* focusWidget = nullptr; - if (m_topLevel) { - focusWidget = m_topLevel->focusWidget(); + if (m_focusParent) { + focusWidget = m_focusParent->focusWidget(); if (!focusWidget) { - focusWidget = m_topLevel; + focusWidget = m_focusParent; } } else { focusWidget = QApplication::focusWidget(); @@ -505,3 +506,13 @@ void InputController::setScreensaverSuspendable(bool suspendable) { GBASDLSetScreensaverSuspendable(&s_sdlEvents, suspendable); } #endif + +void InputController::stealFocus(QWidget* focus) { + m_focusParent = focus; +} + +void InputController::releaseFocus(QWidget* focus) { + if (focus == m_focusParent) { + m_focusParent = m_topLevel; + } +} diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index 4a8501dd3..572210b80 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -74,6 +74,9 @@ public: float gyroSensitivity() const; void setGyroSensitivity(float sensitivity); + void stealFocus(QWidget* focus); + void releaseFocus(QWidget* focus); + GBARumble* rumble(); GBARotationSource* rotationSource(); @@ -101,6 +104,7 @@ private: int m_playerId; bool m_allowOpposing; QWidget* m_topLevel; + QWidget* m_focusParent; #ifdef BUILD_SDL static int s_sdlInited; diff --git a/src/platform/qt/ShortcutView.cpp b/src/platform/qt/ShortcutView.cpp index b0ba9045f..036d9cf76 100644 --- a/src/platform/qt/ShortcutView.cpp +++ b/src/platform/qt/ShortcutView.cpp @@ -6,6 +6,7 @@ #include "ShortcutView.h" #include "GamepadButtonEvent.h" +#include "InputController.h" #include "ShortcutController.h" #include @@ -15,6 +16,7 @@ using namespace QGBA; ShortcutView::ShortcutView(QWidget* parent) : QWidget(parent) , m_controller(nullptr) + , m_input(nullptr) { m_ui.setupUi(this); m_ui.keyEdit->setValueButton(-1); @@ -32,6 +34,14 @@ void ShortcutView::setController(ShortcutController* controller) { m_ui.shortcutTable->setModel(controller); } +void ShortcutView::setInputController(InputController* controller) { + if (m_input) { + m_input->releaseFocus(this); + } + m_input = controller; + m_input->stealFocus(this); +} + bool ShortcutView::eventFilter(QObject*, QEvent* event) { if (event->type() == QEvent::KeyPress) { QKeyEvent* keyEvent = static_cast(event); @@ -111,3 +121,20 @@ void ShortcutView::updateAxis(int axis, int direction) { m_controller->updateAxis(m_ui.shortcutTable->selectionModel()->currentIndex(), axis, static_cast(direction)); } + +void ShortcutView::closeEvent(QCloseEvent*) { + if (m_input) { + m_input->releaseFocus(this); + } +} + +bool ShortcutView::event(QEvent* event) { + if (m_input) { + if (event->type() == QEvent::WindowActivate) { + m_input->stealFocus(this); + } else if (event->type() == QEvent::WindowDeactivate) { + m_input->releaseFocus(this); + } + } + return QWidget::event(event); +} diff --git a/src/platform/qt/ShortcutView.h b/src/platform/qt/ShortcutView.h index af6baadaa..59a997347 100644 --- a/src/platform/qt/ShortcutView.h +++ b/src/platform/qt/ShortcutView.h @@ -14,6 +14,7 @@ namespace QGBA { +class InputController; class ShortcutController; class ShortcutView : public QWidget { @@ -23,9 +24,12 @@ public: ShortcutView(QWidget* parent = nullptr); void setController(ShortcutController* controller); + void setInputController(InputController* input); protected: virtual bool eventFilter(QObject* obj, QEvent* event) override; + virtual bool event(QEvent*) override; + virtual void closeEvent(QCloseEvent*) override; private slots: void load(const QModelIndex&); @@ -38,6 +42,7 @@ private: Ui::ShortcutView m_ui; ShortcutController* m_controller; + InputController* m_input; }; } diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index d0e1e2fd3..29f800c0f 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -315,6 +315,7 @@ void Window::openShortcutWindow() { #endif ShortcutView* shortcutView = new ShortcutView(); shortcutView->setController(m_shortcutController); + shortcutView->setInputController(&m_inputController); openView(shortcutView); }