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!
This commit is contained in:
Jason Wen
2023-08-21 20:29:59 -04:00
committed by GitHub
parent e47b386a23
commit f673a44438
@@ -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 &param, 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<QString> 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<QString, QString> option_label = {};
bool update = false;
};
class SpeedLimitOffsetType : public AbstractControl {
Q_OBJECT