2021-06-09 03:09:30 +08:00
|
|
|
#include "selfdrive/ui/qt/widgets/toggle.h"
|
2020-11-27 17:55:07 +01:00
|
|
|
|
2021-06-16 15:08:02 +08:00
|
|
|
#include <QPainter>
|
|
|
|
|
|
2020-11-27 17:55:07 +01:00
|
|
|
Toggle::Toggle(QWidget *parent) : QAbstractButton(parent),
|
2020-12-01 20:12:54 +01:00
|
|
|
_height(80),
|
|
|
|
|
_height_rect(60),
|
2021-01-27 12:07:17 +01:00
|
|
|
on(false),
|
2020-11-27 17:55:07 +01:00
|
|
|
_anim(new QPropertyAnimation(this, "offset_circle", this))
|
|
|
|
|
{
|
|
|
|
|
_radius = _height / 2;
|
|
|
|
|
_x_circle = _radius;
|
|
|
|
|
_y_circle = _radius;
|
|
|
|
|
_y_rect = (_height - _height_rect)/2;
|
2021-02-01 19:46:03 +01:00
|
|
|
circleColor = QColor(0xffffff); // placeholder
|
|
|
|
|
green = QColor(0xffffff); // placeholder
|
|
|
|
|
setEnabled(true);
|
2020-11-27 17:55:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Toggle::paintEvent(QPaintEvent *e) {
|
|
|
|
|
this->setFixedHeight(_height);
|
|
|
|
|
QPainter p(this);
|
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
|
p.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
|
|
|
|
|
// Draw toggle background left
|
2021-02-01 19:46:03 +01:00
|
|
|
p.setBrush(green);
|
2020-11-27 17:55:07 +01:00
|
|
|
p.drawRoundedRect(QRect(0, _y_rect, _x_circle + _radius, _height_rect), _height_rect/2, _height_rect/2);
|
2020-12-24 14:10:43 -08:00
|
|
|
|
2020-11-27 17:55:07 +01:00
|
|
|
// Draw toggle background right
|
2020-12-24 14:10:43 -08:00
|
|
|
p.setBrush(QColor(0x393939));
|
|
|
|
|
p.drawRoundedRect(QRect(_x_circle - _radius, _y_rect, width() - (_x_circle - _radius), _height_rect), _height_rect/2, _height_rect/2);
|
2020-11-27 17:55:07 +01:00
|
|
|
|
|
|
|
|
// Draw toggle circle
|
2021-02-01 19:46:03 +01:00
|
|
|
p.setBrush(circleColor);
|
2020-11-27 17:55:07 +01:00
|
|
|
p.drawEllipse(QRectF(_x_circle - _radius, _y_circle - _radius, 2 * _radius, 2 * _radius));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Toggle::mouseReleaseEvent(QMouseEvent *e) {
|
2021-07-29 22:37:06 -07:00
|
|
|
if (!enabled) {
|
2021-02-01 19:46:03 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2021-01-27 12:07:17 +01:00
|
|
|
const int left = _radius;
|
|
|
|
|
const int right = width() - _radius;
|
2021-07-29 22:37:06 -07:00
|
|
|
if ((_x_circle != left && _x_circle != right) || !this->rect().contains(e->localPos().toPoint())) {
|
|
|
|
|
// If mouse release isn't in rect or animation is running, don't parse touch events
|
2021-01-27 12:07:17 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2020-11-27 17:55:07 +01:00
|
|
|
if (e->button() & Qt::LeftButton) {
|
|
|
|
|
togglePosition();
|
2021-01-27 12:07:17 +01:00
|
|
|
emit stateChanged(on);
|
2020-11-27 17:55:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 09:33:52 -08:00
|
|
|
void Toggle::togglePosition() {
|
2021-01-27 12:07:17 +01:00
|
|
|
on = !on;
|
2020-11-27 09:33:52 -08:00
|
|
|
const int left = _radius;
|
|
|
|
|
const int right = width() - _radius;
|
2021-01-27 12:07:17 +01:00
|
|
|
_anim->setStartValue(on ? left + immediateOffset : right - immediateOffset);
|
|
|
|
|
_anim->setEndValue(on ? right : left);
|
|
|
|
|
_anim->setDuration(animation_duration);
|
2020-11-27 09:33:52 -08:00
|
|
|
_anim->start();
|
2021-01-27 12:07:17 +01:00
|
|
|
repaint();
|
2020-11-27 17:55:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Toggle::enterEvent(QEvent *e) {
|
|
|
|
|
QAbstractButton::enterEvent(e);
|
|
|
|
|
}
|
2021-02-01 19:46:03 +01:00
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
bool Toggle::getEnabled() {
|
2021-02-01 19:46:03 +01:00
|
|
|
return enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 16:17:52 +08:00
|
|
|
void Toggle::setEnabled(bool value) {
|
2021-02-01 19:46:03 +01:00
|
|
|
enabled = value;
|
2021-07-30 16:21:42 -07:00
|
|
|
if (value) {
|
2021-02-01 19:46:03 +01:00
|
|
|
circleColor.setRgb(0xfafafa);
|
|
|
|
|
green.setRgb(0x33ab4c);
|
2021-07-30 16:21:42 -07:00
|
|
|
} else {
|
2021-02-01 19:46:03 +01:00
|
|
|
circleColor.setRgb(0x888888);
|
|
|
|
|
green.setRgb(0x227722);
|
|
|
|
|
}
|
2021-07-29 22:37:06 -07:00
|
|
|
}
|