mirror of
https://github.com/infiniteCable2/openpilot.git
synced 2026-08-05 08:16:03 +08:00
ui: support dynamic state updates for PushButtonSP (#708)
* ui: support dynamic state updates for `PushButtonSP` * test btn * override mouse release events * Revert "test btn" This reverts commit cd9c9dde9a37a8b2c7de7ad123cbb53f80e9e5e6. * Reapply "test btn" This reverts commit 9b36b2e08500d81cecb95f6c47a66a43e2dcd1b4. * abstract param flipping * Revert "Reapply "test btn"" This reverts commit 8104a262b02303b1c6fe2df00b2d65408fa010ea. * update device panel handling for the new param
This commit is contained in:
@@ -16,23 +16,25 @@ DevicePanelSP::DevicePanelSP(SettingsWindowSP *parent) : DevicePanel(parent) {
|
||||
device_grid_layout->setHorizontalSpacing(5);
|
||||
device_grid_layout->setVerticalSpacing(25);
|
||||
|
||||
std::vector<std::pair<QString, QString>> device_btns = {
|
||||
{"dcamBtn", tr("Driver Camera Preview")},
|
||||
{"retrainingBtn", tr("Training Guide")},
|
||||
{"regulatoryBtn", tr("Regulatory")},
|
||||
{"translateBtn", tr("Language")},
|
||||
{"resetParams", tr("Reset Settings")},
|
||||
std::vector<std::tuple<QString, QString, QString>> device_btns = {
|
||||
{"dcamBtn", tr("Driver Camera Preview"), ""},
|
||||
{"retrainingBtn", tr("Training Guide"), ""},
|
||||
{"regulatoryBtn", tr("Regulatory"), ""},
|
||||
{"translateBtn", tr("Language"), ""},
|
||||
{"resetParams", tr("Reset Settings"), ""},
|
||||
};
|
||||
|
||||
int row = 0, col = 0;
|
||||
for (int i = 0; i < device_btns.size(); i++) {
|
||||
if (device_btns[i].first == "regulatoryBtn" && !Hardware::TICI()) {
|
||||
for (const auto &[id, text, param] : device_btns) {
|
||||
if (id == "regulatoryBtn" && !Hardware::TICI()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto *btn = new PushButtonSP(device_btns[i].second, 720, this);
|
||||
auto *btn = new PushButtonSP(text, 720, this, param);
|
||||
btn->setObjectName(id);
|
||||
|
||||
device_grid_layout->addWidget(btn, row, col);
|
||||
buttons[device_btns[i].first] = btn;
|
||||
buttons[id] = btn;
|
||||
|
||||
col++;
|
||||
if (col > 1) {
|
||||
|
||||
@@ -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,61 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
void updateButton() {
|
||||
if (!key.empty()) {
|
||||
params.putBool(key, !is_enabled);
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// Override mouse release event to handle style updates smoothly
|
||||
void mouseReleaseEvent(QMouseEvent *event) override {
|
||||
if (!key.empty()) {
|
||||
bool next_state = !params.getBool(key);
|
||||
updateStyle(next_state);
|
||||
}
|
||||
QPushButton::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user