Skip to content

Commit

Permalink
QtUtils: Add ModalMessageBox
Browse files Browse the repository at this point in the history
  • Loading branch information
spycrab committed Mar 4, 2019
1 parent a59010f commit d1cb79f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion Source/Core/DolphinQt/DolphinQt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<QtMoc Include="QtUtils\DoubleClickEventFilter.h" />
<QtMoc Include="QtUtils\ElidedButton.h" />
<QtMoc Include="QtUtils\FlowLayout.h" />
<QtMoc Include="QtUtils\ModalMessageBox.h" />
<QtMoc Include="QtUtils\WindowActivationEventFilter.h" />
<QtMoc Include="QtUtils\WrapInScrollArea.h" />
<QtMoc Include="RenderWidget.h" />
Expand Down Expand Up @@ -245,6 +246,7 @@
<ClCompile Include="$(QtMocOutPrefix)MemoryViewWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)MemoryWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)MenuBar.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ModalMessageBox.cpp" />
<ClCompile Include="$(QtMocOutPrefix)NetPlayDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)NetPlaySetupDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)NewBreakpointDialog.cpp" />
Expand Down Expand Up @@ -371,6 +373,7 @@
<ClCompile Include="QtUtils\ElidedButton.cpp" />
<ClCompile Include="QtUtils\FlowLayout.cpp" />
<ClCompile Include="QtUtils\ImageConverter.cpp" />
<ClCompile Include="QtUtils\ModalMessageBox.cpp" />
<ClCompile Include="QtUtils\WindowActivationEventFilter.cpp" />
<ClCompile Include="QtUtils\WrapInScrollArea.cpp" />
<ClCompile Include="RenderWidget.cpp" />
Expand Down Expand Up @@ -491,4 +494,4 @@
<Message Text="Copy: @(BinaryFiles) -&gt; $(BinaryOutputDir)" Importance="High" />
<Copy SourceFiles="@(BinaryFiles)" DestinationFolder="$(BinaryOutputDir)" />
</Target>
</Project>
</Project>
54 changes: 54 additions & 0 deletions Source/Core/DolphinQt/QtUtils/ModalMessageBox.cpp
Original file line number Diff line number Diff line change
@@ -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 <QApplication>

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);
}
23 changes: 23 additions & 0 deletions Source/Core/DolphinQt/QtUtils/ModalMessageBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2019 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <QMessageBox>

// 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);
};

0 comments on commit d1cb79f

Please sign in to comment.