Move variable files to to variables folder

This commit is contained in:
WarmUpTill
2024-01-28 14:45:58 +01:00
committed by WarmUpTill
parent b22913a22c
commit 8ca5e76afb
14 changed files with 15 additions and 14 deletions

View File

@@ -1,45 +0,0 @@
#include "variable-line-edit.hpp"
#include "obs-module-helper.hpp"
#include "ui-helpers.hpp"
namespace advss {
VariableLineEdit::VariableLineEdit(QWidget *parent) : QLineEdit(parent)
{
QLineEdit::setToolTip(
obs_module_text("AdvSceneSwitcher.tooltip.availableVariables"));
QWidget::connect(this, SIGNAL(inputRejected()), this,
SLOT(DisplayValidationMessages()));
}
void VariableLineEdit::setText(const QString &string)
{
QLineEdit::setText(string);
}
void VariableLineEdit::setText(const StringVariable &string)
{
QLineEdit::setText(QString::fromStdString(string.UnresolvedValue()));
}
void VariableLineEdit::setToolTip(const QString &string)
{
QLineEdit::setToolTip(
string + "\n" +
obs_module_text("AdvSceneSwitcher.tooltip.availableVariables"));
}
void VariableLineEdit::DisplayValidationMessages()
{
int maxLength = QLineEdit::maxLength();
if (QLineEdit::text().length() == maxLength) {
DisplayMessage(
QString(obs_module_text(
"AdvSceneSwitcher.validation.text.maxLength"))
.arg(QString::number(maxLength)));
}
}
} // namespace advss

View File

@@ -1,22 +0,0 @@
#pragma once
#include "variable-string.hpp"
#include <QLineEdit>
namespace advss {
class VariableLineEdit : public QLineEdit {
Q_OBJECT
public:
EXPORT VariableLineEdit(QWidget *parent);
EXPORT void setText(const QString &);
EXPORT void setText(const StringVariable &);
EXPORT void setToolTip(const QString &string);
private slots:
void DisplayValidationMessages();
private:
};
} // namespace advss

View File

@@ -1,43 +0,0 @@
#pragma once
#include "variable.hpp"
#include <obs.hpp>
namespace advss {
template<typename T> class NumberVariable {
public:
NumberVariable() = default;
NumberVariable(T);
void Save(obs_data_t *obj, const char *name) const;
void Load(obs_data_t *obj, const char *name);
T GetValue() const;
T GetFixedValue() const { return _value; }
bool HasValidValue() const;
void SetValue(T val) { _value = val; }
void SetValue(const std::weak_ptr<Variable> &var) { _variable = var; }
operator T() const;
enum class Type { FIXED_VALUE, VARIABLE };
Type GetType() const { return _type; }
bool IsFixedType() const { return _type == Type::FIXED_VALUE; }
std::weak_ptr<Variable> GetVariable() const { return _variable; }
private:
Type _type = Type::FIXED_VALUE;
T _value = {};
std::weak_ptr<Variable> _variable;
friend class GenericVaraiableSpinbox;
friend class VariableSpinBox;
friend class VariableDoubleSpinBox;
};
#include "variable-number.tpp"
using IntVariable = NumberVariable<int>;
using DoubleVariable = NumberVariable<double>;
} // namespace advss

View File

@@ -1,95 +0,0 @@
template class NumberVariable<int>;
template class NumberVariable<double>;
template<typename T>
inline NumberVariable<T>::NumberVariable(T value) : _value(value)
{
}
template<typename T>
void NumberVariable<T>::Save(obs_data_t *obj, const char *name) const
{
auto data = obs_data_create();
if constexpr (std::is_same<T, int>::value) {
obs_data_set_int(data, "value", _value);
} else if constexpr (std::is_same<T, double>::value) {
obs_data_set_double(data, "value", _value);
} else {
assert(false);
}
auto var = _variable.lock();
if (var) {
obs_data_set_string(data, "variable", var->Name().c_str());
}
obs_data_set_int(data, "type", static_cast<int>(_type));
obs_data_set_obj(obj, name, data);
obs_data_release(data);
}
template<typename T>
void NumberVariable<T>::Load(obs_data_t *obj, const char *name)
{
auto data = obs_data_get_obj(obj, name);
if constexpr (std::is_same<T, int>::value) {
_value = obs_data_get_int(data, "value");
} else if constexpr (std::is_same<T, double>::value) {
_value = obs_data_get_double(data, "value");
} else {
assert(false);
}
auto variableName = obs_data_get_string(data, "variable");
_variable = GetWeakVariableByName(variableName);
_type = static_cast<Type>(obs_data_get_int(data, "type"));
obs_data_release(data);
}
template<typename T> T NumberVariable<T>::GetValue() const
{
if (_type == Type::FIXED_VALUE) {
return _value;
}
auto var = _variable.lock();
if (!var) {
return {};
}
if constexpr (std::is_same<T, int>::value) {
auto value = var->IntValue();
return value.value_or(0);
} else if constexpr (std::is_same<T, double>::value) {
auto value = var->DoubleValue();
return value.value_or(0.0);
}
assert(false);
return 0;
}
template<typename T> bool NumberVariable<T>::HasValidValue() const
{
if (_type == Type::FIXED_VALUE) {
return true;
}
auto var = _variable.lock();
if (!var) {
return false;
}
if constexpr (std::is_same<T, int>::value) {
auto value = var->IntValue();
return value.has_value();
} else if constexpr (std::is_same<T, double>::value) {
auto value = var->IntValue();
return value.has_value();
}
assert(false);
return false;
}
template<typename T> NumberVariable<T>::operator T() const
{
return GetValue();
}

View File

@@ -1,214 +0,0 @@
#include "variable-spinbox.hpp"
#include "ui-helpers.hpp"
#include <QLayout>
namespace advss {
GenericVaraiableSpinbox::GenericVaraiableSpinbox(QWidget *parent,
bool wholeNumber)
: QWidget(parent),
_fixedValueInt(new QSpinBox()),
_fixedValueDouble(new QDoubleSpinBox()),
_toggleType(new QPushButton()),
_variable(new VariableSelection(this)),
_wholeNumber(wholeNumber)
{
_toggleType->setCheckable(true);
_toggleType->setMaximumWidth(11);
SetButtonIcon(_toggleType, ":/res/images/dots-vert.svg");
QWidget::connect(_fixedValueInt, SIGNAL(valueChanged(int)), this,
SLOT(SetFixedValue(int)));
QWidget::connect(_fixedValueDouble, SIGNAL(valueChanged(double)), this,
SLOT(SetFixedValue(double)));
QWidget::connect(_toggleType, SIGNAL(toggled(bool)), this,
SLOT(ToggleTypeClicked(bool)));
QWidget::connect(_variable, SIGNAL(SelectionChanged(const QString &)),
this, SLOT(VariableChanged(const QString &)));
QWidget::connect(
VariableSignalManager::Instance(),
SIGNAL(Rename(const QString &, const QString &)), this,
SIGNAL(VariableRenamed(const QString &, const QString &)));
QWidget::connect(VariableSignalManager::Instance(),
SIGNAL(Add(const QString &)), this,
SIGNAL(VariableAdded(const QString &)));
QWidget::connect(VariableSignalManager::Instance(),
SIGNAL(Remove(const QString &)), this,
SIGNAL(VariableRemoved(const QString &)));
auto layout = new QHBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(_fixedValueInt);
layout->addWidget(_fixedValueDouble);
layout->addWidget(_variable);
layout->addWidget(_toggleType);
setLayout(layout);
SetVisibility();
}
void GenericVaraiableSpinbox::SetValue(const NumberVariable<int> &number)
{
_numberInt = number;
const QSignalBlocker b1(_toggleType);
const QSignalBlocker b2(_variable);
_toggleType->setChecked(!number.IsFixedType());
_fixedValueInt->setValue(number._value);
_variable->SetVariable(number._variable);
SetVisibility();
}
void GenericVaraiableSpinbox::SetValue(const NumberVariable<double> &number)
{
_numberDouble = number;
const QSignalBlocker b1(_toggleType);
const QSignalBlocker b2(_variable);
_toggleType->setChecked(!number.IsFixedType());
_fixedValueDouble->setValue(number._value);
_variable->SetVariable(number._variable);
SetVisibility();
}
void GenericVaraiableSpinbox::DisableVariableSelection()
{
_hideTypeToggle = true;
_toggleType->hide();
ToggleTypeClicked(false);
}
void GenericVaraiableSpinbox::setMinimum(double value)
{
_fixedValueInt->setMinimum(value);
_fixedValueDouble->setMinimum(value);
}
void GenericVaraiableSpinbox::setMaximum(double value)
{
_fixedValueInt->setMaximum(value);
_fixedValueDouble->setMaximum(value);
}
void GenericVaraiableSpinbox::setPrefix(const QString &prefix)
{
_fixedValueInt->setPrefix(prefix);
_fixedValueDouble->setPrefix(prefix);
}
void GenericVaraiableSpinbox::setSuffix(const QString &suffix)
{
_fixedValueInt->setSuffix(suffix);
_fixedValueDouble->setSuffix(suffix);
}
void GenericVaraiableSpinbox::specialValueText(const QString &text)
{
_fixedValueInt->setSpecialValueText(text);
_fixedValueDouble->setSpecialValueText(text);
}
void GenericVaraiableSpinbox::SetFixedValue(int value)
{
_numberInt._value = value;
const QSignalBlocker b(_fixedValueInt);
_fixedValueInt->setValue(value);
EmitSignals();
}
void GenericVaraiableSpinbox::SetFixedValue(double value)
{
_numberDouble._value = value;
const QSignalBlocker b(_fixedValueDouble);
_fixedValueDouble->setValue(value);
EmitSignals();
}
void GenericVaraiableSpinbox::VariableChanged(const QString &name)
{
_numberInt._variable = GetWeakVariableByQString(name);
_numberDouble._variable = GetWeakVariableByQString(name);
EmitSignals();
}
void GenericVaraiableSpinbox::ToggleTypeClicked(bool useVariable)
{
_numberInt._type = useVariable ? NumberVariable<int>::Type::VARIABLE
: NumberVariable<int>::Type::FIXED_VALUE;
_numberDouble._type =
useVariable ? NumberVariable<double>::Type::VARIABLE
: NumberVariable<double>::Type::FIXED_VALUE;
SetVisibility();
EmitSignals();
}
void GenericVaraiableSpinbox::EmitSignals()
{
if (_wholeNumber) {
emit FixedValueChanged(_numberInt);
emit NumberVariableChanged(_numberInt);
} else {
emit FixedValueChanged(_numberDouble);
emit NumberVariableChanged(_numberDouble);
}
}
void GenericVaraiableSpinbox::SetVisibility()
{
if (_wholeNumber) {
_fixedValueDouble->hide();
SetVisibilityInt();
} else {
_fixedValueInt->hide();
SetVisibilityDouble();
}
}
void GenericVaraiableSpinbox::SetVisibilityInt()
{
if (_numberInt.IsFixedType()) {
_fixedValueInt->show();
_variable->hide();
_toggleType->setVisible(!GetVariables().empty() &&
!_hideTypeToggle);
} else {
_fixedValueInt->hide();
_variable->show();
_toggleType->show();
}
adjustSize();
updateGeometry();
}
void GenericVaraiableSpinbox::SetVisibilityDouble()
{
if (_numberDouble.IsFixedType()) {
_fixedValueDouble->show();
_variable->hide();
_toggleType->setVisible(!GetVariables().empty() &&
!_hideTypeToggle);
} else {
_fixedValueDouble->hide();
_variable->show();
_toggleType->show();
}
adjustSize();
updateGeometry();
}
VariableSpinBox::VariableSpinBox(QWidget *parent)
: GenericVaraiableSpinbox(parent, true)
{
}
VariableDoubleSpinBox::VariableDoubleSpinBox(QWidget *parent)
: GenericVaraiableSpinbox(parent, false)
{
}
void VariableDoubleSpinBox::setDecimals(int prec)
{
_fixedValueDouble->setDecimals(prec);
}
} // namespace advss

View File

@@ -1,76 +0,0 @@
#pragma once
#include "variable-number.hpp"
#include <QWidget>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QPushButton>
namespace advss {
class GenericVaraiableSpinbox : public QWidget {
Q_OBJECT
public:
EXPORT GenericVaraiableSpinbox(QWidget *parent, bool wholeNumber);
EXPORT void SetValue(const NumberVariable<int> &);
EXPORT void SetValue(const NumberVariable<double> &);
EXPORT void DisableVariableSelection();
EXPORT void setMinimum(double value);
EXPORT void setMaximum(double value);
EXPORT void setPrefix(const QString &prefix);
EXPORT void setSuffix(const QString &suffix);
EXPORT void specialValueText(const QString &text);
public slots:
EXPORT void SetFixedValue(int);
EXPORT void SetFixedValue(double);
EXPORT void VariableChanged(const QString &);
EXPORT void ToggleTypeClicked(bool);
signals:
void NumberVariableChanged(const NumberVariable<int> &);
void NumberVariableChanged(const NumberVariable<double> &);
void FixedValueChanged(int);
void FixedValueChanged(double);
void VariableAdded(const QString &);
void VariableRenamed(const QString &oldName, const QString &newName);
void VariableRemoved(const QString &);
protected:
QSpinBox *_fixedValueInt;
QDoubleSpinBox *_fixedValueDouble;
NumberVariable<int> _numberInt;
NumberVariable<double> _numberDouble;
private:
void EmitSignals();
void SetVisibility();
void SetVisibilityInt();
void SetVisibilityDouble();
QPushButton *_toggleType;
VariableSelection *_variable;
bool _wholeNumber;
bool _hideTypeToggle = false;
};
class VariableSpinBox : public GenericVaraiableSpinbox {
public:
EXPORT VariableSpinBox(QWidget *parent = nullptr);
EXPORT NumberVariable<int> Value() { return _numberInt; }
EXPORT QSpinBox *SpinBox() { return _fixedValueInt; }
};
class VariableDoubleSpinBox : public GenericVaraiableSpinbox {
public:
EXPORT VariableDoubleSpinBox(QWidget *parent = nullptr);
EXPORT NumberVariable<double> Value() { return _numberDouble; }
EXPORT QDoubleSpinBox *SpinBox() { return _fixedValueDouble; }
EXPORT void setDecimals(int prec);
};
} // namespace advss

View File

@@ -1,81 +0,0 @@
#include "variable-string.hpp"
#include "utility.hpp"
namespace advss {
void StringVariable::Resolve() const
{
if (GetVariables().empty()) {
_resolvedValue = _value;
return;
}
if (_lastResolve == GetLastVariableChangeTime()) {
return;
}
_resolvedValue = SubstitueVariables(_value);
_lastResolve = GetLastVariableChangeTime();
}
StringVariable::operator std::string() const
{
Resolve();
return _resolvedValue;
}
StringVariable::operator QVariant() const
{
return QVariant::fromValue<StringVariable>(*this);
}
void StringVariable::operator=(std::string value)
{
_value = value;
_lastResolve = {};
}
void StringVariable::operator=(const char *value)
{
_value = value;
_lastResolve = {};
}
void StringVariable::Load(obs_data_t *obj, const char *name)
{
_value = obs_data_get_string(obj, name);
Resolve();
}
void StringVariable::Save(obs_data_t *obj, const char *name) const
{
obs_data_set_string(obj, name, _value.c_str());
}
const char *StringVariable::c_str()
{
Resolve();
return _resolvedValue.c_str();
}
const char *StringVariable::c_str() const
{
Resolve();
return _resolvedValue.c_str();
}
bool StringVariable::empty() const
{
Resolve();
return _resolvedValue.empty();
}
std::string SubstitueVariables(std::string str)
{
for (const auto &v : GetVariables()) {
const auto &variable = std::dynamic_pointer_cast<Variable>(v);
const std::string pattern = "${" + variable->Name() + "}";
ReplaceAll(str, pattern, variable->Value());
}
return str;
}
} // namespace advss

View File

@@ -1,42 +0,0 @@
#pragma once
#include "variable.hpp"
#include <string>
#include <obs.hpp>
namespace advss {
// Helper class which automatically resolves variables contained in strings
// when reading its value as a std::string
class StringVariable {
public:
EXPORT StringVariable() : _value(""){};
EXPORT StringVariable(std::string str) : _value(std::move(str)){};
EXPORT StringVariable(const char *str) : _value(str){};
EXPORT operator std::string() const;
EXPORT operator QVariant() const;
EXPORT void operator=(std::string);
EXPORT void operator=(const char *value);
EXPORT const char *c_str();
EXPORT const char *c_str() const;
EXPORT bool empty() const;
EXPORT const std::string &UnresolvedValue() const { return _value; }
EXPORT void Load(obs_data_t *obj, const char *name);
EXPORT void Save(obs_data_t *obj, const char *name) const;
private:
void Resolve() const;
std::string _value = "";
mutable std::string _resolvedValue = "";
mutable std::chrono::high_resolution_clock::time_point _lastResolve{};
};
std::string SubstitueVariables(std::string str);
} // namespace advss
Q_DECLARE_METATYPE(advss::StringVariable);

View File

@@ -1,32 +0,0 @@
#include "variable-text-edit.hpp"
#include "obs-module-helper.hpp"
namespace advss {
VariableTextEdit::VariableTextEdit(QWidget *parent, const int scrollAt,
const int minLines, const int paddingLines)
: ResizingPlainTextEdit(parent, scrollAt, minLines, paddingLines)
{
QPlainTextEdit::setToolTip(
obs_module_text("AdvSceneSwitcher.tooltip.availableVariables"));
}
void VariableTextEdit::setPlainText(const QString &string)
{
QPlainTextEdit::setPlainText(string);
}
void VariableTextEdit::setPlainText(const StringVariable &string)
{
QPlainTextEdit::setPlainText(
QString::fromStdString(string.UnresolvedValue()));
}
void VariableTextEdit::setToolTip(const QString &string)
{
QPlainTextEdit::setToolTip(
string + "\n" +
obs_module_text("AdvSceneSwitcher.tooltip.availableVariables"));
}
} // namespace advss

View File

@@ -1,20 +0,0 @@
#pragma once
#include "resizing-text-edit.hpp"
#include "variable-string.hpp"
namespace advss {
class VariableTextEdit : public ResizingPlainTextEdit {
Q_OBJECT
public:
EXPORT VariableTextEdit(QWidget *parent, const int scrollAt = 10,
const int minLines = 3,
const int paddingLines = 2);
EXPORT void setPlainText(const QString &);
EXPORT void setPlainText(const StringVariable &);
EXPORT void setToolTip(const QString &string);
private:
};
} // namespace advss

View File

@@ -1,317 +0,0 @@
#include "variable.hpp"
#include "math-helpers.hpp"
#include "obs-module-helper.hpp"
#include <QGridLayout>
namespace advss {
static std::deque<std::shared_ptr<Item>> variables;
// Keep track of the last time a variable was changed to save some work when
// when resolving strings containing variables, etc.
static std::chrono::high_resolution_clock::time_point lastVariableChange{};
std::chrono::high_resolution_clock::time_point GetLastVariableChangeTime()
{
return lastVariableChange;
}
Variable::Variable() : Item()
{
lastVariableChange = std::chrono::high_resolution_clock::now();
}
Variable::~Variable()
{
lastVariableChange = std::chrono::high_resolution_clock::now();
}
void Variable::Load(obs_data_t *obj)
{
Item::Load(obj);
_saveAction =
static_cast<SaveAction>(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;
}
lastVariableChange = std::chrono::high_resolution_clock::now();
}
void Variable::Save(obs_data_t *obj) const
{
Item::Save(obj);
obs_data_set_int(obj, "saveAction", static_cast<int>(_saveAction));
if (_saveAction == SaveAction::SAVE) {
obs_data_set_string(obj, "value", _value.c_str());
}
obs_data_set_string(obj, "defaultValue", _defaultValue.c_str());
}
std::optional<double> Variable::DoubleValue() const
{
return GetDouble(_value);
}
std::optional<int> Variable::IntValue() const
{
return GetInt(_value);
}
void Variable::SetValue(const std::string &val)
{
_value = val;
lastVariableChange = std::chrono::high_resolution_clock::now();
}
void Variable::SetValue(double value)
{
_value = std::to_string(value);
lastVariableChange = std::chrono::high_resolution_clock::now();
}
std::deque<std::shared_ptr<Item>> &GetVariables()
{
return variables;
}
Variable *GetVariableByName(const std::string &name)
{
for (const auto &v : variables) {
if (v->Name() == name) {
return dynamic_cast<Variable *>(v.get());
}
}
return nullptr;
}
Variable *GetVariableByQString(const QString &name)
{
return GetVariableByName(name.toStdString());
}
std::weak_ptr<Variable> GetWeakVariableByName(const std::string &name)
{
for (const auto &v : variables) {
if (v->Name() == name) {
std::weak_ptr<Variable> wp =
std::dynamic_pointer_cast<Variable>(v);
return wp;
}
}
return std::weak_ptr<Variable>();
}
std::weak_ptr<Variable> GetWeakVariableByQString(const QString &name)
{
return GetWeakVariableByName(name.toStdString());
}
QStringList GetVariablesNameList()
{
QStringList list;
for (const auto &var : variables) {
list << QString::fromStdString(var->Name());
}
list.sort();
return list;
}
std::string GetWeakVariableName(std::weak_ptr<Variable> var_)
{
auto var = var_.lock();
if (!var) {
return obs_module_text("AdvSceneSwitcher.variable.invalid");
}
return var->Name();
}
void SaveVariables(obs_data_t *obj)
{
obs_data_array_t *variablesArray = obs_data_array_create();
for (const auto &v : variables) {
obs_data_t *array_obj = obs_data_create();
v->Save(array_obj);
obs_data_array_push_back(variablesArray, array_obj);
obs_data_release(array_obj);
}
obs_data_set_array(obj, "variables", variablesArray);
obs_data_array_release(variablesArray);
}
void LoadVariables(obs_data_t *obj)
{
variables.clear();
obs_data_array_t *variablesArray = obs_data_get_array(obj, "variables");
size_t count = obs_data_array_count(variablesArray);
for (size_t i = 0; i < count; i++) {
obs_data_t *array_obj = obs_data_array_item(variablesArray, i);
auto var = Variable::Create();
variables.emplace_back(var);
variables.back()->Load(array_obj);
obs_data_release(array_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, variables,
"AdvSceneSwitcher.variable.select",
"AdvSceneSwitcher.variable.add",
"AdvSceneSwitcher.item.nameNotAvailable", parent),
_value(new ResizingPlainTextEdit(this)),
_defaultValue(new ResizingPlainTextEdit(this)),
_save(new QComboBox())
{
QWidget::connect(_save, SIGNAL(currentIndexChanged(int)), this,
SLOT(SaveActionChanged(int)));
_value->setPlainText(QString::fromStdString(settings._value));
_defaultValue->setPlainText(
QString::fromStdString(settings._defaultValue));
populateSaveActionSelection(_save);
_save->setCurrentIndex(static_cast<int>(settings._saveAction));
QGridLayout *layout = new QGridLayout;
int row = 0;
layout->addWidget(
new QLabel(obs_module_text("AdvSceneSwitcher.variable.name")),
row, 0);
QHBoxLayout *nameLayout = new QHBoxLayout;
nameLayout->addWidget(_name);
nameLayout->addWidget(_nameHint);
layout->addLayout(nameLayout, row, 1);
++row;
layout->addWidget(
new QLabel(obs_module_text("AdvSceneSwitcher.variable.value")),
row, 0);
layout->addWidget(_value, row, 1);
++row;
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<Variable::SaveAction>(idx);
_defaultValue->setVisible(action == Variable::SaveAction::SET_DEFAULT);
adjustSize();
updateGeometry();
}
bool VariableSettingsDialog::AskForSettings(QWidget *parent, Variable &settings)
{
VariableSettingsDialog dialog(parent, settings);
dialog.setWindowTitle(obs_module_text("AdvSceneSwitcher.windowTitle"));
if (dialog.exec() != DialogCode::Accepted) {
return false;
}
settings._name = dialog._name->text().toStdString();
settings._value = dialog._value->toPlainText().toStdString();
settings._defaultValue =
dialog._defaultValue->toPlainText().toStdString();
settings._saveAction =
static_cast<Variable::SaveAction>(dialog._save->currentIndex());
return true;
}
static bool AskForSettingsWrapper(QWidget *parent, Item &settings)
{
Variable &VariableSettings = dynamic_cast<Variable &>(settings);
if (VariableSettingsDialog::AskForSettings(parent, VariableSettings)) {
lastVariableChange = std::chrono::high_resolution_clock::now();
return true;
}
return false;
}
VariableSelection::VariableSelection(QWidget *parent)
: ItemSelection(variables, Variable::Create, AskForSettingsWrapper,
"AdvSceneSwitcher.variable.select",
"AdvSceneSwitcher.variable.add",
"AdvSceneSwitcher.item.nameNotAvailable",
"AdvSceneSwitcher.variable.configure", parent)
{
// Connect to slots
QWidget::connect(VariableSignalManager::Instance(),
SIGNAL(Rename(const QString &, const QString &)), this,
SLOT(RenameItem(const QString &, const QString &)));
QWidget::connect(VariableSignalManager::Instance(),
SIGNAL(Add(const QString &)), this,
SLOT(AddItem(const QString &)));
QWidget::connect(VariableSignalManager::Instance(),
SIGNAL(Remove(const QString &)), this,
SLOT(RemoveItem(const QString &)));
// Forward signals
QWidget::connect(this,
SIGNAL(ItemRenamed(const QString &, const QString &)),
VariableSignalManager::Instance(),
SIGNAL(Rename(const QString &, const QString &)));
QWidget::connect(this, SIGNAL(ItemAdded(const QString &)),
VariableSignalManager::Instance(),
SIGNAL(Add(const QString &)));
QWidget::connect(this, SIGNAL(ItemRemoved(const QString &)),
VariableSignalManager::Instance(),
SIGNAL(Remove(const QString &)));
}
void VariableSelection::SetVariable(const std::string &variable)
{
const QSignalBlocker blocker(_selection);
if (!!GetVariableByName(variable)) {
_selection->setCurrentText(QString::fromStdString(variable));
} else {
_selection->setCurrentIndex(-1);
}
}
void VariableSelection::SetVariable(const std::weak_ptr<Variable> &variable_)
{
const QSignalBlocker blocker(_selection);
auto var = variable_.lock();
if (var) {
SetVariable(var->Name());
} else {
_selection->setCurrentIndex(-1);
}
}
VariableSignalManager::VariableSignalManager(QObject *parent) : QObject(parent)
{
}
VariableSignalManager *VariableSignalManager::Instance()
{
static VariableSignalManager manager;
return &manager;
}
} // namespace advss

View File

@@ -1,98 +0,0 @@
#pragma once
#include "export-symbol-helper.hpp"
#include "item-selection-helpers.hpp"
#include "resizing-text-edit.hpp"
#include <string>
#include <optional>
#include <QStringList>
#include <obs.hpp>
namespace advss {
class VariableSelection;
class VariableSettingsDialog;
class Variable : public Item {
public:
Variable();
~Variable();
void Load(obs_data_t *obj);
void Save(obs_data_t *obj) const;
EXPORT std::string Value() const { return _value; }
EXPORT std::optional<double> DoubleValue() const;
EXPORT std::optional<int> IntValue() const;
void SetValue(const std::string &val);
void SetValue(double);
static std::shared_ptr<Item> Create()
{
return std::make_shared<Variable>();
}
enum class SaveAction {
DONT_SAVE,
SAVE,
SET_DEFAULT,
};
private:
SaveAction _saveAction = SaveAction::DONT_SAVE;
std::string _value = "";
std::string _defaultValue = "";
friend VariableSelection;
friend VariableSettingsDialog;
};
class ADVSS_EXPORT VariableSettingsDialog : public ItemSettingsDialog {
Q_OBJECT
public:
VariableSettingsDialog(QWidget *parent, const Variable &);
static bool AskForSettings(QWidget *parent, Variable &settings);
private slots:
void SaveActionChanged(int);
private:
ResizingPlainTextEdit *_value;
ResizingPlainTextEdit *_defaultValue;
QComboBox *_save;
};
class ADVSS_EXPORT VariableSelection : public ItemSelection {
Q_OBJECT
public:
VariableSelection(QWidget *parent = 0);
void SetVariable(const std::string &);
void SetVariable(const std::weak_ptr<Variable> &);
};
class ADVSS_EXPORT VariableSignalManager : public QObject {
Q_OBJECT
public:
VariableSignalManager(QObject *parent = nullptr);
static VariableSignalManager *Instance();
signals:
void Rename(const QString &, const QString &);
void Add(const QString &);
void Remove(const QString &);
};
void SaveVariables(obs_data_t *obj);
void LoadVariables(obs_data_t *obj);
std::deque<std::shared_ptr<Item>> &GetVariables();
EXPORT Variable *GetVariableByName(const std::string &name);
EXPORT Variable *GetVariableByQString(const QString &name);
EXPORT std::weak_ptr<Variable> GetWeakVariableByName(const std::string &name);
EXPORT std::weak_ptr<Variable> GetWeakVariableByQString(const QString &name);
std::string GetWeakVariableName(std::weak_ptr<Variable>);
EXPORT QStringList GetVariablesNameList();
std::chrono::high_resolution_clock::time_point GetLastVariableChangeTime();
} // namespace advss