diff --git a/common/control.h b/common/control.h --- a/common/control.h +++ b/common/control.h @@ -22,6 +22,8 @@ #include #include +class QFileSystemWatcher; + class Control : public QObject { Q_OBJECT @@ -39,6 +41,10 @@ virtual ~Control() = default; virtual bool writeFile(); + virtual void activateWatcher(); + +Q_SIGNALS: + void changed(); protected: virtual QString dirPath() const; @@ -53,6 +59,7 @@ private: static QString s_dirName; QVariantMap m_info; + QFileSystemWatcher *m_watcher = nullptr; }; class ControlOutput; @@ -77,6 +84,7 @@ QString filePath() const override; bool writeFile() override; + void activateWatcher() override; private: QVariantList getOutputs() const; diff --git a/common/control.cpp b/common/control.cpp --- a/common/control.cpp +++ b/common/control.cpp @@ -18,6 +18,7 @@ #include "globals.h" #include +#include #include #include @@ -31,6 +32,18 @@ { } +void Control::activateWatcher() +{ + if (m_watcher) { + return; + } + m_watcher = new QFileSystemWatcher({filePath()}, this); + connect(m_watcher, &QFileSystemWatcher::fileChanged, this, [this]() { + readFile(); + Q_EMIT changed(); + }); +} + bool Control::writeFile() { const QString path = filePath(); @@ -137,6 +150,14 @@ // in case of such a change while object exists? } +void ControlConfig::activateWatcher() +{ + for (auto *output : m_outputsControls) { + output->activateWatcher(); + } + Control::activateWatcher(); +} + QString ControlConfig::dirPath() const { return Control::dirPath() % QStringLiteral("configs/");