SceneSwitcher/lib/utils/non-modal-dialog.cpp
WarmUpTill 7d0332dd0e Restructure library and plugins
The "core" macro conditions and actions have been extracted out to the
"base" plugin.

The library now mostly contains functionality which is required across
all plugins and (e.g. definitions for macro segments).

The goal is to reduce the complexity and cross-dependencies and group
the source files in a better way.

This should relsove the "library limit of 65535 objects exceeded" build
issue occuring in some Windows build environments.
2024-01-27 14:10:34 +01:00

143 lines
3.4 KiB
C++

#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 {
QWidget *GetSettingsWindow();
NonModalMessageDialog::NonModalMessageDialog(const QString &message,
bool question)
: NonModalMessageDialog(message, question ? Type::QUESTION : Type::INFO)
{
}
NonModalMessageDialog::NonModalMessageDialog(const QString &message, Type type)
: QDialog(GetSettingsWindow()
? GetSettingsWindow()
: static_cast<QMainWindow *>(
obs_frontend_get_main_window())),
_type(type),
_answer(QMessageBox::No)
{
setWindowTitle(obs_module_text("AdvSceneSwitcher.windowTitle"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
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();
this->deleteLater();
return _answer;
}
std::optional<std::string> NonModalMessageDialog::GetInput()
{
show();
// Trigger resize
_inputEdit->setPlainText(_inputEdit->toPlainText());
exec();
this->deleteLater();
if (_answer == QMessageBox::Yes) {
return _input.toStdString();
}
return {};
}
void NonModalMessageDialog::SetInput(const QString &input)
{
assert(_type == Type::INPUT);
_inputEdit->setPlainText(input);
}
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