ui: singleton uistate (#22789)

* singleton uistate

* merge master

* merge master

* merge master

* connect uistate in sidebar

* move to device
old-commit-hash: 2483fc5d5d
commatwo_master
Dean Lee 3 years ago committed by GitHub
parent 14c8689bab
commit 1fc1819c2b
  1. 3
      selfdrive/ui/SConscript
  2. 6
      selfdrive/ui/qt/home.cc
  3. 4
      selfdrive/ui/qt/home.h
  4. 6
      selfdrive/ui/qt/maps/map.cc
  5. 14
      selfdrive/ui/qt/offroad/settings.cc
  6. 1
      selfdrive/ui/qt/offroad/settings.h
  7. 14
      selfdrive/ui/qt/onroad.cc
  8. 4
      selfdrive/ui/qt/onroad.h
  9. 2
      selfdrive/ui/qt/request_repeater.cc
  10. 2
      selfdrive/ui/qt/sidebar.cc
  11. 4
      selfdrive/ui/qt/widgets/prime.cc
  12. 9
      selfdrive/ui/qt/window.cc
  13. 1
      selfdrive/ui/qt/window.h
  14. 37
      selfdrive/ui/ui.cc
  15. 19
      selfdrive/ui/ui.h

@ -117,8 +117,7 @@ if arch in ['x86_64', 'Darwin'] or GetOption('extras'):
replay_lib = qt_env.Library("qt_replay", replay_lib_src, LIBS=base_libs) replay_lib = qt_env.Library("qt_replay", replay_lib_src, LIBS=base_libs)
replay_libs = [replay_lib, 'avutil', 'avcodec', 'avformat', 'bz2', 'curl', 'yuv'] + qt_libs replay_libs = [replay_lib, 'avutil', 'avcodec', 'avformat', 'bz2', 'curl', 'yuv'] + qt_libs
qt_env.Program("replay/replay", ["replay/main.cc"], LIBS=replay_libs) qt_env.Program("replay/replay", ["replay/main.cc"], LIBS=replay_libs)
qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11', 'zmq', 'visionipc', 'messaging'])
qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11'])
if GetOption('test'): if GetOption('test'):
qt_env.Program('replay/tests/test_replay', ['replay/tests/test_runner.cc', 'replay/tests/test_replay.cc'], LIBS=[replay_libs]) qt_env.Program('replay/tests/test_replay', ['replay/tests/test_runner.cc', 'replay/tests/test_replay.cc'], LIBS=[replay_libs])

@ -19,7 +19,6 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {
sidebar = new Sidebar(this); sidebar = new Sidebar(this);
main_layout->addWidget(sidebar); main_layout->addWidget(sidebar);
QObject::connect(this, &HomeWindow::update, sidebar, &Sidebar::updateState);
QObject::connect(sidebar, &Sidebar::openSettings, this, &HomeWindow::openSettings); QObject::connect(sidebar, &Sidebar::openSettings, this, &HomeWindow::openSettings);
slayout = new QStackedLayout(); slayout = new QStackedLayout();
@ -31,15 +30,13 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {
onroad = new OnroadWindow(this); onroad = new OnroadWindow(this);
slayout->addWidget(onroad); slayout->addWidget(onroad);
QObject::connect(this, &HomeWindow::update, onroad, &OnroadWindow::updateStateSignal);
QObject::connect(this, &HomeWindow::offroadTransitionSignal, onroad, &OnroadWindow::offroadTransitionSignal);
driver_view = new DriverViewWindow(this); driver_view = new DriverViewWindow(this);
connect(driver_view, &DriverViewWindow::done, [=] { connect(driver_view, &DriverViewWindow::done, [=] {
showDriverView(false); showDriverView(false);
}); });
slayout->addWidget(driver_view); slayout->addWidget(driver_view);
setAttribute(Qt::WA_NoSystemBackground); setAttribute(Qt::WA_NoSystemBackground);
QObject::connect(uiState(), &UIState::offroadTransition, this, &HomeWindow::offroadTransition);
} }
void HomeWindow::showSidebar(bool show) { void HomeWindow::showSidebar(bool show) {
@ -53,7 +50,6 @@ void HomeWindow::offroadTransition(bool offroad) {
} else { } else {
slayout->setCurrentWidget(onroad); slayout->setCurrentWidget(onroad);
} }
emit offroadTransitionSignal(offroad);
} }
void HomeWindow::showDriverView(bool show) { void HomeWindow::showDriverView(bool show) {

@ -43,10 +43,6 @@ signals:
void openSettings(); void openSettings();
void closeSettings(); void closeSettings();
// forwarded signals
void update(const UIState &s);
void offroadTransitionSignal(bool offroad);
public slots: public slots:
void offroadTransition(bool offroad); void offroadTransition(bool offroad);
void showDriverView(bool show); void showDriverView(bool show);

@ -106,7 +106,7 @@ void MapWindow::initLayers() {
} }
void MapWindow::timerUpdate() { void MapWindow::timerUpdate() {
if (!QUIState::ui_state.scene.started) { if (!uiState()->scene.started) {
return; return;
} }
@ -387,7 +387,7 @@ void MapInstructions::updateDistance(float d) {
d = std::max(d, 0.0f); d = std::max(d, 0.0f);
QString distance_str; QString distance_str;
if (QUIState::ui_state.scene.is_metric) { if (uiState()->scene.is_metric) {
if (d > 500) { if (d > 500) {
distance_str.setNum(d / 1000, 'f', 1); distance_str.setNum(d / 1000, 'f', 1);
distance_str += " km"; distance_str += " km";
@ -620,7 +620,7 @@ void MapETA::updateETA(float s, float s_typical, float d) {
// Distance // Distance
QString distance_str; QString distance_str;
float num = 0; float num = 0;
if (QUIState::ui_state.scene.is_metric) { if (uiState()->scene.is_metric) {
num = d / 1000.0; num = d / 1000.0;
distance_unit->setText("km"); distance_unit->setText("km");
} else { } else {

@ -98,7 +98,7 @@ TogglesPanel::TogglesPanel(SettingsWindow *parent) : ListWidget(parent) {
bool locked = params.getBool((param + "Lock").toStdString()); bool locked = params.getBool((param + "Lock").toStdString());
toggle->setEnabled(!locked); toggle->setEnabled(!locked);
if (!locked) { if (!locked) {
connect(parent, &SettingsWindow::offroadTransition, toggle, &ParamControl::setEnabled); connect(uiState(), &UIState::offroadTransition, toggle, &ParamControl::setEnabled);
} }
addItem(toggle); addItem(toggle);
} }
@ -144,7 +144,7 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) {
addItem(regulatoryBtn); addItem(regulatoryBtn);
} }
QObject::connect(parent, &SettingsWindow::offroadTransition, [=](bool offroad) { QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) {
for (auto btn : findChildren<ButtonControl *>()) { for (auto btn : findChildren<ButtonControl *>()) {
btn->setEnabled(offroad); btn->setEnabled(offroad);
} }
@ -198,10 +198,10 @@ void DevicePanel::updateCalibDescription() {
} }
void DevicePanel::reboot() { void DevicePanel::reboot() {
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) { if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
if (ConfirmationDialog::confirm("Are you sure you want to reboot?", this)) { if (ConfirmationDialog::confirm("Are you sure you want to reboot?", this)) {
// Check engaged again in case it changed while the dialog was open // Check engaged again in case it changed while the dialog was open
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) { if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
Params().putBool("DoReboot", true); Params().putBool("DoReboot", true);
} }
} }
@ -211,10 +211,10 @@ void DevicePanel::reboot() {
} }
void DevicePanel::poweroff() { void DevicePanel::poweroff() {
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) { if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
if (ConfirmationDialog::confirm("Are you sure you want to power off?", this)) { if (ConfirmationDialog::confirm("Are you sure you want to power off?", this)) {
// Check engaged again in case it changed while the dialog was open // Check engaged again in case it changed while the dialog was open
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) { if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
Params().putBool("DoShutdown", true); Params().putBool("DoShutdown", true);
} }
} }
@ -247,7 +247,7 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) {
params.putBool("DoUninstall", true); params.putBool("DoUninstall", true);
} }
}); });
connect(parent, SIGNAL(offroadTransition(bool)), uninstallBtn, SLOT(setEnabled(bool))); connect(uiState(), &UIState::offroadTransition, uninstallBtn, &QPushButton::setEnabled);
QWidget *widgets[] = {versionLbl, lastUpdateLbl, updateBtn, gitBranchLbl, gitCommitLbl, osVersionLbl, uninstallBtn}; QWidget *widgets[] = {versionLbl, lastUpdateLbl, updateBtn, gitBranchLbl, gitCommitLbl, osVersionLbl, uninstallBtn};
for (QWidget* w : widgets) { for (QWidget* w : widgets) {

@ -24,7 +24,6 @@ protected:
signals: signals:
void closeSettings(); void closeSettings();
void offroadTransition(bool offroad);
void reviewTrainingGuide(); void reviewTrainingGuide();
void showDriverView(); void showDriverView();

@ -41,8 +41,8 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) {
alerts->raise(); alerts->raise();
setAttribute(Qt::WA_OpaquePaintEvent); setAttribute(Qt::WA_OpaquePaintEvent);
QObject::connect(this, &OnroadWindow::updateStateSignal, this, &OnroadWindow::updateState); QObject::connect(uiState(), &UIState::uiUpdate, this, &OnroadWindow::updateState);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, this, &OnroadWindow::offroadTransition); QObject::connect(uiState(), &UIState::offroadTransition, this, &OnroadWindow::offroadTransition);
} }
void OnroadWindow::updateState(const UIState &s) { void OnroadWindow::updateState(const UIState &s) {
@ -76,10 +76,10 @@ void OnroadWindow::mousePressEvent(QMouseEvent* e) {
void OnroadWindow::offroadTransition(bool offroad) { void OnroadWindow::offroadTransition(bool offroad) {
#ifdef ENABLE_MAPS #ifdef ENABLE_MAPS
if (!offroad) { if (!offroad) {
if (map == nullptr && (QUIState::ui_state.has_prime || !MAPBOX_TOKEN.isEmpty())) { if (map == nullptr && (uiState()->has_prime || !MAPBOX_TOKEN.isEmpty())) {
MapWindow * m = new MapWindow(get_mapbox_settings()); MapWindow * m = new MapWindow(get_mapbox_settings());
m->setFixedWidth(topWidget(this)->width() / 2); m->setFixedWidth(topWidget(this)->width() / 2);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, m, &MapWindow::offroadTransition); QObject::connect(uiState(), &UIState::offroadTransition, m, &MapWindow::offroadTransition);
split->addWidget(m, 0, Qt::AlignRight); split->addWidget(m, 0, Qt::AlignRight);
map = m; map = m;
} }
@ -274,7 +274,7 @@ void NvgWindow::initializeGL() {
void NvgWindow::updateFrameMat(int w, int h) { void NvgWindow::updateFrameMat(int w, int h) {
CameraViewWidget::updateFrameMat(w, h); CameraViewWidget::updateFrameMat(w, h);
UIState *s = &QUIState::ui_state; UIState *s = uiState();
s->fb_w = w; s->fb_w = w;
s->fb_h = h; s->fb_h = h;
auto intrinsic_matrix = s->wide_camera ? ecam_intrinsic_matrix : fcam_intrinsic_matrix; auto intrinsic_matrix = s->wide_camera ? ecam_intrinsic_matrix : fcam_intrinsic_matrix;
@ -348,7 +348,7 @@ void NvgWindow::drawLead(QPainter &painter, const cereal::ModelDataV2::LeadDataV
void NvgWindow::paintGL() { void NvgWindow::paintGL() {
CameraViewWidget::paintGL(); CameraViewWidget::paintGL();
UIState *s = &QUIState::ui_state; UIState *s = uiState();
if (s->scene.world_objects_visible) { if (s->scene.world_objects_visible) {
QPainter painter(this); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
@ -379,6 +379,6 @@ void NvgWindow::paintGL() {
void NvgWindow::showEvent(QShowEvent *event) { void NvgWindow::showEvent(QShowEvent *event) {
CameraViewWidget::showEvent(event); CameraViewWidget::showEvent(event);
ui_update_params(&QUIState::ui_state); ui_update_params(uiState());
prev_draw_t = millis_since_boot(); prev_draw_t = millis_since_boot();
} }

@ -97,10 +97,6 @@ private:
QWidget *map = nullptr; QWidget *map = nullptr;
QHBoxLayout* split; QHBoxLayout* split;
signals:
void updateStateSignal(const UIState &s);
void offroadTransitionSignal(bool offroad);
private slots: private slots:
void offroadTransition(bool offroad); void offroadTransition(bool offroad);
void updateState(const UIState &s); void updateState(const UIState &s);

@ -5,7 +5,7 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
timer = new QTimer(this); timer = new QTimer(this);
timer->setTimerType(Qt::VeryCoarseTimer); timer->setTimerType(Qt::VeryCoarseTimer);
QObject::connect(timer, &QTimer::timeout, [=]() { QObject::connect(timer, &QTimer::timeout, [=]() {
if ((!QUIState::ui_state.scene.started || while_onroad) && QUIState::ui_state.awake && !active()) { if ((!uiState()->scene.started || while_onroad) && uiState()->awake && !active()) {
sendRequest(requestURL); sendRequest(requestURL);
} }
}); });

@ -34,6 +34,8 @@ Sidebar::Sidebar(QWidget *parent) : QFrame(parent) {
setAttribute(Qt::WA_OpaquePaintEvent); setAttribute(Qt::WA_OpaquePaintEvent);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
setFixedWidth(300); setFixedWidth(300);
QObject::connect(uiState(), &UIState::uiUpdate, this, &Sidebar::updateState);
} }
void Sidebar::mouseReleaseEvent(QMouseEvent *event) { void Sidebar::mouseReleaseEvent(QMouseEvent *event) {

@ -314,8 +314,8 @@ void SetupWidget::replyFinished(const QString &response, bool success) {
bool prime = json["prime"].toBool(); bool prime = json["prime"].toBool();
if (QUIState::ui_state.has_prime != prime) { if (uiState()->has_prime != prime) {
QUIState::ui_state.has_prime = prime; uiState()->has_prime = prime;
Params().putBool("HasPrime", prime); Params().putBool("HasPrime", prime);
} }

@ -12,14 +12,10 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
main_layout->addWidget(homeWindow); main_layout->addWidget(homeWindow);
QObject::connect(homeWindow, &HomeWindow::openSettings, this, &MainWindow::openSettings); QObject::connect(homeWindow, &HomeWindow::openSettings, this, &MainWindow::openSettings);
QObject::connect(homeWindow, &HomeWindow::closeSettings, this, &MainWindow::closeSettings); QObject::connect(homeWindow, &HomeWindow::closeSettings, this, &MainWindow::closeSettings);
QObject::connect(&qs, &QUIState::uiUpdate, homeWindow, &HomeWindow::update);
QObject::connect(&qs, &QUIState::offroadTransition, homeWindow, &HomeWindow::offroadTransition);
QObject::connect(&qs, &QUIState::offroadTransition, homeWindow, &HomeWindow::offroadTransitionSignal);
settingsWindow = new SettingsWindow(this); settingsWindow = new SettingsWindow(this);
main_layout->addWidget(settingsWindow); main_layout->addWidget(settingsWindow);
QObject::connect(settingsWindow, &SettingsWindow::closeSettings, this, &MainWindow::closeSettings); QObject::connect(settingsWindow, &SettingsWindow::closeSettings, this, &MainWindow::closeSettings);
QObject::connect(&qs, &QUIState::offroadTransition, settingsWindow, &SettingsWindow::offroadTransition);
QObject::connect(settingsWindow, &SettingsWindow::reviewTrainingGuide, [=]() { QObject::connect(settingsWindow, &SettingsWindow::reviewTrainingGuide, [=]() {
onboardingWindow->showTrainingGuide(); onboardingWindow->showTrainingGuide();
main_layout->setCurrentWidget(onboardingWindow); main_layout->setCurrentWidget(onboardingWindow);
@ -37,8 +33,7 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
main_layout->setCurrentWidget(onboardingWindow); main_layout->setCurrentWidget(onboardingWindow);
} }
QObject::connect(&qs, &QUIState::uiUpdate, &device, &Device::update); QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) {
QObject::connect(&qs, &QUIState::offroadTransition, [=](bool offroad) {
if (!offroad) { if (!offroad) {
closeSettings(); closeSettings();
} }
@ -79,7 +74,7 @@ void MainWindow::openSettings() {
void MainWindow::closeSettings() { void MainWindow::closeSettings() {
main_layout->setCurrentWidget(homeWindow); main_layout->setCurrentWidget(homeWindow);
if (QUIState::ui_state.scene.started) { if (uiState()->scene.started) {
homeWindow->showSidebar(false); homeWindow->showSidebar(false);
} }
} }

@ -19,7 +19,6 @@ private:
void closeSettings(); void closeSettings();
Device device; Device device;
QUIState qs;
QStackedLayout *main_layout; QStackedLayout *main_layout;
HomeWindow *homeWindow; HomeWindow *homeWindow;

@ -218,41 +218,43 @@ static void update_status(UIState *s) {
} }
QUIState::QUIState(QObject *parent) : QObject(parent) { UIState::UIState(QObject *parent) : QObject(parent) {
ui_state.sm = std::make_unique<SubMaster, const std::initializer_list<const char *>>({ sm = std::make_unique<SubMaster, const std::initializer_list<const char *>>({
"modelV2", "controlsState", "liveCalibration", "radarState", "deviceState", "roadCameraState", "modelV2", "controlsState", "liveCalibration", "radarState", "deviceState", "roadCameraState",
"pandaStates", "carParams", "driverMonitoringState", "sensorEvents", "carState", "liveLocationKalman", "pandaStates", "carParams", "driverMonitoringState", "sensorEvents", "carState", "liveLocationKalman",
}); });
Params params; Params params;
ui_state.wide_camera = Hardware::TICI() ? params.getBool("EnableWideCamera") : false; wide_camera = Hardware::TICI() ? params.getBool("EnableWideCamera") : false;
ui_state.has_prime = params.getBool("HasPrime"); has_prime = params.getBool("HasPrime");
// update timer // update timer
timer = new QTimer(this); timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, &QUIState::update); QObject::connect(timer, &QTimer::timeout, this, &UIState::update);
timer->start(1000 / UI_FREQ); timer->start(1000 / UI_FREQ);
} }
void QUIState::update() { void UIState::update() {
update_sockets(&ui_state); update_sockets(this);
update_state(&ui_state); update_state(this);
update_status(&ui_state); update_status(this);
if (ui_state.scene.started != started_prev || ui_state.sm->frame == 1) { if (scene.started != started_prev || sm->frame == 1) {
started_prev = ui_state.scene.started; started_prev = scene.started;
emit offroadTransition(!ui_state.scene.started); emit offroadTransition(!scene.started);
} }
if (ui_state.sm->frame % UI_FREQ == 0) { if (sm->frame % UI_FREQ == 0) {
watchdog_kick(); watchdog_kick();
} }
emit uiUpdate(ui_state); emit uiUpdate(*this);
} }
Device::Device(QObject *parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKLIGHT_TS, BACKLIGHT_DT), QObject(parent) { Device::Device(QObject *parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKLIGHT_TS, BACKLIGHT_DT), QObject(parent) {
setAwake(true); setAwake(true);
resetInteractiveTimout(); resetInteractiveTimout();
QObject::connect(uiState(), &UIState::uiUpdate, this, &Device::update);
} }
void Device::update(const UIState &s) { void Device::update(const UIState &s) {
@ -260,7 +262,7 @@ void Device::update(const UIState &s) {
updateWakefulness(s); updateWakefulness(s);
// TODO: remove from UIState and use signals // TODO: remove from UIState and use signals
QUIState::ui_state.awake = awake; uiState()->awake = awake;
} }
void Device::setAwake(bool on) { void Device::setAwake(bool on) {
@ -329,3 +331,8 @@ void Device::updateWakefulness(const UIState &s) {
setAwake(s.scene.ignition || interactive_timeout > 0); setAwake(s.scene.ignition || interactive_timeout > 0);
} }
UIState *uiState() {
static UIState ui_state;
return &ui_state;
}

@ -101,7 +101,12 @@ typedef struct UIScene {
uint64_t started_frame; uint64_t started_frame;
} UIScene; } UIScene;
typedef struct UIState { class UIState : public QObject {
Q_OBJECT
public:
UIState(QObject* parent = 0);
int fb_w = 0, fb_h = 0; int fb_w = 0, fb_h = 0;
std::unique_ptr<SubMaster> sm; std::unique_ptr<SubMaster> sm;
@ -114,17 +119,6 @@ typedef struct UIState {
QTransform car_space_transform; QTransform car_space_transform;
bool wide_camera; bool wide_camera;
} UIState;
class QUIState : public QObject {
Q_OBJECT
public:
QUIState(QObject* parent = 0);
// TODO: get rid of this, only use signal
inline static UIState ui_state = {0};
signals: signals:
void uiUpdate(const UIState &s); void uiUpdate(const UIState &s);
@ -138,6 +132,7 @@ private:
bool started_prev = true; bool started_prev = true;
}; };
UIState *uiState();
// device management class // device management class

Loading…
Cancel
Save