diff --git a/selfdrive/ui/qt/body.cc b/selfdrive/ui/qt/body.cc index 08d3611b46..a83d749577 100644 --- a/selfdrive/ui/qt/body.cc +++ b/selfdrive/ui/qt/body.cc @@ -2,6 +2,8 @@ #include +#include + BodyWindow::BodyWindow(QWidget *parent) : QLabel(parent) { awake = new QMovie("../assets/body/awake.gif"); awake->setCacheMode(QMovie::CacheAll); @@ -19,18 +21,46 @@ BodyWindow::BodyWindow(QWidget *parent) : QLabel(parent) { QObject::connect(uiState(), &UIState::uiUpdate, this, &BodyWindow::updateState); } +void BodyWindow::paintEvent(QPaintEvent *event) { + QLabel::paintEvent(event); + + QPainter p(this); + p.setRenderHint(QPainter::Antialiasing); + p.setPen(Qt::NoPen); + + // draw battery level + const int offset = 90; + const int radius = 60 / 2; + const int levels = 5; + const float interval = 1. / levels; + for (int i = 0; i < levels; i++) { + float level = 1.0 - (i+1)*interval; + float perc = (fuel >= level) ? 1.0 : 0.35; + + p.setBrush(QColor(255, 255, 255, 255 * perc)); + QPoint pt(width() - (i*offset + offset / 2), offset / 2); + p.drawEllipse(pt, radius, radius); + } +} + + void BodyWindow::updateState(const UIState &s) { if (!isVisible()) { return; } const SubMaster &sm = *(s.sm); + auto cs = sm["carState"].getCarState(); + + fuel = cs.getFuelGauge(); // TODO: use carState.standstill when that's fixed - const bool standstill = std::abs(sm["carState"].getCarState().getVEgo()) < 0.01; + const bool standstill = std::abs(cs.getVEgo()) < 0.01; QMovie *m = standstill ? sleep : awake; if (m != movie()) { setMovie(m); movie()->start(); } + + update(); } diff --git a/selfdrive/ui/qt/body.h b/selfdrive/ui/qt/body.h index 7f352715df..c1138b8703 100644 --- a/selfdrive/ui/qt/body.h +++ b/selfdrive/ui/qt/body.h @@ -12,7 +12,9 @@ public: BodyWindow(QWidget* parent = 0); private: + float fuel = 1.0; QMovie *awake, *sleep; + void paintEvent(QPaintEvent*) override; private slots: void updateState(const UIState &s);