From 92b88ca522d77b82c40d89efc21d611bfb22a225 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 20 Mar 2025 16:35:18 +0100 Subject: [PATCH] client move sims from {} based insert to define based insert as in the server --- client/mainwindow.cpp | 23 +++++++++++------------ client/mainwindow.h | 9 ++++++--- client/simulator_base.cpp | 9 +++++++-- client/simulator_base.h | 4 +++- client/simulators/can_debugger.cpp | 2 +- client/simulators/can_debugger.hpp | 2 +- client/simulators/test.cpp | 2 +- client/simulators/test.hpp | 2 +- 8 files changed, 31 insertions(+), 22 deletions(-) diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index 68b7d93..35faa4c 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -8,14 +8,15 @@ MainWindow::MainWindow(QUrl ws_url, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_web_socket(new Web_socket_wrapper(ws_url)), - m_error_dialog(new NetworkError(this)), m_sims{{"Cable Tester", new Cable_Tester(m_web_socket, this)}, - {"Can Debugger", new Can_Debugger(m_web_socket, this)}, - {"Scope Mux Tester", new Scope_Mux_Tester(m_web_socket, this)}, - {"CVS I10", new CVS_I10(m_web_socket, this)}, - {"Test Sim", new Test_Sim(m_web_socket, this)}} + m_error_dialog(new NetworkError(this)) { ui->setupUi(this); this->setWindowTitle("Simulator Selector"); + INSERT_SIMULATOR(Cable_Tester); + INSERT_SIMULATOR(Can_Debugger); + INSERT_SIMULATOR(Scope_Mux_Tester); + INSERT_SIMULATOR(CVS_I10); + INSERT_SIMULATOR(Test_Sim); m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); ui->connection->setText("Connected to " + ws_url.toString()); @@ -29,16 +30,14 @@ MainWindow::MainWindow(QUrl ws_url, QWidget *parent) MainWindow::~MainWindow() { delete ui; - delete m_web_socket; - delete m_error_dialog; } void MainWindow::setup_cb(void) { - 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_connected, this, + connect(m_web_socket.get(), &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); }); + 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, &Web_socket_wrapper::on_closed, this, [=, this] { + connect(m_web_socket.get(), &Web_socket_wrapper::on_closed, this, [=, this] { m_error_dialog->open(); ui->connection->setText("Could not connect to the server"); }); @@ -53,7 +52,7 @@ void MainWindow::showEvent(QShowEvent *event) void MainWindow::hideEvent(QHideEvent *event) { QWidget::hideEvent(event); - disconnect(m_web_socket, nullptr, nullptr, nullptr); + disconnect(m_web_socket.get(), nullptr, nullptr, nullptr); } void MainWindow::defaultSim(QString name) @@ -82,7 +81,7 @@ void MainWindow::open_sim(QString sim_name) { try { - m_selected_sim = m_sims.at(sim_name); + m_selected_sim = m_sims.at(sim_name).get(); this->m_selected_sim_name = sim_name; } catch (const std::out_of_range &ex) diff --git a/client/mainwindow.h b/client/mainwindow.h index 6f5df34..fb65eed 100644 --- a/client/mainwindow.h +++ b/client/mainwindow.h @@ -7,6 +7,9 @@ #include #include +#define INSERT_SIMULATOR(type) \ + m_sims.insert(std::make_pair(type().sim_name(), std::make_unique(m_web_socket.get(), this))) + namespace Ui { class MainWindow; @@ -40,10 +43,10 @@ class MainWindow : public QMainWindow QString m_selected_sim_name; QMainWindow *m_selected_sim; Ui::MainWindow *ui; - Web_socket_wrapper *m_web_socket; - NetworkError *m_error_dialog; + std::unique_ptr m_web_socket; + std::unique_ptr m_error_dialog; - std::map m_sims; + std::map> m_sims; }; #endif // MAINWINDOW_H diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 2649861..b9c21c6 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -1,9 +1,9 @@ #include "simulator_base.h" #include "can_transceive.h" -Simulator_base::Simulator_base(Web_socket_wrapper *web_socket, QWidget *parent) +Simulator_base::Simulator_base(QString sim_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_web_socket(web_socket), m_sim_name(sim_name) { m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); m_ui_lookup.reserve(40); @@ -14,6 +14,11 @@ QWidget *Simulator_base::id_to_ui(size_t id) return m_ui_lookup.at(id); } +QString Simulator_base::sim_name() const +{ + return m_sim_name; +} + void Simulator_base::showEvent(QShowEvent *event) { QWidget::showEvent(event); diff --git a/client/simulator_base.h b/client/simulator_base.h index 97eae5f..cf685c6 100644 --- a/client/simulator_base.h +++ b/client/simulator_base.h @@ -17,8 +17,9 @@ class Simulator_base : public QMainWindow { Q_OBJECT public: - explicit Simulator_base(Web_socket_wrapper *api, QWidget *parent = nullptr); + explicit Simulator_base(QString sim_name, Web_socket_wrapper *api, QWidget *parent = nullptr); void sim_update(void); + QString sim_name() const; QWidget *id_to_ui(size_t id); virtual QLabel *id_to_label(size_t name) { @@ -60,6 +61,7 @@ class Simulator_base : public QMainWindow private: Web_socket_wrapper *m_web_socket; + QString m_sim_name; QTimer *m_timer_update; uint32_t m_refresh_rate; diff --git a/client/simulators/can_debugger.cpp b/client/simulators/can_debugger.cpp index a8d4d12..8993408 100644 --- a/client/simulators/can_debugger.cpp +++ b/client/simulators/can_debugger.cpp @@ -2,7 +2,7 @@ #include "ui_can_debugger.h" Can_Debugger::Can_Debugger(Web_socket_wrapper *web_socket, QWidget *parent) - : Simulator_base(web_socket, parent), ui(new Ui::Can_Debugger) + : Simulator_base("Can Debugger", web_socket, parent), ui(new Ui::Can_Debugger) { ui->setupUi(this); this->setWindowTitle("CAN Debugger"); diff --git a/client/simulators/can_debugger.hpp b/client/simulators/can_debugger.hpp index 4a75154..e738571 100644 --- a/client/simulators/can_debugger.hpp +++ b/client/simulators/can_debugger.hpp @@ -14,7 +14,7 @@ class Can_Debugger : public Simulator_base Q_OBJECT public: - explicit Can_Debugger(Web_socket_wrapper *web_socket, QWidget *parent = nullptr); + explicit Can_Debugger(Web_socket_wrapper *web_socket = nullptr, QWidget *parent = nullptr); ~Can_Debugger(); private: diff --git a/client/simulators/test.cpp b/client/simulators/test.cpp index 7c92962..d75696d 100644 --- a/client/simulators/test.cpp +++ b/client/simulators/test.cpp @@ -2,7 +2,7 @@ #include "ui_test.h" Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent) - : Simulator_base(web_socket, parent), ui(new Ui::Test_Sim) + : Simulator_base("Test Sim", web_socket, parent), ui(new Ui::Test_Sim) { ui->setupUi(this); this->setWindowTitle("Test Sim"); diff --git a/client/simulators/test.hpp b/client/simulators/test.hpp index 704c7fc..602858b 100644 --- a/client/simulators/test.hpp +++ b/client/simulators/test.hpp @@ -15,7 +15,7 @@ class Test_Sim : public Simulator_base Q_OBJECT public: - explicit Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent = nullptr); + explicit Test_Sim(Web_socket_wrapper *web_socket = nullptr, QWidget *parent = nullptr); ~Test_Sim(); QLabel *id_to_label(size_t name) override;