Make a checkbox a UI item instead of UI_x_boxes
This commit is contained in:
2024-10-17 10:21:06 +02:00
parent 5889af5494
commit f64df41df8
3 changed files with 24 additions and 52 deletions
+16 -42
View File
@@ -1,80 +1,54 @@
#include "UI_checkbox.hpp" #include "UI_checkbox.hpp"
UI_checkbox::UI_checkbox(QObject *parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parrent), m_selected() UI_checkbox::UI_checkbox(QObject *parrent) : UI_item(UI_CHECKBOX_TYPE, parrent), m_selected()
{ {
} }
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parrent)
std::vector<std::string> options, QObject *parrent) : UI_item(UI_CHECKBOX_TYPE, text, 0, fg_color, bg_color, parrent), m_selected(false)
: UI_x_boxes(UI_CHECKBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected()
{ {
for (size_t i = 0; i < m_options.size(); i++)
{
m_selected.push_back(false);
}
} }
UI_checkbox::UI_checkbox(const json &j, QObject *parrent) : UI_x_boxes(UI_CHECKBOX_TYPE, parrent), m_selected() UI_checkbox::UI_checkbox(const json &j, QObject *parrent) : UI_item(UI_CHECKBOX_TYPE, parrent), m_selected()
{ {
from_json(j); from_json(j);
} }
void UI_checkbox::toggle_selected(size_t selected) void UI_checkbox::toggle_selected()
{ {
if (selected >= m_options.size()) m_selected = !m_selected;
return;
m_selected[selected] = !m_selected[selected];
emit value_changed(); emit value_changed();
emit on_change(selected_text()[selected]); emit on_change(m_text);
} }
void UI_checkbox::toggle_selected(std::string selected) bool UI_checkbox::selected() const
{
for (size_t i = 0; i < m_options.size(); i++)
{
if (m_options[i] == selected)
{
toggle_selected(i);
return;
}
}
}
std::vector<bool> UI_checkbox::selected() const
{ {
return m_selected; return m_selected;
} }
std::vector<std::string> UI_checkbox::selected_text() const std::string UI_checkbox::selected_text() const
{ {
std::vector<std::string> selected_text; if (m_selected)
for (size_t i = 0; i < m_options.size(); i++)
{ {
if (m_selected[i]) return m_text;
{
selected_text.push_back(m_options[i]);
} }
} return "";
return selected_text;
} }
void UI_checkbox::from_json(const json &j) void UI_checkbox::from_json(const json &j)
{ {
UI_x_boxes::from_json(j); UI_item::from_json(j);
if (j.contains("selected")) if (j.contains("selected"))
{ {
m_selected.clear(); m_selected = j["selected"];
for (auto selected : j["selected"])
{
m_selected.push_back(selected);
}
} }
} }
json UI_checkbox::to_json(size_t id) const json UI_checkbox::to_json(size_t id) const
{ {
json j = UI_x_boxes::to_json(id); json j = UI_item::to_json(id);
j["selected"] = m_selected; j["selected"] = m_selected;
return j; return j;
} }
+6 -8
View File
@@ -1,23 +1,21 @@
#pragma once #pragma once
#include "UI_item.hpp" #include "UI_item.hpp"
#include "UI_x_boxes.hpp"
#include <cstdint> #include <cstdint>
#define UI_CHECKBOX_TYPE std::string("UI_checkbox") #define UI_CHECKBOX_TYPE std::string("UI_checkbox")
class UI_checkbox : public UI_x_boxes class UI_checkbox : public UI_item
{ {
Q_OBJECT Q_OBJECT
public: public:
UI_checkbox(QObject *parrent = nullptr); UI_checkbox(QObject *parrent = nullptr);
UI_checkbox(std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_checkbox(std::string text, Color fg_color, Color bg_color,
QObject *parrent = nullptr); QObject *parrent = nullptr);
UI_checkbox(const json &j, QObject *parrent = nullptr); UI_checkbox(const json &j, QObject *parrent = nullptr);
void toggle_selected(size_t selected); void toggle_selected();
void toggle_selected(std::string selected); bool selected() const;
std::vector<bool> selected() const; std::string selected_text() const;
std::vector<std::string> selected_text() const;
void from_json(const json &j) override; void from_json(const json &j) override;
json to_json(size_t id) const override; json to_json(size_t id) const override;
@@ -26,5 +24,5 @@ class UI_checkbox : public UI_x_boxes
void on_change(std::string); void on_change(std::string);
private: private:
std::vector<bool> m_selected; bool m_selected;
}; };
+2 -2
View File
@@ -6,7 +6,7 @@ Simulator_base("Test_Sim", std::chrono::milliseconds(1000), parrent),
m_button("Off", Color(Color::White), Color(Color::Red)), m_button("Off", Color(Color::White), Color(Color::Red)),
m_combobox("Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0), m_combobox("Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0),
m_label("Label", Color(Color::White), Color(Color::Black)), m_label("Label", Color(Color::White), Color(Color::Black)),
m_checkbox("Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}), m_checkbox("Option 1", Color(Color::White), Color(Color::Black)),
m_slider("Slider", Color(Color::White), Color(Color::Black), 0, 100, 0), m_slider("Slider", Color(Color::White), Color(Color::Black), 0, 100, 0),
m_dial("Dial", Color(Color::White), Color(Color::Black), 0, 100, 0), m_dial("Dial", Color(Color::White), Color(Color::Black), 0, 100, 0),
m_thermo("Thermo", Color(Color::White), Color(Color::Black), 0, 100, 0), m_thermo("Thermo", Color(Color::White), Color(Color::Black), 0, 100, 0),
@@ -22,11 +22,11 @@ m_led("Led", Color(Color::White), Color(Color::Black), Color(Color::Red))
this->add_UI_item(&this->m_button); this->add_UI_item(&this->m_button);
this->add_UI_item(&this->m_combobox); this->add_UI_item(&this->m_combobox);
this->add_UI_item(&this->m_label); this->add_UI_item(&this->m_label);
this->add_UI_item(&this->m_checkbox);
this->add_UI_item(&this->m_slider); this->add_UI_item(&this->m_slider);
this->add_UI_item(&this->m_dial); this->add_UI_item(&this->m_dial);
this->add_UI_item(&this->m_thermo); this->add_UI_item(&this->m_thermo);
this->add_UI_item(&this->m_led); this->add_UI_item(&this->m_led);
this->add_UI_item(&this->m_checkbox);
QObject::connect(&m_button, &UI_button::on_click, [this] { button_clicked(&m_button); }); QObject::connect(&m_button, &UI_button::on_click, [this] { button_clicked(&m_button); });
QObject::connect(&m_slider, &UI_slider::on_change, [this](double value) { slider_changed(&m_slider, value); }); QObject::connect(&m_slider, &UI_slider::on_change, [this](double value) { slider_changed(&m_slider, value); });