update to use size_t ids instead of QString
This commit is contained in:
2026-02-01 19:00:28 +01:00
parent 441232de73
commit 71b238235c
2 changed files with 48 additions and 5 deletions
+40 -3
View File
@@ -6,6 +6,12 @@ SimulatorBase::SimulatorBase(Web_socket_wrapper* web_socket, QWidget* parent) :
{ {
this->m_web_socket = web_socket; this->m_web_socket = web_socket;
m_error_dialog->set_error("Connection timed out\nCheck if the server is running"); m_error_dialog->set_error("Connection timed out\nCheck if the server is running");
ui_lookup.reserve(40);
}
QWidget* SimulatorBase::id_to_ui(size_t id)
{
return ui_lookup.at(id);
} }
void SimulatorBase::showEvent( QShowEvent* event ) void SimulatorBase::showEvent( QShowEvent* event )
@@ -64,6 +70,34 @@ void SimulatorBase::on_event_cb(json& j)
} }
} }
void SimulatorBase::push_ui_item(QWidget* item)
{
ui_lookup.emplace_back(item);
}
void SimulatorBase::button_update(size_t lookup)
{
m_web_socket->send_event(Web_socket_wrapper::Event::clicked, lookup);
}
void SimulatorBase::slider_update(size_t lookup)
{
QwtSlider* slider = qobject_cast<QwtSlider*>(ui_lookup.at(lookup));
if (slider == nullptr)
return;
m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, lookup,
slider->value());
}
void SimulatorBase::combobox_update(size_t lookup)
{
QComboBox* combobox = qobject_cast<QComboBox*>(ui_lookup.at(lookup));
if (combobox == nullptr)
return;
m_web_socket->send_event(Web_socket_wrapper::Event::selected, lookup,
combobox->currentText());
}
void SimulatorBase::UI_item_parser(json& input) void SimulatorBase::UI_item_parser(json& input)
{ {
for (auto& uiItem : input["UI_items"]) for (auto& uiItem : input["UI_items"])
@@ -160,12 +194,13 @@ void SimulatorBase::process_ui_slider(json& uiItem)
double value = uiItem["value"]; double value = uiItem["value"];
bool enable = uiItem["enabled"]; bool enable = uiItem["enabled"];
bool visible = uiItem["visible"]; bool visible = uiItem["visible"];
QString unit = QString::fromStdString(uiItem.at("unit"));
if (slider->value() != value) if (slider->value() != value)
{ {
if (label != nullptr) if (label != nullptr)
{ {
label->setText(format_value((size_t)uiItem.at("id"), value)); label->setText(QString("%0 %1").arg(value).arg(unit));
} }
slider->setValue(value); slider->setValue(value);
} }
@@ -202,12 +237,13 @@ void SimulatorBase::process_ui_dial(json& uiItem)
double value = uiItem["value"]; double value = uiItem["value"];
bool visible = uiItem["visible"]; bool visible = uiItem["visible"];
QString unit = QString::fromStdString(uiItem.at("unit"));
if (dial->value() != value) if (dial->value() != value)
{ {
if (label != nullptr) if (label != nullptr)
{ {
label->setText(format_value(uiItem["id"], value)); label->setText(QString("%0 %1").arg(value).arg(unit));
} }
dial->setValue(value); dial->setValue(value);
} }
@@ -239,12 +275,13 @@ void SimulatorBase::process_ui_thermo(json& uiItem)
double value = uiItem["value"]; double value = uiItem["value"];
bool visible = uiItem["visible"]; bool visible = uiItem["visible"];
QString unit = QString::fromStdString(uiItem.at("unit"));
if (thermo->value() != value) if (thermo->value() != value)
{ {
if (label != nullptr) if (label != nullptr)
{ {
label->setText(format_value(uiItem["id"], value)); label->setText(QString("%0 %1").arg(value).arg(unit));
} }
thermo->setValue(value); thermo->setValue(value);
} }
+8 -2
View File
@@ -20,15 +20,21 @@ class SimulatorBase : public QMainWindow {
explicit SimulatorBase(Web_socket_wrapper* api, QWidget* parent = nullptr); explicit SimulatorBase(Web_socket_wrapper* api, QWidget* parent = nullptr);
void setup_cb(void); void setup_cb(void);
void sim_update(void); void sim_update(void);
virtual QWidget* id_to_ui(size_t id) {return nullptr;} QWidget* id_to_ui(size_t id);
virtual QLabel* id_to_label(size_t name) {return nullptr;} virtual QLabel* id_to_label(size_t name) {return nullptr;}
virtual QString format_value(size_t name, double val) {return QString();}
signals: signals:
void quit(); void quit();
protected: protected:
void on_event_cb(json& j); void on_event_cb(json& j);
void on_cmd_cb(json& j); void on_cmd_cb(json& j);
void push_ui_item(QWidget* item);
void button_update(size_t lookup);
void slider_update(size_t lookup);
void combobox_update(size_t lookup);
void UI_item_parser(json& input); void UI_item_parser(json& input);
virtual void process_ui_label(json& uiItem); virtual void process_ui_label(json& uiItem);
virtual void process_ui_slider(json& uiItem); virtual void process_ui_slider(json& uiItem);