Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sidebar: add button pressed states #25848

Merged
merged 8 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added selfdrive/assets/images/button_flag.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions selfdrive/ui/qt/home.cc
Expand Up @@ -41,6 +41,7 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {
setAttribute(Qt::WA_NoSystemBackground);
QObject::connect(uiState(), &UIState::uiUpdate, this, &HomeWindow::updateState);
QObject::connect(uiState(), &UIState::offroadTransition, this, &HomeWindow::offroadTransition);
QObject::connect(uiState(), &UIState::offroadTransition, sidebar, &Sidebar::offroadTransition);
}

void HomeWindow::showSidebar(bool show) {
Expand Down
31 changes: 25 additions & 6 deletions selfdrive/ui/qt/sidebar.cc
Expand Up @@ -32,8 +32,9 @@ void Sidebar::drawMetric(QPainter &p, const QPair<QString, QString> &label, QCol
p.drawText(label_rect, Qt::AlignCenter, label.second);
}

Sidebar::Sidebar(QWidget *parent) : QFrame(parent) {
Sidebar::Sidebar(QWidget *parent) : QFrame(parent), onroad(false), flag_pressed(false), settings_pressed(false) {
home_img = loadPixmap("../assets/images/button_home.png", home_btn.size());
flag_img = loadPixmap("../assets/images/button_flag.png", home_btn.size());
settings_img = loadPixmap("../assets/images/button_settings.png", settings_btn.size(), Qt::IgnoreAspectRatio);

connect(this, &Sidebar::valueChanged, [=] { update(); });
Expand All @@ -47,17 +48,34 @@ Sidebar::Sidebar(QWidget *parent) : QFrame(parent) {
pm = std::make_unique<PubMaster, const std::initializer_list<const char *>>({"userFlag"});
}

void Sidebar::mousePressEvent(QMouseEvent *event) {
if (onroad && home_btn.contains(event->pos())) {
flag_pressed = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flag_pressed = true;
bookmark_pressed = true;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll keep it as flag in all of the code for now

update();
} else if (settings_btn.contains(event->pos())) {
settings_pressed = true;
update();
}
}

void Sidebar::mouseReleaseEvent(QMouseEvent *event) {
if (flag_pressed || settings_pressed) {
flag_pressed = settings_pressed = false;
update();
}
if (home_btn.contains(event->pos())) {
MessageBuilder msg;
msg.initEvent().initUserFlag();
pm->send("userFlag", msg);
}
if (settings_btn.contains(event->pos())) {
} else if (settings_btn.contains(event->pos())) {
emit openSettings();
}
}

void Sidebar::offroadTransition(bool offroad) {
onroad = !offroad;
}

void Sidebar::updateState(const UIState &s) {
if (!isVisible()) return;

Expand Down Expand Up @@ -102,11 +120,12 @@ void Sidebar::paintEvent(QPaintEvent *event) {

p.fillRect(rect(), QColor(57, 57, 57));

// static imgs
p.setOpacity(0.65);
// buttons
p.setOpacity(settings_pressed ? 0.65 : 1.0);
p.drawPixmap(settings_btn.x(), settings_btn.y(), settings_img);
p.setOpacity(onroad && flag_pressed ? 0.65 : 1.0);
p.drawPixmap(home_btn.x(), home_btn.y(), onroad ? flag_img : home_img);
p.setOpacity(1.0);
p.drawPixmap(home_btn.x(), home_btn.y(), home_img);

// network
int x = 58;
Expand Down
5 changes: 4 additions & 1 deletion selfdrive/ui/qt/sidebar.h
Expand Up @@ -24,14 +24,17 @@ class Sidebar : public QFrame {
void valueChanged();

public slots:
void offroadTransition(bool offroad);
void updateState(const UIState &s);

protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void drawMetric(QPainter &p, const QPair<QString, QString> &label, QColor c, int y);

QPixmap home_img, settings_img;
QPixmap home_img, flag_img, settings_img;
bool onroad, flag_pressed, settings_pressed;
const QMap<cereal::DeviceState::NetworkType, QString> network_type = {
{cereal::DeviceState::NetworkType::NONE, tr("--")},
{cereal::DeviceState::NetworkType::WIFI, tr("Wi-Fi")},
Expand Down