Merge pull request #5829 from ligfx/qtmsgalerthandler

Qt: register MsgAlertHandler
This commit is contained in:
Leo Lam
2017-08-04 09:53:24 +08:00
committed by GitHub
6 changed files with 114 additions and 51 deletions

View File

@ -8,17 +8,46 @@
#include <QMessageBox>
#include <QObject>
#include "Common/MsgHandler.h"
#include "Core/Analytics.h"
#include "Core/BootManager.h"
#include "Core/Core.h"
#include "DolphinQt2/Host.h"
#include "DolphinQt2/InDevelopmentWarning.h"
#include "DolphinQt2/MainWindow.h"
#include "DolphinQt2/QtUtils/RunOnObject.h"
#include "DolphinQt2/Resources.h"
#include "DolphinQt2/Settings.h"
#include "UICommon/CommandLineParse.h"
#include "UICommon/UICommon.h"
bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no, MsgType style)
{
return RunOnObject(QApplication::instance(), [&] {
QMessageBox message_box(QApplication::activeWindow());
message_box.setWindowTitle(QString::fromUtf8(caption));
message_box.setText(QString::fromUtf8(text));
message_box.setStandardButtons(yes_no ? QMessageBox::Yes | QMessageBox::No : QMessageBox::Ok);
message_box.setIcon([&] {
switch (style)
{
case MsgType::Information:
return QMessageBox::Information;
case MsgType::Question:
return QMessageBox::Question;
case MsgType::Warning:
return QMessageBox::Warning;
case MsgType::Critical:
return QMessageBox::Critical;
}
// appease MSVC
return QMessageBox::NoIcon;
}());
return message_box.exec() == QMessageBox::Yes;
});
}
// N.B. On Windows, this should be called from WinMain. Link against qtmain and specify
// /SubSystem:Windows
int main(int argc, char* argv[])
@ -40,6 +69,9 @@ int main(int argc, char* argv[])
UICommon::Init();
Resources::Init();
// Hook up alerts from core
RegisterMsgAlertHandler(QtMsgAlertHandler);
// Whenever the event loop is about to go to sleep, dispatch the jobs
// queued in the Core first.
QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock,

View File

@ -0,0 +1,34 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <type_traits>
#include <utility>
#include "Common/Event.h"
#include "DolphinQt2/QtUtils/QueueOnObject.h"
class QObject;
// QWidget and subclasses are not thread-safe! This helper takes arbitrary code from any thread,
// safely runs it on the appropriate GUI thread, waits for it to finish, and returns the result.
template <typename F>
auto RunOnObject(QObject* object, F&& functor)
{
// If we queue up a functor on the current thread, it won't run until we return to the event loop,
// which means waiting for it to finish will never complete. Instead, run it immediately.
if (object->thread() == QThread::currentThread())
return functor();
Common::Event event;
std::result_of_t<F()> result;
QueueOnObject(object, [&event, &result, functor = std::forward<F>(functor) ] {
result = functor();
event.Set();
});
event.Wait();
return result;
}