From 95a21250a673ee4c0c3585c31265b9ba398eee73 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 10 Oct 2024 19:47:18 +0200 Subject: [PATCH] Move to id is size_t --- src/UI/UI_button.cpp | 10 +++++----- src/UI/UI_button.hpp | 6 +++--- src/UI/UI_checkbox.cpp | 10 +++++----- src/UI/UI_checkbox.hpp | 6 +++--- src/UI/UI_combobox.cpp | 10 +++++----- src/UI/UI_combobox.hpp | 6 +++--- src/UI/UI_dial.cpp | 8 ++++---- src/UI/UI_dial.hpp | 4 ++-- src/UI/UI_item.cpp | 15 +++++---------- src/UI/UI_item.hpp | 7 +++---- src/UI/UI_label.cpp | 8 ++++---- src/UI/UI_label.hpp | 4 ++-- src/UI/UI_led.cpp | 8 ++++---- src/UI/UI_led.hpp | 4 ++-- src/UI/UI_slider.cpp | 10 +++++----- src/UI/UI_slider.hpp | 6 +++--- src/UI/UI_textbox.cpp | 10 +++++----- src/UI/UI_textbox.hpp | 6 +++--- src/UI/UI_thermo.cpp | 8 ++++---- src/UI/UI_thermo.hpp | 4 ++-- src/UI/UI_x_boxes.cpp | 8 ++++---- src/UI/UI_x_boxes.hpp | 4 ++-- tools/message_parser.cpp | 20 ++++++++++---------- tools/simulator_base.cpp | 34 ++++++++++------------------------ tools/simulator_base.hpp | 27 +++++++++++++++------------ tools/simulators/test_sim.cpp | 30 +++++++++++++++--------------- tools/simulators/test_sim.hpp | 7 ++++--- 27 files changed, 132 insertions(+), 148 deletions(-) diff --git a/src/UI/UI_button.cpp b/src/UI/UI_button.cpp index bb3f8a6..4d48e7c 100644 --- a/src/UI/UI_button.cpp +++ b/src/UI/UI_button.cpp @@ -1,7 +1,7 @@ #include "UI_button.hpp" -UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent) - : UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color, parrent) +UI_button::UI_button(std::string text, Color fg_color, Color bg_color, QObject *parrent) + : UI_item(UI_BUTTON_TYPE, text, 12, fg_color, bg_color, parrent) { } @@ -15,13 +15,13 @@ void UI_button::from_json(const json &j) UI_item::from_json(j); } -json UI_button::to_json() const +json UI_button::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); return j; } void UI_button::click() { - emit on_click(m_id); + emit on_click(); } diff --git a/src/UI/UI_button.hpp b/src/UI/UI_button.hpp index 12c4742..18cd691 100644 --- a/src/UI/UI_button.hpp +++ b/src/UI/UI_button.hpp @@ -8,14 +8,14 @@ class UI_button : public UI_item { Q_OBJECT public: - UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent = nullptr); + UI_button(std::string text, Color fg_color, Color bg_color, QObject *parrent = nullptr); UI_button(const json &j, QObject *parrent = nullptr); void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; void click() override; signals: - void on_click(std::string id); + void on_click(); }; diff --git a/src/UI/UI_checkbox.cpp b/src/UI/UI_checkbox.cpp index 33480a7..58649bc 100644 --- a/src/UI/UI_checkbox.cpp +++ b/src/UI/UI_checkbox.cpp @@ -4,9 +4,9 @@ UI_checkbox::UI_checkbox(QObject *parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parren { } -UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, +UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, std::vector options, QObject *parrent) - : UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected() + : UI_x_boxes(UI_CHECKBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected() { for (size_t i = 0; i < m_options.size(); i++) { @@ -25,7 +25,7 @@ void UI_checkbox::toggle_selected(size_t selected) return; m_selected[selected] = !m_selected[selected]; emit value_changed(); - emit on_change(m_id, selected_text()[selected]); + emit on_change(selected_text()[selected]); } void UI_checkbox::toggle_selected(std::string selected) @@ -72,9 +72,9 @@ void UI_checkbox::from_json(const json &j) } } -json UI_checkbox::to_json() const +json UI_checkbox::to_json(size_t id) const { - json j = UI_x_boxes::to_json(); + json j = UI_x_boxes::to_json(id); j["selected"] = m_selected; return j; } diff --git a/src/UI/UI_checkbox.hpp b/src/UI/UI_checkbox.hpp index 30778aa..1fda79a 100644 --- a/src/UI/UI_checkbox.hpp +++ b/src/UI/UI_checkbox.hpp @@ -10,7 +10,7 @@ class UI_checkbox : public UI_x_boxes Q_OBJECT public: UI_checkbox(QObject *parrent = nullptr); - UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, + UI_checkbox(std::string text, Color fg_color, Color bg_color, std::vector options, QObject *parrent = nullptr); UI_checkbox(const json &j, QObject *parrent = nullptr); @@ -20,10 +20,10 @@ class UI_checkbox : public UI_x_boxes std::vector selected_text() const; void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; signals: - void on_change(std::string, std::string); + void on_change(std::string); private: std::vector m_selected; diff --git a/src/UI/UI_combobox.cpp b/src/UI/UI_combobox.cpp index 87ee693..6729443 100644 --- a/src/UI/UI_combobox.cpp +++ b/src/UI/UI_combobox.cpp @@ -4,9 +4,9 @@ UI_combobox::UI_combobox(QObject *parrent) : UI_x_boxes(UI_COMBOBOX_TYPE, parren { } -UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, +UI_combobox::UI_combobox(std::string text, Color fg_color, Color bg_color, std::vector options, size_t selected, QObject *parrent) - : UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected(selected) + : UI_x_boxes(UI_COMBOBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected(selected) { } @@ -21,7 +21,7 @@ void UI_combobox::set_selected(size_t selected) return; m_selected = selected; emit value_changed(); - emit on_change(m_id, selected_text()); + emit on_change(selected_text()); } void UI_combobox::set_selected(std::string selected) @@ -60,9 +60,9 @@ void UI_combobox::from_json(const json &j) } } -json UI_combobox::to_json() const +json UI_combobox::to_json(size_t id) const { - json j = UI_x_boxes::to_json(); + json j = UI_x_boxes::to_json(id); j["selected"] = m_options[m_selected]; return j; } diff --git a/src/UI/UI_combobox.hpp b/src/UI/UI_combobox.hpp index b795db1..a7580d2 100644 --- a/src/UI/UI_combobox.hpp +++ b/src/UI/UI_combobox.hpp @@ -11,7 +11,7 @@ class UI_combobox : public UI_x_boxes Q_OBJECT public: UI_combobox(QObject *parrent = nullptr); - UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, + UI_combobox(std::string text, Color fg_color, Color bg_color, std::vector options, size_t selected = 0, QObject *parrent = nullptr); UI_combobox(const json &j, QObject *parrent = nullptr); @@ -21,9 +21,9 @@ class UI_combobox : public UI_x_boxes std::string selected_text() const; void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; signals: - void on_change(std::string id, std::string selected); + void on_change(std::string selected); private: size_t m_selected; diff --git a/src/UI/UI_dial.cpp b/src/UI/UI_dial.cpp index 76b8147..ac209dd 100644 --- a/src/UI/UI_dial.cpp +++ b/src/UI/UI_dial.cpp @@ -1,8 +1,8 @@ #include "UI_dial.hpp" -UI_dial::UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, +UI_dial::UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject *parrent) - : UI_item(id, UI_DIAL_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) + : UI_item(UI_DIAL_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) { } @@ -46,9 +46,9 @@ void UI_dial::from_json(const json &j) this->m_value = j.at("value"); } -json UI_dial::to_json() const +json UI_dial::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); j["min"] = this->m_min; j["max"] = this->m_max; j["value"] = this->m_value; diff --git a/src/UI/UI_dial.hpp b/src/UI/UI_dial.hpp index 6c3a2a6..52d0ed0 100644 --- a/src/UI/UI_dial.hpp +++ b/src/UI/UI_dial.hpp @@ -7,7 +7,7 @@ class UI_dial : public UI_item { Q_OBJECT public: - UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, + UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject *parrent = nullptr); UI_dial(const json &j, QObject *parrent = nullptr); @@ -18,7 +18,7 @@ class UI_dial : public UI_item double max() const; void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; private: double m_min; diff --git a/src/UI/UI_item.cpp b/src/UI/UI_item.cpp index 7bddad0..1d783fd 100644 --- a/src/UI/UI_item.cpp +++ b/src/UI/UI_item.cpp @@ -1,14 +1,14 @@ #include "UI_item.hpp" -UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, +UI_item::UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, QObject *parrent) - : QObject(parrent), m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), + : QObject(parrent), m_id((size_t)-1), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color), enabled(true), visible(true) { } UI_item::UI_item(std::string type, QObject *parrent) - : QObject(parrent), m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), + : QObject(parrent), m_id((size_t)-1), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White), enabled(true), visible(true) { } @@ -22,11 +22,6 @@ UI_item::~UI_item() { } -std::string UI_item::id() const -{ - return m_id; -} - std::string UI_item::type() const { return m_type; @@ -142,10 +137,10 @@ void UI_item::from_json(const json &j) j.at("visible").get_to(visible); } -json UI_item::to_json() const +json UI_item::to_json(size_t id) const { json j; - j["id"] = m_id; + j["id"] = id; j["type"] = m_type; j["text"] = m_text; j["text_size"] = m_text_size; diff --git a/src/UI/UI_item.hpp b/src/UI/UI_item.hpp index e8c9dee..f7f35ef 100644 --- a/src/UI/UI_item.hpp +++ b/src/UI/UI_item.hpp @@ -16,12 +16,11 @@ class UI_item : public QObject Q_OBJECT public: explicit UI_item(std::string type, QObject *parrent = nullptr); - explicit UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, + explicit UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, QObject *parrent = nullptr); explicit UI_item(const json &j, QObject *parrent = nullptr); ~UI_item(); - std::string id() const; std::string type() const; bool is_type(json j); @@ -45,7 +44,7 @@ class UI_item : public QObject virtual void from_json(const json &j); - virtual json to_json() const; + virtual json to_json(size_t id) const; virtual void click(); virtual void set_selected(std::string selected); @@ -55,7 +54,7 @@ class UI_item : public QObject void value_changed(); protected: - std::string m_id; + size_t m_id; std::string m_type; std::string m_text; uint8_t m_text_size; diff --git a/src/UI/UI_label.cpp b/src/UI/UI_label.cpp index 2d86e4a..5b73242 100644 --- a/src/UI/UI_label.cpp +++ b/src/UI/UI_label.cpp @@ -1,7 +1,7 @@ #include "UI_label.hpp" -UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent) - : UI_item(id, UI_LABEL_TYPE, text, 12, fg_color, bg_color, parrent) +UI_label::UI_label(std::string text, Color fg_color, Color bg_color, QObject *parrent) + : UI_item(UI_LABEL_TYPE, text, 12, fg_color, bg_color, parrent) { } @@ -15,7 +15,7 @@ void UI_label::from_json(const json &j) UI_item::from_json(j); } -json UI_label::to_json() const +json UI_label::to_json(size_t id) const { - return UI_item::to_json(); + return UI_item::to_json(id); } diff --git a/src/UI/UI_label.hpp b/src/UI/UI_label.hpp index 9a1a8cd..a025710 100644 --- a/src/UI/UI_label.hpp +++ b/src/UI/UI_label.hpp @@ -8,9 +8,9 @@ class UI_label : public UI_item { Q_OBJECT public: - UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent = nullptr); + UI_label(std::string text, Color fg_color, Color bg_color, QObject *parrent = nullptr); UI_label(const json &j, QObject *parrent = nullptr); void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; }; diff --git a/src/UI/UI_led.cpp b/src/UI/UI_led.cpp index 9fbedde..78aab90 100644 --- a/src/UI/UI_led.cpp +++ b/src/UI/UI_led.cpp @@ -1,7 +1,7 @@ #include "UI_led.hpp" -UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color, QObject *parrent) - : UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color, parrent), m_led_color(led_color) +UI_led::UI_led(std::string text, Color fg_color, Color bg_color, Color led_color, QObject *parrent) + : UI_item(UI_LED_TYPE, text, 12, fg_color, bg_color, parrent), m_led_color(led_color) { } @@ -32,9 +32,9 @@ void UI_led::from_json(const json &j) m_led_color = Color(color); } -json UI_led::to_json() const +json UI_led::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); j["led_color"] = m_led_color.to_hex(); return j; } diff --git a/src/UI/UI_led.hpp b/src/UI/UI_led.hpp index 6250545..25eefed 100644 --- a/src/UI/UI_led.hpp +++ b/src/UI/UI_led.hpp @@ -8,7 +8,7 @@ class UI_led : public UI_item { Q_OBJECT public: - UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color, + UI_led(std::string text, Color fg_color, Color bg_color, Color led_color, QObject *parrent = nullptr); UI_led(const json &j, QObject *parrent = nullptr); @@ -16,7 +16,7 @@ class UI_led : public UI_item void set_led_color(Color led_color); void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; private: Color m_led_color; diff --git a/src/UI/UI_slider.cpp b/src/UI/UI_slider.cpp index 593185b..c76d3e3 100644 --- a/src/UI/UI_slider.cpp +++ b/src/UI/UI_slider.cpp @@ -1,8 +1,8 @@ #include "UI_slider.hpp" -UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, +UI_slider::UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject *parrent) - : UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) + : UI_item(UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) { } @@ -20,7 +20,7 @@ void UI_slider::set_value(double value) this->m_value = value; emit value_changed(); - emit on_change(m_id, m_value); + emit on_change(m_value); } double UI_slider::value() const @@ -47,9 +47,9 @@ void UI_slider::from_json(const json &j) this->m_value = j.at("value"); } -json UI_slider::to_json() const +json UI_slider::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); j["min"] = this->m_min; j["max"] = this->m_max; j["value"] = this->m_value; diff --git a/src/UI/UI_slider.hpp b/src/UI/UI_slider.hpp index 1ccb6e7..80a0ee4 100644 --- a/src/UI/UI_slider.hpp +++ b/src/UI/UI_slider.hpp @@ -7,7 +7,7 @@ class UI_slider : public UI_item { Q_OBJECT public: - UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, + UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject *parrent = nullptr); UI_slider(const json &j, QObject *parrent = nullptr); @@ -18,9 +18,9 @@ class UI_slider : public UI_item double max() const; void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; signals: - void on_change(std::string id, double value); + void on_change(double value); private: double m_min; diff --git a/src/UI/UI_textbox.cpp b/src/UI/UI_textbox.cpp index 0a10fb3..c260551 100644 --- a/src/UI/UI_textbox.cpp +++ b/src/UI/UI_textbox.cpp @@ -1,7 +1,7 @@ #include "UI_textbox.hpp" -UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject *parrent) - : UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color, parrent), m_hint(hint) +UI_textbox::UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parrent) + : UI_item(UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color, parrent), m_hint(hint) { } @@ -16,7 +16,7 @@ void UI_textbox::set_text(std::string text) return; m_text = text; emit value_changed(); - emit on_change(m_id, m_text); + emit on_change(m_text); } void UI_textbox::set_hint(std::string hint) @@ -36,9 +36,9 @@ void UI_textbox::from_json(const json &j) m_hint = j.at("hint"); } -json UI_textbox::to_json() const +json UI_textbox::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); j["hint"] = m_hint; return j; } diff --git a/src/UI/UI_textbox.hpp b/src/UI/UI_textbox.hpp index 0f90534..f5cbdf5 100644 --- a/src/UI/UI_textbox.hpp +++ b/src/UI/UI_textbox.hpp @@ -8,7 +8,7 @@ class UI_textbox : public UI_item { Q_OBJECT public: - UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject *parrent = nullptr); + UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parrent = nullptr); UI_textbox(const json &j, QObject *parrent = nullptr); void set_text(std::string text) override; @@ -17,9 +17,9 @@ class UI_textbox : public UI_item std::string hint() const; void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; signals: - void on_change(std::string id, std::string text); + void on_change(std::string text); private: std::string m_hint; diff --git a/src/UI/UI_thermo.cpp b/src/UI/UI_thermo.cpp index fb35569..579b265 100644 --- a/src/UI/UI_thermo.cpp +++ b/src/UI/UI_thermo.cpp @@ -1,8 +1,8 @@ #include "UI_thermo.hpp" -UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, +UI_thermo::UI_thermo(std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject *parrent) - : UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) + : UI_item(UI_THERMO_TYPE, text, 12, fg_color, bg_color, parrent), m_min(min), m_max(max), m_value(value) { } @@ -103,9 +103,9 @@ void UI_thermo::from_json(const json &j) } } -json UI_thermo::to_json() const +json UI_thermo::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); j["min"] = this->m_min; j["max"] = this->m_max; j["value"] = this->m_value; diff --git a/src/UI/UI_thermo.hpp b/src/UI/UI_thermo.hpp index dc5a1d5..05081e3 100644 --- a/src/UI/UI_thermo.hpp +++ b/src/UI/UI_thermo.hpp @@ -8,7 +8,7 @@ class UI_thermo : public UI_item { Q_OBJECT public: - UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, + UI_thermo(std::string text, Color fg_color, Color bg_color, double min, double max, double value, QObject *parrent = nullptr); UI_thermo(const json &j, QObject *parrent = nullptr); @@ -26,7 +26,7 @@ class UI_thermo : public UI_item void remove_color_stop_normalized(double value); void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; private: double m_min; diff --git a/src/UI/UI_x_boxes.cpp b/src/UI/UI_x_boxes.cpp index 89d544e..0a9cf54 100644 --- a/src/UI/UI_x_boxes.cpp +++ b/src/UI/UI_x_boxes.cpp @@ -4,9 +4,9 @@ UI_x_boxes::UI_x_boxes(std::string type, QObject *parrent) : UI_item(type, parre { } -UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, +UI_x_boxes::UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color, std::vector options, QObject *parrent) - : UI_item(id, type, text, 12, fg_color, bg_color, parrent), m_options(options) + : UI_item(type, text, 12, fg_color, bg_color, parrent), m_options(options) { } @@ -23,9 +23,9 @@ void UI_x_boxes::from_json(const json &j) } } -json UI_x_boxes::to_json() const +json UI_x_boxes::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); j["options"] = this->m_options; return j; } diff --git a/src/UI/UI_x_boxes.hpp b/src/UI/UI_x_boxes.hpp index 18d8f91..e7ba1b7 100644 --- a/src/UI/UI_x_boxes.hpp +++ b/src/UI/UI_x_boxes.hpp @@ -7,11 +7,11 @@ class UI_x_boxes : public UI_item Q_OBJECT public: UI_x_boxes(std::string type, QObject *parrent = nullptr); - UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, + UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color, std::vector options, QObject *parrent = nullptr); void from_json(const json &j) override; - json to_json() const override; + json to_json(size_t id) const override; protected: std::vector m_options; diff --git a/tools/message_parser.cpp b/tools/message_parser.cpp index 656058e..11adc58 100644 --- a/tools/message_parser.cpp +++ b/tools/message_parser.cpp @@ -30,9 +30,9 @@ json Simulators::command_parser(json command) switch (cmd) { case Command::get_UI_element: - spdlog::info("Command {}: {}", type, std::string(command.at("id"))); + spdlog::info("Command {}: {}", type, (size_t)command.at("id")); j["response"]["type"] = type; - j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(); + j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id")); break; case Command::get_UI_elements: spdlog::info("Command {}", type); @@ -81,26 +81,26 @@ void Simulators::event_handler(json event) switch (e) { case Event::clicked: { - spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); - invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click(); + spdlog::debug("Event {}: {}", type, (size_t)event.at("id")); + invoke_active_simulator()->get_UI_item(event.at("id"))->click(); break; } case Event::value_changed: { - spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); - invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value")); + spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), (double)(event["value"])); + invoke_active_simulator()->get_UI_item(event.at("id"))->set_value((double)event.at("value")); break; } case Event::text_changed: { - spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text"))); + spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("text"))); invoke_active_simulator() - ->get_UI_item(std::string(event.at("id"))) + ->get_UI_item(event.at("id")) ->set_text(std::string(event.at("text"))); break; } case Event::selected: { - spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected"))); + spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("selected"))); invoke_active_simulator() - ->get_UI_item(std::string(event.at("id"))) + ->get_UI_item(event.at("id")) ->set_selected(std::string(event.at("selected"))); break; } diff --git a/tools/simulator_base.cpp b/tools/simulator_base.cpp index 1678199..7789a17 100644 --- a/tools/simulator_base.cpp +++ b/tools/simulator_base.cpp @@ -4,6 +4,7 @@ Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds inter : QObject(parrent), m_name(name), m_timer(new QTimer(this)), m_interval(interval) { connect(m_timer, &QTimer::timeout, this, [this] { timer(); }); + m_UI_items.reserve(50); } std::string Simulator_base::name() const @@ -12,29 +13,10 @@ std::string Simulator_base::name() const return m_name; } -bool Simulator_base::is_ui_id_unique(std::string id) const -{ - // Check if the UI name is unique - for (UI_item *item : m_UI_items) - { - if (item->id() == id) - { - return false; - } - } - - return true; -} - void Simulator_base::add_UI_item(UI_item *item) { // Add a UI item to the simulator - if (is_ui_id_unique(item->id()) == false) - { - throw std::runtime_error("UI item id " + item->id() + " is not unique"); - } connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); }); - m_UI_items_map[item->id()] = item; m_UI_items.push_back(item); } @@ -43,18 +25,22 @@ json Simulator_base::get_UI_items() const // Get the UI items json items; items["name"] = m_name; - for (UI_item *item : m_UI_items) + for (size_t i = 0; i < m_UI_items.size(); i++) { - items["UI_items"].push_back(item->to_json()); + items["UI_items"].push_back(m_UI_items[i]->to_json(i)); } return items; } -UI_item *Simulator_base::get_UI_item(std::string id) const +UI_item *Simulator_base::get_UI_item(size_t id) const { - // Get a UI item by id - return m_UI_items_map.at(id); + // Get a UI item by id using the list + if (id < m_UI_items.size()) + { + return m_UI_items[id]; + } + return nullptr; } void Simulator_base::run() diff --git a/tools/simulator_base.hpp b/tools/simulator_base.hpp index 2062041..30aea13 100644 --- a/tools/simulator_base.hpp +++ b/tools/simulator_base.hpp @@ -24,16 +24,6 @@ class Simulator_base : public QObject */ std::string name() const; - /** - * @brief This function checks if the UI id is unique - * (hence not already pushed in with the add_UI_item function) - * - * @param id The id to check - * @return true The id is unique - * @return false The id is already in use - */ - bool is_ui_id_unique(std::string id) const; - /** * @brief This function adds a UI item to the simulator * only if the id is unique @@ -55,7 +45,21 @@ class Simulator_base : public QObject * @param id The id of the UI item * @return UI_item* A pointer to the UI item object */ - UI_item *get_UI_item(std::string id) const; + UI_item *get_UI_item(size_t id) const; + + size_t ptr_to_id(UI_item *ptr) const + { + size_t id = 0; + while (id < m_UI_items.size()) + { + if (m_UI_items[id] == ptr) + { + return id; + } + id++; + } + return (size_t)-1; + } /** * @brief Run the simulator @@ -89,7 +93,6 @@ class Simulator_base : public QObject protected: std::string m_name; std::vector m_UI_items; - std::map m_UI_items_map; QTimer *m_timer; std::chrono::milliseconds m_interval; }; diff --git a/tools/simulators/test_sim.cpp b/tools/simulators/test_sim.cpp index fd46623..bd3dfc8 100644 --- a/tools/simulators/test_sim.cpp +++ b/tools/simulators/test_sim.cpp @@ -3,14 +3,14 @@ Test_Sim::Test_Sim(QObject* parrent) : Simulator_base("Test_Sim", std::chrono::milliseconds(1000), parrent), -m_button("button", "Off", Color(Color::White), Color(Color::Red)), -m_combobox("combobox", "Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0), -m_label("label", "Label", Color(Color::White), Color(Color::Black)), -m_checkbox("checkbox", "Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}), -m_slider("slider", "Slider", Color(Color::White), Color(Color::Black), 0, 100, 0), -m_dial("dial", "Dial", Color(Color::White), Color(Color::Black), 0, 100, 0), -m_thermo("thermo", "Thermo", Color(Color::White), Color(Color::Black), 0, 100, 0), -m_led("led", "Led", Color(Color::White), Color(Color::Black), Color(Color::Red)) +m_button("Off", Color(Color::White), Color(Color::Red)), +m_combobox("Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0), +m_label("Label", Color(Color::White), Color(Color::Black)), +m_checkbox("Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}), +m_slider("Slider", Color(Color::White), Color(Color::Black), 0, 100, 0), +m_dial("Dial", Color(Color::White), Color(Color::Black), 0, 100, 0), +m_thermo("Thermo", Color(Color::White), Color(Color::Black), 0, 100, 0), +m_led("Led", Color(Color::White), Color(Color::Black), Color(Color::Red)) { this->m_thermo.set_start_color(Color(Color::Red)); this->m_thermo.set_end_color(Color(Color::Red)); @@ -28,8 +28,8 @@ m_led("led", "Led", Color(Color::White), Color(Color::Black), Color(Color::Red)) this->add_UI_item(&this->m_thermo); this->add_UI_item(&this->m_led); - QObject::connect(&m_button, &UI_button::on_click, [this](std::string id) { button_clicked(id); }); - QObject::connect(&m_slider, &UI_slider::on_change, [this](std::string id, double value) { slider_changed(id, value); }); + QObject::connect(&m_button, &UI_button::on_click, [this] { button_clicked(&m_button); }); + QObject::connect(&m_slider, &UI_slider::on_change, [this](double value) { slider_changed(&m_slider, value); }); } void Test_Sim::timer() @@ -49,9 +49,9 @@ void Test_Sim::run_at_startup() spdlog::info("Test_Sim started"); } -void Test_Sim::button_clicked(std::string id) +void Test_Sim::button_clicked(UI_button* id) { - if (id == "button") + if (id == &this->m_button) { bool state = this->m_button.bg_color() == Color(Color::Red) ? true : false; this->m_button.set_bg_color(state ? Color(Color::Green) : Color(Color::Red)); @@ -62,11 +62,11 @@ void Test_Sim::button_clicked(std::string id) } } -void Test_Sim::slider_changed(std::string id, double value) +void Test_Sim::slider_changed(UI_slider* id, double value) { - spdlog::info("Slider {} changed to {}", id, value); - if (id == "slider") + if (id == &this->m_slider) { + spdlog::info("Test slider changed to {}", value); this->m_label.set_text(std::to_string(value)); this->m_dial.set_value(value); this->m_thermo.set_value(value); diff --git a/tools/simulators/test_sim.hpp b/tools/simulators/test_sim.hpp index 11f70a2..bd66131 100644 --- a/tools/simulators/test_sim.hpp +++ b/tools/simulators/test_sim.hpp @@ -8,9 +8,10 @@ class Test_Sim : public Simulator_base { void timer() override; void run_at_startup() override; private: - void button_clicked(std::string id); - void slider_changed(std::string id, double value); - private: + void button_clicked(UI_button* id); + void slider_changed(UI_slider* id, double value); + + private: UI_button m_button; UI_combobox m_combobox; UI_label m_label;