diff --git a/Qt/simulatorbase.cpp b/Qt/simulatorbase.cpp index ce181b1..df4a15f 100644 --- a/Qt/simulatorbase.cpp +++ b/Qt/simulatorbase.cpp @@ -6,6 +6,12 @@ SimulatorBase::SimulatorBase(Web_socket_wrapper* web_socket, QWidget* parent) : { this->m_web_socket = web_socket; 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 ) @@ -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(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(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) { for (auto& uiItem : input["UI_items"]) @@ -160,12 +194,13 @@ void SimulatorBase::process_ui_slider(json& uiItem) double value = uiItem["value"]; bool enable = uiItem["enabled"]; bool visible = uiItem["visible"]; + QString unit = QString::fromStdString(uiItem.at("unit")); if (slider->value() != value) { 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); } @@ -202,12 +237,13 @@ void SimulatorBase::process_ui_dial(json& uiItem) double value = uiItem["value"]; bool visible = uiItem["visible"]; + QString unit = QString::fromStdString(uiItem.at("unit")); if (dial->value() != value) { if (label != nullptr) { - label->setText(format_value(uiItem["id"], value)); + label->setText(QString("%0 %1").arg(value).arg(unit)); } dial->setValue(value); } @@ -239,12 +275,13 @@ void SimulatorBase::process_ui_thermo(json& uiItem) double value = uiItem["value"]; bool visible = uiItem["visible"]; + QString unit = QString::fromStdString(uiItem.at("unit")); if (thermo->value() != value) { if (label != nullptr) { - label->setText(format_value(uiItem["id"], value)); + label->setText(QString("%0 %1").arg(value).arg(unit)); } thermo->setValue(value); } diff --git a/Qt/simulatorbase.h b/Qt/simulatorbase.h index 081f26f..d133dd2 100644 --- a/Qt/simulatorbase.h +++ b/Qt/simulatorbase.h @@ -20,15 +20,21 @@ class SimulatorBase : public QMainWindow { explicit SimulatorBase(Web_socket_wrapper* api, QWidget* parent = nullptr); void setup_cb(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 QString format_value(size_t name, double val) {return QString();} signals: void quit(); protected: void on_event_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); virtual void process_ui_label(json& uiItem); virtual void process_ui_slider(json& uiItem);