sim base clean

UI now uses bg_color as led_color
Move add_UI_item to push_ui_item
This commit is contained in:
2026-02-14 14:30:38 +01:00
parent d345affd63
commit b9d3c432bc
9 changed files with 30 additions and 51 deletions
+3 -3
View File
@@ -393,8 +393,8 @@ void SimulatorBase::process_ui_led(json &uiItem)
return; return;
} }
QColor bg_color = QColor(QString::fromStdString(uiItem["led_color"])); QColor bg_color = QColor(QString::fromStdString(uiItem["bg_color"]));
QColor color = QColor(~bg_color.red(), ~bg_color.green(), ~bg_color.blue()); QColor color = QColor(QString::fromStdString(uiItem["fg_color"]));
QString text = QString::fromStdString(uiItem["text"]); QString text = QString::fromStdString(uiItem["text"]);
if (text != led->text()) if (text != led->text())
@@ -426,7 +426,7 @@ void SimulatorBase::process_ui_led(json &uiItem)
return; return;
} }
if (bg_color != widget_bg_color(led)) if (bg_color != widget_bg_color(led) || color != widget_color(led))
{ {
set_widget_color(led, color, bg_color); set_widget_color(led, color, bg_color);
} }
+1 -1
+1 -1
View File
@@ -13,7 +13,7 @@ std::string Simulator_base::name() const
return m_name; return m_name;
} }
void Simulator_base::add_UI_item(UI_item *item) void Simulator_base::push_ui_item(UI_item *item)
{ {
// Add a UI item to the simulator // Add a UI item to the simulator
connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); }); connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); });
+1 -1
View File
@@ -31,7 +31,7 @@ class Simulator_base : public QObject
* *
* @param item The UI item to add * @param item The UI item to add
*/ */
void add_UI_item(UI_item *item); void push_ui_item(UI_item *item);
/** /**
* @brief Get the UI items object * @brief Get the UI items object
* *
+1 -1
View File
@@ -4,7 +4,7 @@ Can_Debugger::Can_Debugger(CAN_Wrapper *can_wrapper, QObject *parent)
: Simulator_base("Can_Debugger", can_wrapper, std::chrono::milliseconds(100), parent), m_wrapper(can_wrapper), : Simulator_base("Can_Debugger", can_wrapper, std::chrono::milliseconds(100), parent), m_wrapper(can_wrapper),
m_can_ui(Color::Black, Color::White) m_can_ui(Color::Black, Color::White)
{ {
this->add_UI_item(&this->m_can_ui); this->push_ui_item(&this->m_can_ui);
connect(&m_can_ui, &UI_can::on_send, this, [=, this](QCanBusFrame frame) { m_wrapper->send_can_message(frame); }); connect(&m_can_ui, &UI_can::on_send, this, [=, this](QCanBusFrame frame) { m_wrapper->send_can_message(frame); });
} }
+15 -15
View File
@@ -11,21 +11,21 @@ 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_thermo("Thermo", "%", Color(Color::White), Color(Color::Black), 0, 100, 0),
m_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::Red);
this->m_thermo.set_end_color(Color(Color::Red)); this->m_thermo.set_end_color(Color::Red);
this->m_thermo.add_color_stop(0.10, Color(Color::Yellow)); this->m_thermo.add_color_stop(0.10, Color::Yellow);
this->m_thermo.add_color_stop(0.50, Color(Color::Green)); this->m_thermo.add_color_stop(0.50, Color::Green);
this->m_thermo.add_color_stop(0.60, Color(Color::Yellow)); this->m_thermo.add_color_stop(0.60, Color::Yellow);
this->m_thermo.add_color_stop(0.90, Color(Color::Red)); this->m_thermo.add_color_stop(0.90, Color::Red);
this->add_UI_item(&this->m_button); this->push_ui_item(&this->m_button);
this->add_UI_item(&this->m_combobox); this->push_ui_item(&this->m_combobox);
this->add_UI_item(&this->m_label); this->push_ui_item(&this->m_label);
this->add_UI_item(&this->m_slider); this->push_ui_item(&this->m_slider);
this->add_UI_item(&this->m_dial); this->push_ui_item(&this->m_dial);
this->add_UI_item(&this->m_thermo); this->push_ui_item(&this->m_thermo);
this->add_UI_item(&this->m_led); this->push_ui_item(&this->m_led);
this->add_UI_item(&this->m_checkbox); this->push_ui_item(&this->m_checkbox);
QObject::connect(&m_button, &UI_button::on_click, this, [=, this] { button_clicked(&m_button); }); QObject::connect(&m_button, &UI_button::on_click, this, [=, this] { button_clicked(&m_button); });
QObject::connect(&m_slider, &UI_slider::on_change, this, QObject::connect(&m_slider, &UI_slider::on_change, this,
@@ -56,7 +56,7 @@ void Test_Sim::button_clicked(UI_button *id)
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));
this->m_button.set_text(state ? "On" : "Off"); this->m_button.set_text(state ? "On" : "Off");
this->m_led.set_led_color(state ? Color(Color::Green) : Color(Color::Red)); this->m_led.set_bg_color(state ? Color(Color::Green) : Color(Color::Red));
return; return;
} }
} }
+2 -20
View File
@@ -1,7 +1,7 @@
#include "UI_led.hpp" #include "UI_led.hpp"
UI_led::UI_led(std::string text, Color fg_color, Color bg_color, Color led_color, QObject *parent) UI_led::UI_led(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_LED_TYPE, text, 12, fg_color, bg_color, parent), m_led_color(led_color) : UI_item(UI_LED_TYPE, text, 12, fg_color, bg_color, parent)
{ {
} }
@@ -10,31 +10,13 @@ UI_led::UI_led(const json &j, QObject *parent) : UI_item(UI_LED_TYPE, parent)
from_json(j); from_json(j);
} }
Color UI_led::led_color() const
{
return m_led_color;
}
void UI_led::set_led_color(Color led_color)
{
if (m_led_color != led_color)
{
m_led_color = led_color;
emit value_changed();
}
}
void UI_led::from_json(const json &j) void UI_led::from_json(const json &j)
{ {
UI_item::from_json(j); UI_item::from_json(j);
std::string color;
j.at("led_color").get_to(color);
m_led_color = Color(color);
} }
json UI_led::to_json(size_t id) const json UI_led::to_json(size_t id) const
{ {
json j = UI_item::to_json(id); json j = UI_item::to_json(id);
j["led_color"] = m_led_color.to_hex();
return j; return j;
} }
+1 -4
View File
@@ -8,7 +8,7 @@ class UI_led : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_led(std::string text, Color fg_color, Color bg_color, Color led_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); UI_led(const json &j, QObject *parent = nullptr);
Color led_color() const; Color led_color() const;
@@ -16,7 +16,4 @@ class UI_led : public UI_item
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;
private:
Color m_led_color;
}; };
+4 -4
View File
@@ -2,7 +2,7 @@
TEST(UI_led, to_from_json) TEST(UI_led, to_from_json)
{ {
UI_led led("led_text", Color(0, 0, 0), Color(255, 255, 255), Color(255, 0, 0)); 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(j);
EXPECT_EQ(led.text(), led2.text()); EXPECT_EQ(led.text(), led2.text());
@@ -13,7 +13,7 @@ TEST(UI_led, to_from_json)
TEST(UI_led, set_led_color) TEST(UI_led, set_led_color)
{ {
UI_led led("led_text", Color(0, 0, 0), Color(255, 255, 255), Color(255, 0, 0)); UI_led led("led_text", Color(0, 0, 0), Color(255, 255, 255));
led.set_led_color(Color(0, 255, 0)); led.set_bg_color(Color(0, 255, 0));
EXPECT_EQ(led.led_color(), Color(0, 255, 0)); EXPECT_EQ(led.bg_color(), Color(0, 255, 0));
} }