Move UI folder

This commit is contained in:
2026-02-14 14:30:54 +01:00
parent e454e35bf9
commit 0bb146db25
28 changed files with 1 additions and 2 deletions
+62
View File
@@ -0,0 +1,62 @@
add_library(UI)
find_package(Qt6 REQUIRED COMPONENTS Core SerialBus)
set_target_properties(
UI
PROPERTIES
AUTOMOC ON
AUTORCC ON
)
target_compile_options(UI PRIVATE $<$<CONFIG:Debug>:
-Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion
>)
file(GLOB_RECURSE CUSTOM_UI_SOURCES "custom/*.?pp")
target_sources(UI
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/UI_item.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_item.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_button.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_button.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_checkbox.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_checkbox.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_combobox.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_combobox.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_dial.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_dial.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_label.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_label.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_led.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_led.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_slider.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_slider.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_textbox.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_textbox.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_thermo.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_thermo.hpp
${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.cpp
${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.hpp
${CUSTOM_UI_SOURCES}
)
target_include_directories(UI
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/custom/**
PRIVATE
${PROJECT_DIR}/external/**
${PROJECT_DIR}/src/CAN/
${CMAKE_BINARY_DIR}/**
)
target_link_libraries(UI
PUBLIC
Color
nlohmann_json::nlohmann_json
Qt::Core
Qt::SerialBus
)
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#include "UI_button.hpp"
#include "UI_checkbox.hpp"
#include "UI_combobox.hpp"
#include "UI_dial.hpp"
#include "UI_label.hpp"
#include "UI_led.hpp"
#include "UI_slider.hpp"
#include "UI_textbox.hpp"
#include "UI_thermo.hpp"
#include "custom/UI_can.hpp"
+27
View File
@@ -0,0 +1,27 @@
#include "UI_button.hpp"
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_button::UI_button(const json &j, QObject *parent) : UI_item(UI_BUTTON_TYPE, parent)
{
from_json(j);
}
void UI_button::from_json(const json &j)
{
UI_item::from_json(j);
}
json UI_button::to_json(size_t id) const
{
json j = UI_item::to_json(id);
return j;
}
void UI_button::click()
{
emit on_click();
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "UI_item.hpp"
#define UI_BUTTON_TYPE std::string("UI_button")
class UI_button : public UI_item
{
Q_OBJECT
public:
UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_button(const json &j, QObject *parent = nullptr);
void from_json(const json &j) override;
json to_json(size_t id) const override;
void click() override;
signals:
void on_click();
};
+51
View File
@@ -0,0 +1,51 @@
#include "UI_checkbox.hpp"
UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_checked()
{
}
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_checkbox::UI_checkbox(const json &j, QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_checked()
{
from_json(j);
}
void UI_checkbox::click()
{
m_checked = !m_checked;
emit value_changed();
emit on_change(m_checked);
}
void UI_checkbox::set_state(bool state)
{
m_checked = state;
emit value_changed();
emit on_change(m_checked);
}
bool UI_checkbox::state() const
{
return m_checked;
}
void UI_checkbox::from_json(const json &j)
{
UI_item::from_json(j);
if (j.contains("checked"))
{
m_checked = j["checked"];
}
}
json UI_checkbox::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["checked"] = m_checked;
return j;
}
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include "UI_item.hpp"
#include <cstdint>
#define UI_CHECKBOX_TYPE std::string("UI_checkbox")
class UI_checkbox : public UI_item
{
Q_OBJECT
public:
UI_checkbox(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 set_state(bool state);
bool state() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
signals:
void on_change(bool);
private:
bool m_checked;
};
+68
View File
@@ -0,0 +1,68 @@
#include "UI_combobox.hpp"
UI_combobox::UI_combobox(QObject *parent) : UI_x_boxes(UI_COMBOBOX_TYPE, parent)
{
}
UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected,
QObject *parent)
: UI_x_boxes(UI_COMBOBOX_TYPE, "", fg_color, bg_color, options, parent), m_selected(selected)
{
}
UI_combobox::UI_combobox(const json &j, QObject *parent) : UI_x_boxes(UI_COMBOBOX_TYPE, parent)
{
from_json(j);
}
void UI_combobox::set_selected(size_t selected)
{
if (selected == m_selected)
return;
m_selected = selected;
emit value_changed();
emit on_change(selected_text());
}
void UI_combobox::set_selected(std::string selected)
{
for (size_t i = 0; i < m_options.size(); i++)
{
if (m_options[i] == selected)
{
set_selected(i);
return;
}
}
}
size_t UI_combobox::selected() const
{
return m_selected;
}
std::string UI_combobox::selected_text() const
{
return m_options[m_selected];
}
void UI_combobox::from_json(const json &j)
{
UI_x_boxes::from_json(j);
std::string selected_str = j.at("selected");
for (size_t i = 0; i < m_options.size(); i++)
{
if (m_options[i] == selected_str)
{
m_selected = i;
break;
}
}
}
json UI_combobox::to_json(size_t id) const
{
json j = UI_x_boxes::to_json(id);
j["selected"] = m_options[m_selected];
return j;
}
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include "UI_item.hpp"
#include "UI_x_boxes.hpp"
#include <functional>
#define UI_COMBOBOX_TYPE std::string("UI_combobox")
class UI_combobox : public UI_x_boxes
{
Q_OBJECT
public:
UI_combobox(QObject *parent = nullptr);
UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0,
QObject *parent = nullptr);
UI_combobox(const json &j, QObject *parent = nullptr);
void set_selected(size_t selected);
void set_selected(std::string selected) override;
size_t selected() const;
std::string selected_text() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
signals:
void on_change(std::string selected);
private:
size_t m_selected;
};
+64
View File
@@ -0,0 +1,64 @@
#include "UI_dial.hpp"
UI_dial::UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
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_dial::UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent)
: UI_dial(text, "", fg_color, bg_color, min, max, value, parent)
{
}
UI_dial::UI_dial(const json &j, QObject *parent) : UI_item(UI_DIAL_TYPE, parent)
{
from_json(j);
}
void UI_dial::set_value(double value)
{
if (value < m_min || value > m_max)
return;
if (value == m_value)
return;
this->m_value = value;
emit value_changed();
}
double UI_dial::value() const
{
return m_value;
}
double UI_dial::min() const
{
return m_min;
}
double UI_dial::max() const
{
return m_max;
}
void UI_dial::from_json(const json &j)
{
UI_item::from_json(j);
this->m_min = j.at("min");
this->m_max = j.at("max");
this->m_value = j.at("value");
this->m_unit = j.at("unit");
}
json UI_dial::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["min"] = this->m_min;
j["max"] = this->m_max;
j["value"] = this->m_value;
j["unit"] = this->m_unit;
return j;
}
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include "UI_item.hpp"
#define UI_DIAL_TYPE std::string("UI_dial")
class UI_dial : public UI_item
{
Q_OBJECT
public:
UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
UI_dial(const json &j, QObject *parent = nullptr);
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
private:
double m_min;
double m_max;
double m_value;
std::string m_unit;
};
+173
View File
@@ -0,0 +1,173 @@
#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)
: QObject(parent), 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 *parent)
: QObject(parent), m_id((size_t)-1), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Default),
m_bg_color(Color::Default), enabled(true), visible(true)
{
}
UI_item::UI_item(const json &j, QObject *parent) : QObject(parent)
{
from_json(j);
}
UI_item::~UI_item()
{
}
std::string UI_item::type() const
{
return m_type;
}
bool UI_item::is_type(json j)
{
if (!j.contains("type"))
{
return false;
}
return j.at("type") == m_type;
}
void UI_item::set_text(std::string text)
{
if (this->m_text != text)
{
this->m_text = text;
emit value_changed();
}
}
std::string UI_item::text() const
{
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 value_changed();
}
}
uint8_t UI_item::text_size() const
{
return m_text_size;
}
void UI_item::set_fg_color(Color fg_color)
{
if (this->m_fg_color != fg_color)
{
this->m_fg_color = fg_color;
emit value_changed();
}
}
Color UI_item::fg_color() const
{
return m_fg_color;
}
void UI_item::set_bg_color(Color bg_color)
{
if (this->m_bg_color != bg_color)
{
this->m_bg_color = bg_color;
emit value_changed();
}
}
Color UI_item::bg_color() const
{
return m_bg_color;
}
void UI_item::set_enabled(bool enabled)
{
if (this->enabled != enabled)
{
this->enabled = enabled;
emit value_changed();
}
}
bool UI_item::is_enabled() const
{
return enabled;
}
void UI_item::set_visible(bool visible)
{
if (this->visible != visible)
{
this->visible = visible;
emit value_changed();
}
}
bool UI_item::is_visible() const
{
return visible;
}
void UI_item::from_json(const json &j)
{
if (!is_type(j))
return;
j.at("id").get_to(m_id);
j.at("text").get_to(m_text);
j.at("text_size").get_to(m_text_size);
std::string color;
j.at("fg_color").get_to(color);
m_fg_color = Color(color);
color.clear();
j.at("bg_color").get_to(color);
m_bg_color = Color(color);
j.at("enabled").get_to(enabled);
j.at("visible").get_to(visible);
}
json UI_item::to_json(size_t id) const
{
json j;
j["id"] = id;
j["type"] = m_type;
j["text"] = m_text;
j["text_size"] = m_text_size;
j["fg_color"] = m_fg_color.to_hex();
j["bg_color"] = m_bg_color.to_hex();
j["enabled"] = enabled;
j["visible"] = visible;
return j;
}
void UI_item::click() {};
void UI_item::set_selected(std::string selected)
{
UNUSED(selected);
};
void UI_item::set_value(double value)
{
UNUSED(value);
};
void UI_item::can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload)
{
UNUSED(id);
UNUSED(dlc);
UNUSED(payload);
};
void UI_item::can_clear()
{
}
+68
View File
@@ -0,0 +1,68 @@
#pragma once
#include "color.hpp"
#include <cstdint>
#include <functional>
#include <nlohmann/json.hpp>
#include <string>
#include <thread>
#include <QCanBusFrame>
#include <QObject>
using json = nlohmann::json;
#define UNUSED(x) (void)(x)
class UI_item : public QObject
{
Q_OBJECT
public:
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,
QObject *parent = nullptr);
explicit UI_item(const json &j, QObject *parent = nullptr);
~UI_item();
std::string type() const;
bool is_type(json j);
virtual void set_text(std::string text);
std::string text() const;
void set_text_size(uint8_t text_size);
uint8_t text_size() const;
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
void set_enabled(bool enabled);
bool is_enabled() const;
void set_visible(bool visible);
bool is_visible() const;
virtual void from_json(const json &j);
virtual json to_json(size_t id) const;
virtual void click();
virtual void set_selected(std::string selected);
virtual void set_value(double value);
virtual void can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload);
virtual void can_clear();
signals:
void value_changed();
protected:
size_t m_id;
std::string m_type;
std::string m_text;
uint8_t m_text_size;
Color m_fg_color;
Color m_bg_color;
bool enabled;
bool visible;
};
+21
View File
@@ -0,0 +1,21 @@
#include "UI_label.hpp"
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_label::UI_label(const json &j, QObject *parent) : UI_item(UI_LABEL_TYPE, parent)
{
from_json(j);
}
void UI_label::from_json(const json &j)
{
UI_item::from_json(j);
}
json UI_label::to_json(size_t id) const
{
return UI_item::to_json(id);
}
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include "UI_item.hpp"
#include <string>
#define UI_LABEL_TYPE std::string("UI_label")
class UI_label : public UI_item
{
Q_OBJECT
public:
UI_label(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_label(const json &j, QObject *parent = nullptr);
void from_json(const json &j) override;
json to_json(size_t id) const override;
};
+31
View File
@@ -0,0 +1,31 @@
#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(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_LED_TYPE, text, 12, fg_color, bg_color, parent)
{
}
UI_led::UI_led(const json &j, QObject *parent) : UI_item(UI_LED_TYPE, parent)
{
from_json(j);
}
void UI_led::set_led(bool state, Color on, Color off)
{
set_bg_color(state ? on : off);
}
void UI_led::from_json(const json &j)
{
UI_item::from_json(j);
}
json UI_led::to_json(size_t id) const
{
json j = UI_item::to_json(id);
return j;
}
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include "UI_item.hpp"
#include <string>
#define UI_LED_TYPE std::string("UI_led")
class UI_led : public UI_item
{
Q_OBJECT
public:
UI_led(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);
void set_led(bool state, Color on, Color off);
void from_json(const json &j) override;
json to_json(size_t id) const override;
};
+66
View File
@@ -0,0 +1,66 @@
#include "UI_slider.hpp"
UI_slider::UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
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),
m_unit(unit)
{
}
UI_slider::UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent)
: UI_slider(text, "", fg_color, bg_color, min, max, value, parent)
{
}
UI_slider::UI_slider(const json &j, QObject *parent) : UI_item(UI_SLIDER_TYPE, parent)
{
from_json(j);
}
void UI_slider::set_value(double value)
{
if (value < m_min || value > m_max)
return;
if (value == m_value)
return;
this->m_value = value;
emit value_changed();
emit on_change(m_value);
}
double UI_slider::value() const
{
return m_value;
}
double UI_slider::min() const
{
return m_min;
}
double UI_slider::max() const
{
return m_max;
}
void UI_slider::from_json(const json &j)
{
UI_item::from_json(j);
this->m_min = j.at("min");
this->m_max = j.at("max");
this->m_value = j.at("value");
this->m_unit = j.at("unit");
}
json UI_slider::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["min"] = this->m_min;
j["max"] = this->m_max;
j["value"] = this->m_value;
j["unit"] = this->m_unit;
return j;
}
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "UI_item.hpp"
#define UI_SLIDER_TYPE std::string("UI_slider")
class UI_slider : public UI_item
{
Q_OBJECT
public:
UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
UI_slider(const json &j, QObject *parent = nullptr);
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
signals:
void on_change(double value);
private:
double m_min;
double m_max;
double m_value;
std::string m_unit;
};
+44
View File
@@ -0,0 +1,44 @@
#include "UI_textbox.hpp"
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_textbox::UI_textbox(const json &j, QObject *parent) : UI_item(UI_TEXTBOX_TYPE, parent)
{
from_json(j);
}
void UI_textbox::set_text(std::string text)
{
if (text == m_text)
return;
m_text = text;
emit value_changed();
emit on_change(m_text);
}
void UI_textbox::set_hint(std::string hint)
{
m_hint = hint;
emit value_changed();
}
std::string UI_textbox::hint() const
{
return m_hint;
}
void UI_textbox::from_json(const json &j)
{
UI_item::from_json(j);
m_hint = j.at("hint");
}
json UI_textbox::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["hint"] = m_hint;
return j;
}
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include "UI_item.hpp"
#include <string>
#define UI_TEXTBOX_TYPE std::string("UI_textbox")
class UI_textbox : public UI_item
{
Q_OBJECT
public:
UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_textbox(const json &j, QObject *parent = nullptr);
void set_text(std::string text) override;
void set_hint(std::string hint);
std::string hint() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
signals:
void on_change(std::string text);
private:
std::string m_hint;
};
+134
View File
@@ -0,0 +1,134 @@
#include "UI_thermo.hpp"
UI_thermo::UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
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),
m_unit(unit)
{
}
UI_thermo::UI_thermo(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent)
: UI_thermo(text, "", fg_color, bg_color, min, max, value, parent)
{
}
UI_thermo::UI_thermo(const json &j, QObject *parent) : UI_item(UI_THERMO_TYPE, parent)
{
from_json(j);
}
void UI_thermo::set_value(double value)
{
if (value < m_min || value > m_max)
return;
if (value == m_value)
return;
this->m_value = value;
emit value_changed();
}
double UI_thermo::value() const
{
return m_value;
}
double UI_thermo::min() const
{
return m_min;
}
double UI_thermo::max() const
{
return m_max;
}
void UI_thermo::set_start_color(Color color)
{
this->m_color_map[0] = color;
emit value_changed();
}
void UI_thermo::set_end_color(Color color)
{
this->m_color_map[1] = color;
emit value_changed();
}
void UI_thermo::add_color_stop(double value, Color color)
{
if (value <= 0 || value >= 1)
return;
this->m_color_map[value] = color;
emit value_changed();
}
void UI_thermo::add_color_stop_normalized(double value, Color color)
{
if (value < m_min || value > m_max)
return;
double normalized = (value - m_min) / (m_max - m_min);
this->m_color_map[normalized] = color;
emit value_changed();
}
void UI_thermo::remove_color_stop(double value)
{
if (value <= 0 || value >= 1)
return;
this->m_color_map.erase(value);
emit value_changed();
}
void UI_thermo::remove_color_stop_normalized(double value)
{
if (value < m_min || value > m_max)
return;
double normalized = (value - m_min) / (m_max - m_min);
this->m_color_map.erase(normalized);
emit value_changed();
}
void UI_thermo::from_json(const json &j)
{
UI_item::from_json(j);
this->m_min = j.at("min");
this->m_max = j.at("max");
this->m_value = j.at("value");
this->m_unit = j.at("unit");
this->m_color_map.clear();
if (!j.contains("color_map"))
return;
for (auto &i : j["color_map"])
{
double k = i.at("key");
std::string c = i.at("value");
this->m_color_map[k] = Color(c);
}
}
json UI_thermo::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["min"] = this->m_min;
j["max"] = this->m_max;
j["value"] = this->m_value;
j["unit"] = this->m_unit;
if (this->m_color_map.empty())
return j;
j["color_map"] = json::array();
for (auto &[key, value] : this->m_color_map)
{
json item = json::object();
item["key"] = key;
item["value"] = value.to_hex();
j["color_map"].push_back(item);
}
return j;
}
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "UI_item.hpp"
#define UI_THERMO_TYPE std::string("UI_thermo")
// TODO: Add color scale
class UI_thermo : public UI_item
{
Q_OBJECT
public:
UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
UI_thermo(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
UI_thermo(const json &j, QObject *parent = nullptr);
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
void set_start_color(Color color);
void set_end_color(Color color);
void add_color_stop(double value, Color color);
void add_color_stop_normalized(double value, Color color);
void remove_color_stop(double value);
void remove_color_stop_normalized(double value);
void from_json(const json &j) override;
json to_json(size_t id) const override;
private:
double m_min;
double m_max;
double m_value;
std::string m_unit;
std::unordered_map<double, Color> m_color_map;
};
+31
View File
@@ -0,0 +1,31 @@
#include "UI_x_boxes.hpp"
UI_x_boxes::UI_x_boxes(std::string type, QObject *parent) : UI_item(type, parent), m_options()
{
}
UI_x_boxes::UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, QObject *parent)
: UI_item(type, text, 12, fg_color, bg_color, parent), m_options(options)
{
}
void UI_x_boxes::from_json(const json &j)
{
UI_item::from_json(j);
if (j.contains("options"))
{
this->m_options.clear();
for (auto option : j["options"])
{
this->m_options.push_back(option);
}
}
}
json UI_x_boxes::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["options"] = this->m_options;
return j;
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "UI_item.hpp"
class UI_x_boxes : public UI_item
{
Q_OBJECT
public:
UI_x_boxes(std::string type, QObject *parent = nullptr);
UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
QObject *parent = nullptr);
void from_json(const json &j) override;
json to_json(size_t id) const override;
protected:
std::vector<std::string> m_options;
};
+111
View File
@@ -0,0 +1,111 @@
#include "UI_can.hpp"
template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
{
T stdArray;
for (unsigned int i = 0; i < stdArray.size() && i < (unsigned int)byteArray.size(); i++)
{
stdArray[i] = (unsigned char)byteArray.at(i);
}
return stdArray;
}
UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_CAN_TYPE, "", 12, fg_color, bg_color, parent)
{
}
UI_can::UI_can(const json &j, QObject *parent) : UI_item(UI_CAN_TYPE, parent)
{
from_json(j);
}
void UI_can::add_send_message(QCanBusFrame msg)
{
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(),
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
m_can_send_messages.push_back(frame);
emit value_changed();
}
void UI_can::add_received_message(QCanBusFrame msg)
{
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(),
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
m_can_received_messages.push_back(frame);
emit value_changed();
}
void UI_can::clear_send_buffer()
{
m_can_send_messages.clear();
emit value_changed();
}
void UI_can::clear_receive_buffer()
{
m_can_received_messages.clear();
emit value_changed();
}
void UI_can::can_clear()
{
clear_send_buffer();
clear_receive_buffer();
}
void UI_can::from_json(const json &j)
{
UI_item::from_json(j);
m_can_send_messages.clear();
m_can_received_messages.clear();
for (const auto &msg : j.at("send_msgs"))
{
CanFrame can_msg(msg.at("id"), msg.at("dlc"), msg.at("payload"));
m_can_send_messages.push_back(can_msg);
}
for (const auto &msg : j.at("rcvd_msgs"))
{
CanFrame can_msg(msg.at("id"), msg.at("dlc"), msg.at("payload"));
m_can_received_messages.push_back(can_msg);
}
}
json UI_can::to_json(size_t id) const
{
json j = UI_item::to_json(id);
json can_send_messages = json::array();
json can_received_messages = json::array();
for (const auto &msg : m_can_send_messages)
{
json can_msg;
can_msg["id"] = (uint32_t)msg.m_SID;
can_msg["dlc"] = msg.m_DLC;
can_msg["payload"] = msg.m_payload;
can_send_messages.push_back(can_msg);
}
j["send_msgs"] = can_send_messages;
for (const auto &msg : m_can_received_messages)
{
json can_msg;
can_msg["id"] = (uint32_t)msg.m_SID;
can_msg["dlc"] = msg.m_DLC;
can_msg["payload"] = msg.m_payload;
can_received_messages.push_back(can_msg);
}
j["rcvd_msgs"] = can_received_messages;
return j;
}
void UI_can::can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload)
{
QCanBusFrame frame;
frame.setFrameId(id);
frame.setPayload(QByteArray::fromRawData((const char *)(payload.data()), dlc));
emit on_send(frame);
}
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include "UI_item.hpp"
#include <QCanBusFrame>
#define UI_CAN_TYPE std::string("UI_can")
class CanFrame
{
public:
CanFrame(uint32_t sid, uint8_t dlc, std::array<uint8_t, 8> payload) : m_SID(sid), m_DLC(dlc), m_payload(payload)
{
}
uint32_t m_SID;
uint8_t m_DLC;
std::array<uint8_t, 8> m_payload;
};
class UI_can : public UI_item
{
Q_OBJECT
public:
UI_can(Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_can(const json &j, QObject *parent = nullptr);
void add_send_message(QCanBusFrame msg);
void add_received_message(QCanBusFrame msg);
void clear_send_buffer();
void clear_receive_buffer();
void from_json(const json &j) override;
json to_json(size_t id) const override;
void can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload) override;
void can_clear() override;
signals:
void on_send(QCanBusFrame msg);
private:
std::vector<CanFrame> m_can_send_messages;
std::vector<CanFrame> m_can_received_messages;
};