UI: Adjust Alert display with Developer UI (#1299)

* Adjust Alert Rect for Dev UI

* split out into SP classes

* fix includes

* formatting

---------

Co-authored-by: Jason Wen <haibin.wen3@gmail.com>
This commit is contained in:
Nayan
2025-10-09 15:26:14 -04:00
committed by GitHub
parent f429f3191f
commit e9f054b7ee
5 changed files with 127 additions and 9 deletions

View File

@@ -4,9 +4,6 @@
#include <map>
#include "selfdrive/ui/qt/util.h"
#ifdef SUNNYPILOT
#include "selfdrive/ui/sunnypilot/ui.h"
#endif
void OnroadAlerts::updateState(const UIState &s) {
Alert a = getAlert(*(s.sm), s.scene.started_frame);
@@ -76,12 +73,6 @@ void OnroadAlerts::paintEvent(QPaintEvent *event) {
}
QRect r = QRect(0 + margin, height() - h + margin, width() - margin*2, h - margin*2);
#ifdef SUNNYPILOT
const int dev_ui_info = uiStateSP()->scene.dev_ui_info;
const int adjustment = dev_ui_info > 1 && alert.size != cereal::SelfdriveState::AlertSize::FULL ? 30 : 0;
r = QRect(0 + margin, height() - h + margin - adjustment, width() - margin*2, h - margin*2);
#endif
QPainter p(this);
// draw background + gradient

View File

@@ -4,8 +4,10 @@
#ifdef SUNNYPILOT
#include "selfdrive/ui/sunnypilot/qt/onroad/annotated_camera.h"
#include "selfdrive/ui/sunnypilot/qt/onroad/alerts.h"
#define UIState UIStateSP
#define AnnotatedCameraWidget AnnotatedCameraWidgetSP
#define OnroadAlerts OnroadAlertsSP
#else
#include "selfdrive/ui/qt/onroad/annotated_camera.h"
#endif

View File

@@ -38,6 +38,7 @@ qt_src = [
"sunnypilot/qt/offroad/settings/trips_panel.cc",
"sunnypilot/qt/offroad/settings/vehicle_panel.cc",
"sunnypilot/qt/offroad/settings/visuals_panel.cc",
"sunnypilot/qt/onroad/alerts.cc",
"sunnypilot/qt/onroad/annotated_camera.cc",
"sunnypilot/qt/onroad/buttons.cc",
"sunnypilot/qt/onroad/developer_ui/developer_ui.cc",

View File

@@ -0,0 +1,102 @@
/**
* Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
*
* This file is part of sunnypilot and is licensed under the MIT License.
* See the LICENSE.md file in the root directory for more details.
*/
#include "selfdrive/ui/sunnypilot/qt/onroad/alerts.h"
#include <QPainter>
#include <map>
#include <QRect>
#include <QFont>
OnroadAlerts::Alert OnroadAlertsSP::getAlert(const SubMaster &sm, uint64_t started_frame) {
OnroadAlerts::Alert alert = OnroadAlerts::getAlert(sm, started_frame);
alert.text1.replace("openpilot", "sunnypilot");
alert.text2.replace("openpilot", "sunnypilot");
return alert;
}
void OnroadAlertsSP::paintEvent(QPaintEvent *event) {
if (alert.size == cereal::SelfdriveState::AlertSize::NONE) {
return;
} else if (alert.size == cereal::SelfdriveState::AlertSize::FULL) {
OnroadAlerts::paintEvent(event);
return;
}
static std::map<cereal::SelfdriveState::AlertSize, const int> alert_heights = {
{cereal::SelfdriveState::AlertSize::SMALL, 271},
{cereal::SelfdriveState::AlertSize::MID, 420}
};
int h = alert_heights[alert.size];
QPainter p(this);
QFont topFont;
QFont bottomFont;
QRect topTextBoundingRect;
QRect bottomTextBoundingRect;
QRect rect;
int margin = 40;
int radius = 30;
const int dev_ui_info = uiStateSP()->scene.dev_ui_info;
const int v_adjustment = dev_ui_info > 1 && alert.size != cereal::SelfdriveState::AlertSize::FULL ? 40 : 0;
const int h_adjustment = dev_ui_info > 0 && alert.size != cereal::SelfdriveState::AlertSize::FULL ? 230 : 0;
if (alert.size == cereal::SelfdriveState::AlertSize::SMALL) {
topFont = InterFont(74, QFont::DemiBold);
QFontMetrics fmTop(topFont);
topTextBoundingRect = fmTop.boundingRect(
QRect(0 + margin, height() - h + margin - v_adjustment, width() - margin * 2 - h_adjustment, 0), Qt::TextWordWrap,
alert.text1);
h = topTextBoundingRect.height();
rect = QRect(0 + margin, height() - h - margin * 2 - v_adjustment, width() - margin * 2 - h_adjustment, h + margin);
} else if (alert.size == cereal::SelfdriveState::AlertSize::MID) {
topFont = InterFont(88, QFont::Bold);
bottomFont = InterFont(66);
QFontMetrics fmTop(topFont);
QFontMetrics fmBotton(bottomFont);
topTextBoundingRect = fmTop.boundingRect(
QRect(0 + margin, height() - h + margin - v_adjustment, width() - margin * 2 - h_adjustment, 0), Qt::TextWordWrap,
alert.text1);
bottomTextBoundingRect = fmBotton.boundingRect(
QRect(0 + margin, height() - h + margin - v_adjustment + topTextBoundingRect.height(),
width() - margin * 2 - h_adjustment, 0), Qt::TextWordWrap, alert.text2);
h = topTextBoundingRect.height() + bottomTextBoundingRect.height() + margin * 2;
rect = QRect(0 + margin, height() - h - margin * 2 - v_adjustment, width() - margin * 2 - h_adjustment, h + margin);
}
// draw background + gradient
// draw background + gradient
p.setPen(Qt::NoPen);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.setBrush(QBrush(alert_colors[alert.status]));
p.drawRoundedRect(rect, radius, radius);
QLinearGradient g(0, rect.y(), 0, rect.bottom());
g.setColorAt(0, QColor::fromRgbF(0, 0, 0, 0.05));
g.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0.35));
p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
p.setBrush(QBrush(g));
p.drawRoundedRect(rect, radius, radius);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
// text
p.setPen(QColor(0xff, 0xff, 0xff));
p.setRenderHint(QPainter::TextAntialiasing);
p.setFont(topFont);
if (alert.size == cereal::SelfdriveState::AlertSize::SMALL) {
p.drawText(rect, Qt::AlignCenter | Qt::TextWordWrap, alert.text1);
} else if (alert.size == cereal::SelfdriveState::AlertSize::MID) {
QRect topText = QRect(rect.x(), rect.top() + margin, rect.width(), topTextBoundingRect.height());
p.drawText(topText, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap | Qt::AlignCenter, alert.text1);
p.setFont(bottomFont);
p.drawText(QRect(rect.x(), topText.bottom() + margin, rect.width(), bottomTextBoundingRect.height()),
Qt::AlignHCenter | Qt::TextWordWrap | Qt::AlignCenter, alert.text2);
}
}

View File

@@ -0,0 +1,22 @@
/**
* Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
*
* This file is part of sunnypilot and is licensed under the MIT License.
* See the LICENSE.md file in the root directory for more details.
*/
#pragma once
#include "selfdrive/ui/qt/onroad/alerts.h"
#include "selfdrive/ui/sunnypilot/ui.h"
class OnroadAlertsSP : public OnroadAlerts {
Q_OBJECT
public:
OnroadAlertsSP(QWidget *parent = 0) : OnroadAlerts(parent) {}
protected:
void paintEvent(QPaintEvent *) override;
Alert getAlert(const SubMaster &sm, uint64_t started_frame);
};