From 8675c970d49499d541c38172b1c5516dec7df480 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Fri, 18 Jun 2021 14:17:52 +0800 Subject: [PATCH] UI: refactor ButtonControl (#21315) --- selfdrive/ui/qt/offroad/networking.cc | 3 ++- selfdrive/ui/qt/offroad/settings.cc | 33 +++++++++++++------------ selfdrive/ui/qt/widgets/controls.cc | 22 +++++++++++++++++ selfdrive/ui/qt/widgets/controls.h | 31 ++++++------------------ selfdrive/ui/qt/widgets/ssh_keys.cc | 35 ++++++++------------------- selfdrive/ui/qt/widgets/ssh_keys.h | 2 +- 6 files changed, 59 insertions(+), 67 deletions(-) diff --git a/selfdrive/ui/qt/offroad/networking.cc b/selfdrive/ui/qt/offroad/networking.cc index 6dec8ddc45..e5ea703b42 100644 --- a/selfdrive/ui/qt/offroad/networking.cc +++ b/selfdrive/ui/qt/offroad/networking.cc @@ -145,7 +145,8 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid main_layout->addWidget(horizontal_line(), 0); // Change tethering password - editPasswordButton = new ButtonControl("Tethering Password", "EDIT", "", [=]() { + editPasswordButton = new ButtonControl("Tethering Password", "EDIT"); + connect(editPasswordButton, &ButtonControl::released, [=]() { QString pass = InputDialog::getText("Enter new tethering password", 8); if (pass.size()) { wifi->changeTetheringPassword(pass); diff --git a/selfdrive/ui/qt/offroad/settings.cc b/selfdrive/ui/qt/offroad/settings.cc index 3191b76689..48e67c4c52 100644 --- a/selfdrive/ui/qt/offroad/settings.cc +++ b/selfdrive/ui/qt/offroad/settings.cc @@ -113,18 +113,18 @@ DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) { main_layout->addWidget(new LabelControl("Serial", serial)); // offroad-only buttons - QList offroad_btns; - offroad_btns.append(new ButtonControl("Driver Camera", "PREVIEW", - "Preview the driver facing camera to help optimize device mounting position for best driver monitoring experience. (vehicle must be off)", - [=]() { emit showDriverView(); }, "", this)); + auto dcamBtn = new ButtonControl("Driver Camera", "PREVIEW", + "Preview the driver facing camera to help optimize device mounting position for best driver monitoring experience. (vehicle must be off)"); + connect(dcamBtn, &ButtonControl::released, [=]() { emit showDriverView(); }); QString resetCalibDesc = "openpilot requires the device to be mounted within 4° left or right and within 5° up or down. openpilot is continuously calibrating, resetting is rarely required."; - ButtonControl *resetCalibBtn = new ButtonControl("Reset Calibration", "RESET", resetCalibDesc, [=]() { + auto resetCalibBtn = new ButtonControl("Reset Calibration", "RESET", resetCalibDesc); + connect(resetCalibBtn, &ButtonControl::released, [=]() { if (ConfirmationDialog::confirm("Are you sure you want to reset calibration?", this)) { Params().remove("CalibrationParams"); } - }, "", this); + }); connect(resetCalibBtn, &ButtonControl::showDescription, [=]() { QString desc = resetCalibDesc; std::string calib_bytes = Params().get("CalibrationParams"); @@ -146,25 +146,25 @@ DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) { } resetCalibBtn->setDescription(desc); }); - offroad_btns.append(resetCalibBtn); - offroad_btns.append(new ButtonControl("Review Training Guide", "REVIEW", - "Review the rules, features, and limitations of openpilot", [=]() { + auto retrainingBtn = new ButtonControl("Review Training Guide", "REVIEW", "Review the rules, features, and limitations of openpilot"); + connect(retrainingBtn, &ButtonControl::released, [=]() { if (ConfirmationDialog::confirm("Are you sure you want to review the training guide?", this)) { Params().remove("CompletedTrainingVersion"); emit reviewTrainingGuide(); } - }, "", this)); + }); - offroad_btns.append(new ButtonControl("Uninstall " + getBrand(), "UNINSTALL", "", [=]() { + auto uninstallBtn = new ButtonControl("Uninstall " + getBrand(), "UNINSTALL"); + connect(uninstallBtn, &ButtonControl::released, [=]() { if (ConfirmationDialog::confirm("Are you sure you want to uninstall?", this)) { Params().putBool("DoUninstall", true); } - }, "", this)); + }); - for(auto &btn : offroad_btns) { + for (auto btn : {dcamBtn, resetCalibBtn, retrainingBtn, uninstallBtn}) { main_layout->addWidget(horizontal_line()); - QObject::connect(parent, SIGNAL(offroadTransition(bool)), btn, SLOT(setEnabled(bool))); + connect(parent, SIGNAL(offroadTransition(bool)), btn, SLOT(setEnabled(bool))); main_layout->addWidget(btn); } @@ -207,7 +207,8 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : QWidget(parent) { 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", "", "", [=]() { + updateBtn = new ButtonControl("Check for Update", ""); + connect(updateBtn, &ButtonControl::released, [=]() { if (params.getBool("IsOffroad")) { const QString paramsPath = QString::fromStdString(params.getParamsPath()); fs_watch->addPath(paramsPath + "/d/LastUpdateTime"); @@ -216,7 +217,7 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : QWidget(parent) { 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}; diff --git a/selfdrive/ui/qt/widgets/controls.cc b/selfdrive/ui/qt/widgets/controls.cc index 1ba511ef5c..b20fefe9e1 100644 --- a/selfdrive/ui/qt/widgets/controls.cc +++ b/selfdrive/ui/qt/widgets/controls.cc @@ -63,3 +63,25 @@ void AbstractControl::hideEvent(QHideEvent *e) { description->hide(); } } + +// controls + +ButtonControl::ButtonControl(const QString &title, const QString &text, const QString &desc, QWidget *parent) : AbstractControl(title, desc, "", parent) { + btn.setText(text); + btn.setStyleSheet(R"( + QPushButton { + padding: 0; + border-radius: 50px; + font-size: 35px; + font-weight: 500; + color: #E4E4E4; + background-color: #393939; + } + QPushButton:disabled { + color: #33E4E4E4; + } + )"); + btn.setFixedSize(250, 100); + QObject::connect(&btn, &QPushButton::released, this, &ButtonControl::released); + hlayout->addWidget(&btn); +} diff --git a/selfdrive/ui/qt/widgets/controls.h b/selfdrive/ui/qt/widgets/controls.h index 4ffd04bc10..1b6dc1666b 100644 --- a/selfdrive/ui/qt/widgets/controls.h +++ b/selfdrive/ui/qt/widgets/controls.h @@ -56,32 +56,15 @@ class ButtonControl : public AbstractControl { Q_OBJECT public: - template - ButtonControl(const QString &title, const QString &text, const QString &desc, Functor functor, const QString &icon = "", QWidget *parent = nullptr) : AbstractControl(title, desc, icon, parent) { - btn.setText(text); - btn.setStyleSheet(R"( - QPushButton { - padding: 0; - border-radius: 50px; - font-size: 35px; - font-weight: 500; - color: #E4E4E4; - background-color: #393939; - } - QPushButton:disabled { - color: #33E4E4E4; - } - )"); - btn.setFixedSize(250, 100); - QObject::connect(&btn, &QPushButton::released, functor); - hlayout->addWidget(&btn); - } - void setText(const QString &text) { btn.setText(text); } + ButtonControl(const QString &title, const QString &text, const QString &desc = "", QWidget *parent = nullptr); + inline void setText(const QString &text) { btn.setText(text); } + inline QString text() const { return btn.text(); } + +signals: + void released(); public slots: - void setEnabled(bool enabled) { - btn.setEnabled(enabled); - }; + void setEnabled(bool enabled) { btn.setEnabled(enabled); }; private: QPushButton btn; diff --git a/selfdrive/ui/qt/widgets/ssh_keys.cc b/selfdrive/ui/qt/widgets/ssh_keys.cc index c8403797c4..2dfcec0df8 100644 --- a/selfdrive/ui/qt/widgets/ssh_keys.cc +++ b/selfdrive/ui/qt/widgets/ssh_keys.cc @@ -4,32 +4,17 @@ #include "selfdrive/ui/qt/api.h" #include "selfdrive/ui/qt/widgets/input.h" -SshControl::SshControl() : AbstractControl("SSH Keys", "Warning: This grants SSH access to all public keys in your GitHub settings. Never enter a GitHub username other than your own. A comma employee will NEVER ask you to add their GitHub username.", "") { - - // setup widget - hlayout->addStretch(1); - - username_label.setAlignment(Qt::AlignVCenter); +SshControl::SshControl() : ButtonControl("SSH Keys", "", "Warning: This grants SSH access to all public keys in your GitHub settings. Never enter a GitHub username other than your own. A comma employee will NEVER ask you to add their GitHub username.") { + username_label.setAlignment(Qt::AlignRight | Qt::AlignVCenter); username_label.setStyleSheet("color: #aaaaaa"); - hlayout->addWidget(&username_label); - - btn.setStyleSheet(R"( - padding: 0; - border-radius: 50px; - font-size: 35px; - font-weight: 500; - color: #E4E4E4; - background-color: #393939; - )"); - btn.setFixedSize(250, 100); - hlayout->addWidget(&btn); + hlayout->insertWidget(1, &username_label); - QObject::connect(&btn, &QPushButton::released, [=]() { - if (btn.text() == "ADD") { + QObject::connect(this, &ButtonControl::released, [=]() { + if (text() == "ADD") { QString username = InputDialog::getText("Enter your GitHub username"); if (username.length() > 0) { - btn.setText("LOADING"); - btn.setEnabled(false); + setText("LOADING"); + setEnabled(false); getUserKeys(username); } } else { @@ -46,12 +31,12 @@ void SshControl::refresh() { QString param = QString::fromStdString(params.get("GithubSshKeys")); if (param.length()) { username_label.setText(QString::fromStdString(params.get("GithubUsername"))); - btn.setText("REMOVE"); + setText("REMOVE"); } else { username_label.setText(""); - btn.setText("ADD"); + setText("ADD"); } - btn.setEnabled(true); + setEnabled(true); } void SshControl::getUserKeys(const QString &username) { diff --git a/selfdrive/ui/qt/widgets/ssh_keys.h b/selfdrive/ui/qt/widgets/ssh_keys.h index aaa7f80dcd..f670e3ae6a 100644 --- a/selfdrive/ui/qt/widgets/ssh_keys.h +++ b/selfdrive/ui/qt/widgets/ssh_keys.h @@ -18,7 +18,7 @@ public: }; // SSH key management widget -class SshControl : public AbstractControl { +class SshControl : public ButtonControl { Q_OBJECT public: