From 514b3e402a65f2409c2d9036e51157e69ed574be Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 6 Apr 2026 18:55:43 +0200 Subject: [PATCH] UI move type names over to enum --- UI/CMakeLists.txt | 9 +++++++++ UI/enum/ui_enum.hpp | 25 +++++++++++++++++++++++ UI/ui_button.cpp | 5 +++-- UI/ui_button.hpp | 2 -- UI/ui_can.cpp | 5 +++-- UI/ui_can.hpp | 2 -- UI/ui_checkbox.cpp | 4 ++-- UI/ui_checkbox.hpp | 2 -- UI/ui_combobox.cpp | 4 ++-- UI/ui_combobox.hpp | 2 -- UI/ui_dial.cpp | 4 ++-- UI/ui_dial.hpp | 2 -- UI/ui_double_spinbox.cpp | 4 ++-- UI/ui_double_spinbox.hpp | 2 -- UI/ui_hex_spinbox.cpp | 4 ++-- UI/ui_hex_spinbox.hpp | 2 -- UI/ui_item.cpp | 13 ++++++------ UI/ui_item.hpp | 9 +++++---- UI/ui_label.cpp | 4 ++-- UI/ui_label.hpp | 2 -- UI/ui_led.cpp | 4 ++-- UI/ui_led.hpp | 2 -- UI/ui_plot.cpp | 4 ++-- UI/ui_plot.hpp | 1 - UI/ui_slider.cpp | 5 +++-- UI/ui_slider.hpp | 2 -- UI/ui_spinbox.cpp | 4 ++-- UI/ui_spinbox.hpp | 2 -- UI/ui_stacked_widget.cpp | 4 ++-- UI/ui_stacked_widget.hpp | 1 - UI/ui_status_bar.cpp | 5 +++-- UI/ui_status_bar.hpp | 1 - UI/ui_tab_widget.cpp | 4 ++-- UI/ui_tab_widget.hpp | 2 -- UI/ui_table.cpp | 6 +++--- UI/ui_table.hpp | 2 -- UI/ui_textbox.cpp | 4 ++-- UI/ui_textbox.hpp | 2 -- UI/ui_thermo.cpp | 5 +++-- UI/ui_thermo.hpp | 2 -- client/CMakeLists.txt | 2 +- client/ui_base.cpp | 43 ++++++++++++++++++++-------------------- client/ui_base.hpp | 3 ++- 43 files changed, 113 insertions(+), 103 deletions(-) create mode 100644 UI/enum/ui_enum.hpp diff --git a/UI/CMakeLists.txt b/UI/CMakeLists.txt index 117c135..7410c53 100644 --- a/UI/CMakeLists.txt +++ b/UI/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/UI/enum/ui_enum.hpp b/UI/enum/ui_enum.hpp new file mode 100644 index 0000000..976207a --- /dev/null +++ b/UI/enum/ui_enum.hpp @@ -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 +}; diff --git a/UI/ui_button.cpp b/UI/ui_button.cpp index 278b587..bcc1b98 100644 --- a/UI/ui_button.cpp +++ b/UI/ui_button.cpp @@ -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) { } diff --git a/UI/ui_button.hpp b/UI/ui_button.hpp index 38655dd..6b03a11 100644 --- a/UI/ui_button.hpp +++ b/UI/ui_button.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_BUTTON_TYPE std::string("ui_button") - /** * @brief Class representing a UI button * diff --git a/UI/ui_can.cpp b/UI/ui_can.cpp index d25c49d..4946612 100644 --- a/UI/ui_can.cpp +++ b/UI/ui_can.cpp @@ -12,12 +12,13 @@ template 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); } diff --git a/UI/ui_can.hpp b/UI/ui_can.hpp index e31629e..89d10b8 100644 --- a/UI/ui_can.hpp +++ b/UI/ui_can.hpp @@ -2,8 +2,6 @@ #include "ui_item.hpp" #include -#define UI_CAN_TYPE std::string("ui_can") - /** * @brief Class representing a CAN UI item * diff --git a/UI/ui_checkbox.cpp b/UI/ui_checkbox.cpp index 0b05d27..409104c 100644 --- a/UI/ui_checkbox.cpp +++ b/UI/ui_checkbox.cpp @@ -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) { } diff --git a/UI/ui_checkbox.hpp b/UI/ui_checkbox.hpp index ff55b6c..6321a5a 100644 --- a/UI/ui_checkbox.hpp +++ b/UI/ui_checkbox.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_CHECKBOX_TYPE std::string("ui_checkbox") - /** * @brief Class representing a UI checkbox * diff --git a/UI/ui_combobox.cpp b/UI/ui_combobox.cpp index 125aa74..f14d324 100644 --- a/UI/ui_combobox.cpp +++ b/UI/ui_combobox.cpp @@ -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 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); } diff --git a/UI/ui_combobox.hpp b/UI/ui_combobox.hpp index f2c39f6..7912914 100644 --- a/UI/ui_combobox.hpp +++ b/UI/ui_combobox.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_COMBOBOX_TYPE std::string("ui_combobox") - /** * @brief Class representing a UI combobox * diff --git a/UI/ui_dial.cpp b/UI/ui_dial.cpp index 8b2660e..778c569 100644 --- a/UI/ui_dial.cpp +++ b/UI/ui_dial.cpp @@ -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) { } diff --git a/UI/ui_dial.hpp b/UI/ui_dial.hpp index 2f9c36c..1dd4c62 100644 --- a/UI/ui_dial.hpp +++ b/UI/ui_dial.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_DIAL_TYPE std::string("ui_dial") - /** * @brief Class representing a UI dial * diff --git a/UI/ui_double_spinbox.cpp b/UI/ui_double_spinbox.cpp index a090178..a4c4e64 100644 --- a/UI/ui_double_spinbox.cpp +++ b/UI/ui_double_spinbox.cpp @@ -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; diff --git a/UI/ui_double_spinbox.hpp b/UI/ui_double_spinbox.hpp index ef75be2..dd96f60 100644 --- a/UI/ui_double_spinbox.hpp +++ b/UI/ui_double_spinbox.hpp @@ -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 * diff --git a/UI/ui_hex_spinbox.cpp b/UI/ui_hex_spinbox.cpp index 654e01b..1ec194a 100644 --- a/UI/ui_hex_spinbox.cpp +++ b/UI/ui_hex_spinbox.cpp @@ -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; diff --git a/UI/ui_hex_spinbox.hpp b/UI/ui_hex_spinbox.hpp index c0c1748..abee56e 100644 --- a/UI/ui_hex_spinbox.hpp +++ b/UI/ui_hex_spinbox.hpp @@ -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 * diff --git a/UI/ui_item.cpp b/UI/ui_item.cpp index 33b9457..197d131 100644 --- a/UI/ui_item.cpp +++ b/UI/ui_item.cpp @@ -1,20 +1,21 @@ #include "ui_item.hpp" +#include -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(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) diff --git a/UI/ui_item.hpp b/UI/ui_item.hpp index 9bd5edc..a6e984b 100644 --- a/UI/ui_item.hpp +++ b/UI/ui_item.hpp @@ -1,5 +1,6 @@ #pragma once #include "color.hpp" +#include "ui_enum.hpp" #include #include #include @@ -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 * diff --git a/UI/ui_label.cpp b/UI/ui_label.cpp index 2453020..93b9a22 100644 --- a/UI/ui_label.cpp +++ b/UI/ui_label.cpp @@ -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) { } diff --git a/UI/ui_label.hpp b/UI/ui_label.hpp index 80c9be0..47567c7 100644 --- a/UI/ui_label.hpp +++ b/UI/ui_label.hpp @@ -2,8 +2,6 @@ #include "ui_item.hpp" #include -#define UI_LABEL_TYPE std::string("ui_label") - /** * @brief Class representing a UI label * diff --git a/UI/ui_led.cpp b/UI/ui_led.cpp index 4f4b3b0..3f52183 100644 --- a/UI/ui_led.cpp +++ b/UI/ui_led.cpp @@ -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) { } diff --git a/UI/ui_led.hpp b/UI/ui_led.hpp index bcca704..cc4f422 100644 --- a/UI/ui_led.hpp +++ b/UI/ui_led.hpp @@ -2,8 +2,6 @@ #include "ui_item.hpp" #include -#define UI_LED_TYPE std::string("ui_led") - /** * @brief Class representing a UI led * diff --git a/UI/ui_plot.cpp b/UI/ui_plot.cpp index 1b69115..a87da0b 100644 --- a/UI/ui_plot.cpp +++ b/UI/ui_plot.cpp @@ -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); } diff --git a/UI/ui_plot.hpp b/UI/ui_plot.hpp index 9996ee7..f49f380 100644 --- a/UI/ui_plot.hpp +++ b/UI/ui_plot.hpp @@ -1,6 +1,5 @@ #pragma once #include "ui_item.hpp" -#define UI_PLOT_TYPE std::string("ui_plot") /** * @brief Class representing a UI plot diff --git a/UI/ui_slider.cpp b/UI/ui_slider.cpp index e173119..37e4a8c 100644 --- a/UI/ui_slider.cpp +++ b/UI/ui_slider.cpp @@ -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) { } diff --git a/UI/ui_slider.hpp b/UI/ui_slider.hpp index 321954e..a41bb15 100644 --- a/UI/ui_slider.hpp +++ b/UI/ui_slider.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_SLIDER_TYPE std::string("ui_slider") - /** * @brief Class representing a UI slider * diff --git a/UI/ui_spinbox.cpp b/UI/ui_spinbox.cpp index 7f6baa2..3d771ab 100644 --- a/UI/ui_spinbox.cpp +++ b/UI/ui_spinbox.cpp @@ -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; diff --git a/UI/ui_spinbox.hpp b/UI/ui_spinbox.hpp index 43c219d..1d7a8d8 100644 --- a/UI/ui_spinbox.hpp +++ b/UI/ui_spinbox.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_SPINBOX_TYPE std::string("ui_spinbox") - /** * @brief Class representing a UI spinbox * diff --git a/UI/ui_stacked_widget.cpp b/UI/ui_stacked_widget.cpp index f639ce1..c4516e1 100644 --- a/UI/ui_stacked_widget.cpp +++ b/UI/ui_stacked_widget.cpp @@ -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) { } diff --git a/UI/ui_stacked_widget.hpp b/UI/ui_stacked_widget.hpp index d967ab8..ee67472 100644 --- a/UI/ui_stacked_widget.hpp +++ b/UI/ui_stacked_widget.hpp @@ -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 diff --git a/UI/ui_status_bar.cpp b/UI/ui_status_bar.cpp index 4e02038..6add252 100644 --- a/UI/ui_status_bar.cpp +++ b/UI/ui_status_bar.cpp @@ -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); diff --git a/UI/ui_status_bar.hpp b/UI/ui_status_bar.hpp index f79aed1..aae3eb2 100644 --- a/UI/ui_status_bar.hpp +++ b/UI/ui_status_bar.hpp @@ -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 diff --git a/UI/ui_tab_widget.cpp b/UI/ui_tab_widget.cpp index 507c3a7..64d55cc 100644 --- a/UI/ui_tab_widget.cpp +++ b/UI/ui_tab_widget.cpp @@ -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 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); diff --git a/UI/ui_tab_widget.hpp b/UI/ui_tab_widget.hpp index 44850f7..5529225 100644 --- a/UI/ui_tab_widget.hpp +++ b/UI/ui_tab_widget.hpp @@ -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 * diff --git a/UI/ui_table.cpp b/UI/ui_table.cpp index 3c431a4..f5dc680 100644 --- a/UI/ui_table.cpp +++ b/UI/ui_table.cpp @@ -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 row_labels, std::vector 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); diff --git a/UI/ui_table.hpp b/UI/ui_table.hpp index 15bb96c..01c148c 100644 --- a/UI/ui_table.hpp +++ b/UI/ui_table.hpp @@ -1,8 +1,6 @@ #pragma once #include "ui_item.hpp" -#define UI_TABLE_TYPE std::string("ui_table") - /** * @brief Class representing a UI table * diff --git a/UI/ui_textbox.cpp b/UI/ui_textbox.cpp index 1e9abae..284f478 100644 --- a/UI/ui_textbox.cpp +++ b/UI/ui_textbox.cpp @@ -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) { } diff --git a/UI/ui_textbox.hpp b/UI/ui_textbox.hpp index 449e5f2..92bdba9 100644 --- a/UI/ui_textbox.hpp +++ b/UI/ui_textbox.hpp @@ -2,8 +2,6 @@ #include "ui_item.hpp" #include -#define UI_TEXTBOX_TYPE std::string("ui_textbox") - /** * @brief Class representing a UI textbox * diff --git a/UI/ui_thermo.cpp b/UI/ui_thermo.cpp index 72a81ea..c6d2327 100644 --- a/UI/ui_thermo.cpp +++ b/UI/ui_thermo.cpp @@ -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) { } diff --git a/UI/ui_thermo.hpp b/UI/ui_thermo.hpp index f5b7206..9c69d60 100644 --- a/UI/ui_thermo.hpp +++ b/UI/ui_thermo.hpp @@ -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 diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 877a2c9..1a51392 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -62,7 +62,7 @@ target_compile_definitions(client_common PRIVATE ) target_link_libraries(client_common PUBLIC - qwt Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebSockets magic_enum::magic_enum nlohmann_json::nlohmann_json + qwt Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::WebSockets magic_enum::magic_enum nlohmann_json::nlohmann_json UI_enum ) set_target_properties( diff --git a/client/ui_base.cpp b/client/ui_base.cpp index 61ee1a8..ae34503 100644 --- a/client/ui_base.cpp +++ b/client/ui_base.cpp @@ -20,7 +20,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) QPushButton *exit_app = m_error->addButton("Exit Application", QMessageBox::AcceptRole); m_error->addButton("Close", QMessageBox::RejectRole); connect(exit_app, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection); - m_process_lookup.insert("ui_label", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_label, [=, this](json &ui_item, QWidget *widget) { auto label = qobject_cast(widget); if (label == nullptr) { @@ -48,7 +48,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) } }); - m_process_lookup.insert("ui_slider", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_slider, [=, this](json &ui_item, QWidget *widget) { QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); // TODO: Find a cleaner fix #ifdef __WIN32 @@ -93,7 +93,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) } // TODO: Add color for scale }); - m_process_lookup.insert("ui_dial", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_dial, [=, this](json &ui_item, QWidget *widget) { QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); // TODO: Find a cleaner fix #ifdef __WIN32 @@ -131,7 +131,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) dial->setVisible(visible); } }); - m_process_lookup.insert("ui_thermo", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_thermo, [=, this](json &ui_item, QWidget *widget) { QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); // TODO: Find a cleaner fix #ifdef __WIN32 @@ -169,7 +169,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) thermo->setVisible(visible); } }); - m_process_lookup.insert("ui_textbox", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_textbox, [=, this](json &ui_item, QWidget *widget) { auto line_edit = qobject_cast(widget); if (line_edit == nullptr) { @@ -200,7 +200,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) line_edit->setText(text); } }); - m_process_lookup.insert("ui_combobox", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_combobox, [=, this](json &ui_item, QWidget *widget) { auto combobox = qobject_cast(widget); if (combobox == nullptr) { @@ -233,9 +233,9 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) combobox->setCurrentIndex(selected); } }); - m_process_lookup.insert("ui_radiobutton", - [=, this](json &ui_item, QWidget *widget) { LOG_CRITICAL("Not implemented"); }); - m_process_lookup.insert("ui_checkbox", [=, this](json &ui_item, QWidget *widget) { + // m_process_lookup.insert(UI_enum::ui_radiobutton, + // [=, this](json &ui_item, QWidget *widget) { LOG_CRITICAL("Not implemented"); }); + m_process_lookup.insert(UI_enum::ui_checkbox, [=, this](json &ui_item, QWidget *widget) { auto checkbox = qobject_cast(widget); if (checkbox == nullptr) { @@ -299,7 +299,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) set_widget_color(checkbox, fg_color, bg_color); } }); - m_process_lookup.insert("ui_button", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_button, [=, this](json &ui_item, QWidget *widget) { auto button = qobject_cast(widget); if (button == nullptr) { @@ -357,7 +357,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) set_widget_color(button, fg_color, bg_color); } }); - m_process_lookup.insert("ui_led", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_led, [=, this](json &ui_item, QWidget *widget) { auto led = qobject_cast(widget); if (led == nullptr) { @@ -409,7 +409,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) set_widget_color(led, color, bg_color); } }); - m_process_lookup.insert("ui_spinbox", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_spinbox, [=, this](json &ui_item, QWidget *widget) { auto spinbox = qobject_cast(widget); if (spinbox == nullptr) { @@ -446,7 +446,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) spinbox->setValue((int)value); } }); - m_process_lookup.insert("ui_double_spinbox", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_double_spinbox, [=, this](json &ui_item, QWidget *widget) { auto spinbox = qobject_cast(widget); if (spinbox == nullptr) { @@ -483,7 +483,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) spinbox->setValue(value); } }); - m_process_lookup.insert("ui_hex_spinbox", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_hex_spinbox, [=, this](json &ui_item, QWidget *widget) { auto spinbox = qobject_cast(widget); if (spinbox == nullptr) { @@ -520,7 +520,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) spinbox->setValue((int)value); } }); - m_process_lookup.insert("ui_tab_widget", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_tab_widget, [=, this](json &ui_item, QWidget *widget) { auto tab_widget = qobject_cast(widget); if (tab_widget == nullptr) { @@ -565,7 +565,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) tab_widget->setCurrentIndex(ui_item.at("selected")); } }); - m_process_lookup.insert("ui_stacked_widget", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_stacked_widget, [=, this](json &ui_item, QWidget *widget) { auto stacked_widget = qobject_cast(widget); if (stacked_widget == nullptr) { @@ -597,7 +597,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) stacked_widget->setCurrentIndex(ui_item.at("current_tab")); } }); - m_process_lookup.insert("ui_status_bar", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_status_bar, [=, this](json &ui_item, QWidget *widget) { auto status_bar = qobject_cast(widget); if (status_bar == nullptr) { @@ -610,7 +610,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) status_bar->showMessage(QString::fromStdString(ui_item.at("text")), ui_item.at("timeout")); } }); - m_process_lookup.insert("ui_plot", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_plot, [=, this](json &ui_item, QWidget *widget) { auto plot = qobject_cast(widget); if (plot == nullptr) { @@ -670,7 +670,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) plot->setAxisScale(QwtAxis::XBottom, x_vals[0], x_vals[x_vals.size() - 1]); plot->replot(); }); - m_process_lookup.insert("ui_table", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_table, [=, this](json &ui_item, QWidget *widget) { auto table = qobject_cast(widget); if (table == nullptr) { @@ -739,7 +739,7 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) } table->resizeColumnsToContents(); }); - m_process_lookup.insert("ui_can", [=, this](json &ui_item, QWidget *widget) { + m_process_lookup.insert(UI_enum::ui_can, [=, this](json &ui_item, QWidget *widget) { auto can_ui = qobject_cast(widget); if (can_ui == nullptr) { @@ -851,7 +851,8 @@ void UI_base::UI_item_parser(json &input) return; } - m_process_lookup[ui_item.at("type")](ui_item, widget); + m_process_lookup[magic_enum::enum_cast((std::string)ui_item.at("type")).value_or(UI_enum::end)]( + ui_item, widget); } } diff --git a/client/ui_base.hpp b/client/ui_base.hpp index 94019db..6a9d817 100644 --- a/client/ui_base.hpp +++ b/client/ui_base.hpp @@ -1,4 +1,5 @@ #pragma once +#include "ui_enum.hpp" #include #include #include @@ -222,5 +223,5 @@ class EXPORT UI_base : public QMainWindow bool m_open = false; bool m_first_load = true; std::vector m_ui_lookup; - QMap> m_process_lookup; + QMap> m_process_lookup; };