UI: refactor SoftwarePanel (#21244)

* refactor

cleanup

* one params

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
pull/21249/head
Dean Lee 4 years ago committed by GitHub
parent 8f5c7e7bb5
commit 7009ba0344
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 97
      selfdrive/ui/qt/offroad/settings.cc
  2. 14
      selfdrive/ui/qt/offroad/settings.h
  3. 3
      selfdrive/ui/qt/widgets/controls.h

@ -201,16 +201,40 @@ DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) {
} }
SoftwarePanel::SoftwarePanel(QWidget* parent) : QWidget(parent) { SoftwarePanel::SoftwarePanel(QWidget* parent) : QWidget(parent) {
setLayout(new QVBoxLayout()); gitBranchLbl = new LabelControl("Git Branch");
gitCommitLbl = new LabelControl("Git Commit");
osVersionLbl = new LabelControl("OS Version");
versionLbl = new LabelControl("Version", "", QString::fromStdString(params.get("ReleaseNotes")).trimmed());
lastUpdateLbl = new LabelControl("Last Update Check", "", "The last time openpilot successfully checked for an update. The updater only runs while the car is off.");
updateBtn = new ButtonControl("Check for Update", "", "", [=]() {
if (params.getBool("IsOffroad")) {
const QString paramsPath = QString::fromStdString(params.getParamsPath());
fs_watch->addPath(paramsPath + "/d/LastUpdateTime");
fs_watch->addPath(paramsPath + "/d/UpdateFailedCount");
updateBtn->setText("CHECKING");
updateBtn->setEnabled(false);
}
std::system("pkill -1 -f selfdrive.updated");
}, "", this);
QVBoxLayout *main_layout = new QVBoxLayout(this);
QWidget *widgets[] = {versionLbl, lastUpdateLbl, updateBtn, gitBranchLbl, gitCommitLbl, osVersionLbl};
for (int i = 0; i < std::size(widgets); ++i) {
main_layout->addWidget(widgets[i]);
if (i < std::size(widgets) - 1) {
main_layout->addWidget(horizontal_line());
}
}
setStyleSheet(R"(QLabel {font-size: 50px;})"); setStyleSheet(R"(QLabel {font-size: 50px;})");
fs_watch = new QFileSystemWatcher(this); fs_watch = new QFileSystemWatcher(this);
QObject::connect(fs_watch, &QFileSystemWatcher::fileChanged, [=](const QString path) { QObject::connect(fs_watch, &QFileSystemWatcher::fileChanged, [=](const QString path) {
int update_failed_count = Params().get<int>("UpdateFailedCount").value_or(0); int update_failed_count = params.get<int>("UpdateFailedCount").value_or(0);
if (path.contains("UpdateFailedCount") && update_failed_count > 0) { if (path.contains("UpdateFailedCount") && update_failed_count > 0) {
lastUpdateTimeLbl->setText("failed to fetch update"); lastUpdateLbl->setText("failed to fetch update");
updateButton->setText("CHECK"); updateBtn->setText("CHECK");
updateButton->setEnabled(true); updateBtn->setEnabled(true);
} else if (path.contains("LastUpdateTime")) { } else if (path.contains("LastUpdateTime")) {
updateLabels(); updateLabels();
} }
@ -222,62 +246,19 @@ void SoftwarePanel::showEvent(QShowEvent *event) {
} }
void SoftwarePanel::updateLabels() { void SoftwarePanel::updateLabels() {
Params params = Params(); QString lastUpdate = "";
QList<QPair<QString, std::string>> dev_params = { auto tm = params.get("LastUpdateTime");
{"Git Branch", params.get("GitBranch")}, if (!tm.empty()) {
{"Git Commit", params.get("GitCommit").substr(0, 10)}, lastUpdate = timeAgo(QDateTime::fromString(QString::fromStdString(tm + "Z"), Qt::ISODate));
{"OS Version", Hardware::get_os_version()},
};
QString lastUpdateTime = "";
std::string last_update_param = params.get("LastUpdateTime");
if (!last_update_param.empty()) {
QDateTime lastUpdateDate = QDateTime::fromString(QString::fromStdString(last_update_param + "Z"), Qt::ISODate);
lastUpdateTime = timeAgo(lastUpdateDate);
} }
if (labels.size() < dev_params.size()) {
versionLbl = new LabelControl("Version", getBrandVersion(), QString::fromStdString(params.get("ReleaseNotes")).trimmed());
layout()->addWidget(versionLbl);
layout()->addWidget(horizontal_line());
lastUpdateTimeLbl = new LabelControl("Last Update Check", lastUpdateTime, "The last time openpilot successfully checked for an update. The updater only runs while the car is off.");
layout()->addWidget(lastUpdateTimeLbl);
layout()->addWidget(horizontal_line());
updateButton = new ButtonControl("Check for Update", "CHECK", "", [=]() {
Params params = Params();
if (params.getBool("IsOffroad")) {
fs_watch->addPath(QString::fromStdString(params.getParamsPath()) + "/d/LastUpdateTime");
fs_watch->addPath(QString::fromStdString(params.getParamsPath()) + "/d/UpdateFailedCount");
updateButton->setText("CHECKING");
updateButton->setEnabled(false);
}
std::system("pkill -1 -f selfdrive.updated");
}, "", this);
layout()->addWidget(updateButton);
layout()->addWidget(horizontal_line());
} else {
versionLbl->setText(getBrandVersion()); versionLbl->setText(getBrandVersion());
lastUpdateTimeLbl->setText(lastUpdateTime); lastUpdateLbl->setText(lastUpdate);
updateButton->setText("CHECK"); updateBtn->setText("CHECK");
updateButton->setEnabled(true); updateBtn->setEnabled(true);
} gitBranchLbl->setText(QString::fromStdString(params.get("GitBranch")));
gitCommitLbl->setText(QString::fromStdString(params.get("GitCommit")).left(10));
for (int i = 0; i < dev_params.size(); i++) { osVersionLbl->setText(QString::fromStdString(Hardware::get_os_version()));
const auto &[name, value] = dev_params[i];
QString val = QString::fromStdString(value).trimmed();
if (labels.size() > i) {
labels[i]->setText(val);
} else {
labels.push_back(new LabelControl(name, val));
layout()->addWidget(labels[i]);
if (i < (dev_params.size() - 1)) {
layout()->addWidget(horizontal_line());
}
}
}
} }
QWidget * network_panel(QWidget * parent) { QWidget * network_panel(QWidget * parent) {

@ -35,16 +35,18 @@ class SoftwarePanel : public QWidget {
public: public:
explicit SoftwarePanel(QWidget* parent = nullptr); explicit SoftwarePanel(QWidget* parent = nullptr);
protected: private:
void showEvent(QShowEvent *event) override; void showEvent(QShowEvent *event) override;
void updateLabels();
private: LabelControl *gitBranchLbl;
QList<LabelControl *> labels; LabelControl *gitCommitLbl;
LabelControl *osVersionLbl;
LabelControl *versionLbl; LabelControl *versionLbl;
LabelControl *lastUpdateTimeLbl; LabelControl *lastUpdateLbl;
ButtonControl *updateButton; ButtonControl *updateBtn;
void updateLabels();
Params params;
QFileSystemWatcher *fs_watch; QFileSystemWatcher *fs_watch;
}; };

@ -10,7 +10,6 @@
#include "selfdrive/ui/qt/widgets/toggle.h" #include "selfdrive/ui/qt/widgets/toggle.h"
QFrame *horizontal_line(QWidget *parent = nullptr); QFrame *horizontal_line(QWidget *parent = nullptr);
class AbstractControl : public QFrame { class AbstractControl : public QFrame {
Q_OBJECT Q_OBJECT
@ -42,7 +41,7 @@ class LabelControl : public AbstractControl {
Q_OBJECT Q_OBJECT
public: public:
LabelControl(const QString &title, const QString &text, const QString &desc = "", QWidget *parent = nullptr) : AbstractControl(title, desc, "", parent) { LabelControl(const QString &title, const QString &text = "", const QString &desc = "", QWidget *parent = nullptr) : AbstractControl(title, desc, "", parent) {
label.setText(text); label.setText(text);
label.setAlignment(Qt::AlignRight | Qt::AlignVCenter); label.setAlignment(Qt::AlignRight | Qt::AlignVCenter);
hlayout->addWidget(&label); hlayout->addWidget(&label);

Loading…
Cancel
Save