Removes JSON constructor from UI elements

This fixes the error: clang:optin.cplusplus.VirtualCall
When virtual methods are called during construction and destruction the polymorphism is restricted to the class that’s being constructed or destructed because the more derived contexts are either not yet initialized or already destructed.
This commit is contained in:
2026-02-14 14:32:16 +01:00
parent f58028a8a9
commit 2a76e13305
31 changed files with 38 additions and 61 deletions
+1 -2
View File
@@ -5,9 +5,8 @@ UI_button::UI_button(std::string text, Color fg_color, Color bg_color, QObject *
{ {
} }
UI_button::UI_button(const json &j, QObject *parent) : UI_item(UI_BUTTON_TYPE, parent) UI_button::UI_button(QObject *parent) : UI_item(UI_BUTTON_TYPE, parent)
{ {
from_json(j);
} }
void UI_button::from_json(const json &j) void UI_button::from_json(const json &j)
+1 -1
View File
@@ -8,7 +8,7 @@ class UI_button : public UI_item
Q_OBJECT Q_OBJECT
public: public:
UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_button(const json &j, QObject *parent = nullptr); UI_button(QObject *parent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json(size_t id) const override; json to_json(size_t id) const override;
+1 -2
View File
@@ -17,10 +17,9 @@ UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent) : UI_item(UI_CAN
setup_item(false, true, true); setup_item(false, true, true);
} }
UI_can::UI_can(const json &j, QObject *parent) : UI_item(UI_CAN_TYPE, parent) UI_can::UI_can(QObject *parent) : UI_item(UI_CAN_TYPE, parent)
{ {
setup_item(false, true, true); setup_item(false, true, true);
from_json(j);
} }
void UI_can::add_send_message(QCanBusFrame msg) void UI_can::add_send_message(QCanBusFrame msg)
+1 -1
View File
@@ -20,7 +20,7 @@ class UI_can : public UI_item
Q_OBJECT Q_OBJECT
public: public:
UI_can(Color fg_color, Color bg_color, QObject *parent = nullptr); UI_can(Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_can(const json &j, QObject *parent = nullptr); UI_can(QObject *parent = nullptr);
void add_send_message(QCanBusFrame msg); void add_send_message(QCanBusFrame msg);
void add_received_message(QCanBusFrame msg); void add_received_message(QCanBusFrame msg);
-5
View File
@@ -9,11 +9,6 @@ UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObje
{ {
} }
UI_checkbox::UI_checkbox(const json &j, QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_checked()
{
from_json(j);
}
void UI_checkbox::click() void UI_checkbox::click()
{ {
m_checked = !m_checked; m_checked = !m_checked;
-1
View File
@@ -9,7 +9,6 @@ class UI_checkbox : public UI_item
public: public:
UI_checkbox(QObject *parent = nullptr); UI_checkbox(QObject *parent = nullptr);
UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_checkbox(const json &j, QObject *parent = nullptr);
void click() override; void click() override;
void set_state(bool state); void set_state(bool state);
-6
View File
@@ -12,12 +12,6 @@ UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector<std::string
setup_item(false, true, true); setup_item(false, true, true);
} }
UI_combobox::UI_combobox(const json &j, QObject *parent) : UI_item(UI_COMBOBOX_TYPE, parent)
{
setup_item(false, true, true);
from_json(j);
}
void UI_combobox::set_selected(size_t selected) void UI_combobox::set_selected(size_t selected)
{ {
if (selected == m_selected) if (selected == m_selected)
-1
View File
@@ -10,7 +10,6 @@ class UI_combobox : public UI_item
UI_combobox(QObject *parent = nullptr); UI_combobox(QObject *parent = nullptr);
UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0, UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0,
QObject *parent = nullptr); QObject *parent = nullptr);
UI_combobox(const json &j, QObject *parent = nullptr);
void set_selected(size_t selected) override; void set_selected(size_t selected) override;
void set_selected(std::string selected); void set_selected(std::string selected);
+1 -2
View File
@@ -22,9 +22,8 @@ UI_dial::UI_dial(std::string text, double min, double max, double value, QObject
{ {
} }
UI_dial::UI_dial(const json &j, QObject *parent) : UI_item(UI_DIAL_TYPE, parent) UI_dial::UI_dial(QObject *parent) : UI_item(UI_DIAL_TYPE, parent)
{ {
from_json(j);
} }
void UI_dial::set_value(double value) void UI_dial::set_value(double value)
+1 -1
View File
@@ -13,7 +13,7 @@ class UI_dial : public UI_item
UI_dial(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 *parent = nullptr); QObject *parent = nullptr);
UI_dial(std::string text, double min, double max, double value, QObject *parent = nullptr); UI_dial(std::string text, double min, double max, double value, QObject *parent = nullptr);
UI_dial(const json &j, QObject *parent = nullptr); UI_dial(QObject *parent = nullptr);
void set_value(double value) override; void set_value(double value) override;
double value() const; double value() const;
+1 -2
View File
@@ -5,9 +5,8 @@ UI_label::UI_label(std::string text, Color fg_color, Color bg_color, QObject *pa
{ {
} }
UI_label::UI_label(const json &j, QObject *parent) : UI_item(UI_LABEL_TYPE, parent) UI_label::UI_label(QObject *parent) : UI_item(UI_LABEL_TYPE, parent)
{ {
from_json(j);
} }
void UI_label::from_json(const json &j) void UI_label::from_json(const json &j)
+1 -1
View File
@@ -9,7 +9,7 @@ class UI_label : public UI_item
Q_OBJECT Q_OBJECT
public: public:
UI_label(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_label(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_label(const json &j, QObject *parent = nullptr); UI_label(QObject *parent = nullptr);
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json(size_t id) const override; json to_json(size_t id) const override;
-5
View File
@@ -9,11 +9,6 @@ UI_led::UI_led(std::string text, Color fg_color, Color bg_color, QObject *parent
{ {
} }
UI_led::UI_led(const json &j, QObject *parent) : UI_item(UI_LED_TYPE, parent)
{
from_json(j);
}
bool UI_led::set_led(bool state, Color on, Color off) bool UI_led::set_led(bool state, Color on, Color off)
{ {
set_bg_color(state ? on : off); set_bg_color(state ? on : off);
-1
View File
@@ -10,7 +10,6 @@ class UI_led : public UI_item
public: public:
UI_led(QObject *parent = nullptr); UI_led(QObject *parent = nullptr);
UI_led(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_led(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_led(const json &j, QObject *parent = nullptr);
bool set_led(bool state, Color on, Color off); bool set_led(bool state, Color on, Color off);
-5
View File
@@ -10,11 +10,6 @@ UI_plot::UI_plot(std::string text, std::string x_label, std::string y_label, QOb
set_text(text); set_text(text);
} }
UI_plot::UI_plot(const json &j, QObject *parent) : UI_item{UI_PLOT_TYPE, parent}
{
from_json(j);
}
void UI_plot::set_x_label(std::string name) void UI_plot::set_x_label(std::string name)
{ {
m_x_label = name; m_x_label = name;
-1
View File
@@ -8,7 +8,6 @@ class UI_plot : public UI_item
public: public:
explicit UI_plot(QObject *parent = nullptr); explicit UI_plot(QObject *parent = nullptr);
UI_plot(std::string text, std::string x_label, std::string y_label, QObject *parent = nullptr); UI_plot(std::string text, std::string x_label, std::string y_label, QObject *parent = nullptr);
UI_plot(const json &j, QObject *parent = nullptr);
void set_x_label(std::string name); void set_x_label(std::string name);
std::string x_label() const; std::string x_label() const;
+1 -2
View File
@@ -22,9 +22,8 @@ UI_slider::UI_slider(std::string text, double min, double max, double value, QOb
{ {
} }
UI_slider::UI_slider(const json &j, QObject *parent) : UI_item(UI_SLIDER_TYPE, parent) UI_slider::UI_slider(QObject *parent) : UI_item(UI_SLIDER_TYPE, parent)
{ {
from_json(j);
} }
void UI_slider::set_value(double value) void UI_slider::set_value(double value)
+1 -1
View File
@@ -13,7 +13,7 @@ class UI_slider : public UI_item
UI_slider(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 *parent = nullptr); QObject *parent = nullptr);
UI_slider(std::string text, double min, double max, double value, QObject *parent = nullptr); UI_slider(std::string text, double min, double max, double value, QObject *parent = nullptr);
UI_slider(const json &j, QObject *parent = nullptr); UI_slider(QObject *parent = nullptr);
void set_value(double value) override; void set_value(double value) override;
double value() const; double value() const;
+1 -2
View File
@@ -5,9 +5,8 @@ UI_textbox::UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject
{ {
} }
UI_textbox::UI_textbox(const json &j, QObject *parent) : UI_item(UI_TEXTBOX_TYPE, parent) UI_textbox::UI_textbox(QObject *parent) : UI_item(UI_TEXTBOX_TYPE, parent)
{ {
from_json(j);
} }
void UI_textbox::set_text(std::string text) void UI_textbox::set_text(std::string text)
+1 -1
View File
@@ -9,7 +9,7 @@ class UI_textbox : public UI_item
Q_OBJECT Q_OBJECT
public: public:
UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_textbox(const json &j, QObject *parent = nullptr); UI_textbox(QObject *parent = nullptr);
void set_text(std::string text) override; void set_text(std::string text) override;
+1 -2
View File
@@ -22,9 +22,8 @@ UI_thermo::UI_thermo(std::string text, double min, double max, double value, QOb
{ {
} }
UI_thermo::UI_thermo(const json &j, QObject *parent) : UI_item(UI_THERMO_TYPE, parent) UI_thermo::UI_thermo(QObject *parent) : UI_item(UI_THERMO_TYPE, parent)
{ {
from_json(j);
} }
void UI_thermo::set_value(double value) void UI_thermo::set_value(double value)
+1 -1
View File
@@ -14,7 +14,7 @@ class UI_thermo : public UI_item
UI_thermo(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 *parent = nullptr); QObject *parent = nullptr);
UI_thermo(std::string text, double min, double max, double value, QObject *parent = nullptr); UI_thermo(std::string text, double min, double max, double value, QObject *parent = nullptr);
UI_thermo(const json &j, QObject *parent = nullptr); UI_thermo(QObject *parent = nullptr);
void set_value(double value) override; void set_value(double value) override;
double value() const; double value() const;
+2 -1
View File
@@ -5,7 +5,8 @@ TEST(UI_button, to_from_json)
{ {
UI_button button("button_text", Color(255, 255, 255), Color(0, 0, 0), nullptr); UI_button button("button_text", Color(255, 255, 255), Color(0, 0, 0), nullptr);
json j = button.to_json(0); json j = button.to_json(0);
UI_button button2(j); UI_button button2;
button2.from_json(j);
EXPECT_EQ(button.text(), button2.text()); EXPECT_EQ(button.text(), button2.text());
EXPECT_EQ(button.fg_color(), button2.fg_color()); EXPECT_EQ(button.fg_color(), button2.fg_color());
EXPECT_EQ(button.bg_color(), button2.bg_color()); EXPECT_EQ(button.bg_color(), button2.bg_color());
+2 -1
View File
@@ -4,7 +4,8 @@ TEST(UI_checkbox, to_from_json)
{ {
UI_checkbox checkbox("checkbox_text", Color(255, 255, 255), Color(0, 0, 0)); UI_checkbox checkbox("checkbox_text", Color(255, 255, 255), Color(0, 0, 0));
json j = checkbox.to_json(0); json j = checkbox.to_json(0);
UI_checkbox checkbox2(j); UI_checkbox checkbox2;
checkbox2.from_json(j);
EXPECT_EQ(checkbox.text(), checkbox2.text()); EXPECT_EQ(checkbox.text(), checkbox2.text());
EXPECT_EQ(checkbox.fg_color(), checkbox2.fg_color()); EXPECT_EQ(checkbox.fg_color(), checkbox2.fg_color());
EXPECT_EQ(checkbox.bg_color(), checkbox2.bg_color()); EXPECT_EQ(checkbox.bg_color(), checkbox2.bg_color());
+8 -7
View File
@@ -2,11 +2,12 @@
TEST(UI_combobox, to_from_json) TEST(UI_combobox, to_from_json)
{ {
UI_combobox checkbox(Color(255, 255, 255), Color(0, 0, 0), {"option1", "option2"}); UI_combobox combobox(Color(255, 255, 255), Color(0, 0, 0), {"option1", "option2"});
json j = checkbox.to_json(0); json j = combobox.to_json(0);
UI_combobox checkbox2(j); UI_combobox combobox2;
EXPECT_EQ(checkbox.text(), checkbox2.text()); combobox2.from_json(j);
EXPECT_EQ(checkbox.fg_color(), checkbox2.fg_color()); EXPECT_EQ(combobox.text(), combobox2.text());
EXPECT_EQ(checkbox.bg_color(), checkbox2.bg_color()); EXPECT_EQ(combobox.fg_color(), combobox2.fg_color());
EXPECT_EQ(checkbox.selected(), checkbox2.selected()); EXPECT_EQ(combobox.bg_color(), combobox2.bg_color());
EXPECT_EQ(combobox.selected(), combobox2.selected());
} }
+2 -1
View File
@@ -4,7 +4,8 @@ TEST(UI_dial, to_from_json)
{ {
UI_dial dial("slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50); UI_dial dial("slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50);
json j = dial.to_json(0); json j = dial.to_json(0);
UI_dial dial2(j); UI_dial dial2;
dial2.from_json(j);
EXPECT_EQ(dial.text(), dial2.text()); EXPECT_EQ(dial.text(), dial2.text());
EXPECT_EQ(dial.fg_color(), dial2.fg_color()); EXPECT_EQ(dial.fg_color(), dial2.fg_color());
EXPECT_EQ(dial.bg_color(), dial2.bg_color()); EXPECT_EQ(dial.bg_color(), dial2.bg_color());
+2 -1
View File
@@ -10,7 +10,8 @@ TEST(UI_label, to_from_json)
EXPECT_EQ(j["fg_color"], "#000000FF"); EXPECT_EQ(j["fg_color"], "#000000FF");
EXPECT_EQ(j["bg_color"], "#FFFFFFFF"); EXPECT_EQ(j["bg_color"], "#FFFFFFFF");
UI_label label2(j); UI_label label2;
label2.from_json(j);
EXPECT_EQ(label2.text(), "label_text"); EXPECT_EQ(label2.text(), "label_text");
EXPECT_EQ(label2.fg_color(), Color(0, 0, 0)); EXPECT_EQ(label2.fg_color(), Color(0, 0, 0));
EXPECT_EQ(label2.bg_color(), Color(255, 255, 255)); EXPECT_EQ(label2.bg_color(), Color(255, 255, 255));
+2 -1
View File
@@ -4,7 +4,8 @@ TEST(UI_led, to_from_json)
{ {
UI_led led("led_text", Color(0, 0, 0), Color(255, 255, 255)); UI_led led("led_text", Color(0, 0, 0), Color(255, 255, 255));
json j = led.to_json(0); json j = led.to_json(0);
UI_led led2(j); UI_led led2;
led2.from_json(j);
EXPECT_EQ(led.text(), led2.text()); EXPECT_EQ(led.text(), led2.text());
EXPECT_EQ(led.fg_color(), led2.fg_color()); EXPECT_EQ(led.fg_color(), led2.fg_color());
EXPECT_EQ(led.bg_color(), led2.bg_color()); EXPECT_EQ(led.bg_color(), led2.bg_color());
+2 -1
View File
@@ -4,7 +4,8 @@ TEST(UI_slider, to_from_json)
{ {
UI_slider slider("slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50, nullptr); UI_slider slider("slider_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50, nullptr);
json j = slider.to_json(0); json j = slider.to_json(0);
UI_slider slider2(j); UI_slider slider2;
slider2.from_json(j);
EXPECT_EQ(slider.text(), slider2.text()); EXPECT_EQ(slider.text(), slider2.text());
EXPECT_EQ(slider.fg_color(), slider2.fg_color()); EXPECT_EQ(slider.fg_color(), slider2.fg_color());
EXPECT_EQ(slider.bg_color(), slider2.bg_color()); EXPECT_EQ(slider.bg_color(), slider2.bg_color());
+2 -1
View File
@@ -4,7 +4,8 @@ TEST(UI_textbox, to_from_json)
{ {
UI_textbox textbox("hint", Color(0, 0, 0), Color(255, 255, 255)); UI_textbox textbox("hint", Color(0, 0, 0), Color(255, 255, 255));
json j = textbox.to_json(0); json j = textbox.to_json(0);
UI_textbox textbox2(j); UI_textbox textbox2;
textbox2.from_json(j);
EXPECT_EQ(textbox.text(), textbox2.text()); EXPECT_EQ(textbox.text(), textbox2.text());
EXPECT_EQ(textbox.fg_color(), textbox2.fg_color()); EXPECT_EQ(textbox.fg_color(), textbox2.fg_color());
EXPECT_EQ(textbox.bg_color(), textbox2.bg_color()); EXPECT_EQ(textbox.bg_color(), textbox2.bg_color());
+2 -1
View File
@@ -4,7 +4,8 @@ TEST(UI_thermo, to_from_json)
{ {
UI_thermo thermo("thermo_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50); UI_thermo thermo("thermo_text", Color(255, 255, 255), Color(0, 0, 0), 0, 100, 50);
json j = thermo.to_json(0); json j = thermo.to_json(0);
UI_thermo thermo2(j); UI_thermo thermo2;
thermo2.from_json(j);
EXPECT_EQ(thermo.text(), thermo2.text()); EXPECT_EQ(thermo.text(), thermo2.text());
EXPECT_EQ(thermo.fg_color(), thermo2.fg_color()); EXPECT_EQ(thermo.fg_color(), thermo2.fg_color());
EXPECT_EQ(thermo.bg_color(), thermo2.bg_color()); EXPECT_EQ(thermo.bg_color(), thermo2.bg_color());