ui: singleton uistate (#22789)

* singleton uistate

* merge master

* merge master

* merge master

* connect uistate in sidebar

* move to device
pull/23183/head
Dean Lee 2021-12-15 14:28:12 +08:00 committed by GitHub
parent 345fe48338
commit 2483fc5d5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 55 additions and 71 deletions

View File

@ -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_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("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11'])
qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11', 'zmq', 'visionipc', 'messaging'])
if GetOption('test'):
qt_env.Program('replay/tests/test_replay', ['replay/tests/test_runner.cc', 'replay/tests/test_replay.cc'], LIBS=[replay_libs])

View File

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

View File

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

View File

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

View File

@ -98,7 +98,7 @@ TogglesPanel::TogglesPanel(SettingsWindow *parent) : ListWidget(parent) {
bool locked = params.getBool((param + "Lock").toStdString());
toggle->setEnabled(!locked);
if (!locked) {
connect(parent, &SettingsWindow::offroadTransition, toggle, &ParamControl::setEnabled);
connect(uiState(), &UIState::offroadTransition, toggle, &ParamControl::setEnabled);
}
addItem(toggle);
}
@ -144,7 +144,7 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) {
addItem(regulatoryBtn);
}
QObject::connect(parent, &SettingsWindow::offroadTransition, [=](bool offroad) {
QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) {
for (auto btn : findChildren<ButtonControl *>()) {
btn->setEnabled(offroad);
}
@ -198,10 +198,10 @@ void DevicePanel::updateCalibDescription() {
}
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)) {
// 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);
}
}
@ -211,10 +211,10 @@ void DevicePanel::reboot() {
}
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)) {
// 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);
}
}
@ -247,7 +247,7 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) {
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};
for (QWidget* w : widgets) {

View File

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

View File

@ -41,8 +41,8 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) {
alerts->raise();
setAttribute(Qt::WA_OpaquePaintEvent);
QObject::connect(this, &OnroadWindow::updateStateSignal, this, &OnroadWindow::updateState);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, this, &OnroadWindow::offroadTransition);
QObject::connect(uiState(), &UIState::uiUpdate, this, &OnroadWindow::updateState);
QObject::connect(uiState(), &UIState::offroadTransition, this, &OnroadWindow::offroadTransition);
}
void OnroadWindow::updateState(const UIState &s) {
@ -76,10 +76,10 @@ void OnroadWindow::mousePressEvent(QMouseEvent* e) {
void OnroadWindow::offroadTransition(bool offroad) {
#ifdef ENABLE_MAPS
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());
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);
map = m;
}
@ -274,7 +274,7 @@ void NvgWindow::initializeGL() {
void NvgWindow::updateFrameMat(int w, int h) {
CameraViewWidget::updateFrameMat(w, h);
UIState *s = &QUIState::ui_state;
UIState *s = uiState();
s->fb_w = w;
s->fb_h = h;
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() {
CameraViewWidget::paintGL();
UIState *s = &QUIState::ui_state;
UIState *s = uiState();
if (s->scene.world_objects_visible) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
@ -379,6 +379,6 @@ void NvgWindow::paintGL() {
void NvgWindow::showEvent(QShowEvent *event) {
CameraViewWidget::showEvent(event);
ui_update_params(&QUIState::ui_state);
ui_update_params(uiState());
prev_draw_t = millis_since_boot();
}

View File

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

View File

@ -5,7 +5,7 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
timer = new QTimer(this);
timer->setTimerType(Qt::VeryCoarseTimer);
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);
}
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -101,7 +101,12 @@ typedef struct UIScene {
uint64_t started_frame;
} UIScene;
typedef struct UIState {
class UIState : public QObject {
Q_OBJECT
public:
UIState(QObject* parent = 0);
int fb_w = 0, fb_h = 0;
std::unique_ptr<SubMaster> sm;
@ -114,17 +119,6 @@ typedef struct UIState {
QTransform car_space_transform;
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:
void uiUpdate(const UIState &s);
@ -138,6 +132,7 @@ private:
bool started_prev = true;
};
UIState *uiState();
// device management class