From faac84821c4ef6d058f067f75549ad626444f443 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 24 Sep 2025 20:25:59 +0200 Subject: [PATCH] tab widget working prototype --- UI/ui_tab_widget.cpp | 14 ++++++++++++ UI/ui_tab_widget.hpp | 2 ++ client/simulator_base.cpp | 40 ++++++++++++++++++++++++++++++++++ client/simulator_base.hpp | 1 + server/simulators/test_sim.cpp | 5 ++++- server/simulators/test_sim.hpp | 1 + 6 files changed, 62 insertions(+), 1 deletion(-) diff --git a/UI/ui_tab_widget.cpp b/UI/ui_tab_widget.cpp index 6825096..ccde445 100644 --- a/UI/ui_tab_widget.cpp +++ b/UI/ui_tab_widget.cpp @@ -7,6 +7,7 @@ UI_tab_widget::UI_tab_widget(QObject *parent) : UI_item(UI_TAB_WIDGET_TYPE, pare UI_tab_widget::UI_tab_widget(std::vector tab_names, size_t selected, QObject *parent) : UI_item(UI_TAB_WIDGET_TYPE, parent), m_selected_tab(selected), m_tab_names(tab_names) { + m_tab_visible.resize(m_tab_names.size(), true); } void UI_tab_widget::set_selected(size_t selected) @@ -40,6 +41,10 @@ void UI_tab_widget::set_tab_name(size_t index, std::string name) { m_tab_names.resize(index + 1); } + if (index >= m_tab_visible.size()) + { + m_tab_visible.resize(index + 1, true); + } m_tab_names.at(index) = name; emit ui_changed(); } @@ -48,6 +53,15 @@ std::string UI_tab_widget::tab_name(size_t index) const return m_tab_names.at(index); } +void UI_tab_widget::advance_tab() +{ + if (m_tab_names.size() == 0) + return; + m_selected_tab = (m_selected_tab + 1) % m_tab_names.size(); + emit ui_changed(); + emit this->selected(m_selected_tab); +} + void UI_tab_widget::from_json(const json &j) { UI_item::from_json(j); diff --git a/UI/ui_tab_widget.hpp b/UI/ui_tab_widget.hpp index d046709..fc7d139 100644 --- a/UI/ui_tab_widget.hpp +++ b/UI/ui_tab_widget.hpp @@ -21,6 +21,8 @@ class UI_tab_widget : public UI_item void set_tab_name(size_t index, std::string name); std::string tab_name(size_t index) const; + void advance_tab(); + void from_json(const json &j) override; json to_json(size_t id) const override; signals: diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 0e96848..17fc3e6 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -174,6 +174,11 @@ void Simulator_base::UI_item_parser(json &input) process_ui_hex_spinbox(ui_item, widget); continue; } + if (ui_item["type"] == "ui_tab_widget") + { + process_ui_tab_widget(ui_item, widget); + continue; + } if (ui_item["type"] == "ui_plot") { process_ui_plot(ui_item, widget); @@ -614,6 +619,29 @@ void Simulator_base::process_ui_tab_widget(json &ui_item, QWidget *widget) SPDLOG_WARN("widget is not of type QTabWidget"); return; } + + if (tab_widget->count() != ui_item.at("tab_names").size()) + { + SPDLOG_ERROR("QTabWidget has not the same amount of tabs as the server"); + return; + } + auto tab_names = ui_item.at("tab_names"); + for (size_t i = 0; i < tab_names.size(); i++) + { + if (tab_widget->tabText(i) != QString::fromStdString(tab_names[i])) + { + tab_widget->setTabText(i, QString::fromStdString(tab_names[i])); + } + if (tab_widget->isVisible() != ui_item.at("tab_visible")[i]) + { + tab_widget->setTabVisible(i, ui_item.at("tab_visible")[i]); + } + } + + if (tab_widget->currentIndex() != ui_item.at("selected")) + { + tab_widget->setCurrentIndex(ui_item.at("selected")); + } } void Simulator_base::process_ui_plot(json &ui_item, QWidget *widget) @@ -753,6 +781,7 @@ void Simulator_base::setup_ui_item(QWidget *item, size_t index) setup_spinbox(item, index); setup_double_spinbox(item, index); setup_hex_spinbox(item, index); + setup_tab_widget(item, index); setup_qwtplot(item, index); setup_can_ui(item, index); } @@ -839,6 +868,17 @@ void Simulator_base::setup_hex_spinbox(QWidget *item, size_t index) }); } +void Simulator_base::setup_tab_widget(QWidget *item, size_t index) +{ + QTabWidget *tab_widget = qobject_cast(item); + if (tab_widget == nullptr) + return; + + connect(tab_widget, &QTabWidget::currentChanged, this, [=, this](int index) { + m_web_socket->send_event(Web_socket_wrapper::Event::selected, index, (size_t)index); + }); +} + void Simulator_base::setup_qwtplot(QWidget *item, size_t index) { auto *plot = qobject_cast(item); diff --git a/client/simulator_base.hpp b/client/simulator_base.hpp index c1cfaf0..53500ea 100644 --- a/client/simulator_base.hpp +++ b/client/simulator_base.hpp @@ -70,6 +70,7 @@ class Simulator_base : public QMainWindow 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_tab_widget(QWidget *item, size_t index); void setup_qwtplot(QWidget *item, size_t index); void setup_can_ui(QWidget *item, size_t index); diff --git a/server/simulators/test_sim.cpp b/server/simulators/test_sim.cpp index f6c0f85..ae09d1b 100644 --- a/server/simulators/test_sim.cpp +++ b/server/simulators/test_sim.cpp @@ -10,7 +10,8 @@ Test_Sim::Test_Sim(Communication *com, QObject *parent) 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_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_thermo.set_start_color(Color::Red); m_thermo.set_end_color(Color::Red); @@ -32,6 +33,7 @@ Test_Sim::Test_Sim(Communication *com, QObject *parent) PUSH_UI_ITEM(m_spinbox); PUSH_UI_ITEM(m_double_spinbox); PUSH_UI_ITEM(m_hex_spinbox); + PUSH_UI_ITEM(m_tab_widget); for (size_t i = 0; i < m_table.capacity(); i++) { @@ -51,6 +53,7 @@ void Test_Sim::timer() m_slider.set_value(0); } m_slider.set_value(m_slider.value() + 1); + m_tab_widget.advance_tab(); } void Test_Sim::run_at_startup() diff --git a/server/simulators/test_sim.hpp b/server/simulators/test_sim.hpp index 590ad1e..687e5dd 100644 --- a/server/simulators/test_sim.hpp +++ b/server/simulators/test_sim.hpp @@ -27,4 +27,5 @@ class Test_Sim : public Simulator_base UI_spinbox m_spinbox; UI_double_spinbox m_double_spinbox; UI_hex_spinbox m_hex_spinbox; + UI_tab_widget m_tab_widget; };