Update UI

This commit is contained in:
2026-08-20 13:27:31 +02:00
parent ecbd8bf8a9
commit cb41ac2aa2
40 changed files with 773 additions and 1436 deletions
+2 -11
View File
@@ -1,5 +1,5 @@
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT EMSCRIPTEN)
add_library(UI STATIC) add_library(UI STATIC)
find_package(Qt6 REQUIRED COMPONENTS Core SerialBus) find_package(Qt6 REQUIRED COMPONENTS Core SerialBus)
@@ -33,17 +33,8 @@ target_include_directories(UI
target_link_libraries(UI target_link_libraries(UI
PUBLIC PUBLIC
Color Color
nlohmann_json::nlohmann_json
magic_enum::magic_enum magic_enum::magic_enum
Qt::Core Qt::Core
Qt::SerialBus Qt::SerialBus
UI_enum proto-ui
)
endif()
add_library(UI_enum INTERFACE)
target_include_directories(UI_enum
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/enum
) )
-25
View File
@@ -1,25 +0,0 @@
#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
};
+32 -9
View File
@@ -1,24 +1,47 @@
#include "ui_button.hpp" #include "ui_button.hpp"
#include "ui_enum.hpp"
UI_button::UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_button::UI_button(QString text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_button, text, fg_color, bg_color, parent) : UI_item(UI_typeGadget::UI_type::ui_button, parent)
{ {
m_ui_item.setLabel(UI_label_m());
m_ui_item.label().setText(text);
m_ui_item.label().mutColor().setFgColor(fg_color.color());
m_ui_item.label().mutColor().setBgColor(bg_color.color());
} }
UI_button::UI_button(QObject *parent) : UI_item(UI_enum::ui_button, parent) UI_button::UI_button(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_button, parent)
{ {
m_ui_item.setLabel(UI_label_m());
} }
void UI_button::from_json(const json &j) void UI_button::set_text(QString text)
{ {
UI_item::from_json(j); m_ui_item.label().setText(text);
} }
json UI_button::to_json(size_t id) const QString UI_button::text() const
{ {
json j = UI_item::to_json(id); return m_ui_item.label().text();
return j; }
void UI_button::set_fg_color(Color color)
{
m_ui_item.label().mutColor().setFgColor(color.color());
}
Color UI_button::fg_color() const
{
return Color(m_ui_item.label().color().fgColor());
}
void UI_button::set_bg_color(Color color)
{
m_ui_item.label().mutColor().setBgColor(color.color());
}
Color UI_button::bg_color() const
{
return Color(m_ui_item.label().color().bgColor());
} }
void UI_button::click() void UI_button::click()
+13 -10
View File
@@ -17,7 +17,7 @@ class UI_button : public UI_item
* @param bg_color The background color of the button * @param bg_color The background color of the button
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_button(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_button(QString text, Color fg_color, Color bg_color, QObject *parent = nullptr);
/** /**
* @brief Construct a UI button object * @brief Construct a UI button object
* *
@@ -26,18 +26,21 @@ class UI_button : public UI_item
UI_button(QObject *parent = nullptr); UI_button(QObject *parent = nullptr);
/** /**
* @brief Deserialize the button from JSON * @brief set_text
* * @param text The text field of the button
* @param j The JSON object
*/ */
void from_json(const json &j) override; void set_text(QString text) override;
/** /**
* @brief Serialize the button to JSON * @brief text
* * @return The text field of the button
* @param id The id of the button
* @return json The JSON representation of the button
*/ */
json to_json(size_t id) const override; QString text() const;
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
/** /**
* @brief Handle a click event on the button * @brief Handle a click event on the button
+23 -71
View File
@@ -1,53 +1,49 @@
#include "ui_can.hpp" #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_can::UI_can(Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_can, "", fg_color, bg_color, parent) : UI_item(UI_typeGadget::UI_type::ui_can, parent)
{ {
setup_item(false, true, true); m_ui_item.can().mutColor().setFgColor(fg_color.color());
m_ui_item.can().mutColor().setBgColor(bg_color.color());
} }
UI_can::UI_can(QObject *parent) : UI_item(UI_enum::ui_can, parent) UI_can::UI_can(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_can, parent)
{ {
setup_item(false, true, true);
} }
void UI_can::add_send_message(QCanBusFrame msg) void UI_can::add_send_message(QCanBusFrame msg)
{ {
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(), CAN_Frame_m frame;(msg.frameId(), msg.payload().size(), msg.payload());
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload())); frame.setSid(msg.frameId());
m_can_send_messages.push_back(frame); frame.setDlc(msg.payload().size());
frame.setPayload(msg.payload());
auto send_frames = m_ui_item.can().sendFrames();
send_frames.append(frame);
m_ui_item.can().setSendFrames(send_frames);
emit ui_changed(); emit ui_changed();
} }
void UI_can::add_received_message(QCanBusFrame msg) void UI_can::add_received_message(QCanBusFrame msg)
{ {
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(), CAN_Frame_m frame;(msg.frameId(), msg.payload().size(), msg.payload());
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload())); frame.setSid(msg.frameId());
m_can_received_messages.push_back(frame); frame.setDlc(msg.payload().size());
frame.setPayload(msg.payload());
auto recv_frames = m_ui_item.can().recvFrames();
recv_frames.append(frame);
m_ui_item.can().setRecvFrames(recv_frames);
emit ui_changed(); emit ui_changed();
} }
void UI_can::clear_send_buffer() void UI_can::clear_send_buffer()
{ {
m_can_send_messages.clear(); m_ui_item.can().setSendFrames(QList<CAN_Frame_m>());
emit ui_changed(); emit ui_changed();
} }
void UI_can::clear_receive_buffer() void UI_can::clear_receive_buffer()
{ {
m_can_received_messages.clear(); m_ui_item.can().setSendFrames(QList<CAN_Frame_m>());
emit ui_changed(); emit ui_changed();
} }
@@ -57,56 +53,12 @@ void UI_can::clear()
clear_receive_buffer(); clear_receive_buffer();
} }
void UI_can::from_json(const json &j) void UI_can::can_send(uint32_t id, uint64_t dlc, QByteArray payload)
{
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; QCanBusFrame frame;
Q_UNUSED(dlc);
frame.setFrameId(id); frame.setFrameId(id);
frame.setPayload(QByteArray::fromRawData((const char *)(payload.data()), dlc)); frame.setPayload(payload);
emit send(frame); emit send(frame);
} }
+1 -19
View File
@@ -83,20 +83,6 @@ class UI_can : public UI_item
*/ */
void clear_receive_buffer(); void clear_receive_buffer();
/**
* @brief Deserialize the CAN item from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the CAN item to JSON
*
* @param id The id of the CAN item
* @return json The JSON representation of the CAN item
*/
json to_json(size_t id) const override;
/** /**
* @brief Handle a CAN send event * @brief Handle a CAN send event
* *
@@ -104,7 +90,7 @@ class UI_can : public UI_item
* @param dlc The data length code * @param dlc The data length code
* @param payload The data payload * @param payload The data payload
*/ */
void can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload) override; void can_send(uint32_t id, uint64_t dlc, QByteArray payload) override;
/** /**
* @brief Clear the CAN item * @brief Clear the CAN item
* *
@@ -116,8 +102,4 @@ class UI_can : public UI_item
* @param msg The CAN message sent * @param msg The CAN message sent
*/ */
void send(QCanBusFrame msg); void send(QCanBusFrame msg);
private:
std::vector<CanFrame> m_can_send_messages;
std::vector<CanFrame> m_can_received_messages;
}; };
+44 -25
View File
@@ -1,46 +1,65 @@
#include "ui_checkbox.hpp" #include "ui_checkbox.hpp"
UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_enum::ui_checkbox, parent), m_checked() UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_checkbox, parent)
{ {
m_ui_item.setCheckbox(UI_checkbox_m());
} }
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_checkbox::UI_checkbox(QString text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_checkbox, text, fg_color, bg_color, parent), m_checked(false) : UI_item(UI_typeGadget::UI_type::ui_checkbox, parent)
{ {
m_ui_item.setCheckbox(UI_checkbox_m());
m_ui_item.checkbox().setText(text);
m_ui_item.checkbox().setChecked(false);
m_ui_item.label().mutColor().setFgColor(fg_color.color());
m_ui_item.label().mutColor().setBgColor(bg_color.color());
}
void UI_checkbox::set_text(QString text)
{
m_ui_item.label().setText(text);
}
QString UI_checkbox::text() const
{
return m_ui_item.label().text();
}
void UI_checkbox::set_fg_color(Color color)
{
m_ui_item.label().mutColor().setFgColor(color.color());
}
Color UI_checkbox::fg_color() const
{
return Color(m_ui_item.label().color().fgColor());
}
void UI_checkbox::set_bg_color(Color color)
{
m_ui_item.label().mutColor().setBgColor(color.color());
}
Color UI_checkbox::bg_color() const
{
return Color(m_ui_item.label().color().bgColor());
} }
void UI_checkbox::click() void UI_checkbox::click()
{ {
m_checked = !m_checked; m_ui_item.checkbox().setChecked(!m_ui_item.checkbox().checked());
emit ui_changed(); emit ui_changed();
emit changed(m_checked); emit changed(m_ui_item.checkbox().checked());
} }
void UI_checkbox::set_state(bool state) void UI_checkbox::set_state(bool state)
{ {
m_checked = state; m_ui_item.checkbox().setChecked(state);
emit ui_changed(); emit ui_changed();
emit changed(m_checked); emit changed(m_ui_item.checkbox().checked());
} }
bool UI_checkbox::state() const bool UI_checkbox::state() const
{ {
return m_checked; return m_ui_item.checkbox().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;
} }
+18 -18
View File
@@ -23,7 +23,24 @@ class UI_checkbox : public UI_item
* @param bg_color The background color * @param bg_color The background color
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_checkbox(QString text, Color fg_color, Color bg_color, QObject *parent = nullptr);
/**
* @brief set_text
* @param text The text field of the button
*/
void set_text(QString text) override;
/**
* @brief text
* @return The text field of the button
*/
QString text() const;
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
/** /**
* @brief Handle a click event on the checkbox * @brief Handle a click event on the checkbox
@@ -44,20 +61,6 @@ class UI_checkbox : public UI_item
*/ */
bool state() const; bool state() const;
/**
* @brief Deserialize the checkbox from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the checkbox to JSON
*
* @param id The id of the checkbox
* @return json The JSON representation of the checkbox
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the checkbox state changes * @brief Signal emitted when the checkbox state changes
@@ -65,7 +68,4 @@ class UI_checkbox : public UI_item
* @param checked The new state of the checkbox * @param checked The new state of the checkbox
*/ */
void changed(bool checked); void changed(bool checked);
private:
bool m_checked;
}; };
+17 -37
View File
@@ -1,31 +1,33 @@
#include "ui_combobox.hpp" #include "ui_combobox.hpp"
UI_combobox::UI_combobox(QObject *parent) : UI_item(UI_enum::ui_combobox, parent), m_options() UI_combobox::UI_combobox(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_combobox, parent)
{ {
setup_item(false, true, true); m_ui_item.setCombobox(UI_combobox_m());
} }
UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected, UI_combobox::UI_combobox(QList<QString> options, qsizetype selected,
QObject *parent) QObject *parent)
: UI_item(UI_enum::ui_combobox, "", fg_color, bg_color, parent), m_options(options), m_selected(selected) : UI_item(UI_typeGadget::UI_type::ui_combobox, parent)
{ {
setup_item(false, true, true); m_ui_item.setCombobox(UI_combobox_m());
m_ui_item.combobox().setOptions(options);
m_ui_item.combobox().setSelected(selected);
} }
void UI_combobox::set_selected(size_t selected) void UI_combobox::set_selected(qsizetype selected)
{ {
if (selected == m_selected) if (selected == m_ui_item.combobox().selected())
return; return;
m_selected = std::move(selected); m_ui_item.combobox().setSelected(selected);
emit ui_changed(); emit ui_changed();
emit changed(selected_text()); emit changed(selected_text());
} }
void UI_combobox::set_selected(std::string selected) void UI_combobox::set_selected(QString selected)
{ {
for (size_t i = 0; i < m_options.size(); i++) for (qsizetype i = 0; i < m_ui_item.combobox().options().size(); i++)
{ {
if (m_options[i] == selected) if (m_ui_item.combobox().options().at(i) == selected)
{ {
set_selected(i); set_selected(i);
return; return;
@@ -33,34 +35,12 @@ void UI_combobox::set_selected(std::string selected)
} }
} }
size_t UI_combobox::selected() const qsizetype UI_combobox::selected() const
{ {
return m_selected; return m_ui_item.combobox().selected();
} }
std::string UI_combobox::selected_text() const & QString UI_combobox::selected_text() const &
{ {
return m_options[m_selected]; return m_ui_item.combobox().options().at(m_ui_item.combobox().selected());
}
void UI_combobox::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);
}
}
m_selected = j.at("selected");
}
json UI_combobox::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["options"] = this->m_options;
j["selected"] = m_selected;
return j;
} }
+11 -24
View File
@@ -24,56 +24,43 @@ class UI_combobox : public UI_item
* @param selected The initially selected option * @param selected The initially selected option
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0, UI_combobox(QList<QString> options, qsizetype selected = 0,
QObject *parent = nullptr); QObject *parent = nullptr);
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
/** /**
* @brief Set the selected option by index * @brief Set the selected option by index
* *
* @param selected The index of the option to select * @param selected The index of the option to select
*/ */
void set_selected(size_t selected) override; void set_selected(qsizetype selected) override;
/** /**
* @brief Set the selected option by text * @brief Set the selected option by text
* *
* @param selected The text of the option to select * @param selected The text of the option to select
*/ */
void set_selected(std::string selected); void set_selected(QString selected);
/** /**
* @brief Get the index of the selected option * @brief Get the index of the selected option
* *
* @return size_t The index of the selected option * @return size_t The index of the selected option
*/ */
size_t selected() const; qsizetype selected() const;
/** /**
* @brief Get the text of the selected option * @brief Get the text of the selected option
* *
* @return std::string The text of the selected option * @return std::string The text of the selected option
*/ */
std::string selected_text() const &; QString selected_text() const &;
/**
* @brief Deserialize the combobox from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the combobox to JSON
*
* @param id The id of the combobox
* @return json The JSON representation of the combobox
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the selected option changes * @brief Signal emitted when the selected option changes
* *
* @param selected The text of the newly selected option * @param selected The text of the newly selected option
*/ */
void changed(std::string selected); void changed(QString selected);
private:
std::vector<std::string> m_options;
size_t m_selected;
}; };
+19 -43
View File
@@ -1,78 +1,54 @@
#include "ui_dial.hpp" #include "ui_dial.hpp"
UI_dial::UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, UI_dial::UI_dial(QString text, QString unit, double min, double max,
double value, QObject *parent) double value, QObject *parent)
: UI_item(UI_enum::ui_dial, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit) : UI_item(UI_typeGadget::UI_type::ui_dial, parent)
{ {
m_ui_item.setRange(UI_range_m());
m_ui_item.range().setText(text);
m_ui_item.range().setUnit(unit);
m_ui_item.range().setMin(min);
m_ui_item.range().setMax(max);
m_ui_item.range().setValue(value);
} }
UI_dial::UI_dial(std::string text, std::string unit, double min, double max, double value, QObject *parent) UI_dial::UI_dial(QString text, double min, double max, double value,
: UI_dial(text, unit, Color::Default, Color::Default, min, max, value, parent)
{
}
UI_dial::UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent) QObject *parent)
: UI_dial(text, "", fg_color, bg_color, min, max, value, parent) : UI_dial(text, "", min, max, value, parent)
{ {
} }
UI_dial::UI_dial(std::string text, double min, double max, double value, QObject *parent) UI_dial::UI_dial(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_dial, parent)
: UI_dial(text, "", Color::Default, Color::Default, min, max, value, parent)
{
}
UI_dial::UI_dial(QObject *parent) : UI_item(UI_enum::ui_dial, parent)
{ {
} }
void UI_dial::set_value(double value) void UI_dial::set_value(double value)
{ {
if (value < m_min || value > m_max) if (value < m_ui_item.range().min() || value > m_ui_item.range().max())
return; return;
if (value == m_value) if (value == m_ui_item.range().value())
return; return;
this->m_value = value; m_ui_item.range().setValue(value);
emit ui_changed(); emit ui_changed();
} }
double UI_dial::value() const double UI_dial::value() const
{ {
return m_value; return m_ui_item.range().value();
} }
double UI_dial::min() const double UI_dial::min() const
{ {
return m_min; return m_ui_item.range().min();
} }
double UI_dial::max() const double UI_dial::max() const
{ {
return m_max; return m_ui_item.range().max();
} }
std::string UI_dial::unit() const & QString UI_dial::unit() const &
{ {
return m_unit; return m_ui_item.range().unit();
}
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;
} }
+4 -51
View File
@@ -14,39 +14,12 @@ class UI_dial : public UI_item
* *
* @param text The text to display * @param text The text to display
* @param unit The unit of the dial * @param unit The unit of the dial
* @param fg_color The foreground color
* @param bg_color The background color
* @param min The minimum value * @param min The minimum value
* @param max The maximum value * @param max The maximum value
* @param value The initial value * @param value The initial value
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value, UI_dial(QString text, QString unit, double min, double max, double value, QObject *parent = nullptr);
QObject *parent = nullptr);
/**
* @brief Construct a UI dial object
*
* @param text The text to display
* @param unit The unit of the dial
* @param min The minimum value
* @param max The maximum value
* @param value The initial value
* @param parent The parent QObject
*/
UI_dial(std::string text, std::string unit, double min, double max, double value, QObject *parent = nullptr);
/**
* @brief Construct a UI dial object
*
* @param text The text to display
* @param fg_color The foreground color
* @param bg_color The background color
* @param min The minimum value
* @param max The maximum value
* @param value The initial value
* @param parent The parent QObject
*/
UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent = nullptr);
/** /**
* @brief Construct a UI dial object * @brief Construct a UI dial object
* *
@@ -56,7 +29,7 @@ class UI_dial : public UI_item
* @param value The initial value * @param value The initial value
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_dial(std::string text, double min, double max, double value, QObject *parent = nullptr); UI_dial(QString text, double min, double max, double value, QObject *parent = nullptr);
/** /**
* @brief Construct a UI dial object * @brief Construct a UI dial object
* *
@@ -92,27 +65,7 @@ class UI_dial : public UI_item
/** /**
* @brief Get the unit of the dial * @brief Get the unit of the dial
* *
* @return std::string The unit of the dial * @return QString The unit of the dial
*/ */
std::string unit() const &; QString unit() const &;
/**
* @brief Deserialize the dial from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the dial to JSON
*
* @param id The id of the dial
* @return json The JSON representation of the dial
*/
json to_json(size_t id) const override;
private:
double m_min;
double m_max;
double m_value;
std::string m_unit;
}; };
+14 -32
View File
@@ -1,58 +1,40 @@
#include "ui_double_spinbox.hpp" #include "ui_double_spinbox.hpp"
UI_double_spinbox::UI_double_spinbox(QObject *parent) : UI_item(UI_enum::ui_double_spinbox, parent) UI_double_spinbox::UI_double_spinbox(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_double_spinbox, parent)
{ {
setup_item(false, false, false); m_ui_item.setSpinbox(UI_spinbox_m());
} }
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_enum::ui_double_spinbox, parent) : UI_item(UI_typeGadget::UI_type::ui_double_spinbox, parent)
{ {
setup_item(false, false, false); m_ui_item.setSpinbox(UI_spinbox_m());
m_min = min; m_ui_item.spinbox().setMin(min);
m_max = max; m_ui_item.spinbox().setMax(max);
m_value = value; m_ui_item.spinbox().setValue(value);
} }
void UI_double_spinbox::set_value(double value) void UI_double_spinbox::set_value(double value)
{ {
if (value < m_min || value > m_max) if (value < m_ui_item.spinbox().min() || value > m_ui_item.spinbox().max())
return; return;
if (value == m_value) if (value == m_ui_item.spinbox().value())
return; return;
this->m_value = value; m_ui_item.spinbox().setValue(value);
emit ui_changed(); emit ui_changed();
emit changed(m_value); emit changed(m_ui_item.spinbox().value());
} }
double UI_double_spinbox::value() const double UI_double_spinbox::value() const
{ {
return m_value; return m_ui_item.spinbox().value();
} }
double UI_double_spinbox::min() const double UI_double_spinbox::min() const
{ {
return m_min; return m_ui_item.spinbox().min();
} }
double UI_double_spinbox::max() const double UI_double_spinbox::max() const
{ {
return m_max; return m_ui_item.spinbox().max();
}
void UI_double_spinbox::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");
}
json UI_double_spinbox::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;
return j;
} }
-19
View File
@@ -50,20 +50,6 @@ class UI_double_spinbox : public UI_item
* @return double The maximum value * @return double The maximum value
*/ */
double max() const; double max() const;
/**
* @brief Deserialize the double spinbox from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the double spinbox to JSON
*
* @param id The id of the double spinbox
* @return json The JSON representation of the double spinbox
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the double spinbox value changes * @brief Signal emitted when the double spinbox value changes
@@ -71,9 +57,4 @@ class UI_double_spinbox : public UI_item
* @param value The new value * @param value The new value
*/ */
void changed(double value); void changed(double value);
private:
double m_min;
double m_max;
double m_value;
}; };
+14 -32
View File
@@ -1,58 +1,40 @@
#include "ui_hex_spinbox.hpp" #include "ui_hex_spinbox.hpp"
UI_hex_spinbox::UI_hex_spinbox(QObject *parent) : UI_item(UI_enum::ui_hex_spinbox, parent) UI_hex_spinbox::UI_hex_spinbox(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_hex_spinbox, parent)
{ {
setup_item(false, false, false); m_ui_item.setSpinbox(UI_spinbox_m());
} }
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_enum::ui_hex_spinbox, parent) : UI_item(UI_typeGadget::UI_type::ui_hex_spinbox, parent)
{ {
setup_item(false, false, false); m_ui_item.setSpinbox(UI_spinbox_m());
m_min = min; m_ui_item.spinbox().setMin(min);
m_max = max; m_ui_item.spinbox().setMax(max);
m_value = value; m_ui_item.spinbox().setValue(value);
} }
void UI_hex_spinbox::set_value(double value) void UI_hex_spinbox::set_value(double value)
{ {
if (value < m_min || value > m_max) if (value < m_ui_item.spinbox().min() || value > m_ui_item.spinbox().max())
return; return;
if (value == m_value) if (value == m_ui_item.spinbox().value())
return; return;
this->m_value = value; m_ui_item.spinbox().setValue(value);
emit ui_changed(); emit ui_changed();
emit changed(m_value); emit changed(m_ui_item.spinbox().value());
} }
double UI_hex_spinbox::value() const double UI_hex_spinbox::value() const
{ {
return m_value; return m_ui_item.spinbox().value();
} }
double UI_hex_spinbox::min() const double UI_hex_spinbox::min() const
{ {
return m_min; return m_ui_item.spinbox().min();
} }
double UI_hex_spinbox::max() const double UI_hex_spinbox::max() const
{ {
return m_max; return m_ui_item.spinbox().max();
}
void UI_hex_spinbox::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");
}
json UI_hex_spinbox::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;
return j;
} }
-19
View File
@@ -50,20 +50,6 @@ class UI_hex_spinbox : public UI_item
* @return double The maximum value * @return double The maximum value
*/ */
double max() const; double max() const;
/**
* @brief Deserialize the hex spinbox from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the hex spinbox to JSON
*
* @param id The id of the hex spinbox
* @return json The JSON representation of the hex spinbox
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the hex spinbox value changes * @brief Signal emitted when the hex spinbox value changes
@@ -71,9 +57,4 @@ class UI_hex_spinbox : public UI_item
* @param value The new value * @param value The new value
*/ */
void changed(double value); void changed(double value);
private:
double m_min;
double m_max;
double m_value;
}; };
+34 -125
View File
@@ -1,172 +1,81 @@
#include "ui_item.hpp" #include "ui_item.hpp"
#include <magic_enum/magic_enum.hpp>
UI_item::UI_item(UI_enum type, std::string text, Color fg_color, Color bg_color, QObject *parent) UI_item::UI_item(UI_typeGadget::UI_type type, 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), : QObject(parent)
m_enabled(true), m_visible(true)
{ {
m_ui_item.setId_proto((size_t)-1);
m_ui_item.setType(type);
m_ui_item.setEnabled(true);
m_ui_item.setVisible(true);
} }
UI_item::UI_item(UI_enum type, QObject *parent) void UI_item::set_id(size_t id)
: 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)
{ {
} m_ui_item.setId_proto(id);
std::string_view UI_item::type() const &
{
return magic_enum::enum_name(m_type);
}
bool UI_item::is_type(json j)
{
if (!j.contains("type"))
{
return false;
}
return magic_enum::enum_cast<UI_enum>((std::string)j.at("type")).value_or(UI_enum::end) == m_type;
}
void UI_item::set_text(std::string text)
{
if (this->m_text != text)
{
this->m_text = std::move(text);
emit ui_changed();
}
}
void UI_item::append_text(std::string text)
{
m_text.append(text);
emit ui_changed();
}
std::string UI_item::text() const &
{
return m_text;
}
void UI_item::set_fg_color(Color fg_color)
{
if (this->m_fg_color != fg_color)
{
this->m_fg_color = std::move(fg_color);
emit ui_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 = std::move(bg_color);
emit ui_changed();
}
}
Color UI_item::bg_color() const &
{
return m_bg_color;
} }
void UI_item::set_enabled(bool enabled) void UI_item::set_enabled(bool enabled)
{ {
if (this->m_enabled != enabled) if (m_ui_item.enabled() != enabled)
{ {
this->m_enabled = std::move(enabled); m_ui_item.setEnabled(enabled);
emit ui_changed(); emit ui_changed();
} }
} }
bool UI_item::is_enabled() const bool UI_item::is_enabled() const
{ {
return m_enabled; return m_ui_item.enabled();
} }
void UI_item::set_visible(bool visible) void UI_item::set_visible(bool visible)
{ {
if (this->m_visible != visible) if (m_ui_item.visible() != visible)
{ {
this->m_visible = std::move(visible); m_ui_item.setVisible(visible);
emit ui_changed(); emit ui_changed();
} }
} }
bool UI_item::is_visible() const bool UI_item::is_visible() const
{ {
return m_visible; return m_ui_item.visible();
} }
void UI_item::from_json(const json &j) UI_item_m UI_item::to_proto() const
{ {
if (!is_type(j)) return m_ui_item;
return;
j.at("id").get_to(m_id);
if (j.contains("text"))
j.at("text").get_to(m_text);
std::string color;
if (j.contains("fg_color"))
{
j.at("fg_color").get_to(color);
m_fg_color = Color(color);
color.clear();
}
if (j.contains("bg_color"))
{
j.at("bg_color").get_to(color);
m_bg_color = Color(color);
}
j.at("enabled").get_to(m_enabled);
j.at("visible").get_to(m_visible);
} }
json UI_item::to_json(size_t id) const void UI_item::from_proto(UI_item_m item)
{ {
json j; m_ui_item = item;
j["id"] = id;
j["type"] = type();
if (m_text_enabled)
j["text"] = m_text;
if (m_fg_color_enabled)
j["fg_color"] = m_fg_color.to_hex();
if (m_bg_color_enabled)
j["bg_color"] = m_bg_color.to_hex();
j["enabled"] = m_enabled;
j["visible"] = m_visible;
return j;
} }
void UI_item::click() {}; void UI_item::click() {};
void UI_item::set_selected(size_t selected) void UI_item::set_selected(qsizetype selected)
{ {
UNUSED(selected); Q_UNUSED(selected);
}; }
void UI_item::set_text(QString text)
{
Q_UNUSED(text);
}
void UI_item::set_value(double value) void UI_item::set_value(double value)
{ {
UNUSED(value); Q_UNUSED(value);
}; }
void UI_item::can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload) void UI_item::can_send(uint32_t id, uint64_t dlc, QByteArray payload)
{ {
UNUSED(id); Q_UNUSED(id);
UNUSED(dlc); Q_UNUSED(dlc);
UNUSED(payload); Q_UNUSED(payload);
}; }
void UI_item::clear() void UI_item::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;
}
+15 -147
View File
@@ -1,15 +1,13 @@
#pragma once #pragma once
#include "color.hpp" #include "color.hpp"
#include "ui_enum.hpp" #include <magic_enum/magic_enum.hpp>
#include "m_ui_items.qpb.h"
#include <QProtobufSerializer>
#include <cstdint> #include <cstdint>
#include <nlohmann/json.hpp>
#include <string> #include <string>
#include <QCanBusFrame> #include <QCanBusFrame>
#include <QObject> #include <QObject>
using json = nlohmann::json;
#define UNUSED(x) (void)(x)
/** /**
* @brief Base class representing a UI item * @brief Base class representing a UI item
@@ -25,77 +23,14 @@ class UI_item : public QObject
* @param type The type of the UI item * @param type The type of the UI item
* @param parent The parent QObject * @param parent The parent QObject
*/ */
explicit UI_item(UI_enum type, QObject *parent = nullptr); explicit UI_item(UI_typeGadget::UI_type type, QObject *parent = nullptr);
/**
* @brief Construct a new UI item object
*
* @param type The type of the UI item
* @param text The text of the UI item
* @param fg_color The foreground color of the UI item
* @param bg_color The background color of the UI item
* @param parent The parent QObject
*/
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 * @brief set_id Function for setting ID, DO NOT USE THIS IN A SIM YOURSELF
* *
* @return std::string The type of the UI item * @param id
*/ */
std::string_view type() const &; void set_id(size_t id);
/**
* @brief Check if the JSON object is of the correct type
*
* @param j The JSON object
* @return true If the JSON object is of the correct type
* @return false If the JSON object is not of the correct type
*/
bool is_type(json j);
/**
* @brief Set the text of the UI item
*
* @param text The text to set
*/
virtual void set_text(std::string text);
/**
* @brief Append text to the UI item
*
* @param text The text to append
*/
void append_text(std::string text);
/**
* @brief Get the text of the UI item
*
* @return std::string The text of the UI item
*/
std::string text() const &;
/**
* @brief Set the foreground color of the UI item
*
* @param fg_color The foreground color to set
*/
void set_fg_color(Color fg_color);
/**
* @brief Get the foreground color of the UI item
*
* @return Color The foreground color of the UI item
*/
Color fg_color() const &;
/**
* @brief Set the background color of the UI item
*
* @param bg_color The background color to set
*/
void set_bg_color(Color bg_color);
/**
* @brief Get the background color of the UI item
*
* @return Color The background color of the UI item
*/
Color bg_color() const &;
/** /**
* @brief Enable or disable the UI item * @brief Enable or disable the UI item
@@ -125,20 +60,8 @@ class UI_item : public QObject
*/ */
bool is_visible() const; bool is_visible() const;
/** UI_item_m to_proto() const;
* @brief Deserialize the UI item from JSON void from_proto(UI_item_m item);
*
* @param j The JSON object
*/
virtual void from_json(const json &j);
/**
* @brief Serialize the UI item to JSON
*
* @param id The id of the UI item
* @return json The JSON representation of the UI item
*/
virtual json to_json(size_t id) const;
/** /**
* @brief Virtual method to handle click events * @brief Virtual method to handle click events
@@ -150,7 +73,10 @@ class UI_item : public QObject
* *
* @param selected The index of the option to select * @param selected The index of the option to select
*/ */
virtual void set_selected(size_t selected); virtual void set_selected(qsizetype selected);
virtual void set_text(QString text);
/** /**
* @brief Virtual method to set the value (for spinboxes) * @brief Virtual method to set the value (for spinboxes)
* *
@@ -164,7 +90,7 @@ class UI_item : public QObject
* @param dlc The data length code * @param dlc The data length code
* @param payload The payload data * @param payload The payload data
*/ */
virtual void can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload); virtual void can_send(uint32_t id, uint64_t dlc, QByteArray payload);
/** /**
* @brief Virtual method to clear the UI item * @brief Virtual method to clear the UI item
* *
@@ -179,63 +105,5 @@ class UI_item : public QObject
void ui_changed(); void ui_changed();
protected: protected:
/** UI_item_m m_ui_item;
* @brief Setup the UI item properties
*
* @param text Enable text property
* @param fg_color Enable foreground color property
* @param bg_color Enable background color property
*/
void setup_item(bool text, bool fg_color, bool bg_color);
/**
* @brief The id of the UI item
*
*/
size_t m_id;
/**
* @brief The type of the UI item
*
*/
UI_enum m_type;
/**
* @brief The text of the UI item
*
*/
std::string m_text;
/**
* @brief Whether the text property is enabled
*
*/
bool m_text_enabled = true;
/**
* @brief The foreground color of the UI item
*
*/
Color m_fg_color;
/**
* @brief Whether the foreground color property is enabled
*
*/
bool m_fg_color_enabled = true;
/**
* @brief The background color of the UI item
*
*/
Color m_bg_color;
/**
* @brief Whether the background color property is enabled
*
*/
bool m_bg_color_enabled = true;
/**
* @brief Whether the UI item is enabled
*
*/
bool m_enabled;
/**
* @brief Whether the UI item is visible
*
*/
bool m_visible;
}; };
+39 -7
View File
@@ -1,20 +1,52 @@
#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(QString text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_label, text, fg_color, bg_color, parent) : UI_item(UI_typeGadget::UI_type::ui_label, parent)
{ {
m_ui_item.setLabel(UI_label_m());
m_ui_item.label().setText(text);
m_ui_item.label().mutColor().setFgColor(fg_color.color());
m_ui_item.label().mutColor().setBgColor(bg_color.color());
} }
UI_label::UI_label(QObject *parent) : UI_item(UI_enum::ui_label, parent) UI_label::UI_label(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_label, parent)
{ {
m_ui_item.setLabel(UI_label_m());
} }
void UI_label::from_json(const json &j) void UI_label::set_text(QString text)
{ {
UI_item::from_json(j); m_ui_item.label().setText(text);
} }
json UI_label::to_json(size_t id) const void UI_label::append_text(QString text)
{ {
return UI_item::to_json(id); QString prev = m_ui_item.label().text();
prev.append(text);
m_ui_item.label().setText(prev);
}
QString UI_label::text() const
{
return m_ui_item.label().text();
}
void UI_label::set_fg_color(Color color)
{
m_ui_item.label().mutColor().setFgColor(color.color());
}
Color UI_label::fg_color() const
{
return Color(m_ui_item.label().color().fgColor());
}
void UI_label::set_bg_color(Color color)
{
m_ui_item.label().mutColor().setBgColor(color.color());
}
Color UI_label::bg_color() const
{
return Color(m_ui_item.label().color().bgColor());
} }
+19 -10
View File
@@ -18,7 +18,7 @@ class UI_label : public UI_item
* @param bg_color The background color * @param bg_color The background color
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_label(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_label(QString text, Color fg_color, Color bg_color, QObject *parent = nullptr);
/** /**
* @brief Construct a UI label object * @brief Construct a UI label object
* *
@@ -27,16 +27,25 @@ class UI_label : public UI_item
UI_label(QObject *parent = nullptr); UI_label(QObject *parent = nullptr);
/** /**
* @brief Deserialize the label from JSON * @brief set_text
* * @param text The text field of the button
* @param j The JSON object
*/ */
void from_json(const json &j) override; void set_text(QString text);
/** /**
* @brief Serialize the label to JSON * @brief append_text Append
* * @param text
* @param id The id of the label
* @return json The JSON representation of the label
*/ */
json to_json(size_t id) const override; void append_text(QString text);
/**
* @brief text
* @return The text field of the button
*/
QString text() const;
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
}; };
+38 -14
View File
@@ -1,12 +1,47 @@
#include "ui_led.hpp" #include "ui_led.hpp"
UI_led::UI_led(QObject *parent) : UI_item(UI_enum::ui_led, "", Color::Default, Color::Default, parent) UI_led::UI_led(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_led, parent)
{ {
m_ui_item.setLabel(UI_label_m());
} }
UI_led::UI_led(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_led::UI_led(QString text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_led, text, fg_color, bg_color, parent) : UI_item(UI_typeGadget::UI_type::ui_led, parent)
{ {
m_ui_item.setLabel(UI_label_m());
m_ui_item.label().setText(text);
m_ui_item.label().mutColor().setFgColor(fg_color.color());
m_ui_item.label().mutColor().setBgColor(bg_color.color());
}
void UI_led::set_text(QString text)
{
m_ui_item.label().setText(text);
}
QString UI_led::text() const
{
return m_ui_item.label().text();
}
void UI_led::set_fg_color(Color color)
{
m_ui_item.label().mutColor().setFgColor(color.color());
}
Color UI_led::fg_color() const
{
return Color(m_ui_item.label().color().fgColor());
}
void UI_led::set_bg_color(Color color)
{
m_ui_item.label().mutColor().setBgColor(color.color());
}
Color UI_led::bg_color() const
{
return Color(m_ui_item.label().color().bgColor());
} }
bool UI_led::set_led(bool state, Color on, Color off) bool UI_led::set_led(bool state, Color on, Color off)
@@ -14,14 +49,3 @@ bool UI_led::set_led(bool state, Color on, Color off)
set_bg_color(state ? on : off); set_bg_color(state ? on : off);
return state; return state;
} }
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;
}
+18 -15
View File
@@ -24,7 +24,24 @@ class UI_led : public UI_item
* @param bg_color The background color * @param bg_color The background color
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_led(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_led(QString text, Color fg_color, Color bg_color, QObject *parent = nullptr);
/**
* @brief set_text
* @param text The text field of the button
*/
void set_text(QString text) override;
/**
* @brief text
* @return The text field of the button
*/
QString text() const;
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
/** /**
* @brief Set the state of the LED * @brief Set the state of the LED
@@ -35,18 +52,4 @@ class UI_led : public UI_item
* @return state * @return state
*/ */
bool set_led(bool state, Color on, Color off); bool set_led(bool state, Color on, Color off);
/**
* @brief Deserialize the LED from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the LED to JSON
*
* @param id The id of the LED
* @return json The JSON representation of the LED
*/
json to_json(size_t id) const override;
}; };
+58 -41
View File
@@ -1,88 +1,105 @@
#include "ui_plot.hpp" #include "ui_plot.hpp"
UI_plot::UI_plot(QObject *parent) : UI_item{UI_enum::ui_plot, parent} UI_plot::UI_plot(QObject *parent) : UI_item{UI_typeGadget::UI_type::ui_plot, parent}
{ {
m_ui_item.setPlot(UI_plot_m());
} }
UI_plot::UI_plot(std::string text, std::string x_label, std::string y_label, QObject *parent) UI_plot::UI_plot(QString text, QString x_label, QString y_label, QObject *parent)
: UI_item(UI_enum::ui_plot, parent), m_x_label(x_label), m_y_label(y_label) : UI_item(UI_typeGadget::UI_type::ui_plot, parent)
{ {
set_text(text); m_ui_item.setPlot(UI_plot_m());
m_ui_item.plot().setText(text);
m_ui_item.plot().setXLabel(x_label);
m_ui_item.plot().setYLabel(y_label);
} }
void UI_plot::set_x_label(std::string name) void UI_plot::set_x_label(QString name)
{ {
m_x_label = std::move(name); m_ui_item.plot().setXLabel(name);
emit ui_changed(); emit ui_changed();
} }
std::string UI_plot::x_label() const & QString UI_plot::x_label() const &
{ {
return m_x_label; return m_ui_item.plot().xLabel();
} }
void UI_plot::set_y_label(std::string name) void UI_plot::set_y_label(QString name)
{ {
m_y_label = std::move(name); m_ui_item.plot().setYLabel(name);
emit ui_changed(); emit ui_changed();
} }
std::string UI_plot::y_label() const & QString UI_plot::y_label() const &
{ {
return m_y_label; return m_ui_item.plot().yLabel();
}
void UI_plot::set_fg_color(Color color)
{
m_ui_item.plot().mutColor().setFgColor(color.color());
emit ui_changed();
}
Color UI_plot::fg_color() const&
{
return Color(m_ui_item.plot().color().fgColor());
}
void UI_plot::set_bg_color(Color color)
{
m_ui_item.plot().mutColor().setBgColor(color.color());
emit ui_changed();
}
Color UI_plot::bg_color() const
{
return Color(m_ui_item.plot().color().bgColor());
} }
void UI_plot::add_value(double y) void UI_plot::add_value(double y)
{ {
m_x_vals.push_back((double)m_x_vals.size()); auto x_vals = m_ui_item.plot().xVals();
m_y_vals.push_back(y); auto y_vals = m_ui_item.plot().yVals();
x_vals.push_back((double)x_vals.size());
y_vals.push_back(y);
m_ui_item.plot().setXVals(x_vals);
m_ui_item.plot().setYVals(y_vals);
emit ui_changed(); emit ui_changed();
} }
void UI_plot::add_value(double x, double y) void UI_plot::add_value(double x, double y)
{ {
m_x_vals.push_back(x); auto x_vals = m_ui_item.plot().xVals();
m_y_vals.push_back(y); auto y_vals = m_ui_item.plot().yVals();
x_vals.push_back(x);
y_vals.push_back(y);
m_ui_item.plot().setXVals(x_vals);
m_ui_item.plot().setYVals(y_vals);
emit ui_changed(); emit ui_changed();
} }
const std::vector<double> &UI_plot::x_vals() const & const QList<double> &UI_plot::x_vals() const &
{ {
return m_x_vals; return m_ui_item.plot().xVals();
} }
const std::vector<double> &UI_plot::y_vals() const & const QList<double> &UI_plot::y_vals() const &
{ {
return m_y_vals; return m_ui_item.plot().yVals();
} }
void UI_plot::clean_vals() void UI_plot::clean_vals()
{ {
m_x_vals.clear(); m_ui_item.plot().setXVals(QList<double>());
m_y_vals.clear(); m_ui_item.plot().setYVals(QList<double>());
emit ui_changed(); emit ui_changed();
} }
void UI_plot::from_json(const json &j)
{
UI_item::from_json(j);
this->m_x_label = j.at("x_label");
this->m_y_label = j.at("y_label");
this->m_x_vals = (std::vector<double>)j.at("x_vals");
this->m_y_vals = (std::vector<double>)j.at("y_vals");
}
json UI_plot::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["x_label"] = m_x_label;
j["y_label"] = m_y_label;
j["x_vals"] = m_x_vals;
j["y_vals"] = m_y_vals;
return j;
}
+29 -29
View File
@@ -23,33 +23,53 @@ class UI_plot : public UI_item
* @param y_label The label for the y-axis * @param y_label The label for the y-axis
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_plot(std::string text, std::string x_label, std::string y_label, QObject *parent = nullptr); UI_plot(QString text, QString x_label, QString y_label, QObject *parent = nullptr);
/** /**
* @brief Set the label for the x-axis * @brief Set the label for the x-axis
* *
* @param name The name to set * @param name The name to set
*/ */
void set_x_label(std::string name); void set_x_label(QString name);
/** /**
* @brief Get the label for the x-axis * @brief Get the label for the x-axis
* *
* @return std::string The x-axis label * @return QString The x-axis label
*/ */
std::string x_label() const &; QString x_label() const &;
/** /**
* @brief Set the label for the y-axis * @brief Set the label for the y-axis
* *
* @param name The name to set * @param name The name to set
*/ */
void set_y_label(std::string name); void set_y_label(QString name);
/** /**
* @brief Get the label for the y-axis * @brief Get the label for the y-axis
* *
* @return std::string The y-axis label * @return QString The y-axis label
*/ */
std::string y_label() const &; QString y_label() const &;
/**
* @brief set_fg_color Set the pen color
* @param color The color
*/
void set_fg_color(Color color);
/**
* @brief fg_color Get the pen color
* @return The color
*/
Color fg_color() const&;
/**
* @brief set_bg_color Set the background color
* @param color The color
*/
void set_bg_color(Color color);
/**
* @brief bg_color Get the background color
* @return The color
*/
Color bg_color() const;
/** /**
* @brief Add a value to the plot * @brief Add a value to the plot
@@ -70,37 +90,17 @@ class UI_plot : public UI_item
* *
* @return a list of x values and a list of y values * @return a list of x values and a list of y values
*/ */
const std::vector<double> &x_vals() const &; const QList<double> &x_vals() const &;
/** /**
* @brief Get the y values of the plot * @brief Get the y values of the plot
* This is only for testing purposes * This is only for testing purposes
* *
* @return a list of y values * @return a list of y values
*/ */
const std::vector<double> &y_vals() const &; const QList<double> &y_vals() const &;
/** /**
* @brief Clear all values from the plot * @brief Clear all values from the plot
* *
*/ */
void clean_vals(); void clean_vals();
/**
* @brief Deserialize the plot from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the plot to JSON
*
* @param id The id of the plot
* @return json The JSON representation of the plot
*/
json to_json(size_t id) const override;
private:
std::vector<double> m_x_vals;
std::vector<double> m_y_vals;
std::string m_x_label;
std::string m_y_label;
}; };
+19 -43
View File
@@ -1,75 +1,51 @@
#include "ui_slider.hpp" #include "ui_slider.hpp"
UI_slider::UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, UI_slider::UI_slider(QString text, QString unit, double min, double max,
double value, QObject *parent) double value, QObject *parent)
: UI_item(UI_enum::ui_slider, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), : UI_item(UI_typeGadget::UI_type::ui_slider, parent)
m_unit(unit)
{ {
m_ui_item.setRange(UI_range_m());
m_ui_item.range().setText(text);
m_ui_item.range().setUnit(unit);
m_ui_item.range().setMax(max);
m_ui_item.range().setMin(min);
m_ui_item.range().setValue(value);
} }
UI_slider::UI_slider(std::string text, std::string unit, double min, double max, double value, QObject *parent) UI_slider::UI_slider(QString text, double min, double max, double value,
: UI_slider(text, unit, Color::Default, Color::Default, min, max, value, parent)
{
}
UI_slider::UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent) QObject *parent)
: UI_slider(text, "", fg_color, bg_color, min, max, value, parent) : UI_slider(text, "", min, max, value, parent)
{ {
} }
UI_slider::UI_slider(std::string text, double min, double max, double value, QObject *parent) UI_slider::UI_slider(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_slider, parent)
: UI_slider(text, "", Color::Default, Color::Default, min, max, value, parent)
{
}
UI_slider::UI_slider(QObject *parent) : UI_item(UI_enum::ui_slider, parent)
{ {
m_ui_item.setRange(UI_range_m());
} }
void UI_slider::set_value(double value) void UI_slider::set_value(double value)
{ {
if (value < m_min || value > m_max) if (value < m_ui_item.range().min() || value > m_ui_item.range().max())
return; return;
if (value == m_value) if (value == m_ui_item.range().value())
return; return;
this->m_value = value; m_ui_item.range().setValue(value);
emit ui_changed(); emit ui_changed();
emit changed(m_value); emit changed(m_ui_item.range().value());
} }
double UI_slider::value() const double UI_slider::value() const
{ {
return m_value; return m_ui_item.range().value();
} }
double UI_slider::min() const double UI_slider::min() const
{ {
return m_min; return m_ui_item.range().min();
} }
double UI_slider::max() const double UI_slider::max() const
{ {
return m_max; return m_ui_item.range().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;
} }
+3 -45
View File
@@ -14,26 +14,13 @@ class UI_slider : public UI_item
* *
* @param text The text to display * @param text The text to display
* @param unit The unit of the slider * @param unit The unit of the slider
* @param fg_color The foreground color
* @param bg_color The background color
* @param min The minimum value * @param min The minimum value
* @param max The maximum value * @param max The maximum value
* @param value The initial value * @param value The initial value
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value, UI_slider(QString text, QString unit, double min, double max, double value,
QObject *parent = nullptr); QObject *parent = nullptr);
/**
* @brief Construct a UI slider object
*
* @param text The text to display
* @param unit The unit of the slider
* @param min The minimum value
* @param max The maximum value
* @param value The initial value
* @param parent The parent QObject
*/
UI_slider(std::string text, std::string unit, double min, double max, double value, QObject *parent = nullptr);
/** /**
* @brief Construct a UI slider object * @brief Construct a UI slider object
* *
@@ -45,18 +32,9 @@ class UI_slider : public UI_item
* @param value The initial value * @param value The initial value
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value, UI_slider(QString text, double min, double max, double value,
QObject *parent = nullptr); QObject *parent = nullptr);
/**
* @brief Construct a UI slider object
*
* @param text The text to display
* @param min The minimum value
* @param max The maximum value
* @param value The initial value
* @param parent The parent QObject
*/
UI_slider(std::string text, double min, double max, double value, QObject *parent = nullptr);
/** /**
* @brief Construct a UI slider object * @brief Construct a UI slider object
* *
@@ -89,20 +67,6 @@ class UI_slider : public UI_item
* @return double The maximum value * @return double The maximum value
*/ */
double max() const; double max() const;
/**
* @brief Deserialize the slider from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the slider to JSON
*
* @param id The id of the slider
* @return json The JSON representation of the slider
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the slider value changes * @brief Signal emitted when the slider value changes
@@ -110,10 +74,4 @@ class UI_slider : public UI_item
* @param value The new value of the slider * @param value The new value of the slider
*/ */
void changed(double value); void changed(double value);
private:
double m_min;
double m_max;
double m_value;
std::string m_unit;
}; };
+14 -32
View File
@@ -1,57 +1,39 @@
#include "ui_spinbox.hpp" #include "ui_spinbox.hpp"
UI_spinbox::UI_spinbox(QObject *parent) : UI_item(UI_enum::ui_spinbox, parent) UI_spinbox::UI_spinbox(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_spinbox, parent)
{ {
setup_item(false, false, false); m_ui_item.setSpinbox(UI_spinbox_m());
} }
UI_spinbox::UI_spinbox(double min, double max, double value, QObject *parent) : UI_item(UI_enum::ui_spinbox, parent) UI_spinbox::UI_spinbox(double min, double max, double value, QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_spinbox, parent)
{ {
setup_item(false, false, false); m_ui_item.setSpinbox(UI_spinbox_m());
m_min = min; m_ui_item.spinbox().setMin(min);
m_max = max; m_ui_item.spinbox().setMax(max);
m_value = value; m_ui_item.spinbox().setValue(value);
} }
void UI_spinbox::set_value(double value) void UI_spinbox::set_value(double value)
{ {
if (value < m_min || value > m_max) if (value < m_ui_item.spinbox().min() || value > m_ui_item.spinbox().max())
return; return;
if (value == m_value) if (value == m_ui_item.spinbox().value())
return; return;
this->m_value = value; m_ui_item.spinbox().setValue(value);
emit ui_changed(); emit ui_changed();
emit changed(m_value); emit changed(m_ui_item.spinbox().value());
} }
double UI_spinbox::value() const double UI_spinbox::value() const
{ {
return m_value; return m_ui_item.spinbox().value();
} }
double UI_spinbox::min() const double UI_spinbox::min() const
{ {
return m_min; return m_ui_item.spinbox().min();
} }
double UI_spinbox::max() const double UI_spinbox::max() const
{ {
return m_max; return m_ui_item.spinbox().max();
}
void UI_spinbox::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");
}
json UI_spinbox::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;
return j;
} }
-19
View File
@@ -50,20 +50,6 @@ class UI_spinbox : public UI_item
* @return double The maximum value * @return double The maximum value
*/ */
double max() const; double max() const;
/**
* @brief Deserialize the spinbox from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the spinbox to JSON
*
* @param id The id of the spinbox
* @return json The JSON representation of the spinbox
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the spinbox value changes * @brief Signal emitted when the spinbox value changes
@@ -71,9 +57,4 @@ class UI_spinbox : public UI_item
* @param value The new value of the spinbox * @param value The new value of the spinbox
*/ */
void changed(double value); void changed(double value);
private:
double m_min;
double m_max;
double m_value;
}; };
+14 -24
View File
@@ -1,44 +1,34 @@
#include "ui_stacked_widget.hpp" #include "ui_stacked_widget.hpp"
UI_stacked_widget::UI_stacked_widget(QObject *parent) : UI_item(UI_enum::ui_stacked_widget, parent) UI_stacked_widget::UI_stacked_widget(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_stacked_widget, parent)
{ {
m_ui_item.setStackedWidget(UI_stacked_widget_m());
} }
UI_stacked_widget::UI_stacked_widget(size_t tab_count, size_t current_tab, QObject *parent) UI_stacked_widget::UI_stacked_widget(qsizetype tab_count, qsizetype current_tab, QObject *parent)
: UI_item(UI_enum::ui_stacked_widget, parent), m_tab_count(tab_count), m_current_tab(current_tab) : UI_item(UI_typeGadget::UI_type::ui_stacked_widget, parent)
{ {
m_ui_item.setStackedWidget(UI_stacked_widget_m());
m_ui_item.stackedWidget().setTabCount(tab_count);
m_ui_item.stackedWidget().setCurrentTab(current_tab);
} }
void UI_stacked_widget::set_selected(size_t selected) void UI_stacked_widget::set_selected(qsizetype selected)
{ {
if (selected >= m_tab_count) if (selected >= m_ui_item.stackedWidget().tabCount())
return; return;
m_current_tab = selected; m_ui_item.stackedWidget().setCurrentTab(selected);
emit ui_changed(); emit ui_changed();
} }
size_t UI_stacked_widget::selected() const qsizetype UI_stacked_widget::selected() const
{ {
return m_current_tab; return m_ui_item.stackedWidget().currentTab();
} }
void UI_stacked_widget::advance_tab() void UI_stacked_widget::advance_tab()
{ {
if (m_tab_count == 0) if (m_ui_item.stackedWidget().tabCount() == 0)
return; return;
m_current_tab = (m_current_tab + 1) % m_tab_count; m_ui_item.stackedWidget().setCurrentTab((m_ui_item.stackedWidget().currentTab() + 1) % m_ui_item.stackedWidget().tabCount());
emit ui_changed(); emit ui_changed();
} }
void UI_stacked_widget::from_json(const json &j)
{
UI_item::from_json(j);
m_tab_count = j.at("tab_count");
m_current_tab = j.at("current_tab");
}
json UI_stacked_widget::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["tab_count"] = m_tab_count;
j["current_tab"] = m_current_tab;
return j;
}
+3 -21
View File
@@ -22,41 +22,23 @@ class UI_stacked_widget : public UI_item
* @param current_tab The currently selected tab * @param current_tab The currently selected tab
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_stacked_widget(size_t tab_count, size_t current_tab, QObject *parent = nullptr); UI_stacked_widget(qsizetype tab_count, qsizetype current_tab, QObject *parent = nullptr);
/** /**
* @brief Set the selected tab by index * @brief Set the selected tab by index
* *
* @param selected The index of the tab to select * @param selected The index of the tab to select
*/ */
void set_selected(size_t selected) override; void set_selected(qsizetype selected) override;
/** /**
* @brief Get the index of the selected tab * @brief Get the index of the selected tab
* *
* @return size_t The index of the selected tab * @return size_t The index of the selected tab
*/ */
size_t selected() const; qsizetype selected() const;
/** /**
* @brief Advance to the next tab * @brief Advance to the next tab
* *
*/ */
void advance_tab(); void advance_tab();
/**
* @brief Deserialize the stacked widget from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the stacked widget to JSON
*
* @param id The id of the stacked widget
* @return json The JSON representation of the stacked widget
*/
json to_json(size_t id) const override;
private:
size_t m_tab_count;
size_t m_current_tab;
}; };
+14 -25
View File
@@ -1,44 +1,33 @@
#include "ui_status_bar.hpp" #include "ui_status_bar.hpp"
UI_status_bar::UI_status_bar(QObject *parent) : UI_item(UI_enum::ui_status_bar, parent), m_timeout(0) UI_status_bar::UI_status_bar(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_status_bar, parent)
{ {
setup_item(true, false, false); m_ui_item.setStatusBar(UI_status_bar_m());
m_ui_item.statusBar().setTimeout(0);
} }
UI_status_bar::UI_status_bar(std::string message, QObject *parent) UI_status_bar::UI_status_bar(QString message, QObject *parent)
: UI_item(UI_enum::ui_status_bar, parent), m_timeout(0) : UI_item(UI_typeGadget::UI_type::ui_status_bar, parent)
{ {
m_text = std::move(message); m_ui_item.setStatusBar(UI_status_bar_m());
setup_item(true, false, false); m_ui_item.statusBar().setMessage(message);
m_ui_item.statusBar().setTimeout(0);
} }
void UI_status_bar::show_message(std::string message, size_t timeout) void UI_status_bar::show_message(QString message, size_t timeout)
{ {
// This should work because the message gets only send once when it changes // This should work because the message gets only send once when it changes
m_text = std::move(message); m_ui_item.statusBar().setMessage(message);
m_timeout = timeout; m_ui_item.statusBar().setTimeout(timeout);
emit ui_changed(); emit ui_changed();
} }
std::string UI_status_bar::message() const & QString UI_status_bar::message() const &
{ {
return m_text; return m_ui_item.statusBar().message();
} }
size_t UI_status_bar::timeout() const size_t UI_status_bar::timeout() const
{ {
return m_timeout; return m_ui_item.statusBar().timeout();
}
void UI_status_bar::from_json(const json &j)
{
UI_item::from_json(j);
m_timeout = j.at("timeout");
}
json UI_status_bar::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["timeout"] = m_timeout;
return j;
} }
+4 -21
View File
@@ -21,7 +21,7 @@ class UI_status_bar : public UI_item
* @param message The initial message * @param message The initial message
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_status_bar(std::string message, QObject *parent = nullptr); UI_status_bar(QString message, QObject *parent = nullptr);
/** /**
* @brief Show a message on the status bar * @brief Show a message on the status bar
@@ -29,34 +29,17 @@ class UI_status_bar : public UI_item
* @param message The message to show * @param message The message to show
* @param timeout The timeout in milliseconds (0 for no timeout) * @param timeout The timeout in milliseconds (0 for no timeout)
*/ */
void show_message(std::string message, size_t timeout = 0); void show_message(QString message, size_t timeout = 0);
/** /**
* @brief Get the current message * @brief Get the current message
* *
* @return std::string The current message * @return QString The current message
*/ */
std::string message() const &; QString message() const &;
/** /**
* @brief Get the timeout for the current message * @brief Get the timeout for the current message
* *
* @return size_t The timeout in milliseconds * @return size_t The timeout in milliseconds
*/ */
size_t timeout() const; size_t timeout() const;
/**
* @brief Deserialize the status bar from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the status bar to JSON
*
* @param id The id of the status bar
* @return json The JSON representation of the status bar
*/
json to_json(size_t id) const override;
private:
size_t m_timeout;
}; };
+32 -58
View File
@@ -1,104 +1,78 @@
#include "ui_tab_widget.hpp" #include "ui_tab_widget.hpp"
UI_tab_widget::UI_tab_widget(QObject *parent) : UI_item(UI_enum::ui_tab_widget, parent) UI_tab_widget::UI_tab_widget(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_tab_widget, parent)
{ {
setup_item(false, false, false); m_ui_item.setStackedWidget(UI_stacked_widget_m());
} }
UI_tab_widget::UI_tab_widget(std::vector<std::string> tab_names, size_t selected, QObject *parent) UI_tab_widget::UI_tab_widget(QList<QString> tab_names, qsizetype selected, QObject *parent)
: UI_item(UI_enum::ui_tab_widget, parent), m_selected_tab(selected), m_tab_names(tab_names) : UI_item(UI_typeGadget::UI_type::ui_tab_widget, parent)
{ {
setup_item(false, false, false); m_ui_item.setTabWidget(UI_tab_widget_m());
m_tab_visible.resize(m_tab_names.size(), true); m_ui_item.tabWidget().setTabNames(tab_names);
m_ui_item.tabWidget().setSelecteTab(selected);
} }
void UI_tab_widget::set_selected(size_t selected) void UI_tab_widget::set_selected(qsizetype selected)
{ {
if (selected >= m_tab_names.size()) if (selected >= m_ui_item.tabWidget().tabNames().size())
return; return;
m_selected_tab = selected; m_ui_item.tabWidget().setSelecteTab(selected);
emit ui_changed(); emit ui_changed();
emit changed(m_selected_tab); emit changed(selected);
} }
size_t UI_tab_widget::selected() const qsizetype UI_tab_widget::selected() const
{ {
return m_selected_tab; return m_ui_item.tabWidget().selecteTab();
} }
void UI_tab_widget::set_visible(size_t index, bool visible) void UI_tab_widget::set_visible(qsizetype index, bool visible)
{ {
if (index >= m_tab_visible.size()) if (index >= m_ui_item.tabWidget().tabNames().size())
{ {
// Only allow known tab amount from constructor // Only allow known tab amount from constructor
return; return;
} }
m_tab_visible.at(index) = visible; auto list = m_ui_item.tabWidget().tabVisible();
list.replace(index, visible);
m_ui_item.tabWidget().setTabVisible(list);
emit ui_changed(); emit ui_changed();
} }
bool UI_tab_widget::visible(size_t index) const bool UI_tab_widget::visible(qsizetype index) const
{ {
if (index >= m_tab_visible.size()) if (index >= m_ui_item.tabWidget().tabNames().size())
{ {
return false; return false;
} }
return m_tab_visible.at(index); return m_ui_item.tabWidget().tabVisible().at(index);
} }
void UI_tab_widget::set_tab_name(size_t index, std::string name) void UI_tab_widget::set_tab_name(qsizetype index, QString name)
{ {
if (index >= m_tab_names.size() || index >= m_tab_visible.size()) if (index >= m_ui_item.tabWidget().tabNames().size() || index >= m_ui_item.tabWidget().tabVisible().size())
{ {
// Only allow known tab amount from constructor // Only allow known tab amount from constructor
return; return;
} }
m_tab_names.at(index) = name; auto list = m_ui_item.tabWidget().tabNames();
list.replace(index, name);
m_ui_item.tabWidget().setTabNames(list);
emit ui_changed(); emit ui_changed();
} }
std::string UI_tab_widget::tab_name(size_t index) const QString UI_tab_widget::tab_name(qsizetype index) const
{ {
if (index >= m_tab_names.size()) if (index >= m_ui_item.tabWidget().tabNames().size())
{ {
return ""; return "";
} }
return m_tab_names.at(index); return m_ui_item.tabWidget().tabNames().at(index);
} }
void UI_tab_widget::advance_tab() void UI_tab_widget::advance_tab()
{ {
if (m_tab_names.size() == 0) if (m_ui_item.tabWidget().tabNames().empty())
return; return;
m_selected_tab = (m_selected_tab + 1) % m_tab_names.size(); m_ui_item.tabWidget().setSelecteTab((m_ui_item.tabWidget().selecteTab() + 1) % m_ui_item.tabWidget().tabNames().size());
emit ui_changed(); emit ui_changed();
emit changed(m_selected_tab); emit changed(m_ui_item.tabWidget().selecteTab());
}
void UI_tab_widget::from_json(const json &j)
{
UI_item::from_json(j);
if (j.contains("tab_names"))
{
this->m_tab_names.clear();
for (auto &option : j["tab_names"])
{
this->m_tab_names.push_back(option);
}
}
if (j.contains("tab_visible"))
{
this->m_tab_visible.clear();
for (auto &option : j["tab_visible"])
{
this->m_tab_visible.push_back(option);
}
}
m_selected_tab = j.at("selected");
}
json UI_tab_widget::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["tab_names"] = m_tab_names;
j["tab_visible"] = m_tab_visible;
j["selected"] = m_selected_tab;
return j;
} }
+8 -27
View File
@@ -21,20 +21,20 @@ class UI_tab_widget : public UI_item
* @param selected The initially selected tab * @param selected The initially selected tab
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_tab_widget(std::vector<std::string> tab_names, size_t selected = 0, QObject *parent = nullptr); UI_tab_widget(QList<QString> tab_names, qsizetype selected = 0, QObject *parent = nullptr);
/** /**
* @brief Set the selected tab by index * @brief Set the selected tab by index
* *
* @param selected The index of the tab to select * @param selected The index of the tab to select
*/ */
void set_selected(size_t selected) override; void set_selected(qsizetype selected) override;
/** /**
* @brief Get the index of the selected tab * @brief Get the index of the selected tab
* *
* @return size_t The index of the selected tab * @return size_t The index of the selected tab
*/ */
size_t selected() const; qsizetype selected() const;
/** /**
* @brief Set the visibility of a tab by index * @brief Set the visibility of a tab by index
@@ -42,7 +42,7 @@ class UI_tab_widget : public UI_item
* @param index The index of the tab * @param index The index of the tab
* @param visible True to show, false to hide * @param visible True to show, false to hide
*/ */
void set_visible(size_t index, bool visible); void set_visible(qsizetype index, bool visible);
/** /**
* @brief Get the visibility of a tab by index * @brief Get the visibility of a tab by index
* *
@@ -50,7 +50,7 @@ class UI_tab_widget : public UI_item
* @return true If the tab is visible * @return true If the tab is visible
* @return false If the tab is hidden * @return false If the tab is hidden
*/ */
bool visible(size_t index) const; bool visible(qsizetype index) const;
/** /**
* @brief Set the name of a tab by index * @brief Set the name of a tab by index
@@ -58,44 +58,25 @@ class UI_tab_widget : public UI_item
* @param index The index of the tab * @param index The index of the tab
* @param name The name to set * @param name The name to set
*/ */
void set_tab_name(size_t index, std::string name); void set_tab_name(qsizetype index, QString name);
/** /**
* @brief Get the name of a tab by index * @brief Get the name of a tab by index
* *
* @param index The index of the tab * @param index The index of the tab
* @return std::string The name of the tab * @return std::string The name of the tab
*/ */
std::string tab_name(size_t index) const; QString tab_name(qsizetype index) const;
/** /**
* @brief Advance to the next tab * @brief Advance to the next tab
* *
*/ */
void advance_tab(); void advance_tab();
/**
* @brief Deserialize the tab widget from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the tab widget to JSON
*
* @param id The id of the tab widget
* @return json The JSON representation of the tab widget
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the selected tab changes * @brief Signal emitted when the selected tab changes
* *
* @param index The index of the newly selected tab * @param index The index of the newly selected tab
*/ */
void changed(size_t index); void changed(qsizetype index);
private:
size_t m_selected_tab;
std::vector<std::string> m_tab_names;
std::vector<bool> m_tab_visible;
}; };
+80 -74
View File
@@ -1,162 +1,168 @@
#include "ui_table.hpp" #include "ui_table.hpp"
UI_table::UI_table(QObject *parent) : UI_item(UI_enum::ui_table, parent), m_row_count(0), m_column_count(0) UI_table::UI_table(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_table, parent)
{ {
setup_item(false, false, false); m_ui_item.setTable(UI_table_m());
} }
UI_table::UI_table(size_t row_count, size_t column_count, std::vector<std::string> row_labels, UI_table::UI_table(qsizetype row_count, qsizetype column_count, QList<QString> row_labels,
std::vector<std::string> column_labels, QObject *parent) QList<QString> column_labels, QObject *parent)
: UI_item(UI_enum::ui_table, parent), m_row_count(row_count), m_column_count(column_count), : UI_item(UI_typeGadget::UI_type::ui_table, parent)
m_row_labels(row_labels), m_column_labels(column_labels), m_table()
{ {
setup_item(false, false, false); m_ui_item.setTable(UI_table_m());
m_row_labels.reserve(100); m_ui_item.table().setRowCount(row_count);
m_column_labels.reserve(100); m_ui_item.table().setColumnCount(column_count);
m_table.reserve(500); m_ui_item.table().setColumnLabels(column_labels);
m_ui_item.table().setRowLabels(row_labels);
} }
void UI_table::set_row_count(size_t count) void UI_table::set_row_count(qsizetype count)
{ {
m_row_count = count; m_ui_item.table().setRowCount(count);
if (m_row_count != 0 || m_column_count != 0) auto column_count = m_ui_item.table().columnCount();
auto row_count = m_ui_item.table().rowCount();
auto row_labels = m_ui_item.table().rowLabels();
if (row_count != 0 || column_count != 0)
{ {
m_table.resize(m_column_count * m_row_count); auto table = m_ui_item.table().table();
table.resize(column_count * count);
m_ui_item.table().setTable(table);
} }
m_row_labels.resize(m_row_count); row_labels.resize(count);
m_ui_item.table().setRowLabels(row_labels);
emit ui_changed(); emit ui_changed();
} }
size_t UI_table::row_count() const qsizetype UI_table::row_count() const
{ {
return m_row_count; return m_ui_item.table().rowCount();
} }
void UI_table::set_column_count(size_t count) void UI_table::set_column_count(qsizetype count)
{ {
m_column_count = count; m_ui_item.table().setColumnCount(count);
if (m_row_count != 0 || m_column_count != 0) auto column_count = m_ui_item.table().columnCount();
auto row_count = m_ui_item.table().rowCount();
auto column_labels = m_ui_item.table().columnLabels();
if (row_count != 0 || column_count != 0)
{ {
m_table.resize(m_column_count * m_row_count); auto table = m_ui_item.table().table();
table.resize(column_count * count);
m_ui_item.table().setTable(table);
} }
m_column_labels.resize(m_column_count);
column_labels.resize(count);
m_ui_item.table().setColumnLabels(column_labels);
emit ui_changed(); emit ui_changed();
} }
size_t UI_table::column_count() const qsizetype UI_table::column_count() const
{ {
return m_column_count; return m_ui_item.table().columnCount();
} }
size_t UI_table::capacity() const qsizetype UI_table::capacity() const
{ {
return m_row_count * m_column_count; return m_ui_item.table().rowCount() * m_ui_item.table().columnCount();
} }
void UI_table::set_row_label(size_t index, std::string label) void UI_table::set_row_label(qsizetype index, QString label)
{ {
if (m_row_count <= index) if (m_ui_item.table().rowCount() <= index)
{ {
return; return;
} }
m_row_labels[index] = std::move(label); auto labels = m_ui_item.table().rowLabels();
labels.replace(index, label);
m_ui_item.table().setRowLabels(labels);
emit ui_changed(); emit ui_changed();
} }
std::string UI_table::row_label(size_t index) const QString UI_table::row_label(qsizetype index) const
{ {
if (m_row_count <= index) if (m_ui_item.table().rowCount() <= index)
{ {
return ""; return "";
} }
return m_row_labels[index]; return m_ui_item.table().rowLabels().at(index);
} }
void UI_table::set_column_label(size_t index, std::string label) void UI_table::set_column_label(qsizetype index, QString label)
{ {
if (m_column_count <= index) if (m_ui_item.table().columnCount() <= index)
{ {
return; return;
} }
m_column_labels[index] = std::move(label); auto labels = m_ui_item.table().columnLabels();
labels.replace(index, label);
m_ui_item.table().setColumnLabels(labels);
emit ui_changed(); emit ui_changed();
} }
std::string UI_table::column_label(size_t index) const & QString UI_table::column_label(qsizetype index) const &
{ {
if (m_column_count <= index) if (m_ui_item.table().columnCount() <= index)
{ {
return ""; return "";
} }
return m_column_labels[index]; return m_ui_item.table().columnLabels().at(index);
} }
void UI_table::insert_item(size_t row, size_t column, std::string text) void UI_table::insert_item(qsizetype row, qsizetype column, QString text)
{ {
if (m_column_count <= column || m_row_count <= row) if (m_ui_item.table().columnCount() <= column || m_ui_item.table().rowCount() <= row)
{ {
return; return;
} }
if (capacity() > m_table.size()) if (capacity() > m_ui_item.table().table().size())
{ {
m_table.resize(capacity()); auto table = m_ui_item.table().table();
table.resize(capacity());
m_ui_item.table().setTable(table);
} }
size_t index = row * m_column_count + column; qsizetype index = row * m_ui_item.table().columnCount() + column;
m_table.at(index) = std::move(text); auto table = m_ui_item.table().table();
table.replace(index, text);
m_ui_item.table().setTable(table);
emit ui_changed(); emit ui_changed();
} }
std::string UI_table::item(size_t row, size_t column) const & QString UI_table::item(qsizetype row, qsizetype column) const &
{ {
if (m_column_count <= column || m_row_count <= row) if (m_ui_item.table().columnCount() <= column || m_ui_item.table().rowCount() <= row)
{ {
return ""; return "";
} }
size_t index = row * m_column_count + column; qsizetype index = row * m_ui_item.table().columnCount() + column;
return m_table.at(index); return m_ui_item.table().table().at(index);
} }
void UI_table::empty_item(size_t row, size_t column) void UI_table::empty_item(qsizetype row, qsizetype column)
{ {
size_t index = row * m_column_count + column; if (m_ui_item.table().columnCount() <= column || m_ui_item.table().rowCount() <= row)
{
return;
}
qsizetype index = row * m_ui_item.table().columnCount() + column;
m_table[index] = ""; auto table = m_ui_item.table().table();
table.replace(index, "");
m_ui_item.table().setTable(table);
emit ui_changed(); emit ui_changed();
} }
void UI_table::from_json(const json &j)
{
UI_item::from_json(j);
this->m_row_count = j.at("row_count");
this->m_row_labels = j.at("row_labels");
this->m_column_count = j.at("column_count");
this->m_column_labels = j.at("column_labels");
this->m_table = j.at("table");
}
json UI_table::to_json(size_t id) const
{
json j = UI_item::to_json(id);
j["row_count"] = m_row_count;
j["row_labels"] = m_row_labels;
j["column_count"] = m_column_count;
j["column_labels"] = m_column_labels;
j["table"] = m_table;
return j;
}
+17 -39
View File
@@ -24,41 +24,41 @@ class UI_table : public UI_item
* @param column_labels The labels for the columns * @param column_labels The labels for the columns
* @param parent The parent QObject * @param parent The parent QObject
*/ */
explicit UI_table(size_t row_count, size_t column_count, std::vector<std::string> row_labels, explicit UI_table(qsizetype row_count, qsizetype column_count, QList<QString> row_labels,
std::vector<std::string> column_labels, QObject *parent = nullptr); QList<QString> column_labels, QObject *parent = nullptr);
/** /**
* @brief Set the number of rows in the table * @brief Set the number of rows in the table
* *
* @param count The number of rows * @param count The number of rows
*/ */
void set_row_count(size_t count); void set_row_count(qsizetype count);
/** /**
* @brief Get the number of rows in the table * @brief Get the number of rows in the table
* *
* @return size_t The number of rows * @return size_t The number of rows
*/ */
size_t row_count() const; qsizetype row_count() const;
/** /**
* @brief Set the number of columns in the table * @brief Set the number of columns in the table
* *
* @param count The number of columns * @param count The number of columns
*/ */
void set_column_count(size_t count); void set_column_count(qsizetype count);
/** /**
* @brief Get the number of columns in the table * @brief Get the number of columns in the table
* *
* @return size_t The number of columns * @return size_t The number of columns
*/ */
size_t column_count() const; qsizetype column_count() const;
/** /**
* @brief Get the capacity of the table (rows * columns) * @brief Get the capacity of the table (rows * columns)
* *
* @return size_t The capacity of the table * @return size_t The capacity of the table
*/ */
size_t capacity() const; qsizetype capacity() const;
/** /**
* @brief Set the label for a specific row * @brief Set the label for a specific row
@@ -66,14 +66,14 @@ class UI_table : public UI_item
* @param index The index of the row * @param index The index of the row
* @param label The label to set * @param label The label to set
*/ */
void set_row_label(size_t index, std::string label); void set_row_label(qsizetype index, QString label);
/** /**
* @brief Get the label of a specific row * @brief Get the label of a specific row
* *
* @param index The index of the row * @param index The index of the row
* @return std::string The label of the row * @return QString The label of the row
*/ */
std::string row_label(size_t index) const; QString row_label(qsizetype index) const;
/** /**
* @brief Set the label for a specific column * @brief Set the label for a specific column
@@ -81,14 +81,14 @@ class UI_table : public UI_item
* @param index The index of the column * @param index The index of the column
* @param label The label to set * @param label The label to set
*/ */
void set_column_label(size_t index, std::string label); void set_column_label(qsizetype index, QString label);
/** /**
* @brief Get the label of a specific column * @brief Get the label of a specific column
* *
* @param index The index of the column * @param index The index of the column
* @return std::string The label of the column * @return QString The label of the column
*/ */
std::string column_label(size_t index) const &; QString column_label(qsizetype index) const &;
/** /**
* @brief Insert an item into the table at the specified row and column * @brief Insert an item into the table at the specified row and column
@@ -97,42 +97,20 @@ class UI_table : public UI_item
* @param column The column of the item * @param column The column of the item
* @param text The text of the item * @param text The text of the item
*/ */
void insert_item(size_t row, size_t column, std::string text); void insert_item(qsizetype row, qsizetype column, QString text);
/** /**
* @brief Get the item at the specified row and column * @brief Get the item at the specified row and column
* *
* @param row The row of the item * @param row The row of the item
* @param column The column of the item * @param column The column of the item
* @return std::string The text of the item * @return QString The text of the item
*/ */
std::string item(size_t row, size_t column) const &; QString item(qsizetype row, qsizetype column) const &;
/** /**
* @brief empty_item This function clears the item * @brief empty_item This function clears the item
* @param row The row of the item * @param row The row of the item
* @param column The column of the item * @param column The column of the item
*/ */
void empty_item(size_t row, size_t column); void empty_item(qsizetype row, qsizetype column);
/**
* @brief Deserialize the table from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the table to JSON
*
* @param id The id of the table
* @return json The JSON representation of the table
*/
json to_json(size_t id) const override;
private:
size_t m_row_count;
size_t m_column_count;
std::vector<std::string> m_row_labels;
std::vector<std::string> m_column_labels;
std::vector<std::string> m_table;
}; };
+41 -23
View File
@@ -1,43 +1,61 @@
#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(QString hint, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_enum::ui_textbox, "", fg_color, bg_color, parent), m_hint(hint) : UI_item(UI_typeGadget::UI_type::ui_textbox, parent)
{ {
m_ui_item.setTextbox(UI_textbox_m());
m_ui_item.textbox().setHint(hint);
m_ui_item.textbox().mutColor().setFgColor(fg_color.color());
m_ui_item.textbox().mutColor().setBgColor(bg_color.color());
} }
UI_textbox::UI_textbox(QObject *parent) : UI_item(UI_enum::ui_textbox, parent) UI_textbox::UI_textbox(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_textbox, parent)
{ {
m_ui_item.setTextbox(UI_textbox_m());
} }
void UI_textbox::set_text(std::string text) void UI_textbox::set_text(QString text)
{ {
if (text == m_text) if (text == m_ui_item.textbox().text())
return; return;
m_text = std::move(text); m_ui_item.textbox().setText(text);
emit ui_changed(); emit ui_changed();
emit changed(m_text); emit changed(m_ui_item.textbox().text());
} }
void UI_textbox::set_hint(std::string hint) QString UI_textbox::text() const
{ {
m_hint = std::move(hint); return m_ui_item.label().text();
}
void UI_textbox::set_fg_color(Color color)
{
m_ui_item.label().mutColor().setFgColor(color.color());
}
Color UI_textbox::fg_color() const
{
return Color(m_ui_item.label().color().fgColor());
}
void UI_textbox::set_bg_color(Color color)
{
m_ui_item.label().mutColor().setBgColor(color.color());
}
Color UI_textbox::bg_color() const
{
return Color(m_ui_item.label().color().bgColor());
}
void UI_textbox::set_hint(QString hint)
{
m_ui_item.textbox().setHint(hint);
emit ui_changed(); emit ui_changed();
} }
std::string UI_textbox::hint() const & QString UI_textbox::hint() const &
{ {
return m_hint; return m_ui_item.textbox().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;
}
+13 -23
View File
@@ -18,7 +18,7 @@ class UI_textbox : public UI_item
* @param bg_color The background color * @param bg_color The background color
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_textbox(std::string hint, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_textbox(QString hint, Color fg_color, Color bg_color, QObject *parent = nullptr);
/** /**
* @brief Construct a UI textbox object * @brief Construct a UI textbox object
* *
@@ -31,42 +31,32 @@ class UI_textbox : public UI_item
* *
* @param text The text to set * @param text The text to set
*/ */
void set_text(std::string text) override; void set_text(QString text) override;
QString text() const;
void set_fg_color(Color fg_color);
Color fg_color() const;
void set_bg_color(Color bg_color);
Color bg_color() const;
/** /**
* @brief Get the hint text of the textbox * @brief Get the hint text of the textbox
* *
* @param hint The hint text to set * @param hint The hint text to set
*/ */
void set_hint(std::string hint); void set_hint(QString hint);
/** /**
* @brief Get the hint text of the textbox * @brief Get the hint text of the textbox
* *
* @return std::string The hint text * @return QString The hint text
*/ */
std::string hint() const &; QString hint() const &;
/**
* @brief Deserialize the textbox from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the textbox to JSON
*
* @param id The id of the textbox
* @return json The JSON representation of the textbox
*/
json to_json(size_t id) const override;
signals: signals:
/** /**
* @brief Signal emitted when the text changes * @brief Signal emitted when the text changes
* *
* @param text The new text * @param text The new text
*/ */
void changed(std::string text); void changed(QString text);
private:
std::string m_hint;
}; };
+47 -83
View File
@@ -1,143 +1,107 @@
#include "ui_thermo.hpp" #include "ui_thermo.hpp"
UI_thermo::UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, UI_thermo::UI_thermo(QString text, QString unit, double min, double max,
double value, QObject *parent) double value, QObject *parent)
: UI_item(UI_enum::ui_thermo, text, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), : UI_item(UI_typeGadget::UI_type::ui_thermo, parent)
m_unit(unit)
{ {
m_ui_item.setRange(UI_range_m());
m_ui_item.range().setText(text);
m_ui_item.range().setUnit(unit);
m_ui_item.range().setMin(min);
m_ui_item.range().setMax(max);
m_ui_item.range().setValue(value);
} }
UI_thermo::UI_thermo(std::string text, std::string unit, double min, double max, double value, QObject *parent) UI_thermo::UI_thermo(QString text, double min, double max, double value,
: UI_thermo(text, unit, Color::Default, Color::Default, min, max, value, parent)
{
}
UI_thermo::UI_thermo(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent) QObject *parent)
: UI_thermo(text, "", fg_color, bg_color, min, max, value, parent) : UI_thermo(text, "", min, max, value, parent)
{ {
} }
UI_thermo::UI_thermo(std::string text, double min, double max, double value, QObject *parent) UI_thermo::UI_thermo(QObject *parent) : UI_item(UI_typeGadget::UI_type::ui_thermo, parent)
: UI_thermo(text, "", Color::Default, Color::Default, min, max, value, parent)
{
}
UI_thermo::UI_thermo(QObject *parent) : UI_item(UI_enum::ui_thermo, parent)
{ {
} }
void UI_thermo::set_value(double value) void UI_thermo::set_value(double value)
{ {
if (value < m_min || value > m_max) if (value < m_ui_item.range().min() || value > m_ui_item.range().max())
return; return;
if (value == m_value) if (value == m_ui_item.range().value())
return; return;
this->m_value = value; m_ui_item.range().setValue(value);
emit ui_changed(); emit ui_changed();
} }
double UI_thermo::value() const double UI_thermo::value() const
{ {
return m_value; return m_ui_item.range().value();
} }
double UI_thermo::min() const double UI_thermo::min() const
{ {
return m_min; return m_ui_item.range().min();
} }
double UI_thermo::max() const double UI_thermo::max() const
{ {
return m_max; return m_ui_item.range().max();
} }
void UI_thermo::set_start_color(Color color) void UI_thermo::set_start_color(Color color)
{ {
this->m_color_map[0] = color; Q_UNUSED(color);
emit ui_changed(); // this->m_color_map[0] = color;
// emit ui_changed();
} }
void UI_thermo::set_end_color(Color color) void UI_thermo::set_end_color(Color color)
{ {
this->m_color_map[1] = color; Q_UNUSED(color);
emit ui_changed(); // this->m_color_map[1] = color;
// emit ui_changed();
} }
void UI_thermo::add_color_stop(double value, Color color) void UI_thermo::add_color_stop(double value, Color color)
{ {
if (value <= 0 || value >= 1) Q_UNUSED(value);
return; Q_UNUSED(color);
// if (value <= 0 || value >= 1)
// return;
this->m_color_map[value] = color; // this->m_color_map[value] = color;
emit ui_changed(); // emit ui_changed();
} }
void UI_thermo::add_color_stop_normalized(double value, Color color) void UI_thermo::add_color_stop_normalized(double value, Color color)
{ {
if (value < m_min || value > m_max) Q_UNUSED(value);
return; Q_UNUSED(color);
// if (value < m_min || value > m_max)
// return;
double normalized = (value - m_min) / (m_max - m_min); // double normalized = (value - m_min) / (m_max - m_min);
this->m_color_map[normalized] = color; // this->m_color_map[normalized] = color;
emit ui_changed(); // emit ui_changed();
} }
void UI_thermo::remove_color_stop(double value) void UI_thermo::remove_color_stop(double value)
{ {
if (value <= 0 || value >= 1) Q_UNUSED(value);
return; // if (value <= 0 || value >= 1)
// return;
this->m_color_map.erase(value); // this->m_color_map.erase(value);
emit ui_changed(); // emit ui_changed();
} }
void UI_thermo::remove_color_stop_normalized(double value) void UI_thermo::remove_color_stop_normalized(double value)
{ {
if (value < m_min || value > m_max) Q_UNUSED(value);
return; // if (value < m_min || value > m_max)
// return;
double normalized = (value - m_min) / (m_max - m_min); // double normalized = (value - m_min) / (m_max - m_min);
this->m_color_map.erase(normalized); // this->m_color_map.erase(normalized);
emit ui_changed(); // emit ui_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;
} }
+2 -46
View File
@@ -15,26 +15,13 @@ class UI_thermo : public UI_item
* *
* @param text The text to display * @param text The text to display
* @param unit The unit of the thermometer * @param unit The unit of the thermometer
* @param fg_color The foreground color
* @param bg_color The background color
* @param min The minimum value * @param min The minimum value
* @param max The maximum value * @param max The maximum value
* @param value The initial value * @param value The initial value
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value, UI_thermo(QString text, QString unit, double min, double max, double value,
QObject *parent = nullptr); QObject *parent = nullptr);
/**
* @brief Construct a UI thermometer object
*
* @param text The text to display
* @param unit The unit of the thermometer
* @param min The minimum value
* @param max The maximum value
* @param value The initial value
* @param parent The parent QObject
*/
UI_thermo(std::string text, std::string unit, double min, double max, double value, QObject *parent = nullptr);
/** /**
* @brief Construct a UI thermometer object * @brief Construct a UI thermometer object
* *
@@ -46,18 +33,8 @@ class UI_thermo : public UI_item
* @param value The initial value * @param value The initial value
* @param parent The parent QObject * @param parent The parent QObject
*/ */
UI_thermo(std::string text, Color fg_color, Color bg_color, double min, double max, double value, UI_thermo(QString text, double min, double max, double value,
QObject *parent = nullptr); QObject *parent = nullptr);
/**
* @brief Construct a UI thermometer object
*
* @param text The text to display
* @param min The minimum value
* @param max The maximum value
* @param value The initial value
* @param parent The parent QObject
*/
UI_thermo(std::string text, double min, double max, double value, QObject *parent = nullptr);
/** /**
* @brief Construct a UI thermometer object * @brief Construct a UI thermometer object
* *
@@ -129,25 +106,4 @@ class UI_thermo : public UI_item
* @param value The normalized value (0.0 to 1.0) of the color stop to remove * @param value The normalized value (0.0 to 1.0) of the color stop to remove
*/ */
void remove_color_stop_normalized(double value); void remove_color_stop_normalized(double value);
/**
* @brief Deserialize the thermometer from JSON
*
* @param j The JSON object
*/
void from_json(const json &j) override;
/**
* @brief Serialize the thermometer to JSON
*
* @param id The id of the thermometer
* @return json The JSON representation of the thermometer
*/
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;
}; };