From 9afa14c47fa40023cf1abb1956d7e2815aab9409 Mon Sep 17 00:00:00 2001 From: iejMac <61431446+iejMac@users.noreply.github.com> Date: Thu, 1 Apr 2021 14:03:31 -0700 Subject: [PATCH] Qt scroller class (#20549) * initial commit * class works * cleanup * naming * text window * fixes * alerts are now scrollable * dismiss * fixed placement * realease scrolling * better * better * revert text * naming * parent --- selfdrive/ui/SConscript | 4 ++-- selfdrive/ui/qt/offroad/settings.cc | 17 +++------------ selfdrive/ui/qt/widgets/offroad_alerts.cc | 25 ++++++++++++++++++---- selfdrive/ui/qt/widgets/offroad_alerts.hpp | 4 ++++ selfdrive/ui/qt/widgets/scrollview.cc | 18 ++++++++++++++++ selfdrive/ui/qt/widgets/scrollview.hpp | 11 ++++++++++ 6 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 selfdrive/ui/qt/widgets/scrollview.cc create mode 100644 selfdrive/ui/qt/widgets/scrollview.hpp diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index c38e3c90..a663f1a7 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -14,7 +14,7 @@ if arch == "Darwin": widgets_src = ["qt/widgets/input.cc", "qt/widgets/drive_stats.cc", "qt/sound.cc", "qt/widgets/ssh_keys.cc", "qt/widgets/toggle.cc", "qt/widgets/controls.cc", "qt/widgets/offroad_alerts.cc", "qt/widgets/setup.cc", "qt/widgets/keyboard.cc", - "#phonelibs/qrcode/QrCode.cc"] + "qt/widgets/scrollview.cc", "#phonelibs/qrcode/QrCode.cc"] if arch != 'aarch64': widgets_src += ["qt/offroad/networking.cc", "qt/offroad/wifiManager.cc"] @@ -22,7 +22,7 @@ widgets = qt_env.Library("qt_widgets", widgets_src, LIBS=base_libs) qt_libs = base_libs + [widgets] # spinner and text window -qt_env.Program("qt/text", ["qt/text.cc"], LIBS=base_libs) +qt_env.Program("qt/text", ["qt/text.cc"], LIBS=qt_libs) qt_env.Program("qt/spinner", ["qt/spinner.cc"], LIBS=base_libs) # build main UI diff --git a/selfdrive/ui/qt/offroad/settings.cc b/selfdrive/ui/qt/offroad/settings.cc index bd3611c8..2b1763a4 100644 --- a/selfdrive/ui/qt/offroad/settings.cc +++ b/selfdrive/ui/qt/offroad/settings.cc @@ -10,6 +10,7 @@ #include "widgets/input.hpp" #include "widgets/toggle.hpp" #include "widgets/offroad_alerts.hpp" +#include "widgets/scrollview.hpp" #include "widgets/controls.hpp" #include "widgets/ssh_keys.hpp" #include "common/params.h" @@ -17,6 +18,7 @@ #include "selfdrive/hardware/hw.h" #include "home.hpp" + QWidget * toggles_panel() { QVBoxLayout *toggles_list = new QVBoxLayout(); @@ -273,21 +275,8 @@ SettingsWindow::SettingsWindow(QWidget *parent) : QFrame(parent) { sidebar_layout->addWidget(btn, 0, Qt::AlignRight); panel->setContentsMargins(50, 25, 50, 25); - QScrollArea *panel_frame = new QScrollArea; - panel_frame->setWidget(panel); - panel_frame->setWidgetResizable(true); - panel_frame->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - panel_frame->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - panel_frame->setStyleSheet("background-color:transparent;"); - - QScroller *scroller = QScroller::scroller(panel_frame->viewport()); - auto sp = scroller->scrollerProperties(); - - sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QVariant::fromValue(QScrollerProperties::OvershootAlwaysOff)); - - scroller->grabGesture(panel_frame->viewport(), QScroller::LeftMouseButtonGesture); - scroller->setScrollerProperties(sp); + ScrollView *panel_frame = new ScrollView(panel, this); panel_widget->addWidget(panel_frame); QObject::connect(btn, &QPushButton::released, [=, w = panel_frame]() { diff --git a/selfdrive/ui/qt/widgets/offroad_alerts.cc b/selfdrive/ui/qt/widgets/offroad_alerts.cc index 596586f5..f5e5f900 100644 --- a/selfdrive/ui/qt/widgets/offroad_alerts.cc +++ b/selfdrive/ui/qt/widgets/offroad_alerts.cc @@ -10,6 +10,13 @@ OffroadAlert::OffroadAlert(QWidget* parent) : QFrame(parent) { QVBoxLayout *layout = new QVBoxLayout(); layout->setMargin(50); + layout->setSpacing(30); + + QWidget *alerts_widget = new QWidget; + QVBoxLayout *alerts_layout = new QVBoxLayout; + alerts_layout->setMargin(0); + alerts_layout->setSpacing(30); + alerts_widget->setLayout(alerts_layout); // setup labels for each alert QString json = QString::fromStdString(util::read_file("../controls/lib/alerts_offroad.json")); @@ -23,13 +30,22 @@ OffroadAlert::OffroadAlert(QWidget* parent) : QFrame(parent) { l->setWordWrap(true); l->setStyleSheet("background-color: " + QString(severity ? "#E22C2C" : "#292929")); l->setVisible(false); - layout->addWidget(l); + alerts_layout->addWidget(l); } + alerts_layout->addStretch(1); + // release notes + releaseNotes.setWordWrap(true); releaseNotes.setVisible(false); releaseNotes.setStyleSheet("font-size: 48px;"); - layout->addWidget(&releaseNotes); + releaseNotes.setAlignment(Qt::AlignTop); + + releaseNotesScroll = new ScrollView(&releaseNotes, this); + layout->addWidget(releaseNotesScroll); + + alertsScroll = new ScrollView(alerts_widget, this); + layout->addWidget(alertsScroll); // bottom footer, dismiss + reboot buttons QHBoxLayout *footer_layout = new QHBoxLayout(); @@ -70,11 +86,12 @@ void OffroadAlert::refresh() { updateAlerts(); rebootBtn.setVisible(updateAvailable); - releaseNotes.setVisible(updateAvailable); + releaseNotesScroll->setVisible(updateAvailable); releaseNotes.setText(QString::fromStdString(params.get("ReleaseNotes"))); + alertsScroll->setVisible(!updateAvailable); for (const auto& [k, label] : alerts) { - label->setVisible(!updateAvailable && !label->text().isEmpty()); + label->setVisible(!label->text().isEmpty()); } } diff --git a/selfdrive/ui/qt/widgets/offroad_alerts.hpp b/selfdrive/ui/qt/widgets/offroad_alerts.hpp index b05d68d3..a7883623 100644 --- a/selfdrive/ui/qt/widgets/offroad_alerts.hpp +++ b/selfdrive/ui/qt/widgets/offroad_alerts.hpp @@ -6,6 +6,7 @@ #include #include "common/params.h" +#include "widgets/scrollview.hpp" class OffroadAlert : public QFrame { Q_OBJECT @@ -22,6 +23,9 @@ private: QPushButton rebootBtn; void updateAlerts(); + ScrollView *releaseNotesScroll; + ScrollView *alertsScroll; + signals: void closeAlerts(); diff --git a/selfdrive/ui/qt/widgets/scrollview.cc b/selfdrive/ui/qt/widgets/scrollview.cc new file mode 100644 index 00000000..ebb7a736 --- /dev/null +++ b/selfdrive/ui/qt/widgets/scrollview.cc @@ -0,0 +1,18 @@ +#include "scrollview.hpp" + +ScrollView::ScrollView(QWidget *w, QWidget *parent) : QScrollArea(parent){ + setWidget(w); + setWidgetResizable(true); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setStyleSheet("background-color:transparent;"); + + QScroller *scroller = QScroller::scroller(this->viewport()); + QScrollerProperties sp = scroller->scrollerProperties(); + + sp.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QVariant::fromValue(QScrollerProperties::OvershootAlwaysOff)); + sp.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, QVariant::fromValue(QScrollerProperties::OvershootAlwaysOff)); + + scroller->grabGesture(this->viewport(), QScroller::LeftMouseButtonGesture); + scroller->setScrollerProperties(sp); +} diff --git a/selfdrive/ui/qt/widgets/scrollview.hpp b/selfdrive/ui/qt/widgets/scrollview.hpp new file mode 100644 index 00000000..54a0e670 --- /dev/null +++ b/selfdrive/ui/qt/widgets/scrollview.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +class ScrollView : public QScrollArea { + Q_OBJECT + +public: + explicit ScrollView(QWidget *w = nullptr, QWidget *parent = nullptr); +};