File: macro-segment-script-inline.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 (261 lines) | stat: -rw-r--r-- 7,139 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "macro-segment-script-inline.hpp"
#include "layout-helpers.hpp"
#include "obs-module-helper.hpp"
#include "sync-helpers.hpp"
#include "ui-helpers.hpp"

#include <QDesktopServices>
#include <QFile>

namespace advss {

void MacroSegmentScriptInline::SetType(InlineScript::Type type)
{
	_script.SetType(type);
}

void MacroSegmentScriptInline::SetLanguage(obs_script_lang language)
{
	_script.SetLanguage(language);
}

void MacroSegmentScriptInline::SetScript(const std::string &text)
{
	_script.SetText(text);
}

void MacroSegmentScriptInline::SetPath(const std::string &path)
{
	_script.SetPath(path);
}

static void populateLanguageSelection(QComboBox *list)
{
	list->addItem(
		obs_module_text("AdvSceneSwitcher.script.language.python"),
		OBS_SCRIPT_LANG_PYTHON);
	list->addItem(obs_module_text("AdvSceneSwitcher.script.language.lua"),
		      OBS_SCRIPT_LANG_LUA);
	list->setPlaceholderText(
		obs_module_text("AdvSceneSwitcher.script.language.select"));
}

static void populateScriptTypeSelection(QComboBox *list)
{
	list->addItem(obs_module_text("AdvSceneSwitcher.script.type.inline"),
		      InlineScript::INLINE);
	list->addItem(obs_module_text("AdvSceneSwitcher.script.type.file"),
		      InlineScript::FILE);
}

MacroSegmentScriptInlineEdit::MacroSegmentScriptInlineEdit(
	QWidget *parent, std::shared_ptr<MacroSegmentScriptInline> entryData)
	: QWidget(parent),
	  _scriptType(new QComboBox(this)),
	  _language(new QComboBox(this)),
	  _script(new ScriptEditor(this)),
	  _path(new FileSelection(FileSelection::Type::WRITE, this)),
	  _openFile(new QPushButton(
		  obs_module_text("AdvSceneSwitcher.script.file.open"), this)),
	  _fileLayout(new QHBoxLayout()),
	  _contentIssueWarning(new QLabel(this)),
	  _entryData(entryData)
{
	SetupLayout();
	SetupWidgetConnections();
	PopulateWidgets();
	SetWidgetVisibility();
	_loading = false;
}

void MacroSegmentScriptInlineEdit::ScriptTypeChanged(int idx)
{
	GUARD_LOADING_AND_LOCK();
	_entryData->SetType(static_cast<InlineScript::Type>(
		_scriptType->itemData(idx).toInt()));
	SetWidgetVisibility();
}

void MacroSegmentScriptInlineEdit::LanguageChanged(int idx)
{
	{
		GUARD_LOADING_AND_LOCK();
		_entryData->SetLanguage(static_cast<obs_script_lang>(
			_language->itemData(idx).toInt()));
		const QSignalBlocker b(_script);
		_script->setPlainText(_entryData->GetScript());
	}

	if (_entryData->GetType() == InlineScript::Type::FILE) {
		PathChanged(QString::fromStdString(_entryData->GetPath()));
	}
}

void MacroSegmentScriptInlineEdit::ScriptChanged()
{
	GUARD_LOADING_AND_LOCK();
	_entryData->SetScript(_script->toPlainText().toStdString());
	adjustSize();
	updateGeometry();
}

void MacroSegmentScriptInlineEdit::PathChanged(const QString &path)
{
	GUARD_LOADING_AND_LOCK();

	if (path.isEmpty()) {
		_entryData->SetPath(path.toStdString());
		return;
	}

	// Script language will be detected by OBS based on file extension so
	// adjust it if necessary
	QString pathAdjusted = path;
	if (_entryData->GetLanguage() == OBS_SCRIPT_LANG_PYTHON &&
	    !path.endsWith(".py")) {
		if (path.endsWith(".lua")) {
			pathAdjusted.chop(4);
		}
		pathAdjusted += ".py";
	}
	if (_entryData->GetLanguage() == OBS_SCRIPT_LANG_LUA &&
	    !path.endsWith(".lua")) {
		if (path.endsWith(".py")) {
			pathAdjusted.chop(3);
		}
		pathAdjusted += ".lua";
	}

	const QSignalBlocker b(_path);
	_path->SetPath(pathAdjusted);
	_entryData->SetPath(pathAdjusted.toStdString());

	SetupContentIssueWarning();
}

void MacroSegmentScriptInlineEdit::PopulateWidgets()
{
	populateLanguageSelection(_language);
	populateScriptTypeSelection(_scriptType);

	if (!_entryData) {
		return;
	}

	_scriptType->setCurrentIndex(
		_scriptType->findData(_entryData->GetType()));
	_language->setCurrentIndex(
		_language->findData(_entryData->GetLanguage()));
	_script->setPlainText(_entryData->GetScript());
	_path->SetPath(_entryData->GetPath());
}

void MacroSegmentScriptInlineEdit::SetupWidgetConnections()
{
	QWidget::connect(_scriptType, SIGNAL(currentIndexChanged(int)), this,
			 SLOT(ScriptTypeChanged(int)));
	QWidget::connect(_language, SIGNAL(currentIndexChanged(int)), this,
			 SLOT(LanguageChanged(int)));
	QWidget::connect(_script, SIGNAL(ScriptChanged()), this,
			 SLOT(ScriptChanged()));
	QWidget::connect(_path, SIGNAL(PathChanged(const QString &)), this,
			 SLOT(PathChanged(const QString &)));
	QWidget::connect(_openFile, &QPushButton::clicked, this, [this] {
		QUrl fileUrl = QUrl::fromLocalFile(
			QString::fromStdString(_entryData->GetPath()));
		if (!QDesktopServices::openUrl(fileUrl)) {
			DisplayMessage(obs_module_text(
				"AdvSceneSwitcher.script.file.open.failed"));
		}
	});
}

void MacroSegmentScriptInlineEdit::SetupLayout()
{
	auto languageLayout = new QHBoxLayout();
	PlaceWidgets(obs_module_text("AdvSceneSwitcher.script.language.layout"),
		     languageLayout, {{"{{language}}", _language}});
	auto typeLayout = new QHBoxLayout();
	PlaceWidgets(obs_module_text("AdvSceneSwitcher.script.type.layout"),
		     typeLayout, {{"{{scriptType}}", _scriptType}});
	PlaceWidgets(obs_module_text("AdvSceneSwitcher.script.file.layout"),
		     _fileLayout,
		     {{"{{path}}", _path}, {"{{open}}", _openFile}}, false);
	auto layout = new QVBoxLayout();
	layout->addLayout(typeLayout);
	layout->addLayout(languageLayout);
	layout->addLayout(_fileLayout);
	layout->addWidget(_script);
	layout->addWidget(_contentIssueWarning);
	setLayout(layout);
}

void MacroSegmentScriptInlineEdit::SetWidgetVisibility()
{
	_script->setVisible(_entryData->GetType() ==
			    InlineScript::Type::INLINE);
	SetLayoutVisible(_fileLayout,
			 _entryData->GetType() == InlineScript::Type::FILE);
	SetupContentIssueWarning();

	adjustSize();
	updateGeometry();
}

void MacroSegmentScriptInlineEdit::SetupContentIssueWarning() const
{
	if (_entryData->GetType() == InlineScript::Type::INLINE) {
		_contentIssueWarning->hide();
		return;
	}

	const auto path = QString::fromStdString(_entryData->GetPath());
	QFile file(path);

	if (!file.exists()) {
		_contentIssueWarning->setText(obs_module_text(
			"AdvSceneSwitcher.script.file.warning.notFound"));
		_contentIssueWarning->show();
		return;
	}

	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		_contentIssueWarning->setText(obs_module_text(
			"AdvSceneSwitcher.script.file.warning.openFail"));
		_contentIssueWarning->show();
		return;
	}

	QTextStream in(&file);
	QStringList fileLines;

	while (!in.atEnd()) {
		QString line = in.readLine().trimmed();
		fileLines << line.trimmed();
	}

	file.close();

	static const QStringList requiredLinesLUA = {
		"function script_load(settings)"};
	static const QStringList requiredLinesPython = {
		"def script_load(settings):"};

	const auto &requiredLines = _entryData->GetLanguage() ==
						    OBS_SCRIPT_LANG_PYTHON
					    ? requiredLinesPython
					    : requiredLinesLUA;

	for (const QString &required : requiredLines) {
		if (fileLines.contains(required)) {
			_contentIssueWarning->hide();
			return;
		}
	}

	_contentIssueWarning->setText(obs_module_text(
		"AdvSceneSwitcher.script.file.warning.loadMissing"));
	_contentIssueWarning->show();
}

} // namespace advss