From d1cb79f644cade8f763e7ad7c465ff69ae9a49ce Mon Sep 17 00:00:00 2001 From: spycrab Date: Mon, 4 Mar 2019 20:48:40 +0100 Subject: [PATCH] QtUtils: Add ModalMessageBox --- Source/Core/DolphinQt/CMakeLists.txt | 1 + Source/Core/DolphinQt/DolphinQt.vcxproj | 5 +- .../DolphinQt/QtUtils/ModalMessageBox.cpp | 54 +++++++++++++++++++ .../Core/DolphinQt/QtUtils/ModalMessageBox.h | 23 ++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 Source/Core/DolphinQt/QtUtils/ModalMessageBox.cpp create mode 100644 Source/Core/DolphinQt/QtUtils/ModalMessageBox.h diff --git a/Source/Core/DolphinQt/CMakeLists.txt b/Source/Core/DolphinQt/CMakeLists.txt index 04100f7465..85bc526ae6 100644 --- a/Source/Core/DolphinQt/CMakeLists.txt +++ b/Source/Core/DolphinQt/CMakeLists.txt @@ -104,6 +104,7 @@ add_executable(dolphin-emu QtUtils/DoubleClickEventFilter.cpp QtUtils/ElidedButton.cpp QtUtils/FlowLayout.cpp + QtUtils/ModalMessageBox.cpp QtUtils/ImageConverter.cpp QtUtils/SignalDaemon.cpp QtUtils/WindowActivationEventFilter.cpp diff --git a/Source/Core/DolphinQt/DolphinQt.vcxproj b/Source/Core/DolphinQt/DolphinQt.vcxproj index 0dde566e5a..e2fefa340c 100644 --- a/Source/Core/DolphinQt/DolphinQt.vcxproj +++ b/Source/Core/DolphinQt/DolphinQt.vcxproj @@ -154,6 +154,7 @@ + @@ -245,6 +246,7 @@ + @@ -371,6 +373,7 @@ + @@ -491,4 +494,4 @@ - \ No newline at end of file + diff --git a/Source/Core/DolphinQt/QtUtils/ModalMessageBox.cpp b/Source/Core/DolphinQt/QtUtils/ModalMessageBox.cpp new file mode 100644 index 0000000000..25d10de729 --- /dev/null +++ b/Source/Core/DolphinQt/QtUtils/ModalMessageBox.cpp @@ -0,0 +1,54 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "DolphinQt/QtUtils/ModalMessageBox.h" + +#include + +ModalMessageBox::ModalMessageBox(QWidget* parent) : QMessageBox(parent) +{ + setWindowModality(Qt::WindowModal); + + // No parent is still preferable to showing a hidden parent here. + if (parent != nullptr && !parent->isVisible()) + setParent(nullptr); +} + +static inline int ExecMessageBox(ModalMessageBox::Icon icon, QWidget* parent, const QString& title, + const QString& text, ModalMessageBox::StandardButtons buttons, + ModalMessageBox::StandardButton default_button) +{ + ModalMessageBox msg(parent); + msg.setIcon(icon); + msg.setWindowTitle(title); + msg.setText(text); + msg.setStandardButtons(buttons); + msg.setDefaultButton(default_button); + + return msg.exec(); +} + +int ModalMessageBox::critical(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons, StandardButton default_button) +{ + return ExecMessageBox(QMessageBox::Critical, parent, title, text, buttons, default_button); +} + +int ModalMessageBox::information(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons, StandardButton default_button) +{ + return ExecMessageBox(QMessageBox::Information, parent, title, text, buttons, default_button); +} + +int ModalMessageBox::question(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons, StandardButton default_button) +{ + return ExecMessageBox(QMessageBox::Critical, parent, title, text, buttons, default_button); +} + +int ModalMessageBox::warning(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons, StandardButton default_button) +{ + return ExecMessageBox(QMessageBox::Warning, parent, title, text, buttons, default_button); +} diff --git a/Source/Core/DolphinQt/QtUtils/ModalMessageBox.h b/Source/Core/DolphinQt/QtUtils/ModalMessageBox.h new file mode 100644 index 0000000000..5eb3c597a3 --- /dev/null +++ b/Source/Core/DolphinQt/QtUtils/ModalMessageBox.h @@ -0,0 +1,23 @@ +// Copyright 2019 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include + +// Helper for making message boxes modal by default +class ModalMessageBox : public QMessageBox +{ +public: + explicit ModalMessageBox(QWidget* parent); + + static int critical(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons = Ok, StandardButton default_button = NoButton); + static int information(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons = Ok, StandardButton default_button = NoButton); + static int question(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons = Yes | No, StandardButton default_button = NoButton); + static int warning(QWidget* parent, const QString& title, const QString& text, + StandardButtons buttons = Ok, StandardButton default_button = NoButton); +};