move type names over to enum
This commit is contained in:
2026-04-06 18:55:43 +02:00
parent bc61f4990f
commit 514b3e402a
43 changed files with 113 additions and 103 deletions
+9
View File
@@ -34,6 +34,15 @@ target_link_libraries(UI
PUBLIC
Color
nlohmann_json::nlohmann_json
magic_enum::magic_enum
Qt::Core
Qt::SerialBus
UI_enum
)
add_library(UI_enum INTERFACE)
target_include_directories(UI_enum
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/enum
)
+25
View File
@@ -0,0 +1,25 @@
#pragma once
enum class UI_enum
{
ui_button,
ui_can,
ui_checkbox,
ui_combobox,
ui_dial,
ui_double_spinbox,
ui_hex_spinbox,
ui_label,
ui_led,
ui_plot,
ui_slider,
ui_spinbox,
ui_stacked_widget,
ui_status_bar,
ui_tab_widget,
ui_table,
ui_textbox,
ui_thermo,
end,
begin = 0
};
+3 -2
View File
@@ -1,11 +1,12 @@
#include "ui_button.hpp"
#include "ui_enum.hpp"
UI_button::UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_BUTTON_TYPE, text, fg_color, bg_color, parent)
: UI_item(UI_enum::ui_button, text, fg_color, bg_color, parent)
{
}
UI_button::UI_button(QObject *parent) : UI_item(UI_BUTTON_TYPE, parent)
UI_button::UI_button(QObject *parent) : UI_item(UI_enum::ui_button, parent)
{
}
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_BUTTON_TYPE std::string("ui_button")
/**
* @brief Class representing a UI button
*
+3 -2
View File
@@ -12,12 +12,13 @@ template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
return stdArray;
}
UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent) : UI_item(UI_CAN_TYPE, "", fg_color, bg_color, parent)
UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_can, "", fg_color, bg_color, parent)
{
setup_item(false, true, true);
}
UI_can::UI_can(QObject *parent) : UI_item(UI_CAN_TYPE, parent)
UI_can::UI_can(QObject *parent) : UI_item(UI_enum::ui_can, parent)
{
setup_item(false, true, true);
}
-2
View File
@@ -2,8 +2,6 @@
#include "ui_item.hpp"
#include <QCanBusFrame>
#define UI_CAN_TYPE std::string("ui_can")
/**
* @brief Class representing a CAN UI item
*
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_checkbox.hpp"
UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_checked()
UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_enum::ui_checkbox, parent), m_checked()
{
}
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_CHECKBOX_TYPE, text, fg_color, bg_color, parent), m_checked(false)
: UI_item(UI_enum::ui_checkbox, text, fg_color, bg_color, parent), m_checked(false)
{
}
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_CHECKBOX_TYPE std::string("ui_checkbox")
/**
* @brief Class representing a UI checkbox
*
+2 -2
View File
@@ -1,13 +1,13 @@
#include "ui_combobox.hpp"
UI_combobox::UI_combobox(QObject *parent) : UI_item(UI_COMBOBOX_TYPE, parent), m_options()
UI_combobox::UI_combobox(QObject *parent) : UI_item(UI_enum::ui_combobox, 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,
QObject *parent)
: UI_item(UI_COMBOBOX_TYPE, "", fg_color, bg_color, parent), m_options(options), m_selected(selected)
: UI_item(UI_enum::ui_combobox, "", fg_color, bg_color, parent), m_options(options), m_selected(selected)
{
setup_item(false, true, true);
}
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_COMBOBOX_TYPE std::string("ui_combobox")
/**
* @brief Class representing a UI combobox
*
+2 -2
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,
double value, QObject *parent)
: UI_item(UI_DIAL_TYPE, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
: UI_item(UI_enum::ui_dial, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
{
}
@@ -22,7 +22,7 @@ UI_dial::UI_dial(std::string text, double min, double max, double value, QObject
{
}
UI_dial::UI_dial(QObject *parent) : UI_item(UI_DIAL_TYPE, parent)
UI_dial::UI_dial(QObject *parent) : UI_item(UI_enum::ui_dial, parent)
{
}
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_DIAL_TYPE std::string("ui_dial")
/**
* @brief Class representing a UI dial
*
+2 -2
View File
@@ -1,12 +1,12 @@
#include "ui_double_spinbox.hpp"
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_enum::ui_double_spinbox, parent)
{
setup_item(false, false, false);
}
UI_double_spinbox::UI_double_spinbox(double min, double max, double value, QObject *parent)
: UI_item(UI_DOUBLE_SPINBOX_TYPE, parent)
: UI_item(UI_enum::ui_double_spinbox, parent)
{
setup_item(false, false, false);
m_min = min;
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_DOUBLE_SPINBOX_TYPE std::string("ui_double_spinbox")
/**
* @brief Class representing a UI double spinbox
*
+2 -2
View File
@@ -1,12 +1,12 @@
#include "ui_hex_spinbox.hpp"
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_enum::ui_hex_spinbox, parent)
{
setup_item(false, false, false);
}
UI_hex_spinbox::UI_hex_spinbox(double min, double max, double value, QObject *parent)
: UI_item(UI_HEX_SPINBOX_TYPE, parent)
: UI_item(UI_enum::ui_hex_spinbox, parent)
{
setup_item(false, false, false);
m_min = min;
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_HEX_SPINBOX_TYPE std::string("ui_hex_spinbox")
/**
* @brief Class representing a UI hex spinbox
*
+7 -6
View File
@@ -1,20 +1,21 @@
#include "ui_item.hpp"
#include <magic_enum/magic_enum.hpp>
UI_item::UI_item(std::string type, std::string text, Color fg_color, Color bg_color, QObject *parent)
UI_item::UI_item(UI_enum 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_fg_color(fg_color), m_bg_color(bg_color),
m_enabled(true), m_visible(true)
{
}
UI_item::UI_item(std::string type, QObject *parent)
UI_item::UI_item(UI_enum type, QObject *parent)
: QObject(parent), m_id((size_t)-1), m_type(type), m_text(""), m_fg_color(Color::Default),
m_bg_color(Color::Default), m_enabled(true), m_visible(true)
{
}
std::string UI_item::type() const &
std::string_view UI_item::type() const &
{
return m_type;
return magic_enum::enum_name(m_type);
}
bool UI_item::is_type(json j)
@@ -23,7 +24,7 @@ bool UI_item::is_type(json j)
{
return false;
}
return j.at("type") == m_type;
return magic_enum::enum_cast<UI_enum>(j.at("type")).value_or(UI_enum::end) == m_type;
}
void UI_item::set_text(std::string text)
@@ -129,7 +130,7 @@ json UI_item::to_json(size_t id) const
{
json j;
j["id"] = id;
j["type"] = m_type;
j["type"] = type();
if (m_text_enabled)
j["text"] = m_text;
if (m_fg_color_enabled)
+5 -4
View File
@@ -1,5 +1,6 @@
#pragma once
#include "color.hpp"
#include "ui_enum.hpp"
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
@@ -24,7 +25,7 @@ class UI_item : public QObject
* @param type The type of the UI item
* @param parent The parent QObject
*/
explicit UI_item(std::string type, QObject *parent = nullptr);
explicit UI_item(UI_enum type, QObject *parent = nullptr);
/**
* @brief Construct a new UI item object
*
@@ -34,14 +35,14 @@ class UI_item : public QObject
* @param bg_color The background color of the UI item
* @param parent The parent QObject
*/
explicit UI_item(std::string type, std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
explicit UI_item(UI_enum type, std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
/**
* @brief Get the type of the UI item
*
* @return std::string The type of the UI item
*/
std::string type() const &;
std::string_view type() const &;
/**
* @brief Check if the JSON object is of the correct type
*
@@ -196,7 +197,7 @@ class UI_item : public QObject
* @brief The type of the UI item
*
*/
std::string m_type;
UI_enum m_type;
/**
* @brief The text of the UI item
*
+2 -2
View File
@@ -1,11 +1,11 @@
#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, fg_color, bg_color, parent)
: UI_item(UI_enum::ui_label, text, fg_color, bg_color, parent)
{
}
UI_label::UI_label(QObject *parent) : UI_item(UI_LABEL_TYPE, parent)
UI_label::UI_label(QObject *parent) : UI_item(UI_enum::ui_label, parent)
{
}
-2
View File
@@ -2,8 +2,6 @@
#include "ui_item.hpp"
#include <string>
#define UI_LABEL_TYPE std::string("ui_label")
/**
* @brief Class representing a UI label
*
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_led.hpp"
UI_led::UI_led(QObject *parent) : UI_item(UI_LED_TYPE, "", Color::Default, Color::Default, parent)
UI_led::UI_led(QObject *parent) : UI_item(UI_enum::ui_led, "", 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, fg_color, bg_color, parent)
: UI_item(UI_enum::ui_led, text, fg_color, bg_color, parent)
{
}
-2
View File
@@ -2,8 +2,6 @@
#include "ui_item.hpp"
#include <string>
#define UI_LED_TYPE std::string("ui_led")
/**
* @brief Class representing a UI led
*
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_plot.hpp"
UI_plot::UI_plot(QObject *parent) : UI_item{UI_PLOT_TYPE, parent}
UI_plot::UI_plot(QObject *parent) : UI_item{UI_enum::ui_plot, parent}
{
}
UI_plot::UI_plot(std::string text, std::string x_label, std::string y_label, QObject *parent)
: UI_item(UI_PLOT_TYPE, parent), m_x_label(x_label), m_y_label(y_label)
: UI_item(UI_enum::ui_plot, parent), m_x_label(x_label), m_y_label(y_label)
{
set_text(text);
}
-1
View File
@@ -1,6 +1,5 @@
#pragma once
#include "ui_item.hpp"
#define UI_PLOT_TYPE std::string("ui_plot")
/**
* @brief Class representing a UI plot
+3 -2
View File
@@ -2,7 +2,8 @@
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, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
: UI_item(UI_enum::ui_slider, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value),
m_unit(unit)
{
}
@@ -22,7 +23,7 @@ UI_slider::UI_slider(std::string text, double min, double max, double value, QOb
{
}
UI_slider::UI_slider(QObject *parent) : UI_item(UI_SLIDER_TYPE, parent)
UI_slider::UI_slider(QObject *parent) : UI_item(UI_enum::ui_slider, parent)
{
}
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_SLIDER_TYPE std::string("ui_slider")
/**
* @brief Class representing a UI slider
*
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_spinbox.hpp"
UI_spinbox::UI_spinbox(QObject *parent) : UI_item(UI_SPINBOX_TYPE, parent)
UI_spinbox::UI_spinbox(QObject *parent) : UI_item(UI_enum::ui_spinbox, 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_enum::ui_spinbox, parent)
{
setup_item(false, false, false);
m_min = min;
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_SPINBOX_TYPE std::string("ui_spinbox")
/**
* @brief Class representing a UI spinbox
*
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_stacked_widget.hpp"
UI_stacked_widget::UI_stacked_widget(QObject *parent) : UI_item(UI_STACKED_WIDGET_TYPE, parent)
UI_stacked_widget::UI_stacked_widget(QObject *parent) : UI_item(UI_enum::ui_stacked_widget, parent)
{
}
UI_stacked_widget::UI_stacked_widget(size_t tab_count, size_t current_tab, QObject *parent)
: UI_item(UI_STACKED_WIDGET_TYPE, parent), m_tab_count(tab_count), m_current_tab(current_tab)
: UI_item(UI_enum::ui_stacked_widget, parent), m_tab_count(tab_count), m_current_tab(current_tab)
{
}
-1
View File
@@ -1,6 +1,5 @@
#pragma once
#include "ui_item.hpp"
#define UI_STACKED_WIDGET_TYPE std::string("ui_stacked_widget")
/**
* @brief Class representing a UI stacked widget
+3 -2
View File
@@ -1,11 +1,12 @@
#include "ui_status_bar.hpp"
UI_status_bar::UI_status_bar(QObject *parent) : UI_item(UI_STATUS_BAR_TYPE, parent), m_timeout(0)
UI_status_bar::UI_status_bar(QObject *parent) : UI_item(UI_enum::ui_status_bar, parent), m_timeout(0)
{
setup_item(true, false, false);
}
UI_status_bar::UI_status_bar(std::string message, QObject *parent) : UI_item(UI_STATUS_BAR_TYPE, parent), m_timeout(0)
UI_status_bar::UI_status_bar(std::string message, QObject *parent)
: UI_item(UI_enum::ui_status_bar, parent), m_timeout(0)
{
m_text = std::move(message);
setup_item(true, false, false);
-1
View File
@@ -1,6 +1,5 @@
#pragma once
#include "ui_item.hpp"
#define UI_STATUS_BAR_TYPE std::string("ui_status_bar")
/**
* @brief Class representing a UI status bar
+2 -2
View File
@@ -1,12 +1,12 @@
#include "ui_tab_widget.hpp"
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_enum::ui_tab_widget, parent)
{
setup_item(false, false, false);
}
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_enum::ui_tab_widget, parent), m_selected_tab(selected), m_tab_names(tab_names)
{
setup_item(false, false, false);
m_tab_visible.resize(m_tab_names.size(), true);
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_TAB_WIDGET_TYPE std::string("ui_tab_widget")
/**
* @brief Class representing a UI tab widget
*
+3 -3
View File
@@ -1,14 +1,14 @@
#include "ui_table.hpp"
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_enum::ui_table, 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,
std::vector<std::string> column_labels, QObject *parent)
: 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_table()
: UI_item(UI_enum::ui_table, parent), m_row_count(row_count), m_column_count(column_count),
m_row_labels(row_labels), m_column_labels(column_labels), m_table()
{
setup_item(false, false, false);
m_row_labels.reserve(100);
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_TABLE_TYPE std::string("ui_table")
/**
* @brief Class representing a UI table
*
+2 -2
View File
@@ -1,11 +1,11 @@
#include "ui_textbox.hpp"
UI_textbox::UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_TEXTBOX_TYPE, "", fg_color, bg_color, parent), m_hint(hint)
: UI_item(UI_enum::ui_textbox, "", fg_color, bg_color, parent), m_hint(hint)
{
}
UI_textbox::UI_textbox(QObject *parent) : UI_item(UI_TEXTBOX_TYPE, parent)
UI_textbox::UI_textbox(QObject *parent) : UI_item(UI_enum::ui_textbox, parent)
{
}
-2
View File
@@ -2,8 +2,6 @@
#include "ui_item.hpp"
#include <string>
#define UI_TEXTBOX_TYPE std::string("ui_textbox")
/**
* @brief Class representing a UI textbox
*
+3 -2
View File
@@ -2,7 +2,8 @@
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, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
: UI_item(UI_enum::ui_thermo, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value),
m_unit(unit)
{
}
@@ -22,7 +23,7 @@ UI_thermo::UI_thermo(std::string text, double min, double max, double value, QOb
{
}
UI_thermo::UI_thermo(QObject *parent) : UI_item(UI_THERMO_TYPE, parent)
UI_thermo::UI_thermo(QObject *parent) : UI_item(UI_enum::ui_thermo, parent)
{
}
-2
View File
@@ -1,8 +1,6 @@
#pragma once
#include "ui_item.hpp"
#define UI_THERMO_TYPE std::string("ui_thermo")
// TODO: Add color scale
/**
* @brief Class representing a UI thermometer