mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
UI
Make a checkbox a UI item instead of UI_x_boxes
This commit is contained in:
+16
-42
@@ -1,80 +1,54 @@
|
||||
#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,
|
||||
std::vector<std::string> options, QObject *parrent)
|
||||
: UI_x_boxes(UI_CHECKBOX_TYPE, text, fg_color, bg_color, options, parrent), m_selected()
|
||||
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parrent)
|
||||
: UI_item(UI_CHECKBOX_TYPE, text, 0, fg_color, bg_color, parrent), m_selected(false)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
void UI_checkbox::toggle_selected(size_t selected)
|
||||
void UI_checkbox::toggle_selected()
|
||||
{
|
||||
if (selected >= m_options.size())
|
||||
return;
|
||||
m_selected[selected] = !m_selected[selected];
|
||||
m_selected = !m_selected;
|
||||
emit value_changed();
|
||||
emit on_change(selected_text()[selected]);
|
||||
emit on_change(m_text);
|
||||
}
|
||||
|
||||
void UI_checkbox::toggle_selected(std::string selected)
|
||||
{
|
||||
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
|
||||
bool UI_checkbox::selected() const
|
||||
{
|
||||
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;
|
||||
for (size_t i = 0; i < m_options.size(); i++)
|
||||
if (m_selected)
|
||||
{
|
||||
if (m_selected[i])
|
||||
{
|
||||
selected_text.push_back(m_options[i]);
|
||||
}
|
||||
return m_text;
|
||||
}
|
||||
return selected_text;
|
||||
return "";
|
||||
}
|
||||
|
||||
void UI_checkbox::from_json(const json &j)
|
||||
{
|
||||
UI_x_boxes::from_json(j);
|
||||
UI_item::from_json(j);
|
||||
|
||||
if (j.contains("selected"))
|
||||
{
|
||||
m_selected.clear();
|
||||
for (auto selected : j["selected"])
|
||||
{
|
||||
m_selected.push_back(selected);
|
||||
}
|
||||
m_selected = j["selected"];
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
return j;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
#pragma once
|
||||
#include "UI_item.hpp"
|
||||
#include "UI_x_boxes.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
#define UI_CHECKBOX_TYPE std::string("UI_checkbox")
|
||||
|
||||
class UI_checkbox : public UI_x_boxes
|
||||
class UI_checkbox : public UI_item
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
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);
|
||||
UI_checkbox(const json &j, QObject *parrent = nullptr);
|
||||
|
||||
void toggle_selected(size_t selected);
|
||||
void toggle_selected(std::string selected);
|
||||
std::vector<bool> selected() const;
|
||||
std::vector<std::string> selected_text() const;
|
||||
void toggle_selected();
|
||||
bool selected() const;
|
||||
std::string selected_text() const;
|
||||
|
||||
void from_json(const json &j) 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);
|
||||
|
||||
private:
|
||||
std::vector<bool> m_selected;
|
||||
bool m_selected;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user