mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-07-08 22:42:05 +08:00
ui/util: add a helper class ParamWatcher (#28978)
* new class ParamWatcher * Update selfdrive/ui/qt/util.cc * Update selfdrive/ui/qt/util.h * Update selfdrive/ui/qt/util.cc * Update selfdrive/ui/qt/util.cc --------- Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <QButtonGroup>
|
||||
#include <QFileSystemWatcher>
|
||||
#include <QFrame>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
@@ -9,6 +8,7 @@
|
||||
#include <QWidget>
|
||||
|
||||
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
#include "selfdrive/ui/qt/widgets/controls.h"
|
||||
|
||||
// ********** settings window + top-level panels **********
|
||||
@@ -88,5 +88,5 @@ private:
|
||||
ButtonControl *targetBranchBtn;
|
||||
|
||||
Params params;
|
||||
QFileSystemWatcher *fs_watch;
|
||||
ParamWatcher *fs_watch;
|
||||
};
|
||||
|
||||
@@ -83,8 +83,8 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) {
|
||||
});
|
||||
addItem(uninstallBtn);
|
||||
|
||||
fs_watch = new QFileSystemWatcher(this);
|
||||
QObject::connect(fs_watch, &QFileSystemWatcher::fileChanged, [=](const QString path) {
|
||||
fs_watch = new ParamWatcher(this);
|
||||
QObject::connect(fs_watch, &ParamWatcher::paramChanged, [=](const QString ¶m_name, const QString ¶m_value) {
|
||||
updateLabels();
|
||||
});
|
||||
|
||||
@@ -105,10 +105,10 @@ void SoftwarePanel::showEvent(QShowEvent *event) {
|
||||
|
||||
void SoftwarePanel::updateLabels() {
|
||||
// add these back in case the files got removed
|
||||
fs_watch->addPath(QString::fromStdString(params.getParamPath("LastUpdateTime")));
|
||||
fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdateFailedCount")));
|
||||
fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdaterState")));
|
||||
fs_watch->addPath(QString::fromStdString(params.getParamPath("UpdateAvailable")));
|
||||
fs_watch->addParam("LastUpdateTime");
|
||||
fs_watch->addParam("UpdateFailedCount");
|
||||
fs_watch->addParam("UpdaterState");
|
||||
fs_watch->addParam("UpdateAvailable");
|
||||
|
||||
if (!isVisible()) {
|
||||
return;
|
||||
|
||||
+25
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QHash>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
@@ -11,7 +12,6 @@
|
||||
#include <QTextStream>
|
||||
#include <QtXml/QDomDocument>
|
||||
|
||||
#include "common/params.h"
|
||||
#include "common/swaglog.h"
|
||||
#include "system/hardware/hw.h"
|
||||
|
||||
@@ -252,3 +252,27 @@ bool hasLongitudinalControl(const cereal::CarParams::Reader &car_params) {
|
||||
? Params().getBool("ExperimentalLongitudinalEnabled")
|
||||
: car_params.getOpenpilotLongitudinalControl();
|
||||
}
|
||||
|
||||
// ParamWatcher
|
||||
|
||||
ParamWatcher::ParamWatcher(QObject *parent) : QObject(parent) {
|
||||
watcher = new QFileSystemWatcher(this);
|
||||
QObject::connect(watcher, &QFileSystemWatcher::fileChanged, this, &ParamWatcher::fileChanged);
|
||||
}
|
||||
|
||||
void ParamWatcher::fileChanged(const QString &path) {
|
||||
auto param_name = QFileInfo(path).fileName();
|
||||
auto param_value = QString::fromStdString(params.get(param_name.toStdString()));
|
||||
|
||||
auto it = params_hash.find(param_name);
|
||||
bool content_changed = (it == params_hash.end()) || (it.value() != param_value);
|
||||
params_hash[param_name] = param_value;
|
||||
// emit signal when the content changes.
|
||||
if (content_changed) {
|
||||
emit paramChanged(param_name, param_value);
|
||||
}
|
||||
}
|
||||
|
||||
void ParamWatcher::addParam(const QString ¶m_name) {
|
||||
watcher->addPath(QString::fromStdString(params.getParamPath(param_name.toStdString())));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <optional>
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QFileSystemWatcher>
|
||||
#include <QLayout>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
@@ -10,6 +11,7 @@
|
||||
#include <QWidget>
|
||||
|
||||
#include "cereal/gen/cpp/car.capnp.h"
|
||||
#include "common/params.h"
|
||||
|
||||
QString getVersion();
|
||||
QString getBrand();
|
||||
@@ -36,3 +38,21 @@ struct InterFont : public QFont {
|
||||
setWeight(weight);
|
||||
}
|
||||
};
|
||||
|
||||
class ParamWatcher : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ParamWatcher(QObject *parent);
|
||||
void addParam(const QString ¶m_name);
|
||||
|
||||
signals:
|
||||
void paramChanged(const QString ¶m_name, const QString ¶m_value);
|
||||
|
||||
private:
|
||||
void fileChanged(const QString &path);
|
||||
|
||||
QFileSystemWatcher *watcher;
|
||||
QHash<QString, QString> params_hash;
|
||||
Params params;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user