From 4da26083a723c754d3b00ab6897344ba89d3af6c Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 25 Sep 2025 14:30:29 +0200 Subject: [PATCH] stacked widget tested and working --- UI/ui.hpp | 1 + UI/ui_stacked_widget.cpp | 44 +++++++ UI/ui_stacked_widget.hpp | 25 ++++ client/simulator_base.cpp | 27 ++++ client/simulator_base.hpp | 1 + client/simulators/test.cpp | 3 +- client/simulators/test.ui | 226 +++++++++++++++++++-------------- server/simulators/test_sim.cpp | 4 +- server/simulators/test_sim.hpp | 1 + 9 files changed, 236 insertions(+), 96 deletions(-) create mode 100644 UI/ui_stacked_widget.cpp create mode 100644 UI/ui_stacked_widget.hpp diff --git a/UI/ui.hpp b/UI/ui.hpp index dba32b0..81fd9b1 100644 --- a/UI/ui.hpp +++ b/UI/ui.hpp @@ -11,6 +11,7 @@ #include "ui_plot.hpp" #include "ui_slider.hpp" #include "ui_spinbox.hpp" +#include "ui_stacked_widget.hpp" #include "ui_tab_widget.hpp" #include "ui_table.hpp" #include "ui_textbox.hpp" diff --git a/UI/ui_stacked_widget.cpp b/UI/ui_stacked_widget.cpp new file mode 100644 index 0000000..f639ce1 --- /dev/null +++ b/UI/ui_stacked_widget.cpp @@ -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; +} diff --git a/UI/ui_stacked_widget.hpp b/UI/ui_stacked_widget.hpp new file mode 100644 index 0000000..2fd5b40 --- /dev/null +++ b/UI/ui_stacked_widget.hpp @@ -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 diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 17fc3e6..d9a5fe2 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -15,6 +15,7 @@ Simulator_base::Simulator_base(QString name, Web_socket_wrapper *web_socket, QWi { setWindowTitle(m_name); } + this->setWindowState(Qt::WindowMaximized); m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); m_ui_lookup.reserve(40); } @@ -179,6 +180,11 @@ void Simulator_base::UI_item_parser(json &input) process_ui_tab_widget(ui_item, widget); continue; } + if (ui_item["type"] == "ui_stacked_widget") + { + process_ui_stacked_widget(ui_item, widget); + continue; + } if (ui_item["type"] == "ui_plot") { 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(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) { auto plot = qobject_cast(widget); diff --git a/client/simulator_base.hpp b/client/simulator_base.hpp index 53500ea..0635e05 100644 --- a/client/simulator_base.hpp +++ b/client/simulator_base.hpp @@ -57,6 +57,7 @@ class Simulator_base : public QMainWindow 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_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_table(json &ui_item, QWidget *widget); virtual void process_ui_can(json &ui_item, QWidget *widget); diff --git a/client/simulators/test.cpp b/client/simulators/test.cpp index f0709f0..e0d57e9 100644 --- a/client/simulators/test.cpp +++ b/client/simulators/test.cpp @@ -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) { ui->setupUi(this); - this->setWindowTitle("Test Sim"); - this->setWindowState(Qt::WindowMaximized); PUSH_UI_ITEM(button); 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(hexSpinBox); PUSH_UI_ITEM(tabWidget); + PUSH_UI_ITEM(stackedWidget); } Test_Sim::~Test_Sim() diff --git a/client/simulators/test.ui b/client/simulators/test.ui index c9136d6..ad8199f 100644 --- a/client/simulators/test.ui +++ b/client/simulators/test.ui @@ -15,6 +15,20 @@ + + + + + + 0 + + + + + + + + @@ -22,6 +36,125 @@ + + + + 1 + + + + Tab 1 + + + + + Tab 2 + + + + + Tab 3 + + + + + + + + + + 0 + + + + + + + + + + + + PushButton + + + + + + + PushButton + + + + + + + + + + + + + 0 + + + + + + + Page 1 + + + + + + + + + + + Page 2 + + + + + + + + + + + Page 3 + + + + + + + + + + + + + + CheckBox + + + + + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + + + @@ -49,102 +182,9 @@ - - - - - - - PushButton - - - - - - - PushButton - - - - - - - - - - QFrame::Shape::StyledPanel - - - QFrame::Shadow::Raised - - - - - - - - - 0 - - - - - - - - - - - - CheckBox - - - - - - - - - - - - 0 - - - - - - - - - - - - - - - 2 - - - - Tab 1 - - - - - Tab 2 - - - - - Tab 3 - - - - diff --git a/server/simulators/test_sim.cpp b/server/simulators/test_sim.cpp index ae09d1b..a95b599 100644 --- a/server/simulators/test_sim.cpp +++ b/server/simulators/test_sim.cpp @@ -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_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_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_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_hex_spinbox); PUSH_UI_ITEM(m_tab_widget); + PUSH_UI_ITEM(m_stacked_widget); 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_tab_widget.advance_tab(); + m_stacked_widget.advance_tab(); } void Test_Sim::run_at_startup() diff --git a/server/simulators/test_sim.hpp b/server/simulators/test_sim.hpp index 687e5dd..b0eaf9b 100644 --- a/server/simulators/test_sim.hpp +++ b/server/simulators/test_sim.hpp @@ -28,4 +28,5 @@ class Test_Sim : public Simulator_base UI_double_spinbox m_double_spinbox; UI_hex_spinbox m_hex_spinbox; UI_tab_widget m_tab_widget; + UI_stacked_widget m_stacked_widget; };