File: non-modal-dialog.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (160 lines) | stat: -rw-r--r-- 3,857 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "non-modal-dialog.hpp"
#include "obs-module-helper.hpp"

#include <mutex>
#include <obs-frontend-api.h>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLayout>
#include <QMainWindow>

namespace advss {

static std::mutex mutex;
static std::vector<NonModalMessageDialog *> dialogs;

QWidget *GetSettingsWindow();

NonModalMessageDialog::NonModalMessageDialog(const QString &message,
					     bool question,
					     bool focusAdvssWindow)
	: NonModalMessageDialog(message, question ? Type::QUESTION : Type::INFO,
				focusAdvssWindow)
{
}

NonModalMessageDialog::~NonModalMessageDialog()
{
	std::lock_guard<std::mutex> lock(mutex);
	dialogs.erase(std::remove(dialogs.begin(), dialogs.end(), this),
		      dialogs.end());
}

NonModalMessageDialog::NonModalMessageDialog(const QString &message, Type type,
					     bool focusAdvssWindow)
	: QDialog(focusAdvssWindow && 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);
	auto label = new QLabel(message, this);
	label->setTextInteractionFlags(Qt::TextBrowserInteraction);
	label->setOpenExternalLinks(true);
	layout->addWidget(label);

	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);
		_inputEdit->setTabChangesFocus(true);
		_inputEdit->setFocus();
		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);

	std::lock_guard<std::mutex> lock(mutex);
	dialogs.emplace_back(this);
}

QMessageBox::StandardButton NonModalMessageDialog::ShowMessage()
{
	show();
	exec();
	this->deleteLater();
	return _answer;
}

std::optional<std::string> NonModalMessageDialog::GetInput()
{
	show();

	// Trigger resize
	_inputEdit->setPlainText(_inputEdit->toPlainText());

	exec();
	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();
}

void CloseAllInputDialogs()
{
	std::lock_guard<std::mutex> lock(mutex);
	if (dialogs.empty()) {
		return;
	}
	obs_queue_task(
		OBS_TASK_UI,
		[](void *) {
			for (const auto dialog : dialogs) {
				if (dialog->GetType() ==
				    NonModalMessageDialog::Type::INPUT) {
					dialog->close();
				}
			}
		},
		nullptr, true);
}

} // namespace advss