From 1fcbe5325333e44c655fdd682e381d052a7c8010 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 17 Oct 2025 20:37:57 +0200 Subject: [PATCH] client move to QMessageBox from networkerror --- client/CMakeLists.txt | 5 +-- client/mainwindow.cpp | 16 ++++++--- client/mainwindow.hpp | 9 ++--- client/networkerror.cpp | 19 ----------- client/networkerror.hpp | 20 ------------ client/networkerror.ui | 69 --------------------------------------- client/simulator_base.cpp | 16 ++++++--- client/simulator_base.hpp | 4 +-- 8 files changed, 28 insertions(+), 130 deletions(-) delete mode 100644 client/networkerror.cpp delete mode 100644 client/networkerror.hpp delete mode 100644 client/networkerror.ui diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index feba4cd..307cdf4 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -16,9 +16,6 @@ set(PROJECT_SOURCES mainwindow.cpp mainwindow.hpp mainwindow.ui - networkerror.cpp - networkerror.hpp - networkerror.ui simulator_base.cpp simulator_base.hpp web_socket_wrapper.cpp @@ -120,4 +117,4 @@ if (NOT EMSCRIPTEN) endforeach() else() file(GLOB_RECURSE sims "simulators/*") -endif() \ No newline at end of file +endif() diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index 0305ff1..69acf58 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -2,14 +2,15 @@ #include "ui_mainwindow.h" #include #include +#include #include #include typedef Simulator_base *(*Get_UI)(Web_socket_wrapper *, QWidget *); MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow), m_web_socket(new Web_socket_wrapper(ws_url)), - m_error_dialog(new NetworkError(this)) + : QMainWindow(parent), ui(new Ui::MainWindow), m_error(new QMessageBox()), + m_web_socket(new Web_socket_wrapper(ws_url)) { ui->setupUi(this); QLibrary lib; @@ -30,17 +31,22 @@ MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent) m_sims.insert(std::make_pair(widget->name(), widget)); lib.unload(); } + // Insert debug sims here // INSERT_SIMULATOR(SIM_NAME); - m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); + m_error->setIcon(QMessageBox::Critical); + m_error->setWindowTitle("Critical"); + m_error->setText("Connection timed out\nCheck if the server is running"); + QPushButton *exit_app = m_error->addButton("Exit Application", QMessageBox::AcceptRole); + m_error->addButton("Close", QMessageBox::RejectRole); + connect(exit_app, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection); ui->connection->setText("Connected to " + ws_url.toString()); // Default sim if (!sim.isEmpty()) default_sim(sim); connect(ui->pushButton, &QPushButton::clicked, this, [=, this] { open_sim(ui->comboBox->currentText()); }); - connect(this, &MainWindow::quit, this, &QCoreApplication::quit, Qt::QueuedConnection); } MainWindow::~MainWindow() @@ -54,7 +60,7 @@ void MainWindow::setup_cb(void) connect(m_web_socket.get(), &Web_socket_wrapper::on_connected, this, [=, this] { m_web_socket->send_command(Web_socket_wrapper::Command::get_simulators); }); connect(m_web_socket.get(), &Web_socket_wrapper::on_closed, this, [=, this] { - m_error_dialog->open(); + m_error->open(); ui->connection->setText("Could not connect to the server"); }); } diff --git a/client/mainwindow.hpp b/client/mainwindow.hpp index bc72d8f..d493867 100644 --- a/client/mainwindow.hpp +++ b/client/mainwindow.hpp @@ -1,5 +1,4 @@ #pragma once -#include "networkerror.hpp" #include "simulator_base.hpp" #include "web_socket_wrapper.hpp" #include @@ -8,6 +7,8 @@ #define INSERT_SIMULATOR(type) \ m_sims.insert(std::make_pair(type().sim_name(), std::make_unique(m_web_socket.get(), this))) +Q_FORWARD_DECLARE_OBJC_CLASS(QMessageBox); + namespace Ui { class MainWindow; @@ -28,9 +29,6 @@ class MainWindow : public QMainWindow void open_sim(QString sim_name); void setup_cb(void); - signals: - void quit(void); - private: void showEvent(QShowEvent *event); void hideEvent(QHideEvent *event); @@ -40,9 +38,8 @@ class MainWindow : public QMainWindow bool m_default_sim = false; QString m_selected_sim_name; QMainWindow *m_selected_sim; + QMessageBox *m_error; Ui::MainWindow *ui; std::unique_ptr m_web_socket; - std::unique_ptr m_error_dialog; - std::map> m_sims; }; diff --git a/client/networkerror.cpp b/client/networkerror.cpp deleted file mode 100644 index c1c517b..0000000 --- a/client/networkerror.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "networkerror.hpp" -#include "ui_networkerror.h" - -NetworkError::NetworkError(QWidget *parent) : QDialog(parent), ui(new Ui::NetworkError) -{ - ui->setupUi(this); - connect(ui->exit, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection); - connect(ui->close, &QPushButton::clicked, this, [=, this] { this->close(); }); -} - -void NetworkError::set_error(QString error) -{ - ui->Error->setText(error); -} - -NetworkError::~NetworkError() -{ - delete ui; -} diff --git a/client/networkerror.hpp b/client/networkerror.hpp deleted file mode 100644 index 8ea2d0f..0000000 --- a/client/networkerror.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include - -namespace Ui -{ -class NetworkError; -} - -class NetworkError : public QDialog -{ - Q_OBJECT - - public: - explicit NetworkError(QWidget *parent = nullptr); - void set_error(QString error); - ~NetworkError(); - - private: - Ui::NetworkError *ui; -}; diff --git a/client/networkerror.ui b/client/networkerror.ui deleted file mode 100644 index e9a642a..0000000 --- a/client/networkerror.ui +++ /dev/null @@ -1,69 +0,0 @@ - - - NetworkError - - - - 0 - 0 - 400 - 300 - - - - Error - - - - - - - - - 16 - true - - - - Oops, an error occured - - - Qt::AlignmentFlag::AlignCenter - - - - - - - Error... - - - Qt::AlignmentFlag::AlignCenter - - - - - - - - - Close - - - - - - - Exit Application - - - - - - - - - - - - diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 46d2147..0e1ce57 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -2,22 +2,28 @@ #include "can_transceive.hpp" #include "helpers.hpp" #include "led.hpp" -#include "networkerror.hpp" #include "plot_wrapper.hpp" #include "web_socket_wrapper.hpp" +#include #include Simulator_base::Simulator_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) - : QMainWindow{parent}, m_error_dialog(new NetworkError(this)), m_timer_update(new QTimer()), - m_web_socket(web_socket), m_name(name) + : QMainWindow{parent}, m_timer_update(new QTimer()), m_error(new QMessageBox(this)), m_web_socket(web_socket), + m_name(name) { if (windowTitle() != m_name) { setWindowTitle(m_name); } this->setWindowState(Qt::WindowMaximized); - m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); m_ui_lookup.reserve(40); + + m_error->setIcon(QMessageBox::Critical); + m_error->setWindowTitle("Critical"); + m_error->setText("Connection timed out\nCheck if the server is running"); + QPushButton *exit_app = m_error->addButton("Exit Application", QMessageBox::AcceptRole); + m_error->addButton("Close", QMessageBox::RejectRole); + connect(exit_app, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection); } QWidget *Simulator_base::id_to_ui(size_t id) @@ -38,7 +44,7 @@ void Simulator_base::showEvent(QShowEvent *event) connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); }); connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=, this](json &j) { on_event_cb(j); }); - connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this] { m_error_dialog->open(); }); + connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this] { m_error->open(); }); m_web_socket->inhibit_events(true); m_web_socket->send_command(Web_socket_wrapper::Command::get_UI_elements); diff --git a/client/simulator_base.hpp b/client/simulator_base.hpp index f34ac0f..3176a22 100644 --- a/client/simulator_base.hpp +++ b/client/simulator_base.hpp @@ -6,8 +6,8 @@ Q_FORWARD_DECLARE_OBJC_CLASS(Web_socket_wrapper); Q_FORWARD_DECLARE_OBJC_CLASS(QTimer); -Q_FORWARD_DECLARE_OBJC_CLASS(NetworkError); Q_FORWARD_DECLARE_OBJC_CLASS(QLabel); +Q_FORWARD_DECLARE_OBJC_CLASS(QMessageBox); using json = nlohmann::json; @@ -84,7 +84,7 @@ class Simulator_base : public QMainWindow QTimer *m_timer_update; uint32_t m_refresh_rate; - NetworkError *m_error_dialog; + QMessageBox *m_error; bool m_open = false; bool m_first_load = true; std::vector m_ui_lookup;