Move to id is size_t

This commit is contained in:
2024-10-10 19:47:18 +02:00
parent d02b31ab25
commit 95a21250a6
27 changed files with 132 additions and 148 deletions
+5 -5
View File
@@ -1,7 +1,7 @@
#include "UI_button.hpp" #include "UI_button.hpp"
UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent) UI_button::UI_button(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_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); 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; return j;
} }
void UI_button::click() void UI_button::click()
{ {
emit on_click(m_id); emit on_click();
} }
+3 -3
View File
@@ -8,14 +8,14 @@ class UI_button : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); UI_button(const json &j, QObject *parrent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
void click() override; void click() override;
signals: signals:
void on_click(std::string id); void on_click();
}; };
+5 -5
View File
@@ -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<std::string> options, QObject *parrent) std::vector<std::string> 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++) for (size_t i = 0; i < m_options.size(); i++)
{ {
@@ -25,7 +25,7 @@ void UI_checkbox::toggle_selected(size_t selected)
return; return;
m_selected[selected] = !m_selected[selected]; m_selected[selected] = !m_selected[selected];
emit value_changed(); 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) 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; j["selected"] = m_selected;
return j; return j;
} }
+3 -3
View File
@@ -10,7 +10,7 @@ class UI_checkbox : public UI_x_boxes
Q_OBJECT Q_OBJECT
public: public:
UI_checkbox(QObject *parrent = nullptr); UI_checkbox(QObject *parrent = nullptr);
UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_checkbox(std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
QObject *parrent = nullptr); QObject *parrent = nullptr);
UI_checkbox(const json &j, QObject *parrent = nullptr); UI_checkbox(const json &j, QObject *parrent = nullptr);
@@ -20,10 +20,10 @@ class UI_checkbox : public UI_x_boxes
std::vector<std::string> selected_text() const; std::vector<std::string> selected_text() const;
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
signals: signals:
void on_change(std::string, std::string); void on_change(std::string);
private: private:
std::vector<bool> m_selected; std::vector<bool> m_selected;
+5 -5
View File
@@ -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<std::string> options, size_t selected, QObject *parrent) std::vector<std::string> 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; return;
m_selected = selected; m_selected = selected;
emit value_changed(); emit value_changed();
emit on_change(m_id, selected_text()); emit on_change(selected_text());
} }
void UI_combobox::set_selected(std::string selected) 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]; j["selected"] = m_options[m_selected];
return j; return j;
} }
+3 -3
View File
@@ -11,7 +11,7 @@ class UI_combobox : public UI_x_boxes
Q_OBJECT Q_OBJECT
public: public:
UI_combobox(QObject *parrent = nullptr); UI_combobox(QObject *parrent = nullptr);
UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_combobox(std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
size_t selected = 0, QObject *parrent = nullptr); size_t selected = 0, QObject *parrent = nullptr);
UI_combobox(const json &j, 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; std::string selected_text() const;
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
signals: signals:
void on_change(std::string id, std::string selected); void on_change(std::string selected);
private: private:
size_t m_selected; size_t m_selected;
+4 -4
View File
@@ -1,8 +1,8 @@
#include "UI_dial.hpp" #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) 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"); 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["min"] = this->m_min;
j["max"] = this->m_max; j["max"] = this->m_max;
j["value"] = this->m_value; j["value"] = this->m_value;
+2 -2
View File
@@ -7,7 +7,7 @@ class UI_dial : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); QObject *parrent = nullptr);
UI_dial(const json &j, QObject *parrent = nullptr); UI_dial(const json &j, QObject *parrent = nullptr);
@@ -18,7 +18,7 @@ class UI_dial : public UI_item
double max() const; double max() const;
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
private: private:
double m_min; double m_min;
+5 -10
View File
@@ -1,14 +1,14 @@
#include "UI_item.hpp" #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)
: 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) m_bg_color(bg_color), enabled(true), visible(true)
{ {
} }
UI_item::UI_item(std::string type, QObject *parrent) 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) 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 std::string UI_item::type() const
{ {
return m_type; return m_type;
@@ -142,10 +137,10 @@ void UI_item::from_json(const json &j)
j.at("visible").get_to(visible); j.at("visible").get_to(visible);
} }
json UI_item::to_json() const json UI_item::to_json(size_t id) const
{ {
json j; json j;
j["id"] = m_id; j["id"] = id;
j["type"] = m_type; j["type"] = m_type;
j["text"] = m_text; j["text"] = m_text;
j["text_size"] = m_text_size; j["text_size"] = m_text_size;
+3 -4
View File
@@ -16,12 +16,11 @@ class UI_item : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit UI_item(std::string type, QObject *parrent = nullptr); 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); Color bg_color, QObject *parrent = nullptr);
explicit UI_item(const json &j, QObject *parrent = nullptr); explicit UI_item(const json &j, QObject *parrent = nullptr);
~UI_item(); ~UI_item();
std::string id() const;
std::string type() const; std::string type() const;
bool is_type(json j); bool is_type(json j);
@@ -45,7 +44,7 @@ class UI_item : public QObject
virtual void from_json(const json &j); 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 click();
virtual void set_selected(std::string selected); virtual void set_selected(std::string selected);
@@ -55,7 +54,7 @@ class UI_item : public QObject
void value_changed(); void value_changed();
protected: protected:
std::string m_id; size_t m_id;
std::string m_type; std::string m_type;
std::string m_text; std::string m_text;
uint8_t m_text_size; uint8_t m_text_size;
+4 -4
View File
@@ -1,7 +1,7 @@
#include "UI_label.hpp" #include "UI_label.hpp"
UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_color, QObject *parrent) UI_label::UI_label(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_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); 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);
} }
+2 -2
View File
@@ -8,9 +8,9 @@ class UI_label : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); UI_label(const json &j, QObject *parrent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
}; };
+4 -4
View File
@@ -1,7 +1,7 @@
#include "UI_led.hpp" #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_led::UI_led(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_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); 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(); j["led_color"] = m_led_color.to_hex();
return j; return j;
} }
+2 -2
View File
@@ -8,7 +8,7 @@ class UI_led : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); QObject *parrent = nullptr);
UI_led(const json &j, 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 set_led_color(Color led_color);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
private: private:
Color m_led_color; Color m_led_color;
+5 -5
View File
@@ -1,8 +1,8 @@
#include "UI_slider.hpp" #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) 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; this->m_value = value;
emit value_changed(); emit value_changed();
emit on_change(m_id, m_value); emit on_change(m_value);
} }
double UI_slider::value() const double UI_slider::value() const
@@ -47,9 +47,9 @@ void UI_slider::from_json(const json &j)
this->m_value = j.at("value"); 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["min"] = this->m_min;
j["max"] = this->m_max; j["max"] = this->m_max;
j["value"] = this->m_value; j["value"] = this->m_value;
+3 -3
View File
@@ -7,7 +7,7 @@ class UI_slider : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); QObject *parrent = nullptr);
UI_slider(const json &j, QObject *parrent = nullptr); UI_slider(const json &j, QObject *parrent = nullptr);
@@ -18,9 +18,9 @@ class UI_slider : public UI_item
double max() const; double max() const;
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
signals: signals:
void on_change(std::string id, double value); void on_change(double value);
private: private:
double m_min; double m_min;
+5 -5
View File
@@ -1,7 +1,7 @@
#include "UI_textbox.hpp" #include "UI_textbox.hpp"
UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, QObject *parrent) UI_textbox::UI_textbox(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_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; return;
m_text = text; m_text = text;
emit value_changed(); emit value_changed();
emit on_change(m_id, m_text); emit on_change(m_text);
} }
void UI_textbox::set_hint(std::string hint) 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"); 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; j["hint"] = m_hint;
return j; return j;
} }
+3 -3
View File
@@ -8,7 +8,7 @@ class UI_textbox : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); UI_textbox(const json &j, QObject *parrent = nullptr);
void set_text(std::string text) override; void set_text(std::string text) override;
@@ -17,9 +17,9 @@ class UI_textbox : public UI_item
std::string hint() const; std::string hint() const;
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
signals: signals:
void on_change(std::string id, std::string text); void on_change(std::string text);
private: private:
std::string m_hint; std::string m_hint;
+4 -4
View File
@@ -1,8 +1,8 @@
#include "UI_thermo.hpp" #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) 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["min"] = this->m_min;
j["max"] = this->m_max; j["max"] = this->m_max;
j["value"] = this->m_value; j["value"] = this->m_value;
+2 -2
View File
@@ -8,7 +8,7 @@ class UI_thermo : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: 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); QObject *parrent = nullptr);
UI_thermo(const json &j, 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 remove_color_stop_normalized(double value);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
private: private:
double m_min; double m_min;
+4 -4
View File
@@ -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<std::string> options, QObject *parrent) std::vector<std::string> 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; j["options"] = this->m_options;
return j; return j;
} }
+2 -2
View File
@@ -7,11 +7,11 @@ class UI_x_boxes : public UI_item
Q_OBJECT Q_OBJECT
public: public:
UI_x_boxes(std::string type, QObject *parrent = nullptr); 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<std::string> options, QObject *parrent = nullptr); std::vector<std::string> options, QObject *parrent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json() const override; json to_json(size_t id) const override;
protected: protected:
std::vector<std::string> m_options; std::vector<std::string> m_options;
+10 -10
View File
@@ -30,9 +30,9 @@ json Simulators::command_parser(json command)
switch (cmd) switch (cmd)
{ {
case Command::get_UI_element: 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"]["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; break;
case Command::get_UI_elements: case Command::get_UI_elements:
spdlog::info("Command {}", type); spdlog::info("Command {}", type);
@@ -81,26 +81,26 @@ void Simulators::event_handler(json event)
switch (e) switch (e)
{ {
case Event::clicked: { case Event::clicked: {
spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); spdlog::debug("Event {}: {}", type, (size_t)event.at("id"));
invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click(); invoke_active_simulator()->get_UI_item(event.at("id"))->click();
break; break;
} }
case Event::value_changed: { case Event::value_changed: {
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), (double)(event["value"]));
invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value")); invoke_active_simulator()->get_UI_item(event.at("id"))->set_value((double)event.at("value"));
break; break;
} }
case Event::text_changed: { 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() invoke_active_simulator()
->get_UI_item(std::string(event.at("id"))) ->get_UI_item(event.at("id"))
->set_text(std::string(event.at("text"))); ->set_text(std::string(event.at("text")));
break; break;
} }
case Event::selected: { 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() invoke_active_simulator()
->get_UI_item(std::string(event.at("id"))) ->get_UI_item(event.at("id"))
->set_selected(std::string(event.at("selected"))); ->set_selected(std::string(event.at("selected")));
break; break;
} }
+10 -24
View File
@@ -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) : QObject(parrent), m_name(name), m_timer(new QTimer(this)), m_interval(interval)
{ {
connect(m_timer, &QTimer::timeout, this, [this] { timer(); }); connect(m_timer, &QTimer::timeout, this, [this] { timer(); });
m_UI_items.reserve(50);
} }
std::string Simulator_base::name() const std::string Simulator_base::name() const
@@ -12,29 +13,10 @@ std::string Simulator_base::name() const
return m_name; 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) void Simulator_base::add_UI_item(UI_item *item)
{ {
// Add a UI item to the simulator // 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(); }); connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); });
m_UI_items_map[item->id()] = item;
m_UI_items.push_back(item); m_UI_items.push_back(item);
} }
@@ -43,18 +25,22 @@ json Simulator_base::get_UI_items() const
// Get the UI items // Get the UI items
json items; json items;
items["name"] = m_name; 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; 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 // Get a UI item by id using the list
return m_UI_items_map.at(id); if (id < m_UI_items.size())
{
return m_UI_items[id];
}
return nullptr;
} }
void Simulator_base::run() void Simulator_base::run()
+15 -12
View File
@@ -24,16 +24,6 @@ class Simulator_base : public QObject
*/ */
std::string name() const; 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 * @brief This function adds a UI item to the simulator
* only if the id is unique * only if the id is unique
@@ -55,7 +45,21 @@ class Simulator_base : public QObject
* @param id The id of the UI item * @param id The id of the UI item
* @return UI_item* A pointer to the UI item object * @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 * @brief Run the simulator
@@ -89,7 +93,6 @@ class Simulator_base : public QObject
protected: protected:
std::string m_name; std::string m_name;
std::vector<UI_item *> m_UI_items; std::vector<UI_item *> m_UI_items;
std::map<std::string, UI_item *> m_UI_items_map;
QTimer *m_timer; QTimer *m_timer;
std::chrono::milliseconds m_interval; std::chrono::milliseconds m_interval;
}; };
+15 -15
View File
@@ -3,14 +3,14 @@
Test_Sim::Test_Sim(QObject* parrent) : Test_Sim::Test_Sim(QObject* parrent) :
Simulator_base("Test_Sim", std::chrono::milliseconds(1000), parrent), Simulator_base("Test_Sim", std::chrono::milliseconds(1000), parrent),
m_button("button", "Off", Color(Color::White), Color(Color::Red)), m_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_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_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_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_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_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_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_led("Led", Color(Color::White), Color(Color::Black), Color(Color::Red))
{ {
this->m_thermo.set_start_color(Color(Color::Red)); this->m_thermo.set_start_color(Color(Color::Red));
this->m_thermo.set_end_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_thermo);
this->add_UI_item(&this->m_led); 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_button, &UI_button::on_click, [this] { button_clicked(&m_button); });
QObject::connect(&m_slider, &UI_slider::on_change, [this](std::string id, double value) { slider_changed(id, value); }); QObject::connect(&m_slider, &UI_slider::on_change, [this](double value) { slider_changed(&m_slider, value); });
} }
void Test_Sim::timer() void Test_Sim::timer()
@@ -49,9 +49,9 @@ void Test_Sim::run_at_startup()
spdlog::info("Test_Sim started"); 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; 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)); 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 == &this->m_slider)
if (id == "slider")
{ {
spdlog::info("Test slider changed to {}", value);
this->m_label.set_text(std::to_string(value)); this->m_label.set_text(std::to_string(value));
this->m_dial.set_value(value); this->m_dial.set_value(value);
this->m_thermo.set_value(value); this->m_thermo.set_value(value);
+3 -2
View File
@@ -8,8 +8,9 @@ class Test_Sim : public Simulator_base {
void timer() override; void timer() override;
void run_at_startup() override; void run_at_startup() override;
private: private:
void button_clicked(std::string id); void button_clicked(UI_button* id);
void slider_changed(std::string id, double value); void slider_changed(UI_slider* id, double value);
private: private:
UI_button m_button; UI_button m_button;
UI_combobox m_combobox; UI_combobox m_combobox;