qt: faster training guide (#20407)

pull/20411/head
Dean Lee 2021-03-20 04:07:59 +08:00 committed by GitHub
parent e2b79d0483
commit bf3c7e6afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 30 deletions

View File

@ -6,54 +6,46 @@
#include <QGridLayout>
#include <QVBoxLayout>
#include <QDesktopWidget>
#include <QPainter>
#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;
image.load("../assets/training/step0.jpg");
}
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);
}
void TrainingGuide::paintEvent(QPaintEvent *event) {
QPainter painter(this);
QWidget* sw = new QWidget();
sw->setLayout(slayout);
hlayout->addWidget(sw, 1, Qt::AlignCenter);
setLayout(hlayout);
setStyleSheet(R"(
background-color: #072339;
)");
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;

View File

@ -2,9 +2,9 @@
#include <QWidget>
#include <QStackedWidget>
#include <QStackedLayout>
#include <QTextEdit>
#include <QMouseEvent>
#include <QImage>
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<QVector<int>> 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},