From 258a5f6ab73cc845a53fc23f8f4fe7f6b967f59a Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sun, 21 May 2023 00:25:22 +0200 Subject: [PATCH] Move NonModalMessageDialog to separate file --- CMakeLists.txt | 2 + src/utils/non-modal-dialog.cpp | 130 +++++++++++++++++++++++++++++++++ src/utils/non-modal-dialog.hpp | 36 +++++++++ src/utils/utility.cpp | 63 +--------------- 4 files changed, 170 insertions(+), 61 deletions(-) create mode 100644 src/utils/non-modal-dialog.cpp create mode 100644 src/utils/non-modal-dialog.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8159b9a0..8c5535e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,6 +265,8 @@ target_sources( src/utils/mouse-wheel-guard.hpp src/utils/name-dialog.cpp src/utils/name-dialog.hpp + src/utils/non-modal-dialog.cpp + src/utils/non-modal-dialog.hpp src/utils/obs-dock.hpp src/utils/obs-module-helper.cpp src/utils/obs-module-helper.hpp diff --git a/src/utils/non-modal-dialog.cpp b/src/utils/non-modal-dialog.cpp new file mode 100644 index 00000000..e4735ee3 --- /dev/null +++ b/src/utils/non-modal-dialog.cpp @@ -0,0 +1,130 @@ +#include "non-modal-dialog.hpp" +#include "obs-module-helper.hpp" + +#include +#include +#include +#include +#include + +namespace advss { + +NonModalMessageDialog::NonModalMessageDialog(const QString &message, + bool question) + : NonModalMessageDialog(message, question ? Type::QUESTION : Type::INFO) +{ +} + +NonModalMessageDialog::NonModalMessageDialog(const QString &message, Type type) + : QDialog(static_cast(obs_frontend_get_main_window())), + _type(type), + _answer(QMessageBox::No) +{ + setWindowTitle(obs_module_text("AdvSceneSwitcher.windowTitle")); + setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); + setAttribute(Qt::WA_DeleteOnClose); + + auto layout = new QVBoxLayout(this); + layout->addWidget(new QLabel(message, this)); + + switch (type) { + case Type::INFO: { + auto buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok); + connect(buttonbox, &QDialogButtonBox::accepted, this, + &NonModalMessageDialog::YesClicked); + layout->addWidget(buttonbox); + break; + } + case Type::QUESTION: { + auto buttonbox = new QDialogButtonBox(QDialogButtonBox::Yes | + QDialogButtonBox::No); + connect(buttonbox, &QDialogButtonBox::accepted, this, + &NonModalMessageDialog::YesClicked); + connect(buttonbox, &QDialogButtonBox::rejected, this, + &NonModalMessageDialog::NoClicked); + layout->addWidget(buttonbox); + break; + } + case Type::INPUT: { + _inputEdit = new ResizingPlainTextEdit(this); + connect(_inputEdit, &ResizingPlainTextEdit::textChanged, this, + &NonModalMessageDialog::InputChanged); + layout->addWidget(_inputEdit); + auto buttonbox = new QDialogButtonBox(QDialogButtonBox::Ok | + QDialogButtonBox::Cancel); + connect(buttonbox, &QDialogButtonBox::accepted, this, + &NonModalMessageDialog::YesClicked); + connect(buttonbox, &QDialogButtonBox::rejected, this, + &NonModalMessageDialog::NoClicked); + layout->addWidget(buttonbox); + break; + } + } + setLayout(layout); +} + +QMessageBox::StandardButton NonModalMessageDialog::ShowMessage() +{ + show(); + exec(); + return _answer; +} + +std::optional NonModalMessageDialog::GetInput() +{ + show(); + + // Trigger resize + _inputEdit->setPlainText(""); + + exec(); + if (_answer == QMessageBox::Yes) { + return _input.toStdString(); + } + return {}; +} + +void NonModalMessageDialog::YesClicked() +{ + _answer = QMessageBox::Yes; + accept(); +} + +void NonModalMessageDialog::NoClicked() +{ + _answer = QMessageBox::No; + accept(); +} + +void NonModalMessageDialog::InputChanged() +{ + _input = _inputEdit->toPlainText(); + adjustSize(); + updateGeometry(); +} + +bool CloseAllInputDialogs() +{ + auto window = + static_cast(obs_frontend_get_main_window()); + if (!window) { + return false; + } + + bool closedAtLeastOneDialog = false; + QList widgets = window->findChildren(); + for (auto &widget : widgets) { + auto dialog = qobject_cast(widget); + if (!dialog) { + continue; + } + if (dialog->GetType() != NonModalMessageDialog::Type::INPUT) { + continue; + } + dialog->close(); + closedAtLeastOneDialog = true; + } + return closedAtLeastOneDialog; +} + +} // namespace advss diff --git a/src/utils/non-modal-dialog.hpp b/src/utils/non-modal-dialog.hpp new file mode 100644 index 00000000..3af4605b --- /dev/null +++ b/src/utils/non-modal-dialog.hpp @@ -0,0 +1,36 @@ +#pragma once +#include "resizing-text-edit.hpp" + +#include +#include +#include + +namespace advss { + +bool CloseAllInputDialogs(); + +class NonModalMessageDialog : public QDialog { + Q_OBJECT + +public: + enum class Type { INFO, QUESTION, INPUT }; + + NonModalMessageDialog(const QString &message, Type); + NonModalMessageDialog(const QString &message, bool question); + QMessageBox::StandardButton ShowMessage(); + std::optional GetInput(); + Type GetType() { return _type; } + +private slots: + void YesClicked(); + void NoClicked(); + void InputChanged(); + +private: + const Type _type; + QString _input = ""; + ResizingPlainTextEdit *_inputEdit = nullptr; + QMessageBox::StandardButton _answer; +}; + +} // namespace advss diff --git a/src/utils/utility.cpp b/src/utils/utility.cpp index eed564d4..f7255ad5 100644 --- a/src/utils/utility.cpp +++ b/src/utils/utility.cpp @@ -3,6 +3,8 @@ #include "scene-selection.hpp" #include "regex-config.hpp" #include "scene-group.hpp" +#include "non-modal-dialog.hpp" +#include "obs-module-helper.hpp" #include #include @@ -19,12 +21,10 @@ #include #include #include -#include #include #include #include #include -#include namespace advss { @@ -445,65 +445,6 @@ bool SaveTransformState(obs_data_t *obj, const struct obs_transform_info &info, return true; } -class NonModalMessageDialog : public QDialog { -public: - NonModalMessageDialog(const QString &message, bool question) - : QDialog(static_cast( - obs_frontend_get_main_window())), - _answer(QMessageBox::No) - { - setWindowTitle(obs_module_text("AdvSceneSwitcher.windowTitle")); - setWindowFlags(windowFlags() & - ~Qt::WindowContextHelpButtonHint); - setAttribute(Qt::WA_DeleteOnClose); - - auto layout = new QVBoxLayout(this); - layout->addWidget(new QLabel(message, this)); - - if (question) { - auto buttonbox = new QDialogButtonBox( - QDialogButtonBox::Yes | QDialogButtonBox::No); - connect(buttonbox, &QDialogButtonBox::accepted, this, - &NonModalMessageDialog::YesClicked); - connect(buttonbox, &QDialogButtonBox::rejected, this, - &NonModalMessageDialog::NoClicked); - layout->addWidget(buttonbox); - } else { - auto buttonbox = - new QDialogButtonBox(QDialogButtonBox::Ok); - connect(buttonbox, &QDialogButtonBox::accepted, this, - &NonModalMessageDialog::YesClicked); - layout->addWidget(buttonbox); - } - } - - QMessageBox::StandardButton ShowMessage() - { - // Use separate QEventLoop to not block rest of the UI - show(); - QEventLoop loop; - connect(this, &NonModalMessageDialog::finished, &loop, - &QEventLoop::quit); - loop.exec(); - return _answer; - } - -private slots: - void YesClicked() - { - _answer = QMessageBox::Yes; - accept(); - } - void NoClicked() - { - _answer = QMessageBox::No; - accept(); - } - -private: - QMessageBox::StandardButton _answer; -}; - bool DisplayMessage(const QString &msg, bool question, bool modal) { if (!modal) {