diff --git a/selfdrive/ui/qt/offroad/toggle.cc b/selfdrive/ui/qt/offroad/toggle.cc index 5257645693..25f921a48f 100644 --- a/selfdrive/ui/qt/offroad/toggle.cc +++ b/selfdrive/ui/qt/offroad/toggle.cc @@ -1,11 +1,5 @@ #include "toggle.hpp" -#include -#include -#include -#include -#include "common/params.h" - Toggle::Toggle(QWidget *parent) : QAbstractButton(parent), _height(60), _height_rect(45), @@ -18,7 +12,6 @@ _anim(new QPropertyAnimation(this, "offset_circle", this)) _y_rect = (_height - _height_rect)/2; } - void Toggle::paintEvent(QPaintEvent *e) { this->setFixedHeight(_height); QPainter p(this); @@ -42,25 +35,18 @@ void Toggle::mouseReleaseEvent(QMouseEvent *e) { togglePosition(); emit stateChanged(_on); } - } -void Toggle::togglePosition(){ +void Toggle::togglePosition() { _on = !_on; - if (_on) { - _anim->setStartValue(_radius); - _anim->setEndValue(width() - _radius); - _anim->setDuration(120); - _anim->start(); - } else { - _anim->setStartValue(width() - _radius); - _anim->setEndValue(_radius); - _anim->setDuration(120); - _anim->start(); - } + const int left = _radius; + const int right = width() - _radius; + _anim->setStartValue(_on ? left : right); + _anim->setEndValue(_on ? right : left); + _anim->setDuration(120); + _anim->start(); } void Toggle::enterEvent(QEvent *e) { - setCursor(Qt::PointingHandCursor); QAbstractButton::enterEvent(e); } diff --git a/selfdrive/ui/qt/offroad/toggle.hpp b/selfdrive/ui/qt/offroad/toggle.hpp index 9d4f3eceff..98215c25d2 100644 --- a/selfdrive/ui/qt/offroad/toggle.hpp +++ b/selfdrive/ui/qt/offroad/toggle.hpp @@ -2,32 +2,33 @@ #include class Toggle : public QAbstractButton { - Q_OBJECT - Q_PROPERTY(int offset_circle READ offset_circle WRITE set_offset_circle) + Q_OBJECT + Q_PROPERTY(int offset_circle READ offset_circle WRITE set_offset_circle) public: - Toggle(QWidget* parent = nullptr); - void togglePosition();//Toggles the toggle + Toggle(QWidget* parent = nullptr); + void togglePosition(); - int offset_circle() const { - return _x_circle; - } - void set_offset_circle(int o) { - _x_circle = o; - update(); - } + int offset_circle() const { + return _x_circle; + } + + void set_offset_circle(int o) { + _x_circle = o; + update(); + } protected: - void paintEvent(QPaintEvent*) override; - void mouseReleaseEvent(QMouseEvent*) override; - void enterEvent(QEvent*) override; + void paintEvent(QPaintEvent*) override; + void mouseReleaseEvent(QMouseEvent*) override; + void enterEvent(QEvent*) override; private: - bool _on; - int _x_circle, _y_circle; - int _height, _radius; - int _height_rect, _y_rect; - QPropertyAnimation *_anim = nullptr; + bool _on; + int _x_circle, _y_circle; + int _height, _radius; + int _height_rect, _y_rect; + QPropertyAnimation *_anim = nullptr; signals: void stateChanged(int new_state);