File: regex-config.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 (228 lines) | stat: -rw-r--r-- 6,513 bytes parent folder | download | duplicates (2)
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
#include "regex-config.hpp"
#include "obs-module-helper.hpp"
#include "path-helpers.hpp"
#include "ui-helpers.hpp"

#include <QLayout>

namespace advss {

RegexConfig::RegexConfig(bool enabled) : _enable(enabled) {}

void RegexConfig::Save(obs_data_t *obj, const char *name) const
{
	auto data = obs_data_create();
	obs_data_set_bool(data, "enable", _enable);
	obs_data_set_bool(data, "partial", _partialMatch);
	obs_data_set_int(data, "options", _options);
	obs_data_set_obj(obj, name, data);
	obs_data_release(data);
}

void RegexConfig::Load(obs_data_t *obj, const char *name)
{
	auto data = obs_data_get_obj(obj, name);
	_enable = obs_data_get_bool(data, "enable");
	_partialMatch = obs_data_get_bool(data, "partial");
	_options = static_cast<QRegularExpression::PatternOption>(
		obs_data_get_int(data, "options"));
	obs_data_release(data);
}

void RegexConfig::CreateBackwardsCompatibleRegex(bool enable, bool setOptions)
{
	_enable = enable;
	if (setOptions) {
		_options |= QRegularExpression::DotMatchesEverythingOption;
	}
}

QRegularExpression::PatternOptions RegexConfig::GetPatternOptions() const
{
	return _options;
}

void RegexConfig::SetPatternOptions(QRegularExpression::PatternOptions options)
{
	_options = options;
}

QRegularExpression RegexConfig::GetRegularExpression(const QString &expr) const
{
	if (_partialMatch) {
		return QRegularExpression(expr, _options);
	}
	return QRegularExpression(QRegularExpression::anchoredPattern(expr),
				  _options);
}

QRegularExpression
RegexConfig::GetRegularExpression(const std::string &expr) const
{
	return GetRegularExpression(QString::fromStdString(expr));
}

bool RegexConfig::Matches(const QString &text, const QString &expression) const
{
	auto regex = GetRegularExpression(expression);
	if (!regex.isValid()) {
		return false;
	}
	auto match = regex.match(text);
	return match.hasMatch();
}

bool RegexConfig::Matches(const std::string &text,
			  const std::string &expression) const
{
	return Matches(QString::fromStdString(text),
		       QString::fromStdString(expression));
}

RegexConfig RegexConfig::PartialMatchRegexConfig(bool enabled)
{
	RegexConfig regex;
	regex._partialMatch = true;
	regex._enable = enabled;
	return regex;
}

RegexConfigWidget::RegexConfigWidget(QWidget *parent, bool showEnable)
	: QWidget(parent),
	  _openSettings(new QToolButton()),
	  _enable(new QPushButton())
{
	SetButtonIcon(_openSettings,
		      GetThemeTypeName() == "Light"
			      ? ":/settings/images/settings/general.svg"
			      : "theme:Dark/settings/general.svg");
	_openSettings->setToolTip(
		obs_module_text("AdvSceneSwitcher.regex.configure"));

	_enable->setToolTip(obs_module_text("AdvSceneSwitcher.regex.enable"));
	_enable->setMaximumWidth(22);
	_enable->setCheckable(true);
	const auto path = GetDataFilePath("res/images/" + GetThemeTypeName() +
					  "Regex.svg");
	SetButtonIcon(_enable, path.c_str());

	QWidget::connect(_enable, SIGNAL(clicked(bool)), this,
			 SLOT(EnableChanged(bool)));
	QWidget::connect(_openSettings, SIGNAL(clicked()), this,
			 SLOT(OpenSettingsClicked()));

	auto layout = new QHBoxLayout();
	layout->setContentsMargins(0, 0, 0, 0);
	layout->addWidget(_enable);
	layout->addWidget(_openSettings);
	setLayout(layout);

	_enable->setVisible(showEnable);
}

void RegexConfigWidget::SetRegexConfig(const RegexConfig &config)
{
	_config = config;
	_enable->setChecked(config._enable);
	SetVisibility();
}

void RegexConfigWidget::OpenSettingsClicked()
{
	bool accepted = RegexConfigDialog::AskForSettings(this, _config);
	if (!accepted) {
		return;
	}
	emit RegexConfigChanged(_config);
}

void RegexConfigWidget::SetVisibility()
{
	_openSettings->setVisible(_config._enable);
	adjustSize();
	updateGeometry();
}

void RegexConfigWidget::EnableChanged(bool value)
{
	_config._enable = value;
	SetVisibility();
	emit RegexConfigChanged(_config);
}

RegexConfigDialog::RegexConfigDialog(QWidget *parent, const RegexConfig &config)
	: QDialog(parent),
	  _partialMatch(new QCheckBox(
		  obs_module_text("AdvSceneSwitcher.regex.partialMatch"))),
	  _caseInsensitive(new QCheckBox(
		  obs_module_text("AdvSceneSwitcher.regex.caseInsensitive"))),
	  _dotMatch(new QCheckBox(
		  obs_module_text("AdvSceneSwitcher.regex.dotMatchNewline"))),
	  _multiLine(new QCheckBox(
		  obs_module_text("AdvSceneSwitcher.regex.multiLine"))),
	  _extendedPattern(new QCheckBox(
		  obs_module_text("AdvSceneSwitcher.regex.extendedPattern"))),
	  _buttonbox(new QDialogButtonBox(QDialogButtonBox::Ok |
					  QDialogButtonBox::Cancel))
{
	setModal(true);
	setWindowModality(Qt::WindowModality::WindowModal);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	_partialMatch->setChecked(config._partialMatch);
	_caseInsensitive->setChecked(config._options &
				     QRegularExpression::CaseInsensitiveOption);
	_dotMatch->setChecked(config._options &
			      QRegularExpression::DotMatchesEverythingOption);
	_multiLine->setChecked(config._options &
			       QRegularExpression::MultilineOption);
	_extendedPattern->setChecked(
		config._options &
		QRegularExpression::ExtendedPatternSyntaxOption);

	QWidget::connect(_buttonbox, &QDialogButtonBox::accepted, this,
			 &QDialog::accept);
	QWidget::connect(_buttonbox, &QDialogButtonBox::rejected, this,
			 &QDialog::reject);

	auto layout = new QVBoxLayout();
	layout->addWidget(_partialMatch);
	layout->addWidget(_caseInsensitive);
	layout->addWidget(_dotMatch);
	layout->addWidget(_multiLine);
	layout->addWidget(_extendedPattern);
	layout->addWidget(_buttonbox, Qt::AlignHCenter);
	setLayout(layout);
}

void RegexConfigDialog::SetOption(RegexConfig &conf,
				  QRegularExpression::PatternOptions what,
				  QCheckBox *cb)
{
	if (cb->isChecked()) {
		conf._options |= what;
	} else {
		conf._options &= ~what;
	}
}

bool RegexConfigDialog::AskForSettings(QWidget *parent, RegexConfig &settings)
{
	RegexConfigDialog dialog(parent, settings);
	dialog.setWindowTitle(obs_module_text("AdvSceneSwitcher.windowTitle"));
	if (dialog.exec() != DialogCode::Accepted) {
		return false;
	}
	settings._partialMatch = dialog._partialMatch->isChecked();
	SetOption(settings, QRegularExpression::CaseInsensitiveOption,
		  dialog._caseInsensitive);
	SetOption(settings, QRegularExpression::DotMatchesEverythingOption,
		  dialog._dotMatch);
	SetOption(settings, QRegularExpression::MultilineOption,
		  dialog._multiLine);
	SetOption(settings, QRegularExpression::ExtendedPatternSyntaxOption,
		  dialog._extendedPattern);
	return true;
}

} // namespace advss