Checkbox update
This commit is contained in:
2026-02-14 14:30:50 +01:00
parent 806f258c10
commit 1e7badf08c
3 changed files with 23 additions and 25 deletions
+15 -17
View File
@@ -1,53 +1,51 @@
#include "UI_checkbox.hpp" #include "UI_checkbox.hpp"
UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_selected() UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_checked()
{ {
} }
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent) UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_CHECKBOX_TYPE, text, 0, fg_color, bg_color, parent), m_selected(false) : UI_item(UI_CHECKBOX_TYPE, text, 0, fg_color, bg_color, parent), m_checked(false)
{ {
} }
UI_checkbox::UI_checkbox(const json &j, QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_selected() UI_checkbox::UI_checkbox(const json &j, QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_checked()
{ {
from_json(j); from_json(j);
} }
void UI_checkbox::toggle_selected() void UI_checkbox::clicked()
{ {
m_selected = !m_selected; m_checked = !m_checked;
emit value_changed(); emit value_changed();
emit on_change(m_text); emit on_change(m_checked);
} }
bool UI_checkbox::selected() const void UI_checkbox::set_state(bool state)
{ {
return m_selected; m_checked = state;
emit value_changed();
emit on_change(m_checked);
} }
std::string UI_checkbox::selected_text() const bool UI_checkbox::state() const
{ {
if (m_selected) return m_checked;
{
return m_text;
}
return "";
} }
void UI_checkbox::from_json(const json &j) void UI_checkbox::from_json(const json &j)
{ {
UI_item::from_json(j); UI_item::from_json(j);
if (j.contains("selected")) if (j.contains("checked"))
{ {
m_selected = j["selected"]; m_checked = j["checked"];
} }
} }
json UI_checkbox::to_json(size_t id) const json UI_checkbox::to_json(size_t id) const
{ {
json j = UI_item::to_json(id); json j = UI_item::to_json(id);
j["selected"] = m_selected; j["checked"] = m_checked;
return j; return j;
} }
+6 -6
View File
@@ -11,17 +11,17 @@ class UI_checkbox : public UI_item
UI_checkbox(QObject *parent = nullptr); UI_checkbox(QObject *parent = nullptr);
UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr); UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_checkbox(const json &j, QObject *parent = nullptr); UI_checkbox(const json &j, QObject *parent = nullptr);
void toggle_selected(); void clicked();
bool selected() const; void set_state(bool state);
std::string selected_text() const; bool state() 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;
signals: signals:
void on_change(std::string); void on_change(bool);
private: private:
bool m_selected; bool m_checked;
}; };
+2 -2
View File
@@ -8,5 +8,5 @@ TEST(UI_checkbox, to_from_json)
EXPECT_EQ(checkbox.text(), checkbox2.text()); EXPECT_EQ(checkbox.text(), checkbox2.text());
EXPECT_EQ(checkbox.fg_color(), checkbox2.fg_color()); EXPECT_EQ(checkbox.fg_color(), checkbox2.fg_color());
EXPECT_EQ(checkbox.bg_color(), checkbox2.bg_color()); EXPECT_EQ(checkbox.bg_color(), checkbox2.bg_color());
EXPECT_EQ(checkbox.selected(), checkbox2.selected()); EXPECT_EQ(checkbox.state(), checkbox2.state());
} }