ui: refactor ButtonParamControl (#28425)

* refactor ButtonParamControl

* add pressed style

* connect to buttonToggled

* typo
old-commit-hash: 7f41047178
beeps
Dean Lee 2 years ago committed by GitHub
parent cedea3efc7
commit bfecfe3f8e
  1. 3
      selfdrive/ui/qt/offroad/settings.cc
  2. 99
      selfdrive/ui/qt/widgets/controls.h

@ -91,11 +91,10 @@ TogglesPanel::TogglesPanel(SettingsWindow *parent) : ListWidget(parent) {
std::vector<QString> longi_button_texts{tr("Aggressive"), tr("Standard"), tr("Relaxed")}; std::vector<QString> longi_button_texts{tr("Aggressive"), tr("Standard"), tr("Relaxed")};
std::vector<int> longi_button_widths{300, 250, 225};
ButtonParamControl* long_personality_setting = new ButtonParamControl("LongitudinalPersonality", tr("Driving Personality"), ButtonParamControl* long_personality_setting = new ButtonParamControl("LongitudinalPersonality", tr("Driving Personality"),
tr("Standard is recommended. In aggressive mode, openpilot will follow lead cars closer and be more aggressive with the gas and brake."), tr("Standard is recommended. In aggressive mode, openpilot will follow lead cars closer and be more aggressive with the gas and brake."),
"../assets/offroad/icon_speed_limit.png", "../assets/offroad/icon_speed_limit.png",
longi_button_texts, longi_button_widths); longi_button_texts);
for (auto &[param, title, desc, icon] : toggle_defs) { for (auto &[param, title, desc, icon] : toggle_defs) {
auto toggle = new ParamControl(param, title, desc, icon, this); auto toggle = new ParamControl(param, title, desc, icon, this);

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <QButtonGroup>
#include <QFrame> #include <QFrame>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
@ -60,7 +61,7 @@ public:
public slots: public slots:
void showDescription() { void showDescription() {
description->setVisible(true); description->setVisible(true);
}; }
signals: signals:
void showDescriptionEvent(); void showDescriptionEvent();
@ -106,7 +107,7 @@ signals:
void clicked(); void clicked();
public slots: public slots:
void setEnabled(bool enabled) { btn.setEnabled(enabled); }; void setEnabled(bool enabled) { btn.setEnabled(enabled); }
private: private:
QPushButton btn; QPushButton btn;
@ -163,7 +164,7 @@ public:
void setConfirmation(bool _confirm, bool _store_confirm) { void setConfirmation(bool _confirm, bool _store_confirm) {
confirm = _confirm; confirm = _confirm;
store_confirm = _store_confirm; store_confirm = _store_confirm;
}; }
void setActiveIcon(const QString &icon) { void setActiveIcon(const QString &icon) {
active_icon_pixmap = QPixmap(icon).scaledToWidth(80, Qt::SmoothTransformation); active_icon_pixmap = QPixmap(icon).scaledToWidth(80, Qt::SmoothTransformation);
@ -175,11 +176,11 @@ public:
toggle.togglePosition(); toggle.togglePosition();
setIcon(state); setIcon(state);
} }
}; }
void showEvent(QShowEvent *event) override { void showEvent(QShowEvent *event) override {
refresh(); refresh();
}; }
private: private:
void setIcon(bool state) { void setIcon(bool state) {
@ -188,7 +189,7 @@ private:
} else if (!icon_pixmap.isNull()) { } else if (!icon_pixmap.isNull()) {
icon_label->setPixmap(icon_pixmap); icon_label->setPixmap(icon_pixmap);
} }
}; }
std::string key; std::string key;
Params params; Params params;
@ -196,81 +197,55 @@ private:
bool confirm = false; bool confirm = false;
bool store_confirm = false; bool store_confirm = false;
}; };
class ButtonParamControl : public AbstractControl { class ButtonParamControl : public AbstractControl {
Q_OBJECT Q_OBJECT
public: public:
ButtonParamControl(const QString &param, const QString &title, const QString &desc, const QString &icon, ButtonParamControl(const QString &param, const QString &title, const QString &desc, const QString &icon,
std::vector<QString> button_texts, std::vector<int> button_widths) : AbstractControl(title, desc, icon) { const std::vector<QString> &button_texts) : AbstractControl(title, desc, icon) {
select_style = (R"( const QString style = R"(
padding: 0; QPushButton {
border-radius: 50px; border-radius: 50px;
font-size: 45px; font-size: 40px;
font-weight: 500; font-weight: 500;
color: #E4E4E4; height:100px;
background-color: #33Ab4C; padding: 0 25 0 25;
)");
unselect_style = (R"(
padding: 0;
border-radius: 50px;
font-size: 45px;
font-weight: 350;
color: #E4E4E4; color: #E4E4E4;
background-color: #393939; background-color: #393939;
)"); }
QPushButton:pressed {
background-color: #4a4a4a;
}
QPushButton:checked {
background-color: #33Ab4C;
}
)";
key = param.toStdString(); key = param.toStdString();
int value = atoi(params.get(key).c_str());
QButtonGroup *button_group = new QButtonGroup(this);
button_group->setExclusive(true);
for (int i = 0; i < button_texts.size(); i++) { for (int i = 0; i < button_texts.size(); i++) {
QPushButton *button = new QPushButton(); QPushButton *button = new QPushButton(button_texts[i], this);
button->setText(button_texts[i]); button->setCheckable(true);
button->setChecked(i == value);
button->setStyleSheet(style);
hlayout->addWidget(button); hlayout->addWidget(button);
button->setFixedSize(button_widths[i], 100); button_group->addButton(button, i);
button->setStyleSheet(unselect_style);
buttons.push_back(button);
QObject::connect(button, &QPushButton::clicked, [=]() {
params.put(key, (QString::number(i)).toStdString());
refresh();
});
} }
refresh();
}
void read_param() {
auto value = params.get(key);
if (!value.empty()) {
param_value = std::stoi(value);
}
};
void set_param(int new_value) {
QString values = QString::number(new_value);
params.put(key, values.toStdString());
refresh();
};
void refresh() { QObject::connect(button_group, QOverload<int, bool>::of(&QButtonGroup::buttonToggled), [=](int id, bool checked) {
read_param(); if (checked) {
for (int i = 0; i < buttons.size(); i++) { params.put(key, std::to_string(id));
buttons[i]->setStyleSheet(unselect_style);
if (param_value == i) {
buttons[i]->setStyleSheet(select_style);
} }
} });
}; }
void showEvent(QShowEvent *event) override {
refresh();
};
private: private:
std::string key; std::string key;
std::vector<QPushButton*> buttons;
QString unselect_style;
QString select_style;
Params params; Params params;
int param_value = 0;
}; };
class ListWidget : public QWidget { class ListWidget : public QWidget {
Q_OBJECT Q_OBJECT
public: public:
@ -310,7 +285,7 @@ class LayoutWidget : public QWidget {
public: public:
LayoutWidget(QLayout *l, QWidget *parent = nullptr) : QWidget(parent) { LayoutWidget(QLayout *l, QWidget *parent = nullptr) : QWidget(parent) {
setLayout(l); setLayout(l);
}; }
}; };
class ClickableWidget : public QWidget { class ClickableWidget : public QWidget {

Loading…
Cancel
Save