From bf3c7e6afa3f66aee0841c7c74704892ec1139e7 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Sat, 20 Mar 2021 04:07:59 +0800 Subject: [PATCH] qt: faster training guide (#20407) --- selfdrive/ui/qt/offroad/onboarding.cc | 49 +++++++++++--------------- selfdrive/ui/qt/offroad/onboarding.hpp | 5 +-- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/selfdrive/ui/qt/offroad/onboarding.cc b/selfdrive/ui/qt/offroad/onboarding.cc index 55de0f5f14..c3c89131c5 100644 --- a/selfdrive/ui/qt/offroad/onboarding.cc +++ b/selfdrive/ui/qt/offroad/onboarding.cc @@ -6,54 +6,46 @@ #include #include #include - +#include #include "common/params.h" #include "onboarding.hpp" #include "home.hpp" #include "util.h" - void TrainingGuide::mouseReleaseEvent(QMouseEvent *e) { int leftOffset = (geometry().width()-1620)/2; int mousex = e->x()-leftOffset; int mousey = e->y(); // Check for restart - if (currentIndex == boundingBox.size()-1) { - if (1050 <= mousex && mousex <= 1500 && 773 <= mousey && mousey <= 954){ - slayout->setCurrentIndex(0); - currentIndex = 0; - return; - } - } - - if (boundingBox[currentIndex][0] <= mousex && mousex <= boundingBox[currentIndex][1] && boundingBox[currentIndex][2] <= mousey && mousey <= boundingBox[currentIndex][3]) { - slayout->setCurrentIndex(++currentIndex); + if (currentIndex == (boundingBox.size() - 1) && 1050 <= mousex && mousex <= 1500 && 773 <= mousey && mousey <= 954) { + currentIndex = 0; + } else if (boundingBox[currentIndex][0] <= mousex && mousex <= boundingBox[currentIndex][1] && boundingBox[currentIndex][2] <= mousey && mousey <= boundingBox[currentIndex][3]) { + ++currentIndex; } if (currentIndex >= boundingBox.size()) { emit completedTraining(); return; } + + image.load("../assets/training/step" + QString::number(currentIndex) + ".jpg"); + repaint(); } TrainingGuide::TrainingGuide(QWidget* parent) { - QHBoxLayout* hlayout = new QHBoxLayout; - - slayout = new QStackedLayout(this); - for (int i = 0; i < boundingBox.size(); i++) { - QWidget* w = new QWidget; - w->setStyleSheet(".QWidget {background-image: url(../assets/training/step" + QString::number(i) + ".jpg);}"); - w->setFixedSize(1620, 1080); - slayout->addWidget(w); - } + image.load("../assets/training/step0.jpg"); +} - QWidget* sw = new QWidget(); - sw->setLayout(slayout); - hlayout->addWidget(sw, 1, Qt::AlignCenter); - setLayout(hlayout); - setStyleSheet(R"( - background-color: #072339; - )"); +void TrainingGuide::paintEvent(QPaintEvent *event) { + QPainter painter(this); + + QRect devRect(0, 0, painter.device()->width(), painter.device()->height()); + QBrush bgBrush("#072339"); + painter.fillRect(devRect, bgBrush); + + QRect rect(image.rect()); + rect.moveCenter(devRect.center()); + painter.drawImage(rect.topLeft(), image); } @@ -144,7 +136,6 @@ OnboardingWindow::OnboardingWindow(QWidget *parent) : QStackedWidget(parent) { bool accepted_terms = params.get("HasAcceptedTerms", false).compare(current_terms_version) == 0; bool training_done = params.get("CompletedTrainingVersion", false).compare(current_training_version) == 0; - // TODO: fix this, training guide is slow // Don't initialize widgets unless neccesary. if (accepted_terms && training_done) { return; diff --git a/selfdrive/ui/qt/offroad/onboarding.hpp b/selfdrive/ui/qt/offroad/onboarding.hpp index 3d51e5c7d3..55a1ab9514 100644 --- a/selfdrive/ui/qt/offroad/onboarding.hpp +++ b/selfdrive/ui/qt/offroad/onboarding.hpp @@ -2,9 +2,9 @@ #include #include -#include #include #include +#include class TrainingGuide : public QFrame { Q_OBJECT @@ -14,10 +14,11 @@ public: protected: void mouseReleaseEvent(QMouseEvent* e) override; + void paintEvent(QPaintEvent *event) override; private: int currentIndex = 0; - QStackedLayout* slayout; + QImage image; // Vector of bounding boxes for the a given training guide step. (minx, maxx, miny, maxy) QVector> boundingBox {{250, 930, 750, 900}, {280, 1280, 650, 950}, {330, 1130, 590, 900}, {910, 1580, 500, 1000}, {1180, 1300, 630, 720}, {290, 1050, 590, 960},