From f2f336229d5843512dc0afcc344f54a9d55410de Mon Sep 17 00:00:00 2001 From: Bennik2000 Date: Tue, 12 May 2020 00:08:08 +0200 Subject: [PATCH] UI: Allow the use of Esc key to quit settings window --- UI/window-basic-interaction.hpp | 2 +- UI/window-basic-settings.cpp | 57 +++++++++++++++++++++++++++++---- UI/window-basic-settings.hpp | 3 ++ 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/UI/window-basic-interaction.hpp b/UI/window-basic-interaction.hpp index 1788debee..4d53b837a 100644 --- a/UI/window-basic-interaction.hpp +++ b/UI/window-basic-interaction.hpp @@ -80,6 +80,6 @@ protected: return filter(obj, event); } -private: +public: EventFilterFunc filter; }; diff --git a/UI/window-basic-settings.cpp b/UI/window-basic-settings.cpp index 23bf24511..a55a02139 100644 --- a/UI/window-basic-settings.cpp +++ b/UI/window-basic-settings.cpp @@ -55,6 +55,33 @@ using namespace std; +class SettingsEventFilter : public QObject { + QScopedPointer shortcutFilter; + +public: + inline SettingsEventFilter() + : shortcutFilter((OBSEventFilter *)CreateShortcutFilter()) + { + } + +protected: + bool eventFilter(QObject *obj, QEvent *event) override + { + int key; + + switch (event->type()) { + case QEvent::KeyPress: + case QEvent::KeyRelease: + key = static_cast(event)->key(); + if (key == Qt::Key_Escape) { + return false; + } + } + + return shortcutFilter->filter(obj, event); + } +}; + // Used for QVariant in codec comboboxes namespace { static bool StringEquals(QString left, QString right) @@ -656,7 +683,7 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent) // Initialize libff library ff_init(); - installEventFilter(CreateShortcutFilter()); + installEventFilter(new SettingsEventFilter()); LoadEncoderTypes(); LoadColorRanges(); @@ -3611,14 +3638,14 @@ bool OBSBasicSettings::QueryChanges() void OBSBasicSettings::closeEvent(QCloseEvent *event) { - if (Changed() && !QueryChanges()) + if (!AskIfCanCloseSettings()) event->ignore(); +} - if (forceAuthReload) { - main->auth->Save(); - main->auth->Load(); - forceAuthReload = false; - } +void OBSBasicSettings::reject() +{ + if (AskIfCanCloseSettings()) + close(); } void OBSBasicSettings::on_theme_activated(int idx) @@ -3872,6 +3899,22 @@ void OBSBasicSettings::RecalcOutputResPixels(const char *resText) } } +bool OBSBasicSettings::AskIfCanCloseSettings() +{ + bool canCloseSettings = false; + + if (!Changed() || QueryChanges()) + canCloseSettings = true; + + if (forceAuthReload) { + main->auth->Save(); + main->auth->Load(); + forceAuthReload = false; + } + + return canCloseSettings; +} + void OBSBasicSettings::on_filenameFormatting_textEdited(const QString &text) { #ifdef __APPLE__ diff --git a/UI/window-basic-settings.hpp b/UI/window-basic-settings.hpp index bd1a18303..69054d344 100644 --- a/UI/window-basic-settings.hpp +++ b/UI/window-basic-settings.hpp @@ -289,6 +289,8 @@ private: void RecalcOutputResPixels(const char *resText); + bool AskIfCanCloseSettings(); + QIcon generalIcon; QIcon streamIcon; QIcon outputIcon; @@ -376,6 +378,7 @@ private slots: protected: virtual void closeEvent(QCloseEvent *event); + void reject() override; public: OBSBasicSettings(QWidget *parent);