From ced1c7605ac0953e8934cc0d74f710bb68b80896 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sat, 1 Oct 2022 13:16:59 +0200 Subject: [PATCH] Add option to set default value on load --- data/locale/en-US.ini | 6 +++++ src/utils/variable.cpp | 58 ++++++++++++++++++++++++++++++++++-------- src/utils/variable.hpp | 15 +++++++++-- 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index b55fc6a4..d2cf2eff 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -800,6 +800,12 @@ AdvSceneSwitcher.item.nameReserved="Name reserved!" AdvSceneSwitcher.variable.select="--select variable--" AdvSceneSwitcher.variable.add="Add new variable" +AdvSceneSwitcher.variable.name="Name:" +AdvSceneSwitcher.variable.value="Current value:" +AdvSceneSwitcher.variable.save="Save / load behavior" +AdvSceneSwitcher.variable.save.dontSave="Don't save variable value" +AdvSceneSwitcher.variable.save.save="Save variable value" +AdvSceneSwitcher.variable.save.default="Set to value" AdvSceneSwitcher.connection.select="--select connection--" AdvSceneSwitcher.connection.add="Add new connection" diff --git a/src/utils/variable.cpp b/src/utils/variable.cpp index 5ce4ec3f..0f419f55 100644 --- a/src/utils/variable.cpp +++ b/src/utils/variable.cpp @@ -7,19 +7,24 @@ Variable::Variable() : Item() {} void Variable::Load(obs_data_t *obj) { Item::Load(obj); - _persist = obs_data_get_bool(obj, "persist"); - if (_persist) { + _saveAction = + static_cast(obs_data_get_int(obj, "saveAction")); + _defaultValue = obs_data_get_string(obj, "defaultValue"); + if (_saveAction == SaveAction::SAVE) { _value = obs_data_get_string(obj, "value"); + } else if (_saveAction == SaveAction::SET_DEFAULT) { + _value = _defaultValue; } } void Variable::Save(obs_data_t *obj) const { Item::Save(obj); - obs_data_set_bool(obj, "persist", _persist); - if (_persist) { + obs_data_set_int(obj, "saveAction", static_cast(_saveAction)); + if (_saveAction == SaveAction::SAVE) { obs_data_set_string(obj, "value", _value.c_str()); } + obs_data_set_string(obj, "defaultValue", _defaultValue.c_str()); } bool Variable::DoubleValue(double &value) const @@ -106,16 +111,31 @@ void SwitcherData::loadVariables(obs_data_t *obj) obs_data_array_release(variablesArray); } +static void populateSaveActionSelection(QComboBox *list) +{ + list->addItem( + obs_module_text("AdvSceneSwitcher.variable.save.dontSave")); + list->addItem(obs_module_text("AdvSceneSwitcher.variable.save.save")); + list->addItem( + obs_module_text("AdvSceneSwitcher.variable.save.default")); +} + VariableSettingsDialog::VariableSettingsDialog(QWidget *parent, const Variable &settings) : ItemSettingsDialog(settings, switcher->variables, "AdvSceneSwitcher.variable.select", "AdvSceneSwitcher.variable.add", parent), _value(new QLineEdit()), - _persist(new QCheckBox()) + _defaultValue(new QLineEdit()), + _save(new QComboBox()) { + QWidget::connect(_save, SIGNAL(currentIndexChanged(int)), this, + SLOT(SaveActionChanged(int))); + _value->setText(QString::fromStdString(settings._value)); - _persist->setChecked(settings._persist); + _defaultValue->setText(QString::fromStdString(settings._defaultValue)); + populateSaveActionSelection(_save); + _save->setCurrentIndex(static_cast(settings._saveAction)); QGridLayout *layout = new QGridLayout; int row = 0; @@ -132,15 +152,29 @@ VariableSettingsDialog::VariableSettingsDialog(QWidget *parent, row, 0); layout->addWidget(_value, row, 1); ++row; - layout->addWidget(new QLabel(obs_module_text( - "AdvSceneSwitcher.variable.persist")), - row, 0); - layout->addWidget(_persist, row, 1); + layout->addWidget( + new QLabel(obs_module_text("AdvSceneSwitcher.variable.save")), + row, 0); + auto saveLayout = new QVBoxLayout; + saveLayout->addWidget(_save); + saveLayout->addWidget(_defaultValue); + saveLayout->addStretch(); + layout->addLayout(saveLayout, row, 1); ++row; layout->addWidget(_buttonbox, row, 0, 1, -1); + layout->setSizeConstraint(QLayout::SetFixedSize); setLayout(layout); } +void VariableSettingsDialog::SaveActionChanged(int idx) +{ + const Variable::SaveAction action = + static_cast(idx); + _defaultValue->setVisible(action == Variable::SaveAction::SET_DEFAULT); + adjustSize(); + updateGeometry(); +} + bool VariableSettingsDialog::AskForSettings(QWidget *parent, Variable &settings) { VariableSettingsDialog dialog(parent, settings); @@ -151,7 +185,9 @@ bool VariableSettingsDialog::AskForSettings(QWidget *parent, Variable &settings) settings._name = dialog._name->text().toStdString(); settings._value = dialog._value->text().toStdString(); - settings._persist = dialog._persist->isChecked(); + settings._defaultValue = dialog._defaultValue->text().toStdString(); + settings._saveAction = + static_cast(dialog._save->currentIndex()); return true; } diff --git a/src/utils/variable.hpp b/src/utils/variable.hpp index 453becec..cd3c5271 100644 --- a/src/utils/variable.hpp +++ b/src/utils/variable.hpp @@ -22,9 +22,16 @@ public: return std::make_shared(); } + enum class SaveAction { + DONT_SAVE, + SAVE, + SET_DEFAULT, + }; + private: - bool _persist = false; + SaveAction _saveAction = SaveAction::DONT_SAVE; std::string _value = ""; + std::string _defaultValue = ""; friend VariableSelection; friend VariableSettingsDialog; @@ -43,9 +50,13 @@ public: VariableSettingsDialog(QWidget *parent, const Variable &); static bool AskForSettings(QWidget *parent, Variable &settings); +private slots: + void SaveActionChanged(int); + private: QLineEdit *_value; - QCheckBox *_persist; + QLineEdit *_defaultValue; + QComboBox *_save; }; class VariableSelection : public ItemSelection {