add regex, date check to file switch

This commit is contained in:
WarmUpTill 2018-05-22 21:04:06 +02:00 committed by GitHub
parent 104f74bf39
commit 6338aaa891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 130 additions and 62 deletions

View File

@ -249,7 +249,7 @@ SceneSwitcher::SceneSwitcher(QWidget* parent)
string sceneName = GetWeakSourceName(s.scene);
string transitionName = GetWeakSourceName(s.transition);
QString listText = MakeFileSwitchName(
sceneName.c_str(), transitionName.c_str(), s.file.c_str(), s.text.c_str());
sceneName.c_str(), transitionName.c_str(), s.file.c_str(), s.text.c_str(), s.useRegex, s.useTime);
QListWidgetItem* item = new QListWidgetItem(listText, ui->fileScenesList);
item->setData(Qt::UserRole, listText);
@ -573,6 +573,8 @@ static void SaveSceneSwitcher(obs_data_t* save_data, bool saving, void*)
obs_data_set_string(array_obj, "transition", transitionName);
obs_data_set_string(array_obj, "file", s.file.c_str());
obs_data_set_string(array_obj, "text", s.text.c_str());
obs_data_set_bool(array_obj, "useRegex", s.useRegex);
obs_data_set_bool(array_obj, "useTime", s.useTime);
obs_data_array_push_back(fileArray, array_obj);
obs_source_release(source);
obs_source_release(transition);
@ -876,9 +878,11 @@ static void SaveSceneSwitcher(obs_data_t* save_data, bool saving, void*)
const char* transition = obs_data_get_string(array_obj, "transition");
const char* file = obs_data_get_string(array_obj, "file");
const char* text = obs_data_get_string(array_obj, "text");
bool useRegex = obs_data_get_bool(array_obj, "useRegex");
bool useTime = obs_data_get_bool(array_obj, "useTime");
switcher->fileSwitches.emplace_back(
GetWeakSourceByName(scene), GetWeakTransitionByName(transition), file, text);
GetWeakSourceByName(scene), GetWeakTransitionByName(transition), file, text, useRegex, useTime);
obs_data_release(array_obj);
}

View File

@ -1,5 +1,6 @@
#include <QFileDialog>
#include <QTextStream>
#include <QDateTime>
#include <obs.hpp>
#include "advanced-scene-switcher.hpp"
@ -125,30 +126,49 @@ void SwitcherData::checkFileContent(bool& match, OBSWeakSource& scene, OBSWeakSo
if (!file.open(QIODevice::ReadOnly))
continue;
/*Im using QTextStream here so the conversion between different lineendings is done by QT.
*QT itself uses only the linefeed internally so the input by the user is always using that,
*but the files selected by the user might use different line endings.
*If you are reading this and know of a cleaner way to do this, please let me know :)
*/
QTextStream in(&file);
QTextStream text(&t);
while (!in.atEnd() && !text.atEnd())
//check file mod date
if (s.useTime)
{
QString fileLine = in.readLine();
QString textLine = text.readLine();
if (QString::compare(fileLine, textLine, Qt::CaseSensitive) != 0)
QDateTime newLastMod = QFileInfo(file).lastModified();
if (s.lastMod == newLastMod)
continue;
s.lastMod = newLastMod;
}
if (s.useRegex)
{
QTextStream in(&file);
QRegExp rx(t);
equal = rx.exactMatch(in.readAll());
}
else
{
/*Im using QTextStream here so the conversion between different lineendings is done by QT.
*QT itself uses only the linefeed internally so the input by the user is always using that,
*but the files selected by the user might use different line endings.
*If you are reading this and know of a cleaner way to do this, please let me know :)
*/
QTextStream in(&file);
QTextStream text(&t);
while (!in.atEnd() && !text.atEnd())
{
equal = false;
break;
}
else {
equal = true;
QString fileLine = in.readLine();
QString textLine = text.readLine();
if (QString::compare(fileLine, textLine, Qt::CaseSensitive) != 0)
{
equal = false;
break;
}
else {
equal = true;
}
}
}
file.close();
if (equal) {
if (equal)
{
scene = s.scene;
transition = s.transition;
match = true;
@ -173,7 +193,8 @@ void SceneSwitcher::on_fileAdd_clicked()
QString transitionName = ui->fileTransitions->currentText();
QString fileName = ui->filePathLineEdit->text();
QString text = ui->fileTextEdit->toPlainText();
//QFile file(QString::fromStdString(fileName)); //don't check if file exists because it might not yet exist
bool useRegex = ui->fileContentRegExCheckBox->isChecked();
bool useTime = ui->fileContentTimeCheckBox->isChecked();
if (sceneName.isEmpty() || transitionName.isEmpty() || fileName.isEmpty() || text.isEmpty())
return;
@ -181,7 +202,7 @@ void SceneSwitcher::on_fileAdd_clicked()
OBSWeakSource source = GetWeakSourceByQString(sceneName);
OBSWeakSource transition = GetWeakTransitionByQString(transitionName);
QString switchText = MakeFileSwitchName(sceneName, transitionName, fileName, text);
QString switchText = MakeFileSwitchName(sceneName, transitionName, fileName, text, useRegex, useTime);
QVariant v = QVariant::fromValue(switchText);
@ -190,7 +211,7 @@ void SceneSwitcher::on_fileAdd_clicked()
lock_guard<mutex> lock(switcher->m);
switcher->fileSwitches.emplace_back(
source, transition, fileName.toUtf8().constData(), text.toUtf8().constData());
source, transition, fileName.toUtf8().constData(), text.toUtf8().constData(), useRegex, useTime);
}
@ -235,5 +256,6 @@ void SceneSwitcher::on_fileScenesList_currentRowChanged(int idx)
ui->fileTransitions->setCurrentText(transitionName.c_str());
ui->fileTextEdit->setPlainText(s.text.c_str());
ui->filePathLineEdit->setText(s.file.c_str());
ui->fileContentRegExCheckBox->setChecked(s.useRegex);
ui->fileContentTimeCheckBox->setChecked(s.useTime);
}

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>981</width>
<height>428</height>
<height>457</height>
</rect>
</property>
<property name="windowTitle">
@ -2109,7 +2109,7 @@
<attribute name="title">
<string> Write To File / Read From File </string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_10">
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_14">
<item>
@ -2198,37 +2198,64 @@
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_9">
<layout class="QHBoxLayout" name="horizontalLayout_36">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_36">
<item>
<widget class="QLabel" name="label_44">
<property name="text">
<string>If the file</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="filePathLineEdit"/>
</item>
<item>
<widget class="QPushButton" name="browseButton_3">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_40">
<property name="text">
<string>contains:</string>
</property>
</widget>
</item>
</layout>
<widget class="QLabel" name="label_44">
<property name="text">
<string>If the file</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="fileTextEdit"/>
<widget class="QLineEdit" name="filePathLineEdit"/>
</item>
<item>
<widget class="QPushButton" name="browseButton_3">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_40">
<property name="text">
<string>contains:</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="fileTextEdit"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_37">
<item>
<widget class="QCheckBox" name="fileContentRegExCheckBox">
<property name="text">
<string>Use regular expression (pattern matching)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="fileContentTimeCheckBox">
<property name="text">
<string>Only check contents if modification date changed</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_11">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>

View File

@ -7,6 +7,7 @@
#include <regex>
#include <mutex>
#include <fstream>
#include <QDateTime>
#include "utility.hpp"
#define DEFAULT_INTERVAL 300
@ -157,13 +158,19 @@ struct FileSwitch
OBSWeakSource transition;
string file;
string text;
bool useRegex = false;
bool useTime = false;
QDateTime lastMod;
inline FileSwitch(
OBSWeakSource scene_, OBSWeakSource transition_, const char* file_, const char* text_)
OBSWeakSource scene_, OBSWeakSource transition_, const char* file_, const char* text_, bool useRegex_, bool useTime_)
: scene(scene_)
, transition(transition_)
, file(file_)
, text(text_)
, useRegex(useRegex_)
, useTime(useTime_)
, lastMod()
{
}
};

View File

@ -70,15 +70,23 @@ static inline QString MakeRandomSwitchName(
}
static inline QString MakeFileSwitchName(
const QString& scene, const QString& transition, const QString& fileName, const QString& text)
const QString& scene, const QString& transition, const QString& fileName, const QString& text, bool useRegex, bool useTime)
{
if (text.length() > 30)
return QStringLiteral("Switch to ") + scene + QStringLiteral(" using ") + transition + QStringLiteral(" if ")
+ fileName + QStringLiteral(" contains: \n\"") + text.left(27) + QStringLiteral("...\"");
QString switchName = QStringLiteral("Switch to ") + scene + QStringLiteral(" using ") + transition + QStringLiteral(" if ")
+ fileName;
if (useTime)
switchName += QStringLiteral(" was modified and ");
switchName += QStringLiteral("contains");
if (useRegex)
switchName += QStringLiteral(" (RegEx): \n\"");
else
return QStringLiteral("Switch to ") + scene + QStringLiteral(" using ") + transition + QStringLiteral(" if ")
+ fileName + QStringLiteral(" contains: \n\"") + text + QStringLiteral("\"");
switchName += QStringLiteral(": \n\"");
if (text.length() > 30)
switchName += text.left(27) + QStringLiteral("...\"");
else
switchName += text + QStringLiteral("\"");
return switchName;
}
static inline string GetWeakSourceName(obs_weak_source_t* weak_source)