nopenpilot/selfdrive/ui/qt/offroad/onboarding.cc

180 lines
4.9 KiB
C++
Raw Normal View History

#include <QLabel>
2020-11-12 22:37:02 -07:00
#include <QString>
#include <QScroller>
#include <QScrollBar>
2020-11-12 22:37:02 -07:00
#include <QPushButton>
#include <QGridLayout>
2020-11-25 01:01:05 -07:00
#include <QVBoxLayout>
#include <QDesktopWidget>
#include "common/params.h"
2021-01-29 12:47:22 -07:00
#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
2021-01-29 12:47:22 -07:00
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);
}
2021-01-29 12:47:22 -07:00
if (currentIndex >= boundingBox.size()) {
emit completedTraining();
return;
}
}
TrainingGuide::TrainingGuide(QWidget* parent) {
QHBoxLayout* hlayout = new QHBoxLayout;
slayout = new QStackedLayout(this);
2021-01-29 12:47:22 -07:00
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);
}
QWidget* sw = new QWidget();
sw->setLayout(slayout);
hlayout->addWidget(sw, 1, Qt::AlignCenter);
setLayout(hlayout);
2021-01-29 14:41:48 -07:00
setStyleSheet(R"(
background-color: #072339;
)");
}
QWidget* OnboardingWindow::terms_screen() {
QVBoxLayout *main_layout = new QVBoxLayout;
main_layout->setContentsMargins(40, 20, 40, 0);
QString terms_html = QString::fromStdString(util::read_file("../assets/offroad/tc.html"));
terms_text = new QTextEdit();
terms_text->setReadOnly(true);
terms_text->setTextInteractionFlags(Qt::NoTextInteraction);
terms_text->setHtml(terms_html);
main_layout->addWidget(terms_text);
// TODO: add decline page
QHBoxLayout* buttons = new QHBoxLayout;
main_layout->addLayout(buttons);
buttons->addWidget(new QPushButton("Decline"));
buttons->addSpacing(50);
QPushButton *accept_btn = new QPushButton("Scroll to accept");
accept_btn->setEnabled(false);
buttons->addWidget(accept_btn);
QObject::connect(accept_btn, &QPushButton::released, [=]() {
2021-01-29 12:47:22 -07:00
Params().write_db_value("HasAcceptedTerms", current_terms_version);
updateActiveScreen();
});
// TODO: tune the scrolling
auto sb = terms_text->verticalScrollBar();
#ifdef QCOM2
sb->setStyleSheet(R"(
QScrollBar {
width: 150px;
background: grey;
}
QScrollBar::handle {
background-color: white;
}
QScrollBar::add-line, QScrollBar::sub-line{
width: 0;
height: 0;
}
)");
#else
QScroller::grabGesture(terms_text, QScroller::TouchGesture);
terms_text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
#endif
QObject::connect(sb, &QScrollBar::valueChanged, [sb, accept_btn]() {
accept_btn->setEnabled(accept_btn->isEnabled() || (sb->value() == sb->maximum()));
});
2020-11-25 01:01:05 -07:00
QWidget *widget = new QWidget;
widget->setLayout(main_layout);
widget->setStyleSheet(R"(
* {
font-size: 50px;
}
QPushButton {
padding: 50px;
border-radius: 10px;
background-color: #292929;
}
)");
return widget;
}
void OnboardingWindow::updateActiveScreen() {
Params params = Params();
2021-01-29 12:47:22 -07:00
bool accepted_terms = params.get("HasAcceptedTerms", false).compare(current_terms_version) == 0;
bool training_done = params.get("CompletedTrainingVersion", false).compare(current_training_version) == 0;
2020-11-12 22:37:02 -07:00
if (!accepted_terms) {
2020-11-25 01:01:05 -07:00
setCurrentIndex(0);
2020-11-12 22:37:02 -07:00
} else if (!training_done) {
2020-11-25 01:01:05 -07:00
setCurrentIndex(1);
2020-11-12 22:37:02 -07:00
} else {
emit onboardingDone();
}
}
OnboardingWindow::OnboardingWindow(QWidget *parent) : QStackedWidget(parent) {
Params params = Params();
2021-01-29 12:47:22 -07:00
current_terms_version = params.get("TermsVersion", false);
current_training_version = params.get("TrainingVersion", false);
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;
}
2020-11-25 01:01:05 -07:00
addWidget(terms_screen());
2021-01-29 12:47:22 -07:00
TrainingGuide* tr = new TrainingGuide(this);
2021-01-29 12:47:22 -07:00
connect(tr, &TrainingGuide::completedTraining, [=](){
Params().write_db_value("CompletedTrainingVersion", current_training_version);
updateActiveScreen();
});
addWidget(tr);
2020-11-24 05:53:15 -07:00
setStyleSheet(R"(
* {
2021-01-22 14:34:27 -07:00
color: white;
background-color: black;
}
2020-11-12 22:37:02 -07:00
QPushButton {
padding: 50px;
border-radius: 30px;
2020-11-12 22:37:02 -07:00
background-color: #292929;
}
QPushButton:disabled {
color: #777777;
background-color: #222222;
}
)");
updateActiveScreen();
}