From b98330ba763aef0fd6e757c910eeadf2e011beb4 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 24 Sep 2025 16:06:26 +0200 Subject: [PATCH] tab widget started working on implementation --- UI/ui.hpp | 1 + UI/ui_tab_widget.cpp | 80 ++++++++++++++++++++++++++++++++++++++ UI/ui_tab_widget.hpp | 35 +++++++++++++++++ client/simulator_base.cpp | 12 ++++++ client/simulator_base.hpp | 1 + client/simulators/test.cpp | 1 + client/simulators/test.ui | 24 +++++++++++- 7 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 UI/ui_tab_widget.cpp create mode 100644 UI/ui_tab_widget.hpp diff --git a/UI/ui.hpp b/UI/ui.hpp index 68d356b..a00ae22 100644 --- a/UI/ui.hpp +++ b/UI/ui.hpp @@ -12,6 +12,7 @@ #include "ui_slider.hpp" #include "ui_spinbox.hpp" #include "ui_table.hpp" +#include "ui_tab_widget.hpp" #include "ui_textbox.hpp" #include "ui_thermo.hpp" diff --git a/UI/ui_tab_widget.cpp b/UI/ui_tab_widget.cpp new file mode 100644 index 0000000..63c7996 --- /dev/null +++ b/UI/ui_tab_widget.cpp @@ -0,0 +1,80 @@ +#include "ui_tab_widget.hpp" + +UI_tab_widget::UI_tab_widget(QObject *parent) + : UI_item(UI_TAB_WIDGET_TYPE, parent) +{ +} + +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) +{ + +} + +void UI_tab_widget::set_selected(size_t selected) +{ + m_selected_tab = selected; + emit ui_changed(); + emit this->selected(m_selected_tab); +} +size_t UI_tab_widget::selected() const +{ + return m_selected_tab; +} + +void UI_tab_widget::set_visible(size_t index, bool visible) +{ + if (index >= m_tab_visible.size()) { + m_tab_visible.resize(index + 1); + } + m_tab_visible.at(index) = visible; + emit ui_changed(); +} +bool UI_tab_widget::visible(size_t index) const +{ + return m_tab_visible.at(index); +} + +void UI_tab_widget::set_tab_name(size_t index, std::string name) +{ + if (index >= m_tab_names.size()) { + m_tab_names.resize(index + 1); + } + m_tab_names.at(index) = name; + emit ui_changed(); +} +std::string UI_tab_widget::tab_name(size_t index) const +{ + return m_tab_names.at(index); +} + +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; +} diff --git a/UI/ui_tab_widget.hpp b/UI/ui_tab_widget.hpp new file mode 100644 index 0000000..1f96bed --- /dev/null +++ b/UI/ui_tab_widget.hpp @@ -0,0 +1,35 @@ +#ifndef UI_TAB_WIDGET_HPP +#define UI_TAB_WIDGET_HPP + +#include "ui_item.hpp" + +#define UI_TAB_WIDGET_TYPE std::string("ui_tab_widget") + +class UI_tab_widget : public UI_item +{ + Q_OBJECT + public: + explicit UI_tab_widget(QObject *parent = nullptr); + UI_tab_widget(std::vector tab_names, size_t selected = 0, + QObject *parent = nullptr); + + void set_selected(size_t selected) override; + size_t selected() const; + + void set_visible(size_t index, bool visible); + bool visible(size_t index) const; + + void set_tab_name(size_t index, std::string name); + std::string tab_name(size_t index) const; + + void from_json(const json &j) override; + json to_json(size_t id) const override; + signals: + void selected(size_t index); + private: + size_t m_selected_tab; + std::vector m_tab_names; + std::vector m_tab_visible; +}; + +#endif // UI_TAB_WIDGET_HPP diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 91630ed..64201fc 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -606,6 +606,18 @@ void Simulator_base::process_ui_hex_spinbox(json &ui_item, QWidget *widget) } } +void Simulator_base::process_ui_tab_widget(json &ui_item, QWidget *widget) +{ + auto tab_widget = qobject_cast(widget); + if (tab_widget == nullptr) + { + SPDLOG_WARN("widget is not of type QTabWidget"); + return; + } + + +} + 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 1d96a9c..c1cfaf0 100644 --- a/client/simulator_base.hpp +++ b/client/simulator_base.hpp @@ -56,6 +56,7 @@ class Simulator_base : public QMainWindow 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_tab_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 f35ca86..f0709f0 100644 --- a/client/simulators/test.cpp +++ b/client/simulators/test.cpp @@ -23,6 +23,7 @@ Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent) PUSH_UI_ITEM(spinBox); PUSH_UI_ITEM(doubleSpinBox); PUSH_UI_ITEM(hexSpinBox); + PUSH_UI_ITEM(tabWidget); } Test_Sim::~Test_Sim() diff --git a/client/simulators/test.ui b/client/simulators/test.ui index 26c5a47..c9136d6 100644 --- a/client/simulators/test.ui +++ b/client/simulators/test.ui @@ -123,6 +123,28 @@ + + + + 2 + + + + Tab 1 + + + + + Tab 2 + + + + + Tab 3 + + + + @@ -131,7 +153,7 @@ 0 0 800 - 23 + 24