Add spinboxes
This commit is contained in:
2026-02-14 14:31:11 +01:00
parent ce9c88ad04
commit 0854a6ba7e
11 changed files with 382 additions and 46 deletions
+3
View File
@@ -3,11 +3,14 @@
#include "ui_button.hpp" #include "ui_button.hpp"
#include "ui_checkbox.hpp" #include "ui_checkbox.hpp"
#include "ui_combobox.hpp" #include "ui_combobox.hpp"
#include "ui_double_spinbox.hpp"
#include "ui_dial.hpp" #include "ui_dial.hpp"
#include "ui_hex_spinbox.hpp"
#include "ui_label.hpp" #include "ui_label.hpp"
#include "ui_led.hpp" #include "ui_led.hpp"
#include "ui_plot.hpp" #include "ui_plot.hpp"
#include "ui_slider.hpp" #include "ui_slider.hpp"
#include "ui_spinbox.hpp"
#include "ui_table.hpp" #include "ui_table.hpp"
#include "ui_textbox.hpp" #include "ui_textbox.hpp"
#include "ui_thermo.hpp" #include "ui_thermo.hpp"
+55
View File
@@ -0,0 +1,55 @@
#include "ui_double_spinbox.hpp"
UI_double_spinbox::UI_double_spinbox(QObject* parent) : UI_item(UI_DOUBLE_SPINBOX_TYPE, parent)
{
}
UI_double_spinbox::UI_double_spinbox(double min, double max, double value, QObject* parent) : UI_item(UI_DOUBLE_SPINBOX_TYPE, parent)
{
m_min = min;
m_max = max;
m_value = value;
}
void UI_double_spinbox::set_value(double value)
{
if (value < m_min || value > m_max)
return;
if (value == m_value)
return;
this->m_value = value;
emit ui_changed();
emit changed(m_value);
}
double UI_double_spinbox::value() const
{
return m_value;
}
double UI_double_spinbox::min() const
{
return m_min;
}
double UI_double_spinbox::max() const
{
return m_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;
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef UI_DOUBLE_SPINBOX_H
#define UI_DOUBLE_SPINBOX_H
#include "ui_item.hpp"
#define UI_DOUBLE_SPINBOX_TYPE std::string("ui_double_spinbox")
class UI_double_spinbox : public UI_item {
Q_OBJECT
public:
explicit UI_double_spinbox(QObject* parent = nullptr);
UI_double_spinbox(double min, double max, double value, QObject* parent = nullptr);
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
signals:
void changed(double value);
private:
double m_min;
double m_max;
double m_value;
};
#endif // UI_DOUBLE_SPINBOX_H
+54
View File
@@ -0,0 +1,54 @@
#include "ui_hex_spinbox.hpp"
UI_hex_spinbox::UI_hex_spinbox(QObject* parent) : UI_item(UI_HEX_SPINBOX_TYPE, parent) {
}
UI_hex_spinbox::UI_hex_spinbox(double min, double max, double value, QObject* parent) : UI_item(UI_HEX_SPINBOX_TYPE, parent)
{
m_min = min;
m_max = max;
m_value = value;
}
void UI_hex_spinbox::set_value(double value)
{
if (value < m_min || value > m_max)
return;
if (value == m_value)
return;
this->m_value = value;
emit ui_changed();
emit changed(m_value);
}
double UI_hex_spinbox::value() const
{
return m_value;
}
double UI_hex_spinbox::min() const
{
return m_min;
}
double UI_hex_spinbox::max() const
{
return m_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;
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef UI_HEX_SPINBOX_HPP
#define UI_HEX_SPINBOX_HPP
#include "ui_item.hpp"
#define UI_HEX_SPINBOX_TYPE std::string("ui_hex_spinbox")
class UI_hex_spinbox : public UI_item {
Q_OBJECT
public:
explicit UI_hex_spinbox(QObject* parent = nullptr);
UI_hex_spinbox(double min, double max, double value, QObject* parent = nullptr);
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
void from_json(const json &j) override;
json to_json(size_t id) const override;
signals:
void changed(double value);
private:
double m_min;
double m_max;
double m_value;
};
#endif // UI_HEX_SPINBOX_HPP
+129
View File
@@ -147,6 +147,21 @@ void Simulator_base::UI_item_parser(json &input)
process_ui_checkbox(ui_item, widget); process_ui_checkbox(ui_item, widget);
continue; continue;
} }
if (ui_item["type"] == "ui_spinbox")
{
process_ui_spinbox(ui_item, widget);
continue;
}
if (ui_item["type"] == "ui_double_spinbox")
{
process_ui_double_spinbox(ui_item, widget);
continue;
}
if (ui_item["type"] == "ui_hex_spinbox")
{
process_ui_hex_spinbox(ui_item, widget);
continue;
}
if (ui_item["type"] == "ui_plot") if (ui_item["type"] == "ui_plot")
{ {
process_ui_plot(ui_item, widget); process_ui_plot(ui_item, widget);
@@ -480,6 +495,87 @@ void Simulator_base::process_ui_led(json &ui_item, QWidget* widget)
} }
} }
void Simulator_base::process_ui_spinbox(json &ui_item, QWidget* widget)
{
auto spinbox = qobject_cast<QSpinBox *>(widget);
if (spinbox == nullptr)
{
SPDLOG_WARN("widget is not of type QSpinBox");
return;
}
double min = ui_item["min"];
double max = ui_item["max"];
double value = ui_item["value"];
if (min != spinbox->minimum())
{
spinbox->setMinimum(min);
}
if (max != spinbox->maximum())
{
spinbox->setMaximum(max);
}
if (value != spinbox->value())
{
spinbox->setValue((int)value);
}
}
void Simulator_base::process_ui_double_spinbox(json &ui_item, QWidget* widget)
{
auto spinbox = qobject_cast<QDoubleSpinBox *>(widget);
if (spinbox == nullptr)
{
SPDLOG_WARN("widget is not of type QDoubleSpinBox");
return;
}
double min = ui_item["min"];
double max = ui_item["max"];
double value = ui_item["value"];
if (min != spinbox->minimum())
{
spinbox->setMinimum(min);
}
if (max != spinbox->maximum())
{
spinbox->setMaximum(max);
}
if (value != spinbox->value())
{
spinbox->setValue(value);
}
}
void Simulator_base::process_ui_hex_spinbox(json &ui_item, QWidget* widget)
{
auto spinbox = qobject_cast<HexSpinBox *>(widget);
if (spinbox == nullptr)
{
SPDLOG_WARN("widget is not of type HexSpinBox");
return;
}
double min = ui_item["min"];
double max = ui_item["max"];
double value = ui_item["value"];
if (min != spinbox->minimum())
{
spinbox->setMinimum(min);
}
if (max != spinbox->maximum())
{
spinbox->setMaximum(max);
}
if (value != spinbox->value())
{
spinbox->setValue((int)value);
}
}
void Simulator_base::process_ui_plot(json &ui_item, QWidget* widget) void Simulator_base::process_ui_plot(json &ui_item, QWidget* widget)
{ {
auto plot = qobject_cast<Plot_wrapper *>(widget); auto plot = qobject_cast<Plot_wrapper *>(widget);
@@ -614,6 +710,9 @@ void Simulator_base::setup_ui_item(QWidget *item, size_t index)
setup_checkbox(item, index); setup_checkbox(item, index);
setup_dial(item, index); setup_dial(item, index);
setup_slider(item, index); setup_slider(item, index);
setup_spinbox(item, index);
setup_double_spinbox(item, index);
setup_hex_spinbox(item, index);
setup_qwtplot(item, index); setup_qwtplot(item, index);
setup_can_ui(item, index); setup_can_ui(item, index);
} }
@@ -667,6 +766,36 @@ void Simulator_base::setup_slider(QWidget *item, size_t index)
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, slider->value()); }); [=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, slider->value()); });
} }
void Simulator_base::setup_spinbox(QWidget *item, size_t index)
{
QSpinBox *spinbox = qobject_cast<QSpinBox *>(item);
if (spinbox == nullptr)
return;
connect(spinbox, &QSpinBox::valueChanged, this,
[=, this](int value) { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, (double)value); });
}
void Simulator_base::setup_double_spinbox(QWidget *item, size_t index)
{
QDoubleSpinBox *spinbox = qobject_cast<QDoubleSpinBox *>(item);
if (spinbox == nullptr)
return;
connect(spinbox, &QDoubleSpinBox::valueChanged, this,
[=, this](double value) { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, value); });
}
void Simulator_base::setup_hex_spinbox(QWidget *item, size_t index)
{
HexSpinBox *spinbox = qobject_cast<HexSpinBox *>(item);
if (spinbox == nullptr)
return;
connect(spinbox, &HexSpinBox::valueChanged, this,
[=, this](int value) { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, (double)value); });
}
void Simulator_base::setup_qwtplot(QWidget *item, size_t index) void Simulator_base::setup_qwtplot(QWidget *item, size_t index)
{ {
auto *plot = qobject_cast<QwtPlot *>(item); auto *plot = qobject_cast<QwtPlot *>(item);
+6
View File
@@ -48,6 +48,9 @@ class Simulator_base : public QMainWindow
virtual void process_ui_checkbox(json &ui_item, QWidget* widget); virtual void process_ui_checkbox(json &ui_item, QWidget* widget);
virtual void process_ui_button(json &ui_item, QWidget* widget); virtual void process_ui_button(json &ui_item, QWidget* widget);
virtual void process_ui_led(json &ui_item, QWidget* widget); virtual void process_ui_led(json &ui_item, QWidget* widget);
virtual void process_ui_spinbox(json &ui_item, QWidget* widget);
virtual void process_ui_double_spinbox(json &ui_item, QWidget* widget);
virtual void process_ui_hex_spinbox(json &ui_item, QWidget* widget);
virtual void process_ui_plot(json &ui_item, QWidget* widget); virtual void process_ui_plot(json &ui_item, QWidget* widget);
virtual void process_ui_table(json &ui_item, QWidget* widget); virtual void process_ui_table(json &ui_item, QWidget* widget);
virtual void process_ui_can(json &ui_item, QWidget* widget); virtual void process_ui_can(json &ui_item, QWidget* widget);
@@ -58,6 +61,9 @@ class Simulator_base : public QMainWindow
void setup_checkbox(QWidget *item, size_t index); void setup_checkbox(QWidget *item, size_t index);
void setup_dial(QWidget *item, size_t index); void setup_dial(QWidget *item, size_t index);
void setup_slider(QWidget *item, size_t index); void setup_slider(QWidget *item, size_t index);
void setup_spinbox(QWidget *item, size_t index);
void setup_double_spinbox(QWidget *item, size_t index);
void setup_hex_spinbox(QWidget *item, size_t index);
void setup_qwtplot(QWidget *item, size_t index); void setup_qwtplot(QWidget *item, size_t index);
void setup_can_ui(QWidget *item, size_t index); void setup_can_ui(QWidget *item, size_t index);
+3
View File
@@ -18,6 +18,9 @@ Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent)
PUSH_UI_ITEM(checkBox); PUSH_UI_ITEM(checkBox);
PUSH_UI_ITEM(plot); PUSH_UI_ITEM(plot);
PUSH_UI_ITEM(table); PUSH_UI_ITEM(table);
PUSH_UI_ITEM(spinBox);
PUSH_UI_ITEM(doubleSpinBox);
PUSH_UI_ITEM(hexSpinBox);
} }
Test_Sim::~Test_Sim() Test_Sim::~Test_Sim()
+54 -40
View File
@@ -15,24 +15,10 @@
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="2" column="4"> <item row="0" column="4">
<widget class="QPushButton" name="button"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>PushButton</string> <string>TextLabel</string>
</property>
</widget>
</item>
<item row="1" column="4">
<widget class="Led" name="led">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property> </property>
</widget> </widget>
</item> </item>
@@ -63,26 +49,35 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="0" column="4"> <item row="3" column="4">
<widget class="QLabel" name="label"> <widget class="QComboBox" name="combobox"/>
</item>
<item row="2" column="4">
<widget class="QPushButton" name="button">
<property name="text"> <property name="text">
<string>TextLabel</string> <string>PushButton</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="1" column="4">
<layout class="QVBoxLayout" name="verticalLayout"> <widget class="Led" name="led">
<item>
<widget class="QLabel" name="slider_label">
<property name="text"> <property name="text">
<string>0</string> <string>PushButton</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item row="5" column="2">
<widget class="QwtSlider" name="slider"/> <widget class="QTableWidget" name="table"/>
</item> </item>
</layout> <item row="5" column="1">
<widget class="Plot_wrapper" name="plot">
<property name="frameShape">
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Shadow::Raised</enum>
</property>
</widget>
</item> </item>
<item row="4" column="2"> <item row="4" column="2">
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
@@ -98,21 +93,35 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="3" column="4"> <item row="1" column="2">
<widget class="QComboBox" name="combobox"/> <widget class="QCheckBox" name="checkBox">
</item> <property name="text">
<item row="5" column="1"> <string>CheckBox</string>
<widget class="Plot_wrapper" name="plot">
<property name="frameShape">
<enum>QFrame::Shape::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Shadow::Raised</enum>
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="2"> <item row="1" column="1">
<widget class="QTableWidget" name="table"/> <widget class="QSpinBox" name="spinBox"/>
</item>
<item row="4" column="1">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="slider_label">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item>
<widget class="QwtSlider" name="slider"/>
</item>
</layout>
</item>
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBox"/>
</item>
<item row="3" column="1">
<widget class="HexSpinBox" name="hexSpinBox"/>
</item> </item>
</layout> </layout>
</widget> </widget>
@@ -155,6 +164,11 @@
<header>plot_wrapper.hpp</header> <header>plot_wrapper.hpp</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>HexSpinBox</class>
<extends>QSpinBox</extends>
<header>hex_spinbox.hpp</header>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>
+8 -1
View File
@@ -7,7 +7,8 @@ Test_Sim::Test_Sim(CAN_Wrapper *can_wrapper, QSerialPort *serial, QObject *paren
m_label("Label", Color::White, Color::Black), m_checkbox("Option 1", Color::Default, Color::Default), m_label("Label", Color::White, Color::Black), m_checkbox("Option 1", Color::Default, Color::Default),
m_slider("Slider", Color::White, Color::Black, 0, 100, 0), m_dial("Dial", Color::White, Color::Black, 0, 100, 0), m_slider("Slider", Color::White, Color::Black, 0, 100, 0), m_dial("Dial", Color::White, Color::Black, 0, 100, 0),
m_thermo("Thermo", "%", Color::White, Color::Black, 0, 100, 0), m_led("Led", Color::White, Color::Red), m_thermo("Thermo", "%", Color::White, Color::Black, 0, 100, 0), m_led("Led", Color::White, Color::Red),
m_table(4, 4, {"Row 1", "Row 2", "Row 3", "Row 4"}, {"Column 1", "Column 2", "Column 3", "Column 4"}) m_table(4, 4, {"Row 1", "Row 2", "Row 3", "Row 4"}, {"Column 1", "Column 2", "Column 3", "Column 4"}),
m_spinbox(0, 255, 0), m_double_spinbox(0, 255, 0), m_hex_spinbox(0, 255, 0)
{ {
m_thermo.set_start_color(Color::Red); m_thermo.set_start_color(Color::Red);
m_thermo.set_end_color(Color::Red); m_thermo.set_end_color(Color::Red);
@@ -26,6 +27,9 @@ Test_Sim::Test_Sim(CAN_Wrapper *can_wrapper, QSerialPort *serial, QObject *paren
PUSH_UI_ITEM(m_checkbox); PUSH_UI_ITEM(m_checkbox);
PUSH_UI_ITEM(m_plot); PUSH_UI_ITEM(m_plot);
PUSH_UI_ITEM(m_table); PUSH_UI_ITEM(m_table);
PUSH_UI_ITEM(m_spinbox);
PUSH_UI_ITEM(m_double_spinbox);
PUSH_UI_ITEM(m_hex_spinbox);
for (size_t i = 0; i < m_table.capacity(); i++) for (size_t i = 0; i < m_table.capacity(); i++)
{ {
@@ -76,6 +80,9 @@ void Test_Sim::slider_changed(UI_slider *id, double value)
m_dial.set_value(value); m_dial.set_value(value);
m_thermo.set_value(value); m_thermo.set_value(value);
m_plot.add_value(value); m_plot.add_value(value);
m_spinbox.set_value(value);
m_double_spinbox.set_value(value);
m_hex_spinbox.set_value(value);
return; return;
} }
} }
+3
View File
@@ -24,4 +24,7 @@ class Test_Sim : public Simulator_base
UI_led m_led; UI_led m_led;
UI_plot m_plot; UI_plot m_plot;
UI_table m_table; UI_table m_table;
UI_spinbox m_spinbox;
UI_double_spinbox m_double_spinbox;
UI_hex_spinbox m_hex_spinbox;
}; };