Move NonModalMessageDialog to separate file

This commit is contained in:
WarmUpTill
2023-05-21 00:25:22 +02:00
committed by WarmUpTill
parent 2048ca2816
commit 258a5f6ab7
4 changed files with 170 additions and 61 deletions

View File

@@ -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

View File

@@ -0,0 +1,130 @@
#include "non-modal-dialog.hpp"
#include "obs-module-helper.hpp"
#include <QMainWindow>
#include <QLayout>
#include <QLabel>
#include <QDialogButtonBox>
#include <obs-frontend-api.h>
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<QMainWindow *>(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<std::string> 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<QMainWindow *>(obs_frontend_get_main_window());
if (!window) {
return false;
}
bool closedAtLeastOneDialog = false;
QList<QWidget *> widgets = window->findChildren<QWidget *>();
for (auto &widget : widgets) {
auto dialog = qobject_cast<NonModalMessageDialog *>(widget);
if (!dialog) {
continue;
}
if (dialog->GetType() != NonModalMessageDialog::Type::INPUT) {
continue;
}
dialog->close();
closedAtLeastOneDialog = true;
}
return closedAtLeastOneDialog;
}
} // namespace advss

View File

@@ -0,0 +1,36 @@
#pragma once
#include "resizing-text-edit.hpp"
#include <QDialog>
#include <QMessageBox>
#include <optional>
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<std::string> 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

View File

@@ -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 <QStandardPaths>
#include <QFile>
@@ -19,12 +21,10 @@
#include <QSystemTrayIcon>
#include <QGuiApplication>
#include <QCursor>
#include <QMainWindow>
#include <QScreen>
#include <unordered_map>
#include <regex>
#include <set>
#include <obs-module.h>
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<QMainWindow *>(
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) {