diff --git a/selfdrive/ui/sunnypilot/SConscript b/selfdrive/ui/sunnypilot/SConscript index 379adfced..d18975184 100644 --- a/selfdrive/ui/sunnypilot/SConscript +++ b/selfdrive/ui/sunnypilot/SConscript @@ -3,6 +3,7 @@ widgets_src = [ "sunnypilot/qt/widgets/toggle.cc", "sunnypilot/qt/widgets/controls.cc", "sunnypilot/qt/widgets/drive_stats.cc", + "sunnypilot/qt/widgets/expandable_row.cc", "sunnypilot/qt/widgets/prime.cc", "sunnypilot/qt/widgets/scrollview.cc", "sunnypilot/qt/network/networking.cc", diff --git a/selfdrive/ui/sunnypilot/qt/widgets/controls.h b/selfdrive/ui/sunnypilot/qt/widgets/controls.h index b6efd871d..b5411f3bf 100644 --- a/selfdrive/ui/sunnypilot/qt/widgets/controls.h +++ b/selfdrive/ui/sunnypilot/qt/widgets/controls.h @@ -441,15 +441,16 @@ public: class OptionControlSP : public AbstractControlSP_SELECTOR { Q_OBJECT -private: - bool is_inline_layout; - QHBoxLayout *optionSelectorLayout = is_inline_layout ? new QHBoxLayout() : hlayout; - +protected: struct MinMaxValue { int min_value; int max_value; }; +private: + bool is_inline_layout; + QHBoxLayout *optionSelectorLayout = is_inline_layout ? new QHBoxLayout() : hlayout; + int getParamValue() { const auto param_value = QString::fromStdString(params.get(key)); const auto result = valueMap != nullptr ? valueMap->key(param_value) : param_value; @@ -551,6 +552,10 @@ public: request_update = _update; } + void setFixedWidth(int width) { + label.setFixedWidth(width); + } + inline void setLabel(const QString &text) { label.setText(text); } void setEnabled(bool enabled) { diff --git a/selfdrive/ui/sunnypilot/qt/widgets/expandable_row.cc b/selfdrive/ui/sunnypilot/qt/widgets/expandable_row.cc new file mode 100644 index 000000000..b918938d1 --- /dev/null +++ b/selfdrive/ui/sunnypilot/qt/widgets/expandable_row.cc @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors. + * + * This file is part of sunnypilot and is licensed under the MIT License. + * See the LICENSE.md file in the root directory for more details. + */ + +#include "selfdrive/ui/sunnypilot/qt/widgets/expandable_row.h" + +ExpandableToggleRow::ExpandableToggleRow(const QString ¶m, const QString &title, const QString &desc, const QString &icon, QWidget *parent) + : ToggleControlSP(title, desc, icon, false, parent) { + + key = param.toStdString(); + QObject::connect(this, &ExpandableToggleRow::toggleFlipped, this, &ExpandableToggleRow::toggleClicked); + + collapsibleWidget = new QFrame(this); + collapsibleWidget->setContentsMargins(0, 0, 0, 0); + collapsibleWidget->setVisible(false); + QVBoxLayout *collapsible_layout = new QVBoxLayout(); + collapsibleWidget->setLayout(collapsible_layout); + + list = new ListWidgetSP(this, false); + + main_layout->addWidget(collapsibleWidget); + collapsible_layout->addWidget(list); +} + +void ExpandableToggleRow::toggleClicked(bool state) { + params.putBool(key, state); + collapsibleWidget->setVisible(state); +} diff --git a/selfdrive/ui/sunnypilot/qt/widgets/expandable_row.h b/selfdrive/ui/sunnypilot/qt/widgets/expandable_row.h new file mode 100644 index 000000000..4bfcb9ef5 --- /dev/null +++ b/selfdrive/ui/sunnypilot/qt/widgets/expandable_row.h @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors. + * + * This file is part of sunnypilot and is licensed under the MIT License. + * See the LICENSE.md file in the root directory for more details. + */ + +#pragma once + +#include "selfdrive/ui/sunnypilot/qt/widgets/controls.h" + +class ExpandableToggleRow : public ToggleControlSP { + Q_OBJECT + +public: + ExpandableToggleRow(const QString ¶m, const QString &title, const QString &desc, const QString &icon, QWidget *parent = nullptr); + + void addItem(QWidget *widget) { + list->addItem(widget); + } + + ListWidgetSP *innerList() { + return list; + } + + void refresh() { + bool state = params.getBool(key); + if (state != toggle.on) { + toggle.togglePosition(); + } + collapsibleWidget->setVisible(state); + } + + bool isToggled() { + return params.getBool(key); + } + + void showEvent(QShowEvent *event) override { + refresh(); + } + +private: + void toggleClicked(bool state); + + std::string key; + Params params; + + ListWidgetSP *list; + QFrame *collapsibleWidget = nullptr; +};