diff --git a/selfdrive/ui/sunnypilot/qt/offroad/settings/device_panel.cc b/selfdrive/ui/sunnypilot/qt/offroad/settings/device_panel.cc index ca6d6c62f..eef025b0a 100644 --- a/selfdrive/ui/sunnypilot/qt/offroad/settings/device_panel.cc +++ b/selfdrive/ui/sunnypilot/qt/offroad/settings/device_panel.cc @@ -16,23 +16,25 @@ DevicePanelSP::DevicePanelSP(SettingsWindowSP *parent) : DevicePanel(parent) { device_grid_layout->setHorizontalSpacing(5); device_grid_layout->setVerticalSpacing(25); - std::vector> device_btns = { - {"dcamBtn", tr("Driver Camera Preview")}, - {"retrainingBtn", tr("Training Guide")}, - {"regulatoryBtn", tr("Regulatory")}, - {"translateBtn", tr("Language")}, - {"resetParams", tr("Reset Settings")}, + std::vector> device_btns = { + {"dcamBtn", tr("Driver Camera Preview"), ""}, + {"retrainingBtn", tr("Training Guide"), ""}, + {"regulatoryBtn", tr("Regulatory"), ""}, + {"translateBtn", tr("Language"), ""}, + {"resetParams", tr("Reset Settings"), ""}, }; int row = 0, col = 0; - for (int i = 0; i < device_btns.size(); i++) { - if (device_btns[i].first == "regulatoryBtn" && !Hardware::TICI()) { + for (const auto &[id, text, param] : device_btns) { + if (id == "regulatoryBtn" && !Hardware::TICI()) { continue; } - auto *btn = new PushButtonSP(device_btns[i].second, 720, this); + auto *btn = new PushButtonSP(text, 720, this, param); + btn->setObjectName(id); + device_grid_layout->addWidget(btn, row, col); - buttons[device_btns[i].first] = btn; + buttons[id] = btn; col++; if (col > 1) { diff --git a/selfdrive/ui/sunnypilot/qt/widgets/controls.h b/selfdrive/ui/sunnypilot/qt/widgets/controls.h index 1578130bb..acbc3ea3c 100644 --- a/selfdrive/ui/sunnypilot/qt/widgets/controls.h +++ b/selfdrive/ui/sunnypilot/qt/widgets/controls.h @@ -554,8 +554,8 @@ class PushButtonSP : public QPushButton { Q_OBJECT public: - PushButtonSP(const QString &text, const int minimum_button_width = 800, QWidget *parent = nullptr) : QPushButton(text, parent) { - const QString buttonStyle = R"( + PushButtonSP(const QString &text, const int minimum_button_width = 800, QWidget *parent = nullptr, const QString ¶m = "") : QPushButton(text, parent) { + buttonStyle = R"( QPushButton { border-radius: 20px; font-size: 50px; @@ -564,21 +564,61 @@ public: padding: 0 25px 0 25px; color: #FFFFFF; } - QPushButton:enabled { - background-color: #393939; - } - QPushButton:pressed { - background-color: #4A4A4A; - } - QPushButton:disabled { - background-color: #121212; - color: #5C5C5C; - } )"; - setStyleSheet(buttonStyle); + if (!param.isEmpty()) { + key = param.toStdString(); + refresh(); + } else { + updateStyle(false); + } + setFixedWidth(minimum_button_width); } + + void refresh() { + if (!key.empty()) { + bool state = params.getBool(key); + if (state != is_enabled) { + is_enabled = state; + } + updateStyle(is_enabled); + } + } + + void updateButton() { + if (!key.empty()) { + params.putBool(key, !is_enabled); + refresh(); + } + } + +protected: + // Override mouse release event to handle style updates smoothly + void mouseReleaseEvent(QMouseEvent *event) override { + if (!key.empty()) { + bool next_state = !params.getBool(key); + updateStyle(next_state); + } + QPushButton::mouseReleaseEvent(event); + } + +private: + std::string key = ""; + Params params; + bool is_enabled; + QString buttonStyle; + + QString btn_enabled_off_style = "QPushButton:enabled { background-color: #393939; }"; + QString btn_enabled_on_style = "QPushButton:enabled { background-color: #1e79e8; }"; + QString btn_pressed_style = "QPushButton:pressed { background-color: #4A4A4A; }"; + QString btn_disabled_stype = "QPushButton:disabled { background-color: #121212; color: #5C5C5C; }"; + + void updateStyle(bool enabled) { + QString enabled_style = enabled ? btn_enabled_on_style : btn_enabled_off_style; + + setStyleSheet(buttonStyle + enabled_style + btn_pressed_style + btn_disabled_stype); + } }; class PanelBackButton : public QPushButton {