From af5950748da6cec54b70c8a3421865f22921ed5d Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Tue, 10 Jan 2023 23:47:41 +0100 Subject: [PATCH] Switch to using RegexConfig for variable comparison check --- data/locale/en-US.ini | 1 - data/locale/zh-CN.ini | 1 - src/macro-core/macro-condition-variable.cpp | 120 ++++++++------------ src/macro-core/macro-condition-variable.hpp | 7 +- 4 files changed, 51 insertions(+), 78 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index ced20854..c35b7f5d 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -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 diff --git a/data/locale/zh-CN.ini b/data/locale/zh-CN.ini index b95e03b2..8a93efd5 100644 --- a/data/locale/zh-CN.ini +++ b/data/locale/zh-CN.ini @@ -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 diff --git a/src/macro-core/macro-condition-variable.cpp b/src/macro-core/macro-condition-variable.cpp index 423809b8..71a1ac6f 100644 --- a/src/macro-core/macro-condition-variable.cpp +++ b/src/macro-core/macro-condition-variable.cpp @@ -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(_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(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 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(_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 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(); diff --git a/src/macro-core/macro-condition-variable.hpp b/src/macro-core/macro-condition-variable.hpp index 42cb9c18..da89e7c7 100644 --- a/src/macro-core/macro-condition-variable.hpp +++ b/src/macro-core/macro-condition-variable.hpp @@ -2,6 +2,7 @@ #include "macro.hpp" #include "resizing-text-edit.hpp" #include "variable.hpp" +#include "regex-config.hpp" #include #include @@ -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 _entryData; private: