diff --git a/CMakeLists.txt b/CMakeLists.txt index 286840e8..f29cda95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,6 +254,7 @@ set(advanced-scene-switcher_HEADERS src/headers/screenshot-helper.hpp src/headers/name-dialog.hpp src/headers/duration-control.hpp + src/headers/file-selection.hpp src/headers/section.hpp src/headers/platform-funcs.hpp src/headers/utility.hpp @@ -277,6 +278,7 @@ set(advanced-scene-switcher_SOURCES src/switch-window.cpp src/switch-media.cpp src/switch-network.cpp + src/file-selection.cpp src/hotkey.cpp src/general.cpp src/switch-pause.cpp diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 0a7f77b3..adb62624 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -102,7 +102,7 @@ AdvSceneSwitcher.condition.window="Window" AdvSceneSwitcher.condition.window.entry.line1="{{windows}} exist and ..." AdvSceneSwitcher.condition.window.entry.line2="... is {{fullscreen}} fullscreen {{maximized}} maximized {{focused}} focused {{windowFocusChanged}} foreground window changed" AdvSceneSwitcher.condition.file="File" -AdvSceneSwitcher.condition.file.entry.line1="Content of {{fileType}} {{filePath}} {{browseButton}} matches:" +AdvSceneSwitcher.condition.file.entry.line1="Content of {{fileType}} {{filePath}} matches:" AdvSceneSwitcher.condition.file.entry.line2="{{matchText}}" AdvSceneSwitcher.condition.file.entry.line3="{{useRegex}} {{checkModificationDate}} {{checkFileContent}}" AdvSceneSwitcher.condition.media="Media" @@ -240,7 +240,7 @@ AdvSceneSwitcher.action.run="Run" AdvSceneSwitcher.action.run.arguments="Arguments:" AdvSceneSwitcher.action.run.addArgument="Add argument" AdvSceneSwitcher.action.run.addArgumentDescription="Add new argument:" -AdvSceneSwitcher.action.run.entry="Run {{filePath}} {{browseButton}}" +AdvSceneSwitcher.action.run.entry="Run {{filePath}}" AdvSceneSwitcher.action.sceneVisibility="Scene item visibility" AdvSceneSwitcher.action.sceneVisibility.type.show="Show" AdvSceneSwitcher.action.sceneVisibility.type.hide="Hide" @@ -304,7 +304,7 @@ 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}}:" +AdvSceneSwitcher.action.file.entry="{{actions}} to {{filePath}}:" AdvSceneSwitcher.action.previewScene="Switch preview scene" AdvSceneSwitcher.action.previewScene.entry="Switch preview scene to {{scenes}}" AdvSceneSwitcher.action.SceneSwap="Swap scene (Studio mode)" diff --git a/src/file-selection.cpp b/src/file-selection.cpp new file mode 100644 index 00000000..b253d018 --- /dev/null +++ b/src/file-selection.cpp @@ -0,0 +1,49 @@ +#include "headers/file-selection.hpp" + +#include +#include +#include + +FileSelection::FileSelection(FileSelection::Type type, QWidget *parent) + : _type(type), QWidget(parent) +{ + _filePath = new QLineEdit(); + _browseButton = + new QPushButton(obs_module_text("AdvSceneSwitcher.browse")); + + QWidget::connect(_filePath, SIGNAL(editingFinished()), this, + SLOT(PathChange())); + QWidget::connect(_browseButton, SIGNAL(clicked()), this, + SLOT(BrowseButtonClicked())); + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(_filePath); + layout->addWidget(_browseButton); + layout->setContentsMargins(0, 0, 0, 0); + setLayout(layout); +} + +void FileSelection::SetPath(const QString &path) +{ + _filePath->setText(path); +} + +void FileSelection::BrowseButtonClicked() +{ + QString path; + if (_type == FileSelection::Type::WRITE) { + path = QFileDialog::getSaveFileName(this); + } else { + path = QFileDialog::getOpenFileName(this); + } + if (path.isEmpty()) { + return; + } + + _filePath->setText(path); + emit PathChanged(path); +} + +void FileSelection::PathChange() +{ + emit PathChanged(_filePath->text()); +} diff --git a/src/headers/file-selection.hpp b/src/headers/file-selection.hpp new file mode 100644 index 00000000..c158ddc9 --- /dev/null +++ b/src/headers/file-selection.hpp @@ -0,0 +1,30 @@ +#pragma once +#include +#include +#include + +class FileSelection : public QWidget { + Q_OBJECT + +public: + enum class Type { + READ, + WRITE, + }; + + FileSelection(FileSelection::Type type = FileSelection::Type::READ, + QWidget *parent = 0); + void SetPath(const QString &); + QPushButton *Button() { return _browseButton; } + +private slots: + void BrowseButtonClicked(); + void PathChange(); +signals: + void PathChanged(const QString &); + +private: + Type _type; + QLineEdit *_filePath; + QPushButton *_browseButton; +}; diff --git a/src/headers/macro-action-file.hpp b/src/headers/macro-action-file.hpp index 363beaec..cdc8cf66 100644 --- a/src/headers/macro-action-file.hpp +++ b/src/headers/macro-action-file.hpp @@ -2,6 +2,7 @@ #include #include #include "macro-action-edit.hpp" +#include "file-selection.hpp" enum class FileAction { WRITE, @@ -47,16 +48,14 @@ public: } private slots: - void FilePathChanged(); - void BrowseButtonClicked(); + void PathChanged(const QString &text); void TextChanged(); void ActionChanged(int value); signals: void HeaderInfoChanged(const QString &); protected: - QLineEdit *_filePath; - QPushButton *_browseButton; + FileSelection *_filePath; QPlainTextEdit *_text; QComboBox *_actions; std::shared_ptr _entryData; diff --git a/src/headers/macro-action-run.hpp b/src/headers/macro-action-run.hpp index 14d298da..95e92f9b 100644 --- a/src/headers/macro-action-run.hpp +++ b/src/headers/macro-action-run.hpp @@ -1,5 +1,6 @@ #pragma once #include "macro-action-edit.hpp" +#include "file-selection.hpp" #include #include @@ -43,8 +44,7 @@ public: } private slots: - void FilePathChanged(); - void BrowseButtonClicked(); + void PathChanged(const QString &text); void AddArg(); void RemoveArg(); void ArgUp(); @@ -56,8 +56,7 @@ protected: std::shared_ptr _entryData; private: - QLineEdit *_filePath; - QPushButton *_browseButton; + FileSelection *_filePath; QListWidget *_argList; QPushButton *_addArg; QPushButton *_removeArg; diff --git a/src/headers/macro-condition-file.hpp b/src/headers/macro-condition-file.hpp index 55e5c8c0..325ac0b2 100644 --- a/src/headers/macro-condition-file.hpp +++ b/src/headers/macro-condition-file.hpp @@ -1,5 +1,7 @@ #pragma once #include "macro.hpp" +#include "file-selection.hpp" + #include #include #include @@ -61,8 +63,7 @@ public: private slots: void FileTypeChanged(int index); - void FilePathChanged(); - void BrowseButtonClicked(); + void PathChanged(const QString &text); void MatchTextChanged(); void UseRegexChanged(int state); void CheckModificationDateChanged(int state); @@ -72,8 +73,7 @@ signals: protected: QComboBox *_fileType; - QLineEdit *_filePath; - QPushButton *_browseButton; + FileSelection *_filePath; QPlainTextEdit *_matchText; QCheckBox *_useRegex; QCheckBox *_checkModificationDate; diff --git a/src/headers/macro-condition-video.hpp b/src/headers/macro-condition-video.hpp index c02fc676..0107eac3 100644 --- a/src/headers/macro-condition-video.hpp +++ b/src/headers/macro-condition-video.hpp @@ -1,6 +1,7 @@ #pragma once #include "macro.hpp" #include "screenshot-helper.hpp" +#include "file-selection.hpp" #include #include @@ -41,6 +42,10 @@ private: std::unique_ptr _screenshotData = nullptr; QImage _matchImage; + std::string _modelDataPath = + obs_get_module_data_path(obs_current_module()) + + std::string( + "/res/cascadeClassifiers/haarcascade_frontalface_alt.xml"); static bool _registered; static const std::string id; }; diff --git a/src/macro-action-file.cpp b/src/macro-action-file.cpp index 4ca83db9..cf37027f 100644 --- a/src/macro-action-file.cpp +++ b/src/macro-action-file.cpp @@ -86,9 +86,7 @@ MacroActionFileEdit::MacroActionFileEdit( QWidget *parent, std::shared_ptr entryData) : QWidget(parent) { - _filePath = new QLineEdit(); - _browseButton = - new QPushButton(obs_module_text("AdvSceneSwitcher.browse")); + _filePath = new FileSelection(FileSelection::Type::WRITE); _text = new QPlainTextEdit(); _actions = new QComboBox(); @@ -96,10 +94,8 @@ MacroActionFileEdit::MacroActionFileEdit( 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(_filePath, SIGNAL(PathChanged(const QString &)), this, + SLOT(PathChanged(const QString &))); QWidget::connect(_text, SIGNAL(textChanged()), this, SLOT(TextChanged())); ; @@ -107,7 +103,6 @@ MacroActionFileEdit::MacroActionFileEdit( QHBoxLayout *entryLayout = new QHBoxLayout; std::unordered_map widgetPlaceholders = { {"{{filePath}}", _filePath}, - {"{{browseButton}}", _browseButton}, {"{{matchText}}", _text}, {"{{actions}}", _actions}, }; @@ -131,41 +126,22 @@ void MacroActionFileEdit::UpdateEntryData() } _actions->setCurrentIndex(static_cast(_entryData->_action)); - _filePath->setText(QString::fromStdString(_entryData->_file)); + _filePath->SetPath(QString::fromStdString(_entryData->_file)); _text->setPlainText(QString::fromStdString(_entryData->_text)); } -void MacroActionFileEdit::FilePathChanged() +void MacroActionFileEdit::PathChanged(const QString &text) { if (_loading || !_entryData) { return; } std::lock_guard lock(switcher->m); - _entryData->_file = _filePath->text().toUtf8().constData(); + _entryData->_file = 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) { diff --git a/src/macro-action-run.cpp b/src/macro-action-run.cpp index abbaee6a..9dd42b6d 100644 --- a/src/macro-action-run.cpp +++ b/src/macro-action-run.cpp @@ -65,9 +65,7 @@ MacroActionRunEdit::MacroActionRunEdit( QWidget *parent, std::shared_ptr entryData) : QWidget(parent) { - _filePath = new QLineEdit(); - _browseButton = - new QPushButton(obs_module_text("AdvSceneSwitcher.browse")); + _filePath = new FileSelection(); _argList = new QListWidget(); _addArg = new QPushButton(); _addArg->setMaximumSize(QSize(22, 22)); @@ -90,10 +88,8 @@ MacroActionRunEdit::MacroActionRunEdit( "themeID", QVariant(QString::fromUtf8("downArrowIconSmall"))); _argDown->setFlat(true); - QWidget::connect(_filePath, SIGNAL(editingFinished()), this, - SLOT(FilePathChanged())); - QWidget::connect(_browseButton, SIGNAL(clicked()), this, - SLOT(BrowseButtonClicked())); + QWidget::connect(_filePath, SIGNAL(PathChanged(const QString &)), this, + SLOT(PathChanged(const QString &))); QWidget::connect(_addArg, SIGNAL(clicked()), this, SLOT(AddArg())); QWidget::connect(_removeArg, SIGNAL(clicked()), this, SLOT(RemoveArg())); @@ -103,7 +99,6 @@ MacroActionRunEdit::MacroActionRunEdit( auto *entryLayout = new QHBoxLayout; std::unordered_map widgetPlaceholders = { {"{{filePath}}", _filePath}, - {"{{browseButton}}", _browseButton}, }; placeWidgets(obs_module_text("AdvSceneSwitcher.action.run.entry"), entryLayout, widgetPlaceholders); @@ -137,40 +132,25 @@ void MacroActionRunEdit::UpdateEntryData() if (!_entryData) { return; } - _filePath->setText(QString::fromStdString(_entryData->_path)); + _filePath->SetPath(QString::fromStdString(_entryData->_path)); for (auto &arg : _entryData->_args) { QListWidgetItem *item = new QListWidgetItem(arg, _argList); item->setData(Qt::UserRole, arg); } } -void MacroActionRunEdit::FilePathChanged() +void MacroActionRunEdit::PathChanged(const QString &text) { if (_loading || !_entryData) { return; } std::lock_guard lock(switcher->m); - _entryData->_path = _filePath->text().toUtf8().constData(); + _entryData->_path = text.toUtf8().constData(); emit HeaderInfoChanged( QString::fromStdString(_entryData->GetShortDesc())); } -void MacroActionRunEdit::BrowseButtonClicked() -{ - if (_loading || !_entryData) { - return; - } - - QString path = QFileDialog::getOpenFileName(this); - if (path.isEmpty()) { - return; - } - - _filePath->setText(path); - FilePathChanged(); -} - void MacroActionRunEdit::AddArg() { if (_loading || !_entryData) { diff --git a/src/macro-condition-file.cpp b/src/macro-condition-file.cpp index ec3505a8..59ee0bf9 100644 --- a/src/macro-condition-file.cpp +++ b/src/macro-condition-file.cpp @@ -137,10 +137,7 @@ MacroConditionFileEdit::MacroConditionFileEdit( : QWidget(parent) { _fileType = new QComboBox(); - _filePath = new QLineEdit(); - _browseButton = - new QPushButton(obs_module_text("AdvSceneSwitcher.browse")); - _browseButton->setStyleSheet("border:1px solid gray;"); + _filePath = new FileSelection(); _matchText = new QPlainTextEdit(); _useRegex = new QCheckBox( obs_module_text("AdvSceneSwitcher.fileTab.useRegExp")); @@ -151,10 +148,8 @@ MacroConditionFileEdit::MacroConditionFileEdit( QWidget::connect(_fileType, SIGNAL(currentIndexChanged(int)), this, SLOT(FileTypeChanged(int))); - QWidget::connect(_filePath, SIGNAL(editingFinished()), this, - SLOT(FilePathChanged())); - QWidget::connect(_browseButton, SIGNAL(clicked()), this, - SLOT(BrowseButtonClicked())); + QWidget::connect(_filePath, SIGNAL(PathChanged(const QString &)), this, + SLOT(PathChanged(const QString &))); QWidget::connect(_matchText, SIGNAL(textChanged()), this, SLOT(MatchTextChanged())); QWidget::connect(_useRegex, SIGNAL(stateChanged(int)), this, @@ -170,7 +165,6 @@ MacroConditionFileEdit::MacroConditionFileEdit( std::unordered_map widgetPlaceholders = { {"{{fileType}}", _fileType}, {"{{filePath}}", _filePath}, - {"{{browseButton}}", _browseButton}, {"{{matchText}}", _matchText}, {"{{useRegex}}", _useRegex}, {"{{checkModificationDate}}", _checkModificationDate}, @@ -209,7 +203,7 @@ void MacroConditionFileEdit::UpdateEntryData() _fileType->setCurrentIndex(static_cast(_entryData->_fileType)); - _filePath->setText(QString::fromStdString(_entryData->_file)); + _filePath->SetPath(QString::fromStdString(_entryData->_file)); _matchText->setPlainText(QString::fromStdString(_entryData->_text)); _useRegex->setChecked(_entryData->_useRegex); _checkModificationDate->setChecked(_entryData->_useTime); @@ -225,10 +219,10 @@ void MacroConditionFileEdit::FileTypeChanged(int index) FileType type = static_cast(index); if (type == FileType::LOCAL) { - _browseButton->setDisabled(false); + _filePath->Button()->setDisabled(false); _checkModificationDate->setDisabled(false); } else { - _browseButton->setDisabled(true); + _filePath->Button()->setDisabled(true); _checkModificationDate->setDisabled(true); } @@ -236,37 +230,18 @@ void MacroConditionFileEdit::FileTypeChanged(int index) _entryData->_fileType = type; } -void MacroConditionFileEdit::FilePathChanged() +void MacroConditionFileEdit::PathChanged(const QString &text) { if (_loading || !_entryData) { return; } std::lock_guard lock(switcher->m); - _entryData->_file = _filePath->text().toUtf8().constData(); + _entryData->_file = text.toUtf8().constData(); emit HeaderInfoChanged( QString::fromStdString(_entryData->GetShortDesc())); } -void MacroConditionFileEdit::BrowseButtonClicked() -{ - if (_loading || !_entryData) { - return; - } - - QString path = QFileDialog::getOpenFileName( - this, - tr(obs_module_text("AdvSceneSwitcher.fileTab.selectRead")), - QDir::currentPath(), - tr(obs_module_text("AdvSceneSwitcher.fileTab.anyFileType"))); - if (path.isEmpty()) { - return; - } - - _filePath->setText(path); - FilePathChanged(); -} - void MacroConditionFileEdit::MatchTextChanged() { if (_loading || !_entryData) {