diff --git a/selfdrive/ui/qt/widgets/controls.h b/selfdrive/ui/qt/widgets/controls.h index aebf934f2a..ca93110e5b 100644 --- a/selfdrive/ui/qt/widgets/controls.h +++ b/selfdrive/ui/qt/widgets/controls.h @@ -183,10 +183,10 @@ private: bool store_confirm = false; }; -class ButtonParamControl : public AbstractControl { +class MultiButtonControl : public AbstractControl { Q_OBJECT public: - ButtonParamControl(const QString ¶m, const QString &title, const QString &desc, const QString &icon, + MultiButtonControl(const QString &title, const QString &desc, const QString &icon, const std::vector &button_texts, const int minimum_button_width = 225) : AbstractControl(title, desc, icon) { const QString style = R"( QPushButton { @@ -208,24 +208,19 @@ public: color: #33E4E4E4; } )"; - key = param.toStdString(); - int 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->setCheckable(true); - button->setChecked(i == value); button->setStyleSheet(style); button->setMinimumWidth(minimum_button_width); hlayout->addWidget(button); button_group->addButton(button, i); } - QObject::connect(button_group, QOverload::of(&QButtonGroup::buttonClicked), [=](int id) { - params.put(key, std::to_string(id)); - }); + QObject::connect(button_group, QOverload::of(&QButtonGroup::buttonClicked), this, &MultiButtonControl::buttonClicked); } void setEnabled(bool enable) { @@ -238,6 +233,31 @@ public: button_group->button(id)->setChecked(true); } +signals: + void buttonClicked(int id); + +protected: + QButtonGroup *button_group; +}; + +class ButtonParamControl : public MultiButtonControl { + Q_OBJECT +public: + ButtonParamControl(const QString ¶m, const QString &title, const QString &desc, const QString &icon, + const std::vector &button_texts, const int minimum_button_width = 225) : MultiButtonControl(title, desc, icon, + button_texts, minimum_button_width) { + key = param.toStdString(); + int value = atoi(params.get(key).c_str()); + + if (value > 0 && value < button_group->buttons().size()) { + button_group->button(value)->setChecked(true); + } + + QObject::connect(this, QOverload::of(&MultiButtonControl::buttonClicked), [=](int id) { + params.put(key, std::to_string(id)); + }); + } + void refresh() { int value = atoi(params.get(key).c_str()); button_group->button(value)->setChecked(true); @@ -250,7 +270,6 @@ public: private: std::string key; Params params; - QButtonGroup *button_group; }; class ListWidget : public QWidget {