mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Move to id is size_t
This commit is contained in:
+10
-10
@@ -30,9 +30,9 @@ json Simulators::command_parser(json command)
|
||||
switch (cmd)
|
||||
{
|
||||
case Command::get_UI_element:
|
||||
spdlog::info("Command {}: {}", type, std::string(command.at("id")));
|
||||
spdlog::info("Command {}: {}", type, (size_t)command.at("id"));
|
||||
j["response"]["type"] = type;
|
||||
j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json();
|
||||
j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
|
||||
break;
|
||||
case Command::get_UI_elements:
|
||||
spdlog::info("Command {}", type);
|
||||
@@ -81,26 +81,26 @@ void Simulators::event_handler(json event)
|
||||
switch (e)
|
||||
{
|
||||
case Event::clicked: {
|
||||
spdlog::debug("Event {}: {}", type, std::string(event.at("id")));
|
||||
invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click();
|
||||
spdlog::debug("Event {}: {}", type, (size_t)event.at("id"));
|
||||
invoke_active_simulator()->get_UI_item(event.at("id"))->click();
|
||||
break;
|
||||
}
|
||||
case Event::value_changed: {
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"]));
|
||||
invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value"));
|
||||
spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), (double)(event["value"]));
|
||||
invoke_active_simulator()->get_UI_item(event.at("id"))->set_value((double)event.at("value"));
|
||||
break;
|
||||
}
|
||||
case Event::text_changed: {
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text")));
|
||||
spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("text")));
|
||||
invoke_active_simulator()
|
||||
->get_UI_item(std::string(event.at("id")))
|
||||
->get_UI_item(event.at("id"))
|
||||
->set_text(std::string(event.at("text")));
|
||||
break;
|
||||
}
|
||||
case Event::selected: {
|
||||
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected")));
|
||||
spdlog::debug("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("selected")));
|
||||
invoke_active_simulator()
|
||||
->get_UI_item(std::string(event.at("id")))
|
||||
->get_UI_item(event.at("id"))
|
||||
->set_selected(std::string(event.at("selected")));
|
||||
break;
|
||||
}
|
||||
|
||||
+10
-24
@@ -4,6 +4,7 @@ Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds inter
|
||||
: QObject(parrent), m_name(name), m_timer(new QTimer(this)), m_interval(interval)
|
||||
{
|
||||
connect(m_timer, &QTimer::timeout, this, [this] { timer(); });
|
||||
m_UI_items.reserve(50);
|
||||
}
|
||||
|
||||
std::string Simulator_base::name() const
|
||||
@@ -12,29 +13,10 @@ std::string Simulator_base::name() const
|
||||
return m_name;
|
||||
}
|
||||
|
||||
bool Simulator_base::is_ui_id_unique(std::string id) const
|
||||
{
|
||||
// Check if the UI name is unique
|
||||
for (UI_item *item : m_UI_items)
|
||||
{
|
||||
if (item->id() == id)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Simulator_base::add_UI_item(UI_item *item)
|
||||
{
|
||||
// Add a UI item to the simulator
|
||||
if (is_ui_id_unique(item->id()) == false)
|
||||
{
|
||||
throw std::runtime_error("UI item id " + item->id() + " is not unique");
|
||||
}
|
||||
connect(item, &UI_item::value_changed, this, [this] { emit sim_changed(); });
|
||||
m_UI_items_map[item->id()] = item;
|
||||
m_UI_items.push_back(item);
|
||||
}
|
||||
|
||||
@@ -43,18 +25,22 @@ json Simulator_base::get_UI_items() const
|
||||
// Get the UI items
|
||||
json items;
|
||||
items["name"] = m_name;
|
||||
for (UI_item *item : m_UI_items)
|
||||
for (size_t i = 0; i < m_UI_items.size(); i++)
|
||||
{
|
||||
items["UI_items"].push_back(item->to_json());
|
||||
items["UI_items"].push_back(m_UI_items[i]->to_json(i));
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
UI_item *Simulator_base::get_UI_item(std::string id) const
|
||||
UI_item *Simulator_base::get_UI_item(size_t id) const
|
||||
{
|
||||
// Get a UI item by id
|
||||
return m_UI_items_map.at(id);
|
||||
// Get a UI item by id using the list
|
||||
if (id < m_UI_items.size())
|
||||
{
|
||||
return m_UI_items[id];
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Simulator_base::run()
|
||||
|
||||
+15
-12
@@ -24,16 +24,6 @@ class Simulator_base : public QObject
|
||||
*/
|
||||
std::string name() const;
|
||||
|
||||
/**
|
||||
* @brief This function checks if the UI id is unique
|
||||
* (hence not already pushed in with the add_UI_item function)
|
||||
*
|
||||
* @param id The id to check
|
||||
* @return true The id is unique
|
||||
* @return false The id is already in use
|
||||
*/
|
||||
bool is_ui_id_unique(std::string id) const;
|
||||
|
||||
/**
|
||||
* @brief This function adds a UI item to the simulator
|
||||
* only if the id is unique
|
||||
@@ -55,7 +45,21 @@ class Simulator_base : public QObject
|
||||
* @param id The id of the UI item
|
||||
* @return UI_item* A pointer to the UI item object
|
||||
*/
|
||||
UI_item *get_UI_item(std::string id) const;
|
||||
UI_item *get_UI_item(size_t id) const;
|
||||
|
||||
size_t ptr_to_id(UI_item *ptr) const
|
||||
{
|
||||
size_t id = 0;
|
||||
while (id < m_UI_items.size())
|
||||
{
|
||||
if (m_UI_items[id] == ptr)
|
||||
{
|
||||
return id;
|
||||
}
|
||||
id++;
|
||||
}
|
||||
return (size_t)-1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Run the simulator
|
||||
@@ -89,7 +93,6 @@ class Simulator_base : public QObject
|
||||
protected:
|
||||
std::string m_name;
|
||||
std::vector<UI_item *> m_UI_items;
|
||||
std::map<std::string, UI_item *> m_UI_items_map;
|
||||
QTimer *m_timer;
|
||||
std::chrono::milliseconds m_interval;
|
||||
};
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
|
||||
Test_Sim::Test_Sim(QObject* parrent) :
|
||||
Simulator_base("Test_Sim", std::chrono::milliseconds(1000), parrent),
|
||||
m_button("button", "Off", Color(Color::White), Color(Color::Red)),
|
||||
m_combobox("combobox", "Combobox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}, 0),
|
||||
m_label("label", "Label", Color(Color::White), Color(Color::Black)),
|
||||
m_checkbox("checkbox", "Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}),
|
||||
m_slider("slider", "Slider", Color(Color::White), Color(Color::Black), 0, 100, 0),
|
||||
m_dial("dial", "Dial", Color(Color::White), Color(Color::Black), 0, 100, 0),
|
||||
m_thermo("thermo", "Thermo", Color(Color::White), Color(Color::Black), 0, 100, 0),
|
||||
m_led("led", "Led", Color(Color::White), Color(Color::Black), 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_label("Label", Color(Color::White), Color(Color::Black)),
|
||||
m_checkbox("Checkbox", Color(Color::White), Color(Color::Black), {"Option 1", "Option 2", "Option 3"}),
|
||||
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_thermo("Thermo", Color(Color::White), Color(Color::Black), 0, 100, 0),
|
||||
m_led("Led", Color(Color::White), Color(Color::Black), Color(Color::Red))
|
||||
{
|
||||
this->m_thermo.set_start_color(Color(Color::Red));
|
||||
this->m_thermo.set_end_color(Color(Color::Red));
|
||||
@@ -28,8 +28,8 @@ m_led("led", "Led", Color(Color::White), Color(Color::Black), Color(Color::Red))
|
||||
this->add_UI_item(&this->m_thermo);
|
||||
this->add_UI_item(&this->m_led);
|
||||
|
||||
QObject::connect(&m_button, &UI_button::on_click, [this](std::string id) { button_clicked(id); });
|
||||
QObject::connect(&m_slider, &UI_slider::on_change, [this](std::string id, double value) { slider_changed(id, value); });
|
||||
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); });
|
||||
}
|
||||
|
||||
void Test_Sim::timer()
|
||||
@@ -49,9 +49,9 @@ void Test_Sim::run_at_startup()
|
||||
spdlog::info("Test_Sim started");
|
||||
}
|
||||
|
||||
void Test_Sim::button_clicked(std::string id)
|
||||
void Test_Sim::button_clicked(UI_button* id)
|
||||
{
|
||||
if (id == "button")
|
||||
if (id == &this->m_button)
|
||||
{
|
||||
bool state = this->m_button.bg_color() == Color(Color::Red) ? true : false;
|
||||
this->m_button.set_bg_color(state ? Color(Color::Green) : Color(Color::Red));
|
||||
@@ -62,11 +62,11 @@ void Test_Sim::button_clicked(std::string id)
|
||||
}
|
||||
}
|
||||
|
||||
void Test_Sim::slider_changed(std::string id, double value)
|
||||
void Test_Sim::slider_changed(UI_slider* id, double value)
|
||||
{
|
||||
spdlog::info("Slider {} changed to {}", id, value);
|
||||
if (id == "slider")
|
||||
if (id == &this->m_slider)
|
||||
{
|
||||
spdlog::info("Test slider changed to {}", value);
|
||||
this->m_label.set_text(std::to_string(value));
|
||||
this->m_dial.set_value(value);
|
||||
this->m_thermo.set_value(value);
|
||||
|
||||
@@ -8,9 +8,10 @@ class Test_Sim : public Simulator_base {
|
||||
void timer() override;
|
||||
void run_at_startup() override;
|
||||
private:
|
||||
void button_clicked(std::string id);
|
||||
void slider_changed(std::string id, double value);
|
||||
private:
|
||||
void button_clicked(UI_button* id);
|
||||
void slider_changed(UI_slider* id, double value);
|
||||
|
||||
private:
|
||||
UI_button m_button;
|
||||
UI_combobox m_combobox;
|
||||
UI_label m_label;
|
||||
|
||||
Reference in New Issue
Block a user