From 4b2814ec511f8b6dfa425e7e7f56d82f962ebb89 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Sun, 23 Mar 2025 21:26:24 -0400 Subject: [PATCH] ui: support dynamic state updates for `PushButtonSP` --- selfdrive/ui/sunnypilot/qt/widgets/controls.h | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/selfdrive/ui/sunnypilot/qt/widgets/controls.h b/selfdrive/ui/sunnypilot/qt/widgets/controls.h index 1578130bbb..4a5c29c8bd 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,44 @@ 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); + } + } + +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 {