From 4643d88af6721417428fff92c92ad7dbc499bd8a Mon Sep 17 00:00:00 2001 From: DevTekVE Date: Sun, 14 Jul 2024 20:15:12 +0200 Subject: [PATCH] Refactor settings to improve layout and functionality This commit introduces several layout and functionality changes in the settings. It extracts various functionalities into separate methods, including those related to on-road settings, and removes redundant panel definitions. The changes aim to make the settings more modular, manageable, and user-friendly. Furthermore, a new file for device panel settings is added to provide a more structured way of managing device-related settings. Please note that some functionalities, such as the fleet manager pin, file system watcher, and off-road button, have been moved and included in the device panel settings. These changes do not affect the functioning of the application but provide a more maintainable code structure. --- selfdrive/ui/qt/offroad/settings.cc | 154 +---------------- selfdrive/ui/qt/offroad/settings.h | 17 +- selfdrive/ui/sunnypilot/SConscript | 1 + .../sunnypilot/qt/offroad/sp_priv_settings.cc | 7 +- .../offroad/sp_priv_settings_device_panel.cc | 160 ++++++++++++++++++ .../offroad/sp_priv_settings_device_panel.h | 25 +++ 6 files changed, 202 insertions(+), 162 deletions(-) create mode 100644 selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.cc create mode 100644 selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.h diff --git a/selfdrive/ui/qt/offroad/settings.cc b/selfdrive/ui/qt/offroad/settings.cc index 4a293c32bb..3e28106bbc 100644 --- a/selfdrive/ui/qt/offroad/settings.cc +++ b/selfdrive/ui/qt/offroad/settings.cc @@ -199,45 +199,6 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) { setSpacing(50); addItem(new LabelControl(tr("Dongle ID"), getDongleId().value_or(tr("N/A")))); addItem(new LabelControl(tr("Serial"), params.get("HardwareSerial").c_str())); - - fleetManagerPin = new ButtonControl( - pin_title + pin, tr("TOGGLE"), - tr("Enable or disable PIN requirement for Fleet Manager access.")); - connect(fleetManagerPin, &ButtonControl::clicked, [=]() { - if (params.getBool("FleetManagerPin")) { - if (ConfirmationDialog::confirm(tr("Are you sure you want to turn off PIN requirement?"), tr("Turn Off"), this)) { - params.remove("FleetManagerPin"); - refreshPin(); - } - } else { - params.putBool("FleetManagerPin", true); - refreshPin(); - } - }); - addItem(fleetManagerPin); - - fs_watch = new QFileSystemWatcher(this); - connect(fs_watch, &QFileSystemWatcher::fileChanged, this, &DevicePanel::onPinFileChanged); - - QString pin_path = "/data/otp/otp.conf"; - QString pin_require = "/data/params/d/FleetManagerPin"; - fs_watch->addPath(pin_path); - fs_watch->addPath(pin_require); - refreshPin(); - - // Error Troubleshoot - auto errorBtn = new ButtonControl( - tr("Error Troubleshoot"), tr("VIEW"), - tr("Display error from the tmux session when an error has occurred from a system process.")); - QFileInfo file("/data/community/crashes/error.txt"); - QDateTime modifiedTime = file.lastModified(); - QString modified_time = modifiedTime.toString("yyyy-MM-dd hh:mm:ss "); - connect(errorBtn, &ButtonControl::clicked, [=]() { - const std::string txt = util::read_file("/data/community/crashes/error.txt"); - ConfirmationDialog::rich(modified_time + QString::fromStdString(txt), this); - }); - addItem(errorBtn); - pair_device = new ButtonControl(tr("Pair Device"), tr("PAIR"), tr("Pair your device with comma connect (connect.comma.ai) and claim your comma prime offer.")); connect(pair_device, &ButtonControl::clicked, [=]() { @@ -263,33 +224,7 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) { }); addItem(resetCalibBtn); - auto resetMapboxTokenBtn = new ButtonControl(tr("Reset Access Tokens for Map Services"), tr("RESET"), tr("Reset self-service access tokens for Mapbox, Amap, and Google Maps.")); - connect(resetMapboxTokenBtn, &ButtonControl::clicked, [=]() { - if (ConfirmationDialog::confirm(tr("Are you sure you want to reset access tokens for all map services?"), tr("Reset"), this)) { - std::vector tokens = { - "CustomMapboxTokenPk", - "CustomMapboxTokenSk", - "AmapKey1", - "AmapKey2", - "GmapKey" - }; - for (const auto& token : tokens) { - params.remove(token); - } - } - }); - addItem(resetMapboxTokenBtn); - - auto resetParamsBtn = new ButtonControl(tr("Reset sunnypilot Settings"), tr("RESET"), ""); - connect(resetParamsBtn, &ButtonControl::clicked, [=]() { - if (ConfirmationDialog::confirm(tr("Are you sure you want to reset all sunnypilot settings?"), tr("Reset"), this)) { - std::system("sudo rm -rf /data/params/d/*"); - Hardware::reboot(); - } - }); - addItem(resetParamsBtn); - - auto retrainingBtn = new ButtonControl(tr("Review Training Guide"), tr("REVIEW"), tr("Review the rules, features, and limitations of sunnypilot")); + auto retrainingBtn = new ButtonControl(tr("Review Training Guide"), tr("REVIEW"), tr("Review the rules, features, and limitations of openpilot")); connect(retrainingBtn, &ButtonControl::clicked, [=]() { if (ConfirmationDialog::confirm(tr("Are you sure you want to review the training guide?"), tr("Review"), this)) { emit reviewTrainingGuide(); @@ -322,16 +257,19 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) { QObject::connect(uiState(), &UIState::primeTypeChanged, [this] (PrimeType type) { pair_device->setVisible(type == PrimeType::UNPAIRED); }); + +#ifndef SUNNYPILOT QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) { for (auto btn : findChildren()) { - if ((btn != pair_device) && (btn != errorBtn)) { + if (btn != pair_device) { btn->setEnabled(offroad); } } }); +#endif // power buttons - QHBoxLayout *power_layout = new QHBoxLayout(); + power_layout = new QHBoxLayout(); power_layout->setSpacing(30); QPushButton *reboot_btn = new QPushButton(tr("Reboot")); @@ -348,52 +286,20 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) { connect(uiState(), &UIState::offroadTransition, poweroff_btn, &QPushButton::setVisible); } - offroad_btn = new QPushButton(tr("Toggle Onroad/Offroad")); - offroad_btn->setObjectName("offroad_btn"); - QObject::connect(offroad_btn, &QPushButton::clicked, this, &DevicePanel::forceoffroad); - - QVBoxLayout *buttons_layout = new QVBoxLayout(); - buttons_layout->setSpacing(24); - buttons_layout->addLayout(power_layout); - buttons_layout->addWidget(offroad_btn); - setStyleSheet(R"( #reboot_btn { height: 120px; border-radius: 15px; background-color: #393939; } #reboot_btn:pressed { background-color: #4a4a4a; } #poweroff_btn { height: 120px; border-radius: 15px; background-color: #E22C2C; } #poweroff_btn:pressed { background-color: #FF2424; } )"); - addItem(buttons_layout); - - updateLabels(); -} - -void DevicePanel::onPinFileChanged(const QString &file_path) { - if (file_path == "/data/params/d/FleetManagerPin") { - refreshPin(); - } else if (file_path == "/data/otp/otp.conf") { - refreshPin(); - } -} - -void DevicePanel::refreshPin() { - QFile f("/data/otp/otp.conf"); - QFile require("/data/params/d/FleetManagerPin"); - if (!require.exists()) { - setSpacing(50); - fleetManagerPin->setTitle(pin_title + tr("OFF")); - } else if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { - pin = f.readAll(); - f.close(); - setSpacing(50); - fleetManagerPin->setTitle(pin_title + pin); - } + RETURN_IF_SUNNYPILOT + addItem(power_layout); } void DevicePanel::updateCalibDescription() { QString desc = tr("sunnypilot requires the device to be mounted within 4° left or right and " - "within 5° up or 9° down. sunnypilot is continuously calibrating, resetting is rarely required."); + "within 5° up or 9° down. openpilot is continuously calibrating, resetting is rarely required."); std::string calib_bytes = params.get("CalibrationParams"); if (!calib_bytes.empty()) { try { @@ -440,51 +346,9 @@ void DevicePanel::poweroff() { } } -void DevicePanel::forceoffroad() { - if (!uiState()->engaged()) { - if (params.getBool("ForceOffroad")) { - if (ConfirmationDialog::confirm(tr("Are you sure you want to unforce offroad?"), tr("Unforce"), this)) { - // Check engaged again in case it changed while the dialog was open - if (!uiState()->engaged()) { - params.remove("ForceOffroad"); - } - } - } else { - if (ConfirmationDialog::confirm(tr("Are you sure you want to force offroad?"), tr("Force"), this)) { - // Check engaged again in case it changed while the dialog was open - if (!uiState()->engaged()) { - params.putBool("ForceOffroad", true); - } - } - } - } else { - ConfirmationDialog::alert(tr("Disengage to Force Offroad"), this); - } - - updateLabels(); -} - void DevicePanel::showEvent(QShowEvent *event) { pair_device->setVisible(uiState()->primeType() == PrimeType::UNPAIRED); ListWidget::showEvent(event); - updateLabels(); -} - -void DevicePanel::updateLabels() { - if (!isVisible()) { - return; - } - - bool force_offroad_param = params.getBool("ForceOffroad"); - QString offroad_btn_style = force_offroad_param ? "#393939" : "#E22C2C"; - QString offroad_btn_pressed_style = force_offroad_param ? "#4a4a4a" : "#FF2424"; - QString btn_common_style = QString("QPushButton { height: 120px; border-radius: 15px; background-color: %1; }" - "QPushButton:pressed { background-color: %2; }") - .arg(offroad_btn_style, - offroad_btn_pressed_style); - - offroad_btn->setText(force_offroad_param ? tr("Unforce Offroad") : tr("Force Offroad")); - offroad_btn->setStyleSheet(btn_common_style + offroad_btn_style + offroad_btn_pressed_style); } void SettingsWindow::showEvent(QShowEvent *event) { diff --git a/selfdrive/ui/qt/offroad/settings.h b/selfdrive/ui/qt/offroad/settings.h index 6adba6834d..734f47e01e 100644 --- a/selfdrive/ui/qt/offroad/settings.h +++ b/selfdrive/ui/qt/offroad/settings.h @@ -57,26 +57,15 @@ signals: void reviewTrainingGuide(); void showDriverView(); -private slots: +protected slots: void poweroff(); void reboot(); void updateCalibDescription(); - void onPinFileChanged(const QString &file_path); - void refreshPin(); - void forceoffroad(); - void updateLabels(); - -private: +protected: Params params; ButtonControl *pair_device; - - ButtonControl *fleetManagerPin; - QString pin_title = tr("Fleet Manager PIN:") + " "; - QString pin = "OFF"; - QFileSystemWatcher *fs_watch; - - QPushButton *offroad_btn; + QHBoxLayout *power_layout; }; class TogglesPanel : public ListWidget { diff --git a/selfdrive/ui/sunnypilot/SConscript b/selfdrive/ui/sunnypilot/SConscript index 69bee7c1d1..426fc0df21 100644 --- a/selfdrive/ui/sunnypilot/SConscript +++ b/selfdrive/ui/sunnypilot/SConscript @@ -30,6 +30,7 @@ network_src = [ qt_src = [ "sunnypilot/qt/sp_priv_home.cc", "sunnypilot/qt/sp_priv_offroad_home.cc", + "sunnypilot/qt/offroad/sp_priv_settings_device_panel.cc", "sunnypilot/qt/offroad/sp_priv_settings.cc", "sunnypilot/qt/onroad/onroad_settings.cc", "sunnypilot/qt/onroad/onroad_settings_panel.cc", diff --git a/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings.cc b/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings.cc index a73d360028..c517521082 100644 --- a/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings.cc +++ b/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings.cc @@ -1,5 +1,6 @@ #include "selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings.h" +#include "selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.h" #include "selfdrive/ui/qt/network/networking.h" #include "selfdrive/ui/sunnypilot/sunnypilot_main.h" @@ -318,9 +319,9 @@ SettingsWindowSP::SettingsWindowSP(QWidget *parent) : SettingsWindow(parent) { buttons_layout->addSpacing(10); // setup panels - DevicePanel *device = new DevicePanel(this); - QObject::connect(device, &DevicePanel::reviewTrainingGuide, this, &SettingsWindow::reviewTrainingGuide); - QObject::connect(device, &DevicePanel::showDriverView, this, &SettingsWindow::showDriverView); + DevicePanelSP *device = new DevicePanelSP(this); + QObject::connect(device, &DevicePanelSP::reviewTrainingGuide, this, &SettingsWindow::reviewTrainingGuide); + QObject::connect(device, &DevicePanelSP::showDriverView, this, &SettingsWindow::showDriverView); TogglesPanelSP *toggles = new TogglesPanelSP(this); QObject::connect(this, &SettingsWindow::expandToggleDescription, toggles, &TogglesPanel::expandToggleDescription); diff --git a/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.cc b/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.cc new file mode 100644 index 0000000000..06b1d96589 --- /dev/null +++ b/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.cc @@ -0,0 +1,160 @@ +#include "selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.h" +#include +#include "common/watchdog.h" +#include "selfdrive/ui/qt/qt_window.h" +#include "selfdrive/ui/qt/widgets/prime.h" + +DevicePanelSP::DevicePanelSP(SettingsWindow *parent) : DevicePanel(parent) { + fleetManagerPin = new ButtonControl( + pin_title + pin, tr("TOGGLE"), + tr("Enable or disable PIN requirement for Fleet Manager access.")); + connect(fleetManagerPin, &ButtonControl::clicked, [=]() { + if (params.getBool("FleetManagerPin")) { + if (ConfirmationDialog::confirm(tr("Are you sure you want to turn off PIN requirement?"), tr("Turn Off"), this)) { + params.remove("FleetManagerPin"); + refreshPin(); + } + } else { + params.putBool("FleetManagerPin", true); + refreshPin(); + } + }); + AddWidgetAt(2, fleetManagerPin); + + fs_watch = new QFileSystemWatcher(this); + connect(fs_watch, &QFileSystemWatcher::fileChanged, this, &DevicePanelSP::onPinFileChanged); + + QString pin_path = "/data/otp/otp.conf"; + QString pin_require = "/data/params/d/FleetManagerPin"; + fs_watch->addPath(pin_path); + fs_watch->addPath(pin_require); + refreshPin(); + + // Error Troubleshoot + auto errorBtn = new ButtonControl( + tr("Error Troubleshoot"), tr("VIEW"), + tr("Display error from the tmux session when an error has occurred from a system process.")); + QFileInfo file("/data/community/crashes/error.txt"); + QDateTime modifiedTime = file.lastModified(); + QString modified_time = modifiedTime.toString("yyyy-MM-dd hh:mm:ss "); + connect(errorBtn, &ButtonControl::clicked, [=]() { + const std::string txt = util::read_file("/data/community/crashes/error.txt"); + ConfirmationDialog::rich(modified_time + QString::fromStdString(txt), this); + }); + AddWidgetAt(3, errorBtn); + + + auto resetMapboxTokenBtn = new ButtonControl(tr("Reset Access Tokens for Map Services"), tr("RESET"), tr("Reset self-service access tokens for Mapbox, Amap, and Google Maps.")); + connect(resetMapboxTokenBtn, &ButtonControl::clicked, [=]() { + if (ConfirmationDialog::confirm(tr("Are you sure you want to reset access tokens for all map services?"), tr("Reset"), this)) { + std::vector tokens = { + "CustomMapboxTokenPk", + "CustomMapboxTokenSk", + "AmapKey1", + "AmapKey2", + "GmapKey" + }; + for (const auto& token : tokens) { + params.remove(token); + } + } + }); + AddWidgetAt(6, resetMapboxTokenBtn); + + auto resetParamsBtn = new ButtonControl(tr("Reset sunnypilot Settings"), tr("RESET"), ""); + connect(resetParamsBtn, &ButtonControl::clicked, [=]() { + if (ConfirmationDialog::confirm(tr("Are you sure you want to reset all sunnypilot settings?"), tr("Reset"), this)) { + std::system("sudo rm -rf /data/params/d/*"); + Hardware::reboot(); + } + }); + AddWidgetAt(6, resetParamsBtn); + + QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) { + for (auto btn : findChildren()) { + if (btn != pair_device && btn != errorBtn) { + btn->setEnabled(offroad); + } + } + }); + + offroad_btn = new QPushButton(tr("Toggle Onroad/Offroad")); + offroad_btn->setObjectName("offroad_btn"); + QObject::connect(offroad_btn, &QPushButton::clicked, this, &DevicePanelSP::forceoffroad); + + QVBoxLayout *buttons_layout = new QVBoxLayout(); + buttons_layout->setSpacing(24); + buttons_layout->addLayout(power_layout); + buttons_layout->addWidget(offroad_btn); + addItem(buttons_layout); + + updateLabels(); +} + +void DevicePanelSP::onPinFileChanged(const QString &file_path) { + if (file_path == "/data/params/d/FleetManagerPin") { + refreshPin(); + } else if (file_path == "/data/otp/otp.conf") { + refreshPin(); + } +} + +void DevicePanelSP::refreshPin() { + QFile f("/data/otp/otp.conf"); + QFile require("/data/params/d/FleetManagerPin"); + if (!require.exists()) { + setSpacing(50); + fleetManagerPin->setTitle(pin_title + tr("OFF")); + } else if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { + pin = f.readAll(); + f.close(); + setSpacing(50); + fleetManagerPin->setTitle(pin_title + pin); + } +} + +void DevicePanelSP::forceoffroad() { + if (!uiState()->engaged()) { + if (params.getBool("ForceOffroad")) { + if (ConfirmationDialog::confirm(tr("Are you sure you want to unforce offroad?"), tr("Unforce"), this)) { + // Check engaged again in case it changed while the dialog was open + if (!uiState()->engaged()) { + params.remove("ForceOffroad"); + } + } + } else { + if (ConfirmationDialog::confirm(tr("Are you sure you want to force offroad?"), tr("Force"), this)) { + // Check engaged again in case it changed while the dialog was open + if (!uiState()->engaged()) { + params.putBool("ForceOffroad", true); + } + } + } + } else { + ConfirmationDialog::alert(tr("Disengage to Force Offroad"), this); + } + + updateLabels(); +} + +void DevicePanelSP::showEvent(QShowEvent *event) { + DevicePanel::showEvent(event); + updateLabels(); +} + +void DevicePanelSP::updateLabels() { + if (!isVisible()) { + return; + } + + bool force_offroad_param = params.getBool("ForceOffroad"); + QString offroad_btn_style = force_offroad_param ? "#393939" : "#E22C2C"; + QString offroad_btn_pressed_style = force_offroad_param ? "#4a4a4a" : "#FF2424"; + QString btn_common_style = QString("QPushButton { height: 120px; border-radius: 15px; background-color: %1; }" + "QPushButton:pressed { background-color: %2; }") + .arg(offroad_btn_style, + offroad_btn_pressed_style); + + offroad_btn->setText(force_offroad_param ? tr("Unforce Offroad") : tr("Force Offroad")); + offroad_btn->setStyleSheet(btn_common_style + offroad_btn_style + offroad_btn_pressed_style); +} diff --git a/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.h b/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.h new file mode 100644 index 0000000000..e78b319ba4 --- /dev/null +++ b/selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings_device_panel.h @@ -0,0 +1,25 @@ +#pragma once + +#include "selfdrive/ui/sunnypilot/qt/offroad/sp_priv_settings.h" + +class DevicePanelSP : public DevicePanel { + Q_OBJECT +public: + explicit DevicePanelSP(SettingsWindow *parent); + void showEvent(QShowEvent *event) override; + +private slots: + void onPinFileChanged(const QString &file_path); + void refreshPin(); + void forceoffroad(); + + void updateLabels(); + +private: + ButtonControl *fleetManagerPin; + QString pin_title = tr("Fleet Manager PIN:") + " "; + QString pin = "OFF"; + QFileSystemWatcher *fs_watch; + + QPushButton *offroad_btn; +}; \ No newline at end of file