stacked widget

tested and working
This commit is contained in:
2026-02-14 14:32:07 +01:00
parent faac84821c
commit 4da26083a7
9 changed files with 236 additions and 96 deletions
+1
View File
@@ -11,6 +11,7 @@
#include "ui_plot.hpp" #include "ui_plot.hpp"
#include "ui_slider.hpp" #include "ui_slider.hpp"
#include "ui_spinbox.hpp" #include "ui_spinbox.hpp"
#include "ui_stacked_widget.hpp"
#include "ui_tab_widget.hpp" #include "ui_tab_widget.hpp"
#include "ui_table.hpp" #include "ui_table.hpp"
#include "ui_textbox.hpp" #include "ui_textbox.hpp"
+44
View File
@@ -0,0 +1,44 @@
#include "ui_stacked_widget.hpp"
UI_stacked_widget::UI_stacked_widget(QObject *parent) : UI_item(UI_STACKED_WIDGET_TYPE, parent)
{
}
UI_stacked_widget::UI_stacked_widget(size_t tab_count, size_t current_tab, QObject *parent)
: UI_item(UI_STACKED_WIDGET_TYPE, parent), m_tab_count(tab_count), m_current_tab(current_tab)
{
}
void UI_stacked_widget::set_selected(size_t selected)
{
if (selected >= m_tab_count)
return;
m_current_tab = selected;
emit ui_changed();
}
size_t UI_stacked_widget::selected() const
{
return m_current_tab;
}
void UI_stacked_widget::advance_tab()
{
if (m_tab_count == 0)
return;
m_current_tab = (m_current_tab + 1) % m_tab_count;
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;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef UI_STACKED_WIDGET_HPP
#define UI_STACKED_WIDGET_HPP
#include "ui_item.hpp"
#define UI_STACKED_WIDGET_TYPE std::string("ui_stacked_widget")
class UI_stacked_widget : public UI_item
{
Q_OBJECT
public:
explicit UI_stacked_widget(QObject *parent = nullptr);
UI_stacked_widget(size_t tab_count, size_t current_tab, QObject *parent = nullptr);
void set_selected(size_t selected) override;
size_t selected() const;
void advance_tab();
void from_json(const json &j) override;
json to_json(size_t id) const override;
private:
size_t m_tab_count;
size_t m_current_tab;
};
#endif // UI_STACKED_WIDGET_HPP
+27
View File
@@ -15,6 +15,7 @@ Simulator_base::Simulator_base(QString name, Web_socket_wrapper *web_socket, QWi
{ {
setWindowTitle(m_name); setWindowTitle(m_name);
} }
this->setWindowState(Qt::WindowMaximized);
m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); m_error_dialog->set_error("Connection timed out\nCheck if the server is running");
m_ui_lookup.reserve(40); m_ui_lookup.reserve(40);
} }
@@ -179,6 +180,11 @@ void Simulator_base::UI_item_parser(json &input)
process_ui_tab_widget(ui_item, widget); process_ui_tab_widget(ui_item, widget);
continue; continue;
} }
if (ui_item["type"] == "ui_stacked_widget")
{
process_ui_stacked_widget(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);
@@ -644,6 +650,27 @@ void Simulator_base::process_ui_tab_widget(json &ui_item, QWidget *widget)
} }
} }
void Simulator_base::process_ui_stacked_widget(json &ui_item, QWidget *widget)
{
auto stacked_widget = qobject_cast<QStackedWidget *>(widget);
if (stacked_widget == nullptr)
{
SPDLOG_WARN("widget is not of type QStackedWidget");
return;
}
if (stacked_widget->count() != ui_item.at("tab_count"))
{
SPDLOG_ERROR("QStackedWidget has not the same amount of tabs as the server");
return;
}
if (stacked_widget->currentIndex() != ui_item.at("current_tab"))
{
stacked_widget->setCurrentIndex(ui_item.at("current_tab"));
}
}
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);
+1
View File
@@ -57,6 +57,7 @@ class Simulator_base : public QMainWindow
virtual void process_ui_double_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_hex_spinbox(json &ui_item, QWidget *widget);
virtual void process_ui_tab_widget(json &ui_item, QWidget *widget); virtual void process_ui_tab_widget(json &ui_item, QWidget *widget);
virtual void process_ui_stacked_widget(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);
+1 -2
View File
@@ -7,8 +7,6 @@ Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent)
: Simulator_base("Test Sim", web_socket, parent), ui(new Ui::Test_Sim) : Simulator_base("Test Sim", web_socket, parent), ui(new Ui::Test_Sim)
{ {
ui->setupUi(this); ui->setupUi(this);
this->setWindowTitle("Test Sim");
this->setWindowState(Qt::WindowMaximized);
PUSH_UI_ITEM(button); PUSH_UI_ITEM(button);
PUSH_UI_ITEM(combobox); PUSH_UI_ITEM(combobox);
@@ -24,6 +22,7 @@ Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent)
PUSH_UI_ITEM(doubleSpinBox); PUSH_UI_ITEM(doubleSpinBox);
PUSH_UI_ITEM(hexSpinBox); PUSH_UI_ITEM(hexSpinBox);
PUSH_UI_ITEM(tabWidget); PUSH_UI_ITEM(tabWidget);
PUSH_UI_ITEM(stackedWidget);
} }
Test_Sim::~Test_Sim() Test_Sim::~Test_Sim()
+133 -93
View File
@@ -15,6 +15,20 @@
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<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="0" column="4"> <item row="0" column="4">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
@@ -22,6 +36,125 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Tab 3</string>
</attribute>
</widget>
</widget>
</item>
<item row="4" column="2">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="thermo_label">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item>
<widget class="QwtThermo" name="thermo"/>
</item>
</layout>
</item>
<item row="2" column="4">
<widget class="QPushButton" name="button">
<property name="text">
<string>PushButton</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="2" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBox"/>
</item>
<item row="3" column="4">
<widget class="QComboBox" name="combobox"/>
</item>
<item row="0" column="2">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Page 1</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Page 2</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_3">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Page 3</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="5" column="2">
<widget class="QTableWidget" name="table"/>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<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 row="1" column="1">
<widget class="QSpinBox" name="spinBox"/>
</item>
<item row="4" column="4"> <item row="4" column="4">
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
@@ -49,102 +182,9 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="3" column="4">
<widget class="QComboBox" name="combobox"/>
</item>
<item row="2" column="4">
<widget class="QPushButton" name="button">
<property name="text">
<string>PushButton</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="5" column="2">
<widget class="QTableWidget" name="table"/>
</item>
<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 row="4" column="2">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="thermo_label">
<property name="text">
<string>0</string>
</property>
</widget>
</item>
<item>
<widget class="QwtThermo" name="thermo"/>
</item>
</layout>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<item row="1" column="1">
<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"> <item row="3" column="1">
<widget class="HexSpinBox" name="hexSpinBox"/> <widget class="HexSpinBox" name="hexSpinBox"/>
</item> </item>
<item row="0" column="1">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Tab 3</string>
</attribute>
</widget>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">
+3 -1
View File
@@ -11,7 +11,7 @@ Test_Sim::Test_Sim(Communication *com, QObject *parent)
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_spinbox(0, 255, 0), m_double_spinbox(0, 255, 0), m_hex_spinbox(0, 255, 0),
m_tab_widget({"Tab 1", "Tab 2", "Tab 3"}, 0, this) m_tab_widget({"Tab 1", "Tab 2", "Tab 3"}, 0), m_stacked_widget(3, 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);
@@ -34,6 +34,7 @@ Test_Sim::Test_Sim(Communication *com, QObject *parent)
PUSH_UI_ITEM(m_double_spinbox); PUSH_UI_ITEM(m_double_spinbox);
PUSH_UI_ITEM(m_hex_spinbox); PUSH_UI_ITEM(m_hex_spinbox);
PUSH_UI_ITEM(m_tab_widget); PUSH_UI_ITEM(m_tab_widget);
PUSH_UI_ITEM(m_stacked_widget);
for (size_t i = 0; i < m_table.capacity(); i++) for (size_t i = 0; i < m_table.capacity(); i++)
{ {
@@ -54,6 +55,7 @@ void Test_Sim::timer()
} }
m_slider.set_value(m_slider.value() + 1); m_slider.set_value(m_slider.value() + 1);
m_tab_widget.advance_tab(); m_tab_widget.advance_tab();
m_stacked_widget.advance_tab();
} }
void Test_Sim::run_at_startup() void Test_Sim::run_at_startup()
+1
View File
@@ -28,4 +28,5 @@ class Test_Sim : public Simulator_base
UI_double_spinbox m_double_spinbox; UI_double_spinbox m_double_spinbox;
UI_hex_spinbox m_hex_spinbox; UI_hex_spinbox m_hex_spinbox;
UI_tab_widget m_tab_widget; UI_tab_widget m_tab_widget;
UI_stacked_widget m_stacked_widget;
}; };