Remove Text_size
Add setup_item to configure if an item uses text, fg color and bg color.
This commit is contained in:
2026-02-14 14:32:08 +01:00
parent ef4df1718b
commit efcb183682
19 changed files with 78 additions and 76 deletions
+3 -2
View File
@@ -12,13 +12,14 @@ template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
return stdArray; return stdArray;
} }
UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent) UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent) : UI_item(UI_CAN_TYPE, "", fg_color, bg_color, parent)
: UI_item(UI_CAN_TYPE, "", 12, fg_color, bg_color, parent)
{ {
setup_item(false, true, true);
} }
UI_can::UI_can(const json &j, QObject *parent) : UI_item(UI_CAN_TYPE, parent) UI_can::UI_can(const json &j, QObject *parent) : UI_item(UI_CAN_TYPE, parent)
{ {
setup_item(false, true, true);
from_json(j); from_json(j);
} }
+1 -1
View File
@@ -1,7 +1,7 @@
#include "ui_button.hpp" #include "ui_button.hpp"
UI_button::UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_button::UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_BUTTON_TYPE, text, 12, fg_color, bg_color, parent) : UI_item(UI_BUTTON_TYPE, text, fg_color, bg_color, parent)
{ {
} }
+1 -1
View File
@@ -5,7 +5,7 @@ UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m
} }
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_CHECKBOX_TYPE, text, 0, fg_color, bg_color, parent), m_checked(false) : UI_item(UI_CHECKBOX_TYPE, text, fg_color, bg_color, parent), m_checked(false)
{ {
} }
+4 -1
View File
@@ -2,16 +2,19 @@
UI_combobox::UI_combobox(QObject *parent) : UI_item(UI_COMBOBOX_TYPE, parent), m_options() UI_combobox::UI_combobox(QObject *parent) : UI_item(UI_COMBOBOX_TYPE, parent), m_options()
{ {
setup_item(false, true, true);
} }
UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected, UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected,
QObject *parent) QObject *parent)
: UI_item(UI_COMBOBOX_TYPE, "", 12, fg_color, bg_color, parent), m_options(options), m_selected(selected) : UI_item(UI_COMBOBOX_TYPE, "", fg_color, bg_color, parent), m_options(options), m_selected(selected)
{ {
setup_item(false, true, true);
} }
UI_combobox::UI_combobox(const json &j, QObject *parent) : UI_item(UI_COMBOBOX_TYPE, parent) UI_combobox::UI_combobox(const json &j, QObject *parent) : UI_item(UI_COMBOBOX_TYPE, parent)
{ {
setup_item(false, true, true);
from_json(j); from_json(j);
} }
+1 -1
View File
@@ -2,7 +2,7 @@
UI_dial::UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, UI_dial::UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent) double value, QObject *parent)
: UI_item(UI_DIAL_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit) : UI_item(UI_DIAL_TYPE, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
{ {
} }
+2
View File
@@ -2,11 +2,13 @@
UI_double_spinbox::UI_double_spinbox(QObject *parent) : UI_item(UI_DOUBLE_SPINBOX_TYPE, parent) UI_double_spinbox::UI_double_spinbox(QObject *parent) : UI_item(UI_DOUBLE_SPINBOX_TYPE, parent)
{ {
setup_item(false, false, false);
} }
UI_double_spinbox::UI_double_spinbox(double min, double max, double value, QObject *parent) UI_double_spinbox::UI_double_spinbox(double min, double max, double value, QObject *parent)
: UI_item(UI_DOUBLE_SPINBOX_TYPE, parent) : UI_item(UI_DOUBLE_SPINBOX_TYPE, parent)
{ {
setup_item(false, false, false);
m_min = min; m_min = min;
m_max = max; m_max = max;
m_value = value; m_value = value;
+2
View File
@@ -2,11 +2,13 @@
UI_hex_spinbox::UI_hex_spinbox(QObject *parent) : UI_item(UI_HEX_SPINBOX_TYPE, parent) UI_hex_spinbox::UI_hex_spinbox(QObject *parent) : UI_item(UI_HEX_SPINBOX_TYPE, parent)
{ {
setup_item(false, false, false);
} }
UI_hex_spinbox::UI_hex_spinbox(double min, double max, double value, QObject *parent) UI_hex_spinbox::UI_hex_spinbox(double min, double max, double value, QObject *parent)
: UI_item(UI_HEX_SPINBOX_TYPE, parent) : UI_item(UI_HEX_SPINBOX_TYPE, parent)
{ {
setup_item(false, false, false);
m_min = min; m_min = min;
m_max = max; m_max = max;
m_value = value; m_value = value;
+32 -31
View File
@@ -1,14 +1,14 @@
#include "ui_item.hpp" #include "ui_item.hpp"
UI_item::UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, QObject *parent) UI_item::UI_item(std::string type, std::string text, Color fg_color, Color bg_color, QObject *parent)
: QObject(parent), m_id((size_t)-1), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), : QObject(parent), m_id((size_t)-1), m_type(type), m_text(text), m_fg_color(fg_color), m_bg_color(bg_color),
m_bg_color(bg_color), enabled(true), visible(true) m_enabled(true), m_visible(true)
{ {
} }
UI_item::UI_item(std::string type, QObject *parent) UI_item::UI_item(std::string type, QObject *parent)
: QObject(parent), m_id((size_t)-1), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Default), : QObject(parent), m_id((size_t)-1), m_type(type), m_text(""), m_fg_color(Color::Default),
m_bg_color(Color::Default), enabled(true), visible(true) m_bg_color(Color::Default), m_enabled(true), m_visible(true)
{ {
} }
@@ -55,20 +55,6 @@ std::string UI_item::text() const
return m_text; return m_text;
} }
void UI_item::set_text_size(uint8_t text_size)
{
if (this->m_text_size != text_size)
{
this->m_text_size = text_size;
emit ui_changed();
}
}
uint8_t UI_item::text_size() const
{
return m_text_size;
}
void UI_item::set_fg_color(Color fg_color) void UI_item::set_fg_color(Color fg_color)
{ {
if (this->m_fg_color != fg_color) if (this->m_fg_color != fg_color)
@@ -99,30 +85,30 @@ Color UI_item::bg_color() const
void UI_item::set_enabled(bool enabled) void UI_item::set_enabled(bool enabled)
{ {
if (this->enabled != enabled) if (this->m_enabled != enabled)
{ {
this->enabled = enabled; this->m_enabled = enabled;
emit ui_changed(); emit ui_changed();
} }
} }
bool UI_item::is_enabled() const bool UI_item::is_enabled() const
{ {
return enabled; return m_enabled;
} }
void UI_item::set_visible(bool visible) void UI_item::set_visible(bool visible)
{ {
if (this->visible != visible) if (this->m_visible != visible)
{ {
this->visible = visible; this->m_visible = visible;
emit ui_changed(); emit ui_changed();
} }
} }
bool UI_item::is_visible() const bool UI_item::is_visible() const
{ {
return visible; return m_visible;
} }
void UI_item::from_json(const json &j) void UI_item::from_json(const json &j)
@@ -130,16 +116,22 @@ void UI_item::from_json(const json &j)
if (!is_type(j)) if (!is_type(j))
return; return;
j.at("id").get_to(m_id); j.at("id").get_to(m_id);
if (j.contains("text"))
j.at("text").get_to(m_text); j.at("text").get_to(m_text);
j.at("text_size").get_to(m_text_size);
std::string color; std::string color;
if (j.contains("fg_color"))
{
j.at("fg_color").get_to(color); j.at("fg_color").get_to(color);
m_fg_color = Color(color); m_fg_color = Color(color);
color.clear(); color.clear();
}
if (j.contains("bg_color"))
{
j.at("bg_color").get_to(color); j.at("bg_color").get_to(color);
m_bg_color = Color(color); m_bg_color = Color(color);
j.at("enabled").get_to(enabled); }
j.at("visible").get_to(visible); j.at("enabled").get_to(m_enabled);
j.at("visible").get_to(m_visible);
} }
json UI_item::to_json(size_t id) const json UI_item::to_json(size_t id) const
@@ -147,12 +139,14 @@ json UI_item::to_json(size_t id) const
json j; json j;
j["id"] = id; j["id"] = id;
j["type"] = m_type; j["type"] = m_type;
if (m_text_enabled)
j["text"] = m_text; j["text"] = m_text;
j["text_size"] = m_text_size; if (m_fg_color_enabled)
j["fg_color"] = m_fg_color.to_hex(); j["fg_color"] = m_fg_color.to_hex();
if (m_bg_color_enabled)
j["bg_color"] = m_bg_color.to_hex(); j["bg_color"] = m_bg_color.to_hex();
j["enabled"] = enabled; j["enabled"] = m_enabled;
j["visible"] = visible; j["visible"] = m_visible;
return j; return j;
} }
@@ -177,3 +171,10 @@ void UI_item::can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload)
void UI_item::can_clear() void UI_item::can_clear()
{ {
} }
void UI_item::setup_item(bool text, bool fg_color, bool bg_color)
{
m_text_enabled = text;
m_fg_color_enabled = fg_color;
m_bg_color_enabled = bg_color;
}
+8 -8
View File
@@ -15,8 +15,7 @@ class UI_item : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit UI_item(std::string type, QObject *parent = nullptr); explicit UI_item(std::string type, QObject *parent = nullptr);
explicit UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, explicit UI_item(std::string type, std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
QObject *parent = nullptr);
explicit UI_item(const json &j, QObject *parent = nullptr); explicit UI_item(const json &j, QObject *parent = nullptr);
~UI_item(); ~UI_item();
@@ -27,9 +26,6 @@ class UI_item : public QObject
void append_text(std::string text); void append_text(std::string text);
std::string text() const; std::string text() const;
void set_text_size(uint8_t text_size);
uint8_t text_size() const;
void set_fg_color(Color fg_color); void set_fg_color(Color fg_color);
Color fg_color() const; Color fg_color() const;
@@ -56,12 +52,16 @@ class UI_item : public QObject
void ui_changed(); void ui_changed();
protected: protected:
void setup_item(bool text, bool fg_color, bool bg_color);
size_t 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; bool m_text_enabled = true;
Color m_fg_color; Color m_fg_color;
bool m_fg_color_enabled = true;
Color m_bg_color; Color m_bg_color;
bool enabled; bool m_bg_color_enabled = true;
bool visible; bool m_enabled;
bool m_visible;
}; };
+1 -1
View File
@@ -1,7 +1,7 @@
#include "ui_label.hpp" #include "ui_label.hpp"
UI_label::UI_label(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_label::UI_label(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_LABEL_TYPE, text, 12, fg_color, bg_color, parent) : UI_item(UI_LABEL_TYPE, text, fg_color, bg_color, parent)
{ {
} }
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_led.hpp" #include "ui_led.hpp"
UI_led::UI_led(QObject *parent) : UI_item(UI_LED_TYPE, "", 12, Color::Default, Color::Default, parent) UI_led::UI_led(QObject *parent) : UI_item(UI_LED_TYPE, "", Color::Default, Color::Default, parent)
{ {
} }
UI_led::UI_led(std::string text, Color fg_color, Color bg_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) : UI_item(UI_LED_TYPE, text, fg_color, bg_color, parent)
{ {
} }
+1 -2
View File
@@ -2,8 +2,7 @@
UI_slider::UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, UI_slider::UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent) double value, QObject *parent)
: UI_item(UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), : UI_item(UI_SLIDER_TYPE, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
m_unit(unit)
{ {
} }
+2
View File
@@ -2,10 +2,12 @@
UI_spinbox::UI_spinbox(QObject *parent) : UI_item(UI_SPINBOX_TYPE, parent) UI_spinbox::UI_spinbox(QObject *parent) : UI_item(UI_SPINBOX_TYPE, parent)
{ {
setup_item(false, false, false);
} }
UI_spinbox::UI_spinbox(double min, double max, double value, QObject *parent) : UI_item(UI_SPINBOX_TYPE, parent) UI_spinbox::UI_spinbox(double min, double max, double value, QObject *parent) : UI_item(UI_SPINBOX_TYPE, parent)
{ {
setup_item(false, false, false);
m_min = min; m_min = min;
m_max = max; m_max = max;
m_value = value; m_value = value;
+2
View File
@@ -2,11 +2,13 @@
UI_tab_widget::UI_tab_widget(QObject *parent) : UI_item(UI_TAB_WIDGET_TYPE, parent) UI_tab_widget::UI_tab_widget(QObject *parent) : UI_item(UI_TAB_WIDGET_TYPE, parent)
{ {
setup_item(false, false, false);
} }
UI_tab_widget::UI_tab_widget(std::vector<std::string> tab_names, size_t selected, QObject *parent) UI_tab_widget::UI_tab_widget(std::vector<std::string> tab_names, size_t selected, QObject *parent)
: UI_item(UI_TAB_WIDGET_TYPE, parent), m_selected_tab(selected), m_tab_names(tab_names) : UI_item(UI_TAB_WIDGET_TYPE, parent), m_selected_tab(selected), m_tab_names(tab_names)
{ {
setup_item(false, false, false);
m_tab_visible.resize(m_tab_names.size(), true); m_tab_visible.resize(m_tab_names.size(), true);
} }
+2
View File
@@ -2,6 +2,7 @@
UI_table::UI_table(QObject *parent) : UI_item(UI_TABLE_TYPE, parent), m_row_count(0), m_column_count(0) UI_table::UI_table(QObject *parent) : UI_item(UI_TABLE_TYPE, parent), m_row_count(0), m_column_count(0)
{ {
setup_item(false, false, false);
} }
UI_table::UI_table(size_t row_count, size_t column_count, std::vector<std::string> row_labels, UI_table::UI_table(size_t row_count, size_t column_count, std::vector<std::string> row_labels,
@@ -9,6 +10,7 @@ UI_table::UI_table(size_t row_count, size_t column_count, std::vector<std::strin
: UI_item(UI_TABLE_TYPE, parent), m_row_count(row_count), m_column_count(column_count), m_row_labels(row_labels), : UI_item(UI_TABLE_TYPE, parent), m_row_count(row_count), m_column_count(column_count), m_row_labels(row_labels),
m_column_labels(column_labels), m_valid(), m_table() m_column_labels(column_labels), m_valid(), m_table()
{ {
setup_item(false, false, false);
m_table.resize(m_column_count * m_row_count); m_table.resize(m_column_count * m_row_count);
m_valid.resize(m_column_count * m_row_count); m_valid.resize(m_column_count * m_row_count);
} }
+1 -1
View File
@@ -1,7 +1,7 @@
#include "ui_textbox.hpp" #include "ui_textbox.hpp"
UI_textbox::UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent) UI_textbox::UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color, parent), m_hint(hint) : UI_item(UI_TEXTBOX_TYPE, "", fg_color, bg_color, parent), m_hint(hint)
{ {
} }
+1 -2
View File
@@ -2,8 +2,7 @@
UI_thermo::UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, UI_thermo::UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent) double value, QObject *parent)
: UI_item(UI_THERMO_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), : UI_item(UI_THERMO_TYPE, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
m_unit(unit)
{ {
} }
+1 -10
View File
@@ -14,13 +14,6 @@ TEST(UI_item, set_text)
EXPECT_EQ(item.text(), "text"); EXPECT_EQ(item.text(), "text");
} }
TEST(UI_item, text_size)
{
UI_item item(std::string("item"));
item.set_text_size(12);
EXPECT_EQ(item.text_size(), 12);
}
TEST(UI_item, set_fg_color) TEST(UI_item, set_fg_color)
{ {
UI_item item(std::string("item")); UI_item item(std::string("item"));
@@ -56,19 +49,17 @@ TEST(UI_item, from_json)
item.from_json(j); item.from_json(j);
EXPECT_EQ(item.text(), "item_text"); EXPECT_EQ(item.text(), "item_text");
EXPECT_EQ(item.text_size(), 12);
EXPECT_EQ(item.fg_color(), Color(255, 255, 255)); EXPECT_EQ(item.fg_color(), Color(255, 255, 255));
EXPECT_EQ(item.bg_color(), Color(0, 0, 0)); EXPECT_EQ(item.bg_color(), Color(0, 0, 0));
} }
TEST(UI_item, to_json) TEST(UI_item, to_json)
{ {
UI_item item("item", "item_text", 12, Color(255, 255, 255), Color(0, 0, 0)); UI_item item("item", "item_text", Color(255, 255, 255), Color(0, 0, 0));
json j = item.to_json(0); json j = item.to_json(0);
EXPECT_EQ(j.at("type"), "item"); EXPECT_EQ(j.at("type"), "item");
EXPECT_EQ(j.at("id"), 0); EXPECT_EQ(j.at("id"), 0);
EXPECT_EQ(j.at("text"), "item_text"); EXPECT_EQ(j.at("text"), "item_text");
EXPECT_EQ(j.at("text_size"), 12);
EXPECT_EQ(j.at("fg_color"), Color(255, 255, 255).to_hex()); EXPECT_EQ(j.at("fg_color"), Color(255, 255, 255).to_hex());
EXPECT_EQ(j.at("bg_color"), Color(0, 0, 0).to_hex()); EXPECT_EQ(j.at("bg_color"), Color(0, 0, 0).to_hex());
} }
-2
View File
@@ -7,13 +7,11 @@ TEST(UI_label, to_from_json)
EXPECT_EQ(j["type"], "ui_label"); EXPECT_EQ(j["type"], "ui_label");
EXPECT_EQ(j["id"], 0); EXPECT_EQ(j["id"], 0);
EXPECT_EQ(j["text"], "label_text"); EXPECT_EQ(j["text"], "label_text");
EXPECT_EQ(j["text_size"], 12);
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(j);
EXPECT_EQ(label2.text(), "label_text"); EXPECT_EQ(label2.text(), "label_text");
EXPECT_EQ(label2.text_size(), 12);
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));
} }