From f673a444382b72c644aacaa5c55014edba799d5d Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Mon, 21 Aug 2023 20:29:59 -0400 Subject: [PATCH] ui: add `SPOptionControl` to sunnypilot settings (#241) * ui: `SPOptionControl` * This works * only need to initialize * Use QButtonGroup * Add method to update specific labels * Always init label with value * add method to update external methods * Update in another PR * Unnecessary * construct std map with defined range * Test that it works * In separate PR * change var * refreshLabels() instead * Revert "refreshLabels() instead" This reverts commit ceed9935cf48c7db974e45ab5ab496a7bc3f9f26. * allow individual `refresh` classes * now we can do it in another PR! --- selfdrive/ui/qt/offroad/sunnypilot_settings.h | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/selfdrive/ui/qt/offroad/sunnypilot_settings.h b/selfdrive/ui/qt/offroad/sunnypilot_settings.h index 0623e596aec1d7..d0da59c75764ab 100644 --- a/selfdrive/ui/qt/offroad/sunnypilot_settings.h +++ b/selfdrive/ui/qt/offroad/sunnypilot_settings.h @@ -8,6 +8,90 @@ #include "selfdrive/ui/qt/widgets/controls.h" +class SPOptionControl : public AbstractControl { + Q_OBJECT + +private: + struct MinMaxValue { + int min_value; + int max_value; + }; + +public: + SPOptionControl(const QString ¶m, const QString &title, const QString &desc, const QString &icon, + const MinMaxValue &range, const int per_value_change = 1) : AbstractControl(title, desc, icon) { + const QString style = R"( + QPushButton { + padding: 0; + border-radius: 50px; + font-size: 35px; + font-weight: 500; + color: #E4E4E4; + background-color: #393939; + } + QPushButton:pressed { + background-color: #4a4a4a; + } + QPushButton:disabled { + color: #33E4E4E4; + } + )"; + + label.setAlignment(Qt::AlignVCenter|Qt::AlignRight); + label.setStyleSheet("color: #e0e879"); + hlayout->addWidget(&label); + + const std::vector button_texts{"-", "+"}; + + key = param.toStdString(); + value = atoi(params.get(key).c_str()); + + button_group = new QButtonGroup(this); + button_group->setExclusive(true); + for (int i = 0; i < button_texts.size(); i++) { + QPushButton *button = new QPushButton(button_texts[i], this); + button->setStyleSheet(style); + button->setFixedSize(150, 100); + hlayout->addWidget(button); + button_group->addButton(button, i); + + int change_value = (i == 0) ? -per_value_change : per_value_change; + + QObject::connect(button, &QPushButton::clicked, [=]() { + key = param.toStdString(); + value = atoi(params.get(key).c_str()); + value += change_value; + value = std::clamp(value, range.min_value, range.max_value); + params.put(key, QString::number(value).toStdString()); + updateLabels(); + + if (update) { + emit updateOtherToggles(); + } + }); + } + } + + void setUpdateOtherToggles(bool _update) { + update = _update; + } + + inline void setLabel(const QString &text) { label.setText(text); } + +signals: + void updateLabels(); + void updateOtherToggles(); + +private: + std::string key; + int value; + QButtonGroup *button_group; + QLabel label; + Params params; + std::map option_label = {}; + bool update = false; +}; + class SpeedLimitOffsetType : public AbstractControl { Q_OBJECT