Switch to using RegexConfig for variable comparison check

This commit is contained in:
WarmUpTill 2023-01-10 23:47:41 +01:00 committed by WarmUpTill
parent df9dc1e002
commit af5950748d
4 changed files with 51 additions and 78 deletions

View File

@ -392,7 +392,6 @@ AdvSceneSwitcher.condition.variable.type.valueChanged="value changed"
AdvSceneSwitcher.condition.variable.type.equalsVariable="equals variable"
AdvSceneSwitcher.condition.variable.type.lessThanVariable="is less than variable"
AdvSceneSwitcher.condition.variable.type.greaterThanVariable="is greater than variable"
AdvSceneSwitcher.condition.variable.regex="Use regular expressions"
AdvSceneSwitcher.condition.variable.entry="{{variables}}{{conditions}}{{strValue}}{{numValue}}{{variables2}}"
; Macro Actions

View File

@ -361,7 +361,6 @@ AdvSceneSwitcher.condition.variable.type.number="是一个数字"
AdvSceneSwitcher.condition.variable.type.lessThan="小于"
AdvSceneSwitcher.condition.variable.type.greaterThan="大于"
AdvSceneSwitcher.condition.variable.type.valueChanged="值已更改"
AdvSceneSwitcher.condition.variable.regex="使用正则表达式"
AdvSceneSwitcher.condition.variable.entry="{{variables}}{{conditions}}{{strValue}}{{numValue}}{{variables2}}"
; Macro Actions

View File

@ -55,16 +55,16 @@ static bool compareNumber(const Variable &var, double value, bool less)
bool MacroConditionVariable::Compare(const Variable &var) const
{
if (_regex) {
try {
std::regex expr(_strValue);
return std::regex_match(var.Value(), expr);
} catch (const std::regex_error &) {
if (_regex.Enabled()) {
auto expr = _regex.GetRegularExpression(_strValue);
if (!expr.isValid()) {
return false;
}
} else {
return _strValue == var.Value();
auto match = expr.match(QString::fromStdString(var.Value()));
return match.hasMatch();
}
return _strValue == var.Value();
}
bool MacroConditionVariable::ValueChanged(const Variable &var)
@ -140,7 +140,7 @@ bool MacroConditionVariable::Save(obs_data_t *obj) const
obs_data_set_string(obj, "strValue", _strValue.c_str());
obs_data_set_double(obj, "numValue", _numValue);
obs_data_set_int(obj, "condition", static_cast<int>(_type));
obs_data_set_bool(obj, "regex", _regex);
_regex.Save(obj);
return true;
}
@ -152,7 +152,12 @@ bool MacroConditionVariable::Load(obs_data_t *obj)
_strValue = obs_data_get_string(obj, "strValue");
_numValue = obs_data_get_double(obj, "numValue");
_type = static_cast<Type>(obs_data_get_int(obj, "condition"));
_regex = obs_data_get_bool(obj, "regex");
_regex.Load(obj);
// TODO: remove in future version
if (obs_data_has_user_value(obj, "regex")) {
_regex.CreateBackwardsCompatibleRegex(
obs_data_get_bool(obj, "regex"));
}
return true;
}
@ -176,8 +181,7 @@ MacroConditionVariableEdit::MacroConditionVariableEdit(
_conditions(new QComboBox()),
_strValue(new ResizingPlainTextEdit(this, 5, 1, 1)),
_numValue(new QDoubleSpinBox()),
_regex(new QCheckBox(
obs_module_text("AdvSceneSwitcher.condition.variable.regex")))
_regex(new RegexConfigWidget(parent))
{
_numValue->setMinimum(-9999999999);
_numValue->setMaximum(9999999999);
@ -193,8 +197,8 @@ MacroConditionVariableEdit::MacroConditionVariableEdit(
SLOT(StrValueChanged()));
QWidget::connect(_numValue, SIGNAL(valueChanged(double)), this,
SLOT(NumValueChanged(double)));
QWidget::connect(_regex, SIGNAL(stateChanged(int)), this,
SLOT(RegexChanged(int)));
QWidget::connect(_regex, SIGNAL(RegexConfigChanged(RegexConfig)), this,
SLOT(RegexChanged(RegexConfig)));
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
{"{{variables}}", _variables},
@ -209,9 +213,14 @@ MacroConditionVariableEdit::MacroConditionVariableEdit(
placeWidgets(
obs_module_text("AdvSceneSwitcher.condition.variable.entry"),
layout, widgetPlaceholders);
auto *regexLayout = new QHBoxLayout;
regexLayout->addWidget(_regex);
regexLayout->addStretch();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(layout);
mainLayout->addWidget(_regex);
mainLayout->addLayout(regexLayout);
setLayout(mainLayout);
_entryData = entryData;
@ -230,7 +239,7 @@ void MacroConditionVariableEdit::UpdateEntryData()
_conditions->setCurrentIndex(static_cast<int>(_entryData->_type));
_strValue->setPlainText(QString::fromStdString(_entryData->_strValue));
_numValue->setValue(_entryData->_numValue);
_regex->setChecked(_entryData->_regex);
_regex->SetRegexConfig(_entryData->_regex);
SetWidgetVisibility();
}
@ -287,14 +296,16 @@ void MacroConditionVariableEdit::NumValueChanged(double val)
_entryData->_numValue = val;
}
void MacroConditionVariableEdit::RegexChanged(int state)
void MacroConditionVariableEdit::RegexChanged(RegexConfig conf)
{
if (_loading || !_entryData) {
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
_entryData->_regex = state;
_entryData->_regex = conf;
adjustSize();
updateGeometry();
}
void MacroConditionVariableEdit::SetWidgetVisibility()
@ -303,62 +314,25 @@ void MacroConditionVariableEdit::SetWidgetVisibility()
return;
}
switch (_entryData->_type) {
case MacroConditionVariable::Type::EQUALS:
_regex->show();
_strValue->show();
_numValue->hide();
_variables2->hide();
break;
case MacroConditionVariable::Type::IS_EMPTY:
_regex->hide();
_strValue->hide();
_numValue->hide();
_variables2->hide();
break;
case MacroConditionVariable::Type::IS_NUMBER:
_regex->hide();
_strValue->hide();
_numValue->hide();
_variables2->hide();
break;
case MacroConditionVariable::Type::LESS_THAN:
_regex->hide();
_strValue->hide();
_numValue->show();
_variables2->hide();
break;
case MacroConditionVariable::Type::GREATER_THAN:
_regex->hide();
_strValue->hide();
_numValue->show();
_variables2->hide();
break;
case MacroConditionVariable::Type::VALUE_CHANGED:
_regex->hide();
_strValue->hide();
_numValue->hide();
_variables2->hide();
break;
case MacroConditionVariable::Type::EQUALS_VARIABLE:
_regex->hide();
_strValue->hide();
_numValue->hide();
_variables2->show();
break;
case MacroConditionVariable::Type::LESS_THAN_VARIABLE:
_regex->hide();
_strValue->hide();
_numValue->hide();
_variables2->show();
break;
case MacroConditionVariable::Type::GREATER_THAN_VARIABLE:
_regex->hide();
_strValue->hide();
_numValue->hide();
_variables2->show();
break;
}
_regex->setVisible(_entryData->_type ==
MacroConditionVariable::Type::EQUALS);
_strValue->setVisible(_entryData->_type ==
MacroConditionVariable::Type::EQUALS);
_numValue->setVisible(
_entryData->_type == MacroConditionVariable::Type::LESS_THAN ||
_entryData->_type ==
MacroConditionVariable::Type::GREATER_THAN);
_numValue->setVisible(
_entryData->_type == MacroConditionVariable::Type::LESS_THAN ||
_entryData->_type ==
MacroConditionVariable::Type::GREATER_THAN);
_variables2->setVisible(
_entryData->_type ==
MacroConditionVariable::Type::EQUALS_VARIABLE ||
_entryData->_type ==
MacroConditionVariable::Type::LESS_THAN_VARIABLE ||
_entryData->_type ==
MacroConditionVariable::Type::GREATER_THAN_VARIABLE);
adjustSize();
updateGeometry();

View File

@ -2,6 +2,7 @@
#include "macro.hpp"
#include "resizing-text-edit.hpp"
#include "variable.hpp"
#include "regex-config.hpp"
#include <QWidget>
#include <QComboBox>
@ -38,7 +39,7 @@ public:
std::string _variable2Name = "";
std::string _strValue = "";
double _numValue = 0;
bool _regex = false;
RegexConfig _regex;
private:
bool Compare(const Variable &) const;
@ -74,7 +75,7 @@ private slots:
void ConditionChanged(int);
void StrValueChanged();
void NumValueChanged(double);
void RegexChanged(int state);
void RegexChanged(RegexConfig);
signals:
void HeaderInfoChanged(const QString &);
@ -85,7 +86,7 @@ protected:
QComboBox *_conditions;
ResizingPlainTextEdit *_strValue;
QDoubleSpinBox *_numValue;
QCheckBox *_regex;
RegexConfigWidget *_regex;
std::shared_ptr<MacroConditionVariable> _entryData;
private: