diff --git a/CMakeLists.txt b/CMakeLists.txt index 130492ca..10b46f26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -240,6 +240,7 @@ target_sources( lib/variables/variable-string.cpp lib/variables/variable-string.hpp lib/variables/variable-tab.cpp + lib/variables/variable-tab.hpp lib/variables/variable-text-edit.cpp lib/variables/variable-text-edit.hpp lib/variables/variable.cpp diff --git a/forms/advanced-scene-switcher.ui b/forms/advanced-scene-switcher.ui index 7774e9ce..7354280d 100644 --- a/forms/advanced-scene-switcher.ui +++ b/forms/advanced-scene-switcher.ui @@ -1602,104 +1602,6 @@ - - - AdvSceneSwitcher.variableTab.title - - - - - - - - QAbstractItemView::NoEditTriggers - - - QAbstractItemView::SelectRows - - - false - - - false - - - false - - - - - - - AdvSceneSwitcher.variableTab.help - - - Qt::AlignCenter - - - true - - - - - - - - - - - - 22 - 22 - - - - AdvSceneSwitcher.variableTab.variableAddButton.tooltip - - - true - - - addIconSmall - - - - - - - - 22 - 22 - - - - AdvSceneSwitcher.variableTab.variableRemoveButton.tooltip - - - true - - - removeIconSmall - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - AdvSceneSwitcher.networkTab.title diff --git a/lib/advanced-scene-switcher.cpp b/lib/advanced-scene-switcher.cpp index 23209992..26904e8d 100644 --- a/lib/advanced-scene-switcher.cpp +++ b/lib/advanced-scene-switcher.cpp @@ -113,7 +113,6 @@ void AdvSceneSwitcher::LoadUI() SetupSceneGroupTab(); SetupTriggerTab(); SetupMacroTab(); - SetupVariableTab(); SetupOtherTabs(ui->tabWidget); SetDeprecationWarnings(); @@ -147,12 +146,6 @@ bool AdvSceneSwitcher::eventFilter(QObject *obj, QEvent *event) } else if (pressedKey == Qt::Key_Delete) { RemoveSelectedMacros(); } - } else if (obj == ui->variables && ui->variables->isVisible()) { - if (pressedKey == Qt::Key_F2) { - OpenSettingsForSelectedVariable(); - } else if (pressedKey == Qt::Key_Delete) { - RemoveSelectedVariables(); - } } } diff --git a/lib/advanced-scene-switcher.hpp b/lib/advanced-scene-switcher.hpp index 159c378d..9d52a29a 100644 --- a/lib/advanced-scene-switcher.hpp +++ b/lib/advanced-scene-switcher.hpp @@ -206,19 +206,6 @@ private: /* --- End of macro tab section --- */ - /* --- Begin of variable tab section --- */ -public: - void SetupVariableTab(); - -public slots: - void on_variableAdd_clicked(); - void on_variableRemove_clicked(); - - void OpenSettingsForSelectedVariable(); - void RemoveSelectedVariables(); - - /* --- End of variable tab section --- */ - /* --- Begin of legacy tab section --- */ public: void ClearFrames(QListWidget *list); diff --git a/lib/variables/variable-tab.cpp b/lib/variables/variable-tab.cpp index 3b6bd453..73be6dee 100644 --- a/lib/variables/variable-tab.cpp +++ b/lib/variables/variable-tab.cpp @@ -1,28 +1,61 @@ -#include "advanced-scene-switcher.hpp" +#include "variable-tab.hpp" +#include "log-helper.hpp" +#include "obs-module-helper.hpp" +#include "plugin-state-helpers.hpp" +#include "sync-helpers.hpp" +#include "tab-helpers.hpp" #include "ui-helpers.hpp" #include "variable.hpp" -#include +#include namespace advss { -static void setVariableTabVisible(QTabWidget *tabWidget, bool visible) -{ - for (int idx = 0; idx < tabWidget->count(); idx++) { - if (tabWidget->tabText(idx) != - obs_module_text("AdvSceneSwitcher.variableTab.title")) { - continue; - } +static bool registerTab(); +static void setupTab(QTabWidget *); +static bool registerTabDone = registerTab(); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - // TODO: Switch to setTabVisible() once QT 5.15 is more wide spread - tabWidget->setTabEnabled(idx, visible); - tabWidget->setStyleSheet( - "QTabBar::tab::disabled {width: 0; height: 0; margin: 0; padding: 0; border: none;} "); -#else - tabWidget->setTabVisible(idx, visible); -#endif +static VariableTable *tabWidget = nullptr; + +static bool registerTab() +{ + AddPluginInitStep([]() { + AddSetupTabCallback("variableTab", VariableTable::Create, + setupTab); + }); + return true; +} + +static void setTabVisible(QTabWidget *tabWidget, bool visible) +{ + SetTabVisibleByName( + tabWidget, visible, + obs_module_text("AdvSceneSwitcher.variableTab.title")); +} + +VariableTable *VariableTable::Create() +{ + tabWidget = new VariableTable(); + return tabWidget; +} + +void VariableTable::Add() +{ + auto newVariable = std::make_shared(); + auto accepted = + VariableSettingsDialog::AskForSettings(this, *newVariable); + if (!accepted) { + return; } + + { + auto lock = LockContext(); + auto &variables = GetVariables(); + variables.emplace_back(newVariable); + } + + VariableSignalManager::Instance()->Add( + QString::fromStdString(newVariable->Name())); } static QString formatSaveActionText(Variable *variable) @@ -91,46 +124,18 @@ static QString formatLastChangedTooltip(Variable *variable) .arg(QString::fromStdString(variable->GetPreviousValue())); } -static void addVariableRow(QTableWidget *table, Variable *variable) +static QStringList getCellLabels(Variable *variable, bool addName = true) { - if (!variable) { - blog(LOG_INFO, "%s called with nullptr", __func__); - assert(false); - return; + assert(variable); + + auto result = QStringList(); + if (addName) { + result << QString::fromStdString(variable->Name()); } - - int row = table->rowCount(); - table->setRowCount(row + 1); - - int col = 0; - auto *item = - new QTableWidgetItem(QString::fromStdString(variable->Name())); - table->setItem(row, col, item); - auto varValue = QString::fromStdString(variable->Value(false)); - item = new QTableWidgetItem(varValue); - item->setToolTip(varValue); - table->setItem(row, ++col, item); - item = new QTableWidgetItem(formatSaveActionText(variable)); - table->setItem(row, ++col, item); - item = new QTableWidgetItem(formatLastUsedText(variable)); - table->setItem(row, ++col, item); - item = new QTableWidgetItem(formatLastChangedText(variable)); - item->setToolTip(formatLastChangedTooltip(variable)); - table->setItem(row, ++col, item); - - table->sortByColumn(0, Qt::AscendingOrder); -} - -static void removeVariableRow(QTableWidget *table, const QString &name) -{ - for (int row = 0; row < table->rowCount(); ++row) { - auto item = table->item(row, 0); - if (item && item->text() == name) { - table->removeRow(row); - return; - } - } - table->sortByColumn(0, Qt::AscendingOrder); + result << QString::fromStdString(variable->Value(false)) + << formatSaveActionText(variable) << formatLastUsedText(variable) + << formatLastChangedText(variable); + return result; } static void updateVariableStatus(QTableWidget *table) @@ -147,116 +152,20 @@ static void updateVariableStatus(QTableWidget *table) continue; } - item = table->item(row, 1); - auto varValue = QString::fromStdString(variable->Value(false)); - item->setText(varValue); - item->setToolTip(varValue); - item = table->item(row, 2); - item->setText(formatSaveActionText(variable.get())); - item = table->item(row, 3); - item->setText(formatLastUsedText(variable.get())); - item = table->item(row, 4); - item->setText(formatLastChangedText(variable.get())); - item->setToolTip(formatLastChangedTooltip(variable.get())); + UpdateItemTableRow(table, row, + getCellLabels(variable.get(), false)); } } -static void renameVariable(QTableWidget *table, const QString &oldName, - const QString &newName) +static void openSettingsDialog() { - for (int row = 0; row < table->rowCount(); row++) { - auto item = table->item(row, 0); - if (!item) { - continue; - } - - if (item->text() == oldName) { - item->setText(newName); - table->sortByColumn(0, Qt::AscendingOrder); - return; - } - } - blog(LOG_INFO, "%s called but entry \"%s\" not found", __func__, - oldName.toStdString().c_str()); - assert(false); -} - -void AdvSceneSwitcher::SetupVariableTab() -{ - ui->variables->installEventFilter(this); - - if (GetVariables().empty()) { - setVariableTabVisible(ui->tabWidget, false); - } else { - ui->variablesHelp->hide(); - } - - static const QStringList horizontalHeaders = - QStringList() - << obs_module_text("AdvSceneSwitcher.variableTab.name.header") - << obs_module_text("AdvSceneSwitcher.variableTab.value.header") - << obs_module_text( - "AdvSceneSwitcher.variableTab.saveLoadBehavior.header") - << obs_module_text( - "AdvSceneSwitcher.variableTab.lastUsed.header") - << obs_module_text( - "AdvSceneSwitcher.variableTab.lastChanged.header"); - - auto &variables = GetVariables(); - - ui->variables->setColumnCount(horizontalHeaders.size()); - ui->variables->horizontalHeader()->setSectionResizeMode( - QHeaderView::ResizeMode::Stretch); - ui->variables->setHorizontalHeaderLabels(horizontalHeaders); - - for (const auto &var : variables) { - auto variable = std::static_pointer_cast(var); - addVariableRow(ui->variables, variable.get()); - } - - ui->variables->resizeColumnsToContents(); - ui->variables->resizeRowsToContents(); - - QWidget::connect( - VariableSignalManager::Instance(), - &VariableSignalManager::Rename, - [this](const QString &oldName, const QString &newName) { - renameVariable(ui->variables, oldName, newName); - }); - QWidget::connect(VariableSignalManager::Instance(), - &VariableSignalManager::Add, this, - [this](const QString &name) { - addVariableRow(ui->variables, - GetVariableByQString(name)); - ui->variablesHelp->hide(); - setVariableTabVisible(ui->tabWidget, true); - }); - QWidget::connect(VariableSignalManager::Instance(), - &VariableSignalManager::Remove, this, - [this](const QString &name) { - removeVariableRow(ui->variables, name); - if (ui->variables->rowCount() == 0) { - ui->variablesHelp->show(); - } - }); - QWidget::connect(ui->variables, &QTableWidget::cellDoubleClicked, this, - &AdvSceneSwitcher::OpenSettingsForSelectedVariable); - - auto timer = new QTimer(this); - timer->setInterval(1000); - QWidget::connect(timer, &QTimer::timeout, - [this]() { updateVariableStatus(ui->variables); }); - timer->start(); -} - -void AdvSceneSwitcher::OpenSettingsForSelectedVariable() -{ - auto selectedRows = ui->variables->selectionModel()->selectedRows(); + auto selectedRows = + tabWidget->Table()->selectionModel()->selectedRows(); if (selectedRows.empty()) { return; } - auto cell = ui->variables->item(selectedRows.last().row(), 0); + auto cell = tabWidget->Table()->item(selectedRows.last().row(), 0); if (!cell) { return; } @@ -268,8 +177,8 @@ void AdvSceneSwitcher::OpenSettingsForSelectedVariable() } auto oldName = variable->Name(); - bool accepted = VariableSettingsDialog::AskForSettings(ui->variables, - *variable.get()); + bool accepted = VariableSettingsDialog::AskForSettings( + tabWidget->Table(), *variable.get()); if (accepted && oldName != variable->Name()) { VariableSignalManager::Instance()->Rename( QString::fromStdString(oldName), @@ -277,35 +186,17 @@ void AdvSceneSwitcher::OpenSettingsForSelectedVariable() } } -static void removeVariablesWithNames(const QStringList &varNames) +void VariableTable::Remove() { - for (const auto &name : varNames) { - auto variable = GetVariableByQString(name); - if (!variable) { - continue; - } - - auto &variables = GetVariables(); - variables.erase( - std::remove_if( - variables.begin(), variables.end(), - [variable](const std::shared_ptr &item) { - return item.get() == variable; - }), - variables.end()); - } -} - -void AdvSceneSwitcher::RemoveSelectedVariables() -{ - auto selectedRows = ui->variables->selectionModel()->selectedRows(); + auto selectedRows = + tabWidget->Table()->selectionModel()->selectedRows(); if (selectedRows.empty()) { return; } QStringList varNames; - for (auto row : selectedRows) { - auto cell = ui->variables->item(row.row(), 0); + for (const auto &row : selectedRows) { + auto cell = tabWidget->Table()->item(row.row(), 0); if (!cell) { continue; } @@ -330,7 +221,7 @@ void AdvSceneSwitcher::RemoveSelectedVariables() { auto lock = LockContext(); - removeVariablesWithNames(varNames); + RemoveItemsByName(GetVariables(), varNames); } for (const auto &name : varNames) { @@ -338,28 +229,71 @@ void AdvSceneSwitcher::RemoveSelectedVariables() } } -void AdvSceneSwitcher::on_variableAdd_clicked() +VariableTable::VariableTable(QTabWidget *parent) + : ResourceTable( + parent, obs_module_text("AdvSceneSwitcher.variableTab.help"), + obs_module_text( + "AdvSceneSwitcher.variableTab.variableAddButton.tooltip"), + obs_module_text( + "AdvSceneSwitcher.variableTab.variableRemoveButton.tooltip"), + QStringList() + << obs_module_text( + "AdvSceneSwitcher.variableTab.name.header") + << obs_module_text( + "AdvSceneSwitcher.variableTab.value.header") + << obs_module_text( + "AdvSceneSwitcher.variableTab.saveLoadBehavior.header") + << obs_module_text( + "AdvSceneSwitcher.variableTab.lastUsed.header") + << obs_module_text( + "AdvSceneSwitcher.variableTab.lastChanged.header"), + openSettingsDialog) { - auto newVariable = std::make_shared(); - auto accepted = - VariableSettingsDialog::AskForSettings(this, *newVariable); - if (!accepted) { - return; + for (const auto &variable : GetVariables()) { + auto v = std::static_pointer_cast(variable); + AddItemTableRow(Table(), getCellLabels(v.get())); } - { - auto lock = LockContext(); - auto &variables = GetVariables(); - variables.emplace_back(newVariable); - } - - VariableSignalManager::Instance()->Add( - QString::fromStdString(newVariable->Name())); + SetHelpVisible(GetVariables().empty()); } -void AdvSceneSwitcher::on_variableRemove_clicked() +static void setupTab(QTabWidget *tab) { - RemoveSelectedVariables(); + if (GetVariables().empty()) { + setTabVisible(tab, false); + } + + QWidget::connect(VariableSignalManager::Instance(), + &VariableSignalManager::Rename, + [](const QString &oldName, const QString &newName) { + RenameItemTableRow(tabWidget->Table(), oldName, + newName); + }); + QWidget::connect( + VariableSignalManager::Instance(), &VariableSignalManager::Add, + [tab](const QString &name) { + AddItemTableRow( + tabWidget->Table(), + getCellLabels(GetVariableByQString(name))); + tabWidget->SetHelpVisible(false); + tabWidget->HighlightAddButton(false); + setTabVisible(tab, true); + }); + QWidget::connect(VariableSignalManager::Instance(), + &VariableSignalManager::Remove, + [](const QString &name) { + RemoveItemTableRow(tabWidget->Table(), name); + if (tabWidget->Table()->rowCount() == 0) { + tabWidget->SetHelpVisible(true); + tabWidget->HighlightAddButton(true); + } + }); + + auto timer = new QTimer(tabWidget); + timer->setInterval(1000); + QWidget::connect(timer, &QTimer::timeout, + []() { updateVariableStatus(tabWidget->Table()); }); + timer->start(); } } // namespace advss diff --git a/lib/variables/variable-tab.hpp b/lib/variables/variable-tab.hpp new file mode 100644 index 00000000..61a75475 --- /dev/null +++ b/lib/variables/variable-tab.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "resource-table.hpp" + +namespace advss { + +class VariableTable final : public ResourceTable { + Q_OBJECT + +public: + static VariableTable *Create(); + +private slots: + void Add(); + void Remove(); + +private: + VariableTable(QTabWidget *parent = nullptr); +}; + +} // namespace advss