From 124a230296586b1d8462a65e4f80f19dd0d6dfd2 Mon Sep 17 00:00:00 2001 From: Jaosn Wen Date: Wed, 17 Jul 2024 20:23:24 -0400 Subject: [PATCH] AbstractControlSP: Inherit from stock AbstractControl --- selfdrive/ui/qt/widgets/controls.cc | 2 + selfdrive/ui/qt/widgets/controls.h | 8 +- .../settings/sp_priv_sunnypilot_settings.cc | 2 +- .../sunnypilot/qt/widgets/sp_priv_controls.cc | 32 +++++-- .../sunnypilot/qt/widgets/sp_priv_controls.h | 96 +++++-------------- 5 files changed, 55 insertions(+), 85 deletions(-) diff --git a/selfdrive/ui/qt/widgets/controls.cc b/selfdrive/ui/qt/widgets/controls.cc index 3fbf506bb4..ba7ab5df40 100644 --- a/selfdrive/ui/qt/widgets/controls.cc +++ b/selfdrive/ui/qt/widgets/controls.cc @@ -2,8 +2,10 @@ #include #include +#include AbstractControl::AbstractControl(const QString &title, const QString &desc, const QString &icon, QWidget *parent) : QFrame(parent) { + RETURN_IF_SUNNYPILOT QVBoxLayout *main_layout = new QVBoxLayout(this); main_layout->setMargin(0); diff --git a/selfdrive/ui/qt/widgets/controls.h b/selfdrive/ui/qt/widgets/controls.h index 98ee30389a..8eb83d1705 100644 --- a/selfdrive/ui/qt/widgets/controls.h +++ b/selfdrive/ui/qt/widgets/controls.h @@ -40,7 +40,7 @@ class AbstractControl : public QFrame { Q_OBJECT public: - void setDescription(const QString &desc) { + virtual void setDescription(const QString &desc) { if (description) description->setText(desc); } @@ -48,11 +48,11 @@ public: title_label->setText(title); } - void setValue(const QString &val) { + virtual void setValue(const QString &val) { value->setText(val); } - const QString getDescription() { + virtual const QString getDescription() { return description->text(); } @@ -60,7 +60,7 @@ public: QPixmap icon_pixmap; public slots: - void showDescription() { + virtual void showDescription() { description->setVisible(true); } diff --git a/selfdrive/ui/sunnypilot/qt/offroad/settings/sp_priv_sunnypilot_settings.cc b/selfdrive/ui/sunnypilot/qt/offroad/settings/sp_priv_sunnypilot_settings.cc index 29bc5c8793..4a49488aba 100644 --- a/selfdrive/ui/sunnypilot/qt/offroad/settings/sp_priv_sunnypilot_settings.cc +++ b/selfdrive/ui/sunnypilot/qt/offroad/settings/sp_priv_sunnypilot_settings.cc @@ -575,7 +575,7 @@ void SunnypilotPanel::updateToggles() { } // toggle names to update when CustomTorqueLateral is flipped - std::vector customTorqueGroup{friction, lat_accel_factor}; + std::vector customTorqueGroup{friction, lat_accel_factor}; for (const auto& customTorqueControl : customTorqueGroup) { customTorqueControl->setVisible(!(nnff_toggle->isToggled() || !custom_torque_lateral->isToggled())); customTorqueControl->setEnabled(!(nnff_toggle->isToggled() || !custom_torque_lateral->isToggled())); diff --git a/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.cc b/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.cc index 7f9985a70c..dc0974eee5 100644 --- a/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.cc +++ b/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.cc @@ -2,6 +2,7 @@ #include #include +#include QFrame *horizontal_line(QWidget *parent) { QFrame *line = new QFrame(parent); @@ -15,8 +16,10 @@ QFrame *horizontal_line(QWidget *parent) { return line; } -AbstractControlSP_TITLED::AbstractControlSP_TITLED(const QString &title, const QString &desc, const QString &icon, QWidget *parent) : QFrame(parent) { - QVBoxLayout *main_layout = new QVBoxLayout(this); +AbstractControlSP::AbstractControlSP(const QString &title, const QString &desc, const QString &icon, QWidget *parent) + : AbstractControl(title, desc, icon, parent) { + + main_layout = new QVBoxLayout(this); main_layout->setMargin(0); hlayout = new QHBoxLayout; @@ -68,14 +71,28 @@ AbstractControlSP_TITLED::AbstractControlSP_TITLED(const QString &title, const Q main_layout->addStretch(); } -void AbstractControlSP_TITLED::hideEvent(QHideEvent *e) { +void AbstractControlSP::hideEvent(QHideEvent *e) { if (description != nullptr) { description->hide(); } } -AbstractControlSP::AbstractControlSP(const QString &title, const QString &desc, const QString &icon, QWidget *parent) : QFrame(parent) { - QVBoxLayout *main_layout = new QVBoxLayout(this); +AbstractControlSP_SELECTOR::AbstractControlSP_SELECTOR(const QString &title, const QString &desc, const QString &icon, QWidget *parent) + : AbstractControlSP(title, desc, icon, parent) { + + if (value != nullptr) { + ReplaceWidget(value, new QWidget()); + value = nullptr; + } + + QLayoutItem* item; + while ((item = main_layout->takeAt(0)) != nullptr) { + if (item->widget()) { + delete item->widget(); + } + delete item; + } + main_layout->setMargin(0); hlayout = new QHBoxLayout; @@ -116,12 +133,9 @@ AbstractControlSP::AbstractControlSP(const QString &title, const QString &desc, main_layout->addStretch(); } -void AbstractControlSP::hideEvent(QHideEvent *e) { -} - // controls -ButtonControlSP::ButtonControlSP(const QString &title, const QString &text, const QString &desc, QWidget *parent) : AbstractControlSP_TITLED(title, desc, "", parent) { +ButtonControlSP::ButtonControlSP(const QString &title, const QString &text, const QString &desc, QWidget *parent) : AbstractControlSP(title, desc, "", parent) { btn.setText(text); btn.setStyleSheet(R"( QPushButton { diff --git a/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.h b/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.h index 44255b2d99..c11d489bb4 100644 --- a/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.h +++ b/selfdrive/ui/sunnypilot/qt/widgets/sp_priv_controls.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -12,8 +13,8 @@ #include #include "common/params.h" +#include "selfdrive/ui/qt/widgets/controls.h" #include "selfdrive/ui/qt/widgets/input.h" -//#include "selfdrive/ui/qt/widgets/toggle.h" #include "selfdrive/ui/sunnypilot/qt/widgets/sp_priv_toggle.h" // This is for compatibility purposes, until we properly do inheritance splitting @@ -52,18 +53,14 @@ protected: QString lastText_, elidedText_; }; -class AbstractControlSP_TITLED : public QFrame { +class AbstractControlSP : public AbstractControl { Q_OBJECT public: - void setDescription(const QString &desc) { + void setDescription(const QString &desc) override { if (description) description->setText(desc); } - void setTitle(const QString &title) { - title_label->setText(title); - } - void setValue(const QString &val, std::optional color = std::nullopt) { value->setText(val); if (color.has_value()) { @@ -71,84 +68,41 @@ public: } } - const QString getDescription() { + const QString getDescription() override { return description->text(); } - QLabel *icon_label; - QPixmap icon_pixmap; - - public slots: - void showDescription() { - description->setVisible(true); - } - void hideDescription() { description->setVisible(false); } - signals: - void showDescriptionEvent(); - -protected: - AbstractControlSP_TITLED(const QString &title, const QString &desc = "", const QString &icon = "", QWidget *parent = nullptr); - void hideEvent(QHideEvent *e) override; - - QHBoxLayout *hlayout; - QPushButton *title_label; - -private: - ElidedLabel *value; - QLabel *description = nullptr; -}; - - -class AbstractControlSP : public QFrame { - Q_OBJECT - -public: - void setDescription(const QString &desc) { - if (description) description->setText(desc); - } - - void setTitle(const QString &title) { - title_label->setText(title); - } - - const QString getDescription() { - return description->text(); - } - - public slots: - void showDescription() { +public slots: + void showDescription() override { description->setVisible(true); } - void hideDescription() { - description->setVisible(false); - } - - signals: - void showDescriptionEvent(); - protected: AbstractControlSP(const QString &title, const QString &desc = "", const QString &icon = "", QWidget *parent = nullptr); void hideEvent(QHideEvent *e) override; - QHBoxLayout *hlayout; - QPushButton *title_label; - -private: + QVBoxLayout *main_layout; + ElidedLabel *value; QLabel *description = nullptr; }; +class AbstractControlSP_SELECTOR : public AbstractControlSP { + Q_OBJECT + +protected: + AbstractControlSP_SELECTOR(const QString &title, const QString &desc = "", const QString &icon = "", QWidget *parent = nullptr); +}; // widget to display a value -class LabelControlSP : public AbstractControlSP_TITLED { +class LabelControlSP : public AbstractControlSP { Q_OBJECT public: - LabelControlSP(const QString &title, const QString &text = "", const QString &desc = "", QWidget *parent = nullptr) : AbstractControlSP_TITLED(title, desc, "", parent) { + LabelControlSP(const QString &title, const QString &text = "", const QString &desc = "", QWidget *parent = nullptr) : AbstractControlSP(title, desc, "", parent) { label.setText(text); label.setAlignment(Qt::AlignRight | Qt::AlignVCenter); hlayout->addWidget(&label); @@ -160,7 +114,7 @@ private: }; // widget for a button with a label -class ButtonControlSP : public AbstractControlSP_TITLED { +class ButtonControlSP : public AbstractControlSP { Q_OBJECT public: @@ -179,11 +133,11 @@ private: QPushButton btn; }; -class ToggleControlSP : public AbstractControlSP_TITLED { +class ToggleControlSP : public AbstractControlSP { Q_OBJECT public: - ToggleControlSP(const QString &title, const QString &desc = "", const QString &icon = "", const bool state = false, QWidget *parent = nullptr) : AbstractControlSP_TITLED(title, desc, icon, parent) { + ToggleControlSP(const QString &title, const QString &desc = "", const QString &icon = "", const bool state = false, QWidget *parent = nullptr) : AbstractControlSP(title, desc, icon, parent) { toggle.setFixedSize(150, 100); if (state) { toggle.togglePosition(); @@ -250,11 +204,11 @@ private: bool store_confirm = false; }; -class ButtonParamControlSP : public AbstractControlSP { +class ButtonParamControlSP : public AbstractControlSP_SELECTOR { Q_OBJECT public: ButtonParamControlSP(const QString ¶m, const QString &title, const QString &desc, const QString &icon, - const std::vector &button_texts, const int minimum_button_width = 300) : AbstractControlSP(title, desc, icon), button_texts(button_texts) { + const std::vector &button_texts, const int minimum_button_width = 300) : AbstractControlSP_SELECTOR(title, desc, icon), button_texts(button_texts) { const QString style = R"( QPushButton { border-radius: 20px; @@ -399,7 +353,7 @@ class ListWidgetSP : public QWidget { inline void AddWidgetAt(const int index, QWidget *new_widget) { inner_layout.insertWidget(index, new_widget); } inline void RemoveWidgetAt(const int index) { if (QLayoutItem* item; (item = inner_layout.takeAt(index)) != nullptr) { - if(item->widget()) delete item->widget(); + if (item->widget()) delete item->widget(); delete item; } } @@ -442,7 +396,7 @@ public: } }; -class OptionControlSP : public AbstractControlSP { +class OptionControlSP : public AbstractControlSP_SELECTOR { Q_OBJECT private: @@ -453,7 +407,7 @@ private: public: OptionControlSP(const QString ¶m, const QString &title, const QString &desc, const QString &icon, - const MinMaxValue &range, const int per_value_change = 1) : _title(title), AbstractControlSP(title, desc, icon) { + const MinMaxValue &range, const int per_value_change = 1) : _title(title), AbstractControlSP_SELECTOR(title, desc, icon) { const QString style = R"( QPushButton { border-radius: 20px;