From b61f533709b16b8cfed650d8d1fa6044598e2dc7 Mon Sep 17 00:00:00 2001 From: Karl Robillard Date: Sat, 9 Mar 2024 21:28:48 -0500 Subject: [PATCH] guiqt.cpp: Use CreateMessageDialog parent argument & set default button. Cleanup MessageDialogImplQt. --- src/platform/guiqt.cpp | 68 +++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/src/platform/guiqt.cpp b/src/platform/guiqt.cpp index 063ecd2b..a6efee52 100644 --- a/src/platform/guiqt.cpp +++ b/src/platform/guiqt.cpp @@ -33,7 +33,7 @@ namespace Platform { //----------------------------------------------------------------------------- // Fatal errors //----------------------------------------------------------------------------- - + void FatalError(const std::string& message) { QMessageBox::critical(NULL, QString("Fatal Error"), @@ -48,72 +48,69 @@ void FatalError(const std::string& message) class MessageDialogImplQt final : public MessageDialog { public: - QMessageBox* messageBoxQ; + QMessageBox messageBoxQ; - MessageDialogImplQt() { - messageBoxQ = new QMessageBox(0); - } - - ~MessageDialogImplQt() { - delete messageBoxQ; + MessageDialogImplQt(QWidget* parent) : messageBoxQ(parent) { } #undef ERROR void SetType(Type type) override { switch (type) { case Type::INFORMATION: - messageBoxQ->setIcon(QMessageBox::Information); + messageBoxQ.setIcon(QMessageBox::Information); break; case Type::QUESTION: - messageBoxQ->setIcon(QMessageBox::Question); + messageBoxQ.setIcon(QMessageBox::Question); break; case Type::WARNING: - messageBoxQ->setIcon(QMessageBox::Warning); + messageBoxQ.setIcon(QMessageBox::Warning); break; case Type::ERROR: - messageBoxQ->setIcon(QMessageBox::Critical); + messageBoxQ.setIcon(QMessageBox::Critical); break; } } void SetTitle(std::string title) override { - messageBoxQ->setWindowTitle(QString::fromStdString(title)); + messageBoxQ.setWindowTitle(QString::fromStdString(title)); } void SetMessage(std::string message) override { - messageBoxQ->setText(QString::fromStdString(message)); + messageBoxQ.setText(QString::fromStdString(message)); } void SetDescription(std::string description) override { - messageBoxQ->setInformativeText(QString::fromStdString(description)); + messageBoxQ.setInformativeText(QString::fromStdString(description)); } - void AddButton(std::string label, Response response, bool isDefault = false) override { + void AddButton(std::string label, Response response, bool isDefault) override { + QMessageBox::StandardButton std; switch (response) { case Response::CANCEL: - messageBoxQ->addButton(QMessageBox::StandardButton::Cancel); + std = QMessageBox::StandardButton::Cancel; break; case Response::NO: - messageBoxQ->addButton(QMessageBox::StandardButton::No); + std = QMessageBox::StandardButton::No; break; case Response::YES: - messageBoxQ->addButton(QMessageBox::StandardButton::Yes); + std = QMessageBox::StandardButton::Yes; break; case Response::OK: - messageBoxQ->addButton(QMessageBox::StandardButton::Ok); + std = QMessageBox::StandardButton::Ok; break; case Response::NONE: - messageBoxQ->addButton(QMessageBox::StandardButton::Ignore); + default: + std = QMessageBox::StandardButton::Ignore; break; } + QPushButton* button = messageBoxQ.addButton(std); + if (isDefault) + messageBoxQ.setDefaultButton(button); } Response RunModal() { - //return Response::CANCEL; - QMessageBox::StandardButton responseQ = (QMessageBox::StandardButton)messageBoxQ->exec(); - - switch (responseQ) + switch (messageBoxQ.exec()) { case QMessageBox::StandardButton::Cancel: return Response::CANCEL; @@ -126,17 +123,9 @@ public: default: // NONE return Response::NONE; } - - return Response::NONE; } }; -MessageDialogRef CreateMessageDialog(WindowRef parentWindow) { - std::shared_ptr dialog = std::make_shared(); - //dialog->mbp.hwndOwner = std::static_pointer_cast(parentWindow)->hWindow; - return dialog; -} - //----------------------------------------------------------------------------- // File dialogs //----------------------------------------------------------------------------- @@ -287,14 +276,6 @@ public: TimerImplQt() {} void RunAfter(unsigned milliseconds) override { - #if 0 - QEventLoop loop; - QTimer::singleShot(milliseconds, &loop, &QEventLoop::quit); - loop.exec(); - if (onTimeout) { - onTimeout(); - } - #endif QTimer::singleShot(milliseconds, this, &TimerImplQt::runOnTimeOut); } @@ -923,6 +904,11 @@ WindowRef CreateWindow(Window::Kind kind, WindowRef parentWindow) { std::static_pointer_cast(parentWindow)); } +MessageDialogRef CreateMessageDialog(WindowRef parentWindow) { + return std::make_shared( + std::static_pointer_cast(parentWindow)->ssWindow); +} + FileDialogRef CreateOpenFileDialog(WindowRef parentWindow) { return std::make_shared( std::static_pointer_cast(parentWindow)->ssWindow,