From 0bfdc3890beb7c968212d8997d94257f01abd493 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Sat, 19 Jun 2021 15:27:35 +0200 Subject: [PATCH] Add macro action "file" --- CMakeLists.txt | 2 + data/locale/en-US.ini | 4 + src/headers/macro-action-file.hpp | 66 +++++++++++ src/macro-action-file.cpp | 187 ++++++++++++++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 src/headers/macro-action-file.hpp create mode 100644 src/macro-action-file.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c982ff6e..8f7f6668 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,7 @@ set(advanced-scene-switcher_HEADERS src/headers/switch-generic.hpp src/headers/macro-action-edit.hpp src/headers/macro-action-audio.hpp + src/headers/macro-action-file.hpp src/headers/macro-action-filter.hpp src/headers/macro-action-hotkey.hpp src/headers/macro-action-macro.hpp @@ -159,6 +160,7 @@ set(advanced-scene-switcher_SOURCES src/switch-generic.cpp src/macro-action-edit.cpp src/macro-action-audio.cpp + src/macro-action-file.cpp src/macro-action-filter.cpp src/macro-action-hotkey.cpp src/macro-action-macro.cpp diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 6552dddb..c59ff0a1 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -272,6 +272,10 @@ AdvSceneSwitcher.action.sceneOrder.entry="On {{scenes}} {{actions}} {{sources}} AdvSceneSwitcher.action.sceneTransform="Scene item transform" AdvSceneSwitcher.action.sceneTransform.getTransform="Get transform" AdvSceneSwitcher.action.sceneTransform.entry="On {{scenes}} transform {{sources}}" +AdvSceneSwitcher.action.file="File" +AdvSceneSwitcher.action.file.type.write="Write" +AdvSceneSwitcher.action.file.type.append="Append" +AdvSceneSwitcher.action.file.entry="{{actions}} to {{filePath}} {{browseButton}}:" ; Transition Tab AdvSceneSwitcher.transitionTab.title="Transition" diff --git a/src/headers/macro-action-file.hpp b/src/headers/macro-action-file.hpp new file mode 100644 index 00000000..363beaec --- /dev/null +++ b/src/headers/macro-action-file.hpp @@ -0,0 +1,66 @@ +#pragma once +#include +#include +#include "macro-action-edit.hpp" + +enum class FileAction { + WRITE, + APPEND, +}; + +class MacroActionFile : public MacroAction { +public: + bool PerformAction(); + void LogAction(); + bool Save(obs_data_t *obj); + bool Load(obs_data_t *obj); + std::string GetShortDesc(); + std::string GetId() { return id; }; + static std::shared_ptr Create() + { + return std::make_shared(); + } + + std::string _file = obs_module_text("AdvSceneSwitcher.enterPath"); + std::string _text = obs_module_text("AdvSceneSwitcher.enterText"); + FileAction _action = FileAction::WRITE; + +private: + static bool _registered; + static const std::string id; +}; + +class MacroActionFileEdit : public QWidget { + Q_OBJECT + +public: + MacroActionFileEdit( + QWidget *parent, + std::shared_ptr entryData = nullptr); + void UpdateEntryData(); + static QWidget *Create(QWidget *parent, + std::shared_ptr action) + { + return new MacroActionFileEdit( + parent, + std::dynamic_pointer_cast(action)); + } + +private slots: + void FilePathChanged(); + void BrowseButtonClicked(); + void TextChanged(); + void ActionChanged(int value); +signals: + void HeaderInfoChanged(const QString &); + +protected: + QLineEdit *_filePath; + QPushButton *_browseButton; + QPlainTextEdit *_text; + QComboBox *_actions; + std::shared_ptr _entryData; + +private: + bool _loading = true; +}; diff --git a/src/macro-action-file.cpp b/src/macro-action-file.cpp new file mode 100644 index 00000000..4ca83db9 --- /dev/null +++ b/src/macro-action-file.cpp @@ -0,0 +1,187 @@ +#include "headers/macro-action-file.hpp" +#include "headers/advanced-scene-switcher.hpp" +#include "headers/utility.hpp" + +#include +#include +#include + +const std::string MacroActionFile::id = "file"; + +bool MacroActionFile::_registered = MacroActionFactory::Register( + MacroActionFile::id, + {MacroActionFile::Create, MacroActionFileEdit::Create, + "AdvSceneSwitcher.action.file"}); + +const static std::map actionTypes = { + {FileAction::WRITE, "AdvSceneSwitcher.action.file.type.write"}, + {FileAction::APPEND, "AdvSceneSwitcher.action.file.type.append"}, +}; + +bool MacroActionFile::PerformAction() +{ + QString path = QString::fromStdString(_file); + QFile file(path); + bool open = false; + switch (_action) { + case FileAction::WRITE: + open = file.open(QIODevice::WriteOnly); + break; + case FileAction::APPEND: + open = file.open(QIODevice::WriteOnly | QIODevice::Append); + break; + default: + break; + } + if (open) { + QTextStream out(&file); + out << QString::fromStdString(_text); + } + return true; +} + +void MacroActionFile::LogAction() +{ + auto it = actionTypes.find(_action); + if (it != actionTypes.end()) { + vblog(LOG_INFO, "performed action \"%s\" for file \"%s\"", + it->second.c_str(), _file.c_str()); + } else { + blog(LOG_WARNING, "ignored unknown file action %d", + static_cast(_action)); + } +} + +bool MacroActionFile::Save(obs_data_t *obj) +{ + MacroAction::Save(obj); + obs_data_set_string(obj, "file", _file.c_str()); + obs_data_set_string(obj, "text", _text.c_str()); + obs_data_set_int(obj, "action", static_cast(_action)); + return true; +} + +bool MacroActionFile::Load(obs_data_t *obj) +{ + MacroAction::Load(obj); + _file = obs_data_get_string(obj, "file"); + _text = obs_data_get_string(obj, "text"); + _action = static_cast(obs_data_get_int(obj, "action")); + return true; +} + +std::string MacroActionFile::GetShortDesc() +{ + return _file; +} + +static inline void populateActionSelection(QComboBox *list) +{ + for (auto entry : actionTypes) { + list->addItem(obs_module_text(entry.second.c_str())); + } +} + +MacroActionFileEdit::MacroActionFileEdit( + QWidget *parent, std::shared_ptr entryData) + : QWidget(parent) +{ + _filePath = new QLineEdit(); + _browseButton = + new QPushButton(obs_module_text("AdvSceneSwitcher.browse")); + _text = new QPlainTextEdit(); + _actions = new QComboBox(); + + populateActionSelection(_actions); + + QWidget::connect(_actions, SIGNAL(currentIndexChanged(int)), this, + SLOT(ActionChanged(int))); + QWidget::connect(_filePath, SIGNAL(editingFinished()), this, + SLOT(FilePathChanged())); + QWidget::connect(_browseButton, SIGNAL(clicked()), this, + SLOT(BrowseButtonClicked())); + QWidget::connect(_text, SIGNAL(textChanged()), this, + SLOT(TextChanged())); + ; + + QHBoxLayout *entryLayout = new QHBoxLayout; + std::unordered_map widgetPlaceholders = { + {"{{filePath}}", _filePath}, + {"{{browseButton}}", _browseButton}, + {"{{matchText}}", _text}, + {"{{actions}}", _actions}, + }; + placeWidgets(obs_module_text("AdvSceneSwitcher.action.file.entry"), + entryLayout, widgetPlaceholders); + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addLayout(entryLayout); + mainLayout->addWidget(_text); + setLayout(mainLayout); + + _entryData = entryData; + UpdateEntryData(); + _loading = false; +} + +void MacroActionFileEdit::UpdateEntryData() +{ + if (!_entryData) { + return; + } + + _actions->setCurrentIndex(static_cast(_entryData->_action)); + _filePath->setText(QString::fromStdString(_entryData->_file)); + _text->setPlainText(QString::fromStdString(_entryData->_text)); +} + +void MacroActionFileEdit::FilePathChanged() +{ + if (_loading || !_entryData) { + return; + } + + std::lock_guard lock(switcher->m); + _entryData->_file = _filePath->text().toUtf8().constData(); + emit HeaderInfoChanged( + QString::fromStdString(_entryData->GetShortDesc())); +} + +void MacroActionFileEdit::BrowseButtonClicked() +{ + if (_loading || !_entryData) { + return; + } + + QString path = QFileDialog::getSaveFileName( + this, + tr(obs_module_text("AdvSceneSwitcher.fileTab.selectWrite")), + QDir::currentPath(), + tr(obs_module_text("AdvSceneSwitcher.fileTab.anyFileType"))); + if (path.isEmpty()) { + return; + } + + _filePath->setText(path); + FilePathChanged(); +} + +void MacroActionFileEdit::TextChanged() +{ + if (_loading || !_entryData) { + return; + } + + std::lock_guard lock(switcher->m); + _entryData->_text = _text->toPlainText().toStdString(); +} + +void MacroActionFileEdit::ActionChanged(int value) +{ + if (_loading || !_entryData) { + return; + } + + std::lock_guard lock(switcher->m); + _entryData->_action = static_cast(value); +}