From d440a493c9bf266d3e6312d3a5f7a1c4eb41d5a0 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 29 Mar 2026 13:11:58 +0200 Subject: [PATCH] client UI base move over to a QMap that holds all of the process functions and call them when parsing UI items --- client/ui_base.cpp | 1612 ++++++++++++++++++++------------------------ client/ui_base.hpp | 138 +--- 2 files changed, 741 insertions(+), 1009 deletions(-) diff --git a/client/ui_base.cpp b/client/ui_base.cpp index f9530a0..61ee1a8 100644 --- a/client/ui_base.cpp +++ b/client/ui_base.cpp @@ -20,6 +20,744 @@ UI_base::UI_base(QString name, Web_socket_wrapper *web_socket, QWidget *parent) QPushButton *exit_app = m_error->addButton("Exit Application", QMessageBox::AcceptRole); m_error->addButton("Close", QMessageBox::RejectRole); connect(exit_app, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection); + m_process_lookup.insert("ui_label", [=, this](json &ui_item, QWidget *widget) { + auto label = qobject_cast(widget); + if (label == nullptr) + { + LOG_WARN("widget is not of type QLabel"); + return; + } + + QString value = QString::fromStdString(ui_item.at("text")); + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (label->isEnabled() != enabled) + { + label->setEnabled(enabled); + } + + if (label->isVisible() != visible) + { + label->setVisible(visible); + } + + if (label->text() != value) + { + label->setText(value); + } + }); + + m_process_lookup.insert("ui_slider", [=, this](json &ui_item, QWidget *widget) { + QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); +// TODO: Find a cleaner fix +#ifdef __WIN32 + if (!widget->inherits("QwtSlider")) + { + LOG_WARN("widget is not of type QwtSlider"); + return; + } + auto slider = static_cast(widget); +#else + auto slider = qobject_cast(widget); + if (slider == nullptr) + { + LOG_WARN("widget is not of type QwtSlider"); + return; + } +#endif + + double value = ui_item.at("value"); + bool enable = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + QString unit = QString::fromStdString(ui_item.at("unit")); + + if (label != nullptr) + { + label->setText(QString("%0 %1").arg(value).arg(unit)); + } + + if (slider->value() != value) + { + slider->setValue(value); + } + + if (slider->isReadOnly() != !enable) + { + slider->setReadOnly(!enable); + } + + if (slider->isVisible() != visible) + { + slider->setVisible(visible); + } + // TODO: Add color for scale + }); + m_process_lookup.insert("ui_dial", [=, this](json &ui_item, QWidget *widget) { + QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); +// TODO: Find a cleaner fix +#ifdef __WIN32 + if (!widget->inherits("QwtDial")) + { + LOG_WARN("widget is not of type QwtDial"); + return; + } + auto dial = static_cast(widget); +#else + auto dial = qobject_cast(widget); + if (dial == nullptr) + { + LOG_WARN("widget is not of type QwtDial"); + return; + } +#endif + + double value = ui_item.at("value"); + bool visible = ui_item.at("visible"); + QString unit = QString::fromStdString(ui_item.at("unit")); + + if (label != nullptr) + { + label->setText(QString("%0 %1").arg(value).arg(unit)); + } + + if (dial->value() != value) + { + dial->setValue(value); + } + + if (dial->isVisible() != visible) + { + dial->setVisible(visible); + } + }); + m_process_lookup.insert("ui_thermo", [=, this](json &ui_item, QWidget *widget) { + QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); +// TODO: Find a cleaner fix +#ifdef __WIN32 + if (!widget->inherits("QwtThermo")) + { + LOG_WARN("widget is not of type QwtThermo"); + return; + } + auto thermo = static_cast(widget); +#else + auto thermo = qobject_cast(widget); + if (thermo == nullptr) + { + LOG_WARN("widget is not of type QwtThermo"); + return; + } +#endif + + double value = ui_item.at("value"); + bool visible = ui_item.at("visible"); + QString unit = QString::fromStdString(ui_item.at("unit")); + + if (label != nullptr) + { + label->setText(QString("%0 %1").arg(value).arg(unit)); + } + + if (thermo->value() != value) + { + thermo->setValue(value); + } + + if (thermo->isVisible() != visible) + { + thermo->setVisible(visible); + } + }); + m_process_lookup.insert("ui_textbox", [=, this](json &ui_item, QWidget *widget) { + auto line_edit = qobject_cast(widget); + if (line_edit == nullptr) + { + LOG_WARN("widget is not of type QLineEdit"); + return; + } + + bool visible = ui_item.at("visible"); + bool enabled = ui_item.at("enabled"); + QString hint = QString::fromStdString(ui_item.at("hint")); + QString text = QString::fromStdString(ui_item.at("text")); + + if (line_edit->isVisible() != visible) + { + line_edit->setVisible(visible); + } + + if (line_edit->isEnabled() != enabled) + { + line_edit->setEnabled(enabled); + } + if (line_edit->placeholderText() != hint) + { + line_edit->setPlaceholderText(hint); + } + if (line_edit->text() != text) + { + line_edit->setText(text); + } + }); + m_process_lookup.insert("ui_combobox", [=, this](json &ui_item, QWidget *widget) { + auto combobox = qobject_cast(widget); + if (combobox == nullptr) + { + LOG_WARN("widget is not of type QComboBox"); + return; + } + + size_t selected = ui_item.at("selected"); + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (combobox->isEnabled() != enabled) + { + combobox->setEnabled(enabled); + } + + if (combobox->isVisible() != visible) + { + combobox->setVisible(visible); + } + + for (json &item : ui_item.at("options")) + { + if (combobox->findText(QString::fromStdString(item)) == -1) + combobox->addItem(QString::fromStdString(item)); + } + + if (combobox->currentIndex() != selected) + { + combobox->setCurrentIndex(selected); + } + }); + m_process_lookup.insert("ui_radiobutton", + [=, this](json &ui_item, QWidget *widget) { LOG_CRITICAL("Not implemented"); }); + m_process_lookup.insert("ui_checkbox", [=, this](json &ui_item, QWidget *widget) { + auto checkbox = qobject_cast(widget); + if (checkbox == nullptr) + { + LOG_WARN("widget is not of type QCheckbox"); + return; + } + + QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); + QColor fg_color = QColor(QString::fromStdString(ui_item.at("fg_color"))); + QString text = QString::fromStdString(ui_item.at("text")); + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + bool checked = ui_item.at("checked"); + + if (checkbox->isChecked() != checked) + { + checkbox->setChecked(checked); + } + + if (checkbox->text() != text) + { + checkbox->setText(text); + } + + if (checkbox->isEnabled() != enabled) + { + checkbox->setEnabled(enabled); + } + + if (checkbox->isVisible() != visible) + { + checkbox->setVisible(visible); + } + + if (bg_color == QColor(0x83, 0x91, 0x92)) + { + reset_widget_color(checkbox); + return; + } + + if (bg_color == QColor(0xFF, 0x00, 0x00)) + { + set_widget_color(checkbox, QString("black"), QString("darkred")); + return; + } + + if (bg_color == QColor(0x00, 0xFF, 0x00)) + { + set_widget_color(checkbox, QString("white"), QString("darkgreen")); + return; + } + + if (bg_color == QColor(0xFF8800)) + { + set_widget_color(checkbox, QString("darkred"), QString("darkorange")); + return; + } + + if (bg_color != widget_bg_color(checkbox) || fg_color != widget_color(checkbox)) + { + set_widget_color(checkbox, fg_color, bg_color); + } + }); + m_process_lookup.insert("ui_button", [=, this](json &ui_item, QWidget *widget) { + auto button = qobject_cast(widget); + if (button == nullptr) + { + LOG_WARN("widget is not of type QPushButton"); + return; + } + + QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); + QColor fg_color = QColor(QString::fromStdString(ui_item.at("fg_color"))); + QString text = QString::fromStdString(ui_item.at("text")); + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (button->isEnabled() != enabled) + { + button->setEnabled(enabled); + } + + if (button->isVisible() != visible) + { + button->setVisible(visible); + } + + if (button->text() != text) + { + button->setText(text); + } + + if (bg_color == QColor(0x83, 0x91, 0x92)) + { + reset_widget_color(button); + return; + } + + if (bg_color == QColor(0xFF, 0x00, 0x00)) + { + set_widget_color(button, QString("black"), QString("darkred")); + return; + } + + if (bg_color == QColor(0x00, 0xFF, 0x00)) + { + set_widget_color(button, QString("white"), QString("darkgreen")); + return; + } + + if (bg_color == QColor(0xFF8800)) + { + set_widget_color(button, QString("darkred"), QString("darkorange")); + return; + } + + if (bg_color != widget_bg_color(button) || fg_color != widget_color(button)) + { + set_widget_color(button, fg_color, bg_color); + } + }); + m_process_lookup.insert("ui_led", [=, this](json &ui_item, QWidget *widget) { + auto led = qobject_cast(widget); + if (led == nullptr) + { + LOG_WARN("widget is not of type led"); + return; + } + + QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); + QColor color = QColor(QString::fromStdString(ui_item.at("fg_color"))); + QString text = QString::fromStdString(ui_item.at("text")); + bool visible = ui_item.at("visible"); + + if (led->isVisible() != visible) + { + led->setVisible(visible); + } + + if (text != led->text()) + { + led->setText(text); + } + + if (bg_color == QColor(0x83, 0x91, 0x92)) + { + reset_widget_color(led); + return; + } + + if (bg_color == QColor(0xFF, 0x00, 0x00)) + { + set_widget_color(led, QString("black"), QString("darkred")); + return; + } + + if (bg_color == QColor(0x00, 0xFF, 0x00)) + { + set_widget_color(led, QString("white"), QString("darkgreen")); + return; + } + + if (bg_color == QColor(0xFF8800)) + { + set_widget_color(led, QString("darkred"), QString("darkorange")); + return; + } + + if (bg_color != widget_bg_color(led) || color != widget_color(led)) + { + set_widget_color(led, color, bg_color); + } + }); + m_process_lookup.insert("ui_spinbox", [=, this](json &ui_item, QWidget *widget) { + auto spinbox = qobject_cast(widget); + if (spinbox == nullptr) + { + LOG_WARN("widget is not of type QSpinBox"); + return; + } + + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + double min = ui_item.at("min"); + double max = ui_item.at("max"); + double value = ui_item.at("value"); + + if (enabled != spinbox->isEnabled()) + { + spinbox->setEnabled(enabled); + } + + if (visible != spinbox->isVisible()) + { + spinbox->setVisible(visible); + } + + if (min != spinbox->minimum()) + { + spinbox->setMinimum(min); + } + if (max != spinbox->maximum()) + { + spinbox->setMaximum(max); + } + if (value != spinbox->value()) + { + spinbox->setValue((int)value); + } + }); + m_process_lookup.insert("ui_double_spinbox", [=, this](json &ui_item, QWidget *widget) { + auto spinbox = qobject_cast(widget); + if (spinbox == nullptr) + { + LOG_WARN("widget is not of type QDoubleSpinBox"); + return; + } + + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + double min = ui_item.at("min"); + double max = ui_item.at("max"); + double value = ui_item.at("value"); + + if (enabled != spinbox->isEnabled()) + { + spinbox->setEnabled(enabled); + } + + if (visible != spinbox->isVisible()) + { + spinbox->setVisible(visible); + } + + if (min != spinbox->minimum()) + { + spinbox->setMinimum(min); + } + if (max != spinbox->maximum()) + { + spinbox->setMaximum(max); + } + if (value != spinbox->value()) + { + spinbox->setValue(value); + } + }); + m_process_lookup.insert("ui_hex_spinbox", [=, this](json &ui_item, QWidget *widget) { + auto spinbox = qobject_cast(widget); + if (spinbox == nullptr) + { + LOG_WARN("widget is not of type HexSpinBox"); + return; + } + + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + double min = ui_item.at("min"); + double max = ui_item.at("max"); + double value = ui_item.at("value"); + + if (enabled != spinbox->isEnabled()) + { + spinbox->setEnabled(enabled); + } + + if (visible != spinbox->isVisible()) + { + spinbox->setVisible(visible); + } + + if (min != spinbox->minimum()) + { + spinbox->setMinimum(min); + } + if (max != spinbox->maximum()) + { + spinbox->setMaximum(max); + } + if (value != spinbox->value()) + { + spinbox->setValue((int)value); + } + }); + m_process_lookup.insert("ui_tab_widget", [=, this](json &ui_item, QWidget *widget) { + auto tab_widget = qobject_cast(widget); + if (tab_widget == nullptr) + { + LOG_WARN("widget is not of type QTabWidget"); + return; + } + + if (tab_widget->count() != ui_item.at("tab_names").size()) + { + LOG_ERROR("QTabWidget has not the same amount of tabs as the server"); + return; + } + + auto tab_names = ui_item.at("tab_names"); + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (tab_widget->isEnabled() != enabled) + { + tab_widget->setEnabled(enabled); + } + + if (tab_widget->isVisible() != visible) + { + tab_widget->setVisible(visible); + } + + for (size_t i = 0; i < tab_names.size(); i++) + { + if (tab_widget->tabText(i) != QString::fromStdString(tab_names[i])) + { + tab_widget->setTabText(i, QString::fromStdString(tab_names[i])); + } + if (tab_widget->isVisible() != ui_item.at("tab_visible")[i]) + { + tab_widget->setTabVisible(i, ui_item.at("tab_visible")[i]); + } + } + + if (tab_widget->currentIndex() != ui_item.at("selected")) + { + tab_widget->setCurrentIndex(ui_item.at("selected")); + } + }); + m_process_lookup.insert("ui_stacked_widget", [=, this](json &ui_item, QWidget *widget) { + auto stacked_widget = qobject_cast(widget); + if (stacked_widget == nullptr) + { + LOG_WARN("widget is not of type QStackedWidget"); + return; + } + + if (stacked_widget->count() != ui_item.at("tab_count")) + { + LOG_ERROR("QStackedWidget has not the same amount of tabs as the server"); + return; + } + + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (stacked_widget->isEnabled() != enabled) + { + stacked_widget->setEnabled(enabled); + } + + if (stacked_widget->isVisible() != visible) + { + stacked_widget->setVisible(visible); + } + + if (stacked_widget->currentIndex() != ui_item.at("current_tab")) + { + stacked_widget->setCurrentIndex(ui_item.at("current_tab")); + } + }); + m_process_lookup.insert("ui_status_bar", [=, this](json &ui_item, QWidget *widget) { + auto status_bar = qobject_cast(widget); + if (status_bar == nullptr) + { + LOG_WARN("widget is not of type QStatusBar"); + return; + } + + if (status_bar->currentMessage() != QString::fromStdString(ui_item.at("text"))) + { + status_bar->showMessage(QString::fromStdString(ui_item.at("text")), ui_item.at("timeout")); + } + }); + m_process_lookup.insert("ui_plot", [=, this](json &ui_item, QWidget *widget) { + auto plot = qobject_cast(widget); + if (plot == nullptr) + { + LOG_WARN("widget is not of type QwtPlot"); + return; + } + + QString text = QString::fromStdString(ui_item.at("text")); + QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); + QColor color = QColor(QString::fromStdString(ui_item.at("fg_color"))); + QString x_label = QString::fromStdString(ui_item.at("x_label")); + QString y_label = QString::fromStdString(ui_item.at("y_label")); + + std::vector x_vals = (std::vector)ui_item.at("x_vals"); + std::vector y_vals = (std::vector)ui_item.at("y_vals"); + + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (plot->isEnabled() != enabled) + { + plot->setEnabled(enabled); + } + + if (plot->isVisible() != visible) + { + plot->setVisible(visible); + } + + if (text != plot->title().text()) + { + plot->setTitle(text); + } + + if (x_label != plot->axisTitle(QwtAxis::XBottom).text()) + { + plot->setAxisTitle(QwtAxis::XBottom, x_label); + } + + if (y_label != plot->axisTitle(QwtAxis::YLeft).text()) + { + plot->setAxisTitle(QwtAxis::YLeft, y_label); + } + + if (color != plot->curve()->pen().color()) + { + plot->curve()->setPen(QPen(color)); + } + + if (color == QColor(0x83, 0x91, 0x92)) + { + plot->curve()->setPen(QPen(Qt::red)); + } + + plot->curve()->setSamples(x_vals.data(), y_vals.data(), x_vals.size()); + if (x_vals.size() > 0) + plot->setAxisScale(QwtAxis::XBottom, x_vals[0], x_vals[x_vals.size() - 1]); + plot->replot(); + }); + m_process_lookup.insert("ui_table", [=, this](json &ui_item, QWidget *widget) { + auto table = qobject_cast(widget); + if (table == nullptr) + { + LOG_WARN("widget is not of type QTableWidget"); + return; + } + + size_t row_count = ui_item.at("row_count"); + std::vector row_labels = (std::vector)ui_item.at("row_labels"); + size_t column_count = ui_item.at("column_count"); + std::vector column_labels = (std::vector)ui_item.at("column_labels"); + std::vector table_data = (std::vector)ui_item.at("table"); + bool enabled = ui_item.at("enabled"); + bool visible = ui_item.at("visible"); + + if (table->isEnabled() != enabled) + { + table->setEnabled(enabled); + } + + if (table->isVisible() != visible) + { + table->setVisible(visible); + } + + table->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers); + table->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); + + if (table->rowCount() != row_count) + { + table->setRowCount(row_count); + } + if (table->columnCount() != column_count) + { + table->setColumnCount(column_count); + } + if (row_count != 0) + { + for (size_t i = 0; i < row_labels.size(); i++) + { + if (table->verticalHeaderItem(i) == nullptr) + table->setVerticalHeaderItem(i, new QTableWidgetItem()); + if (row_labels[i] != table->verticalHeaderItem(i)->text()) + table->verticalHeaderItem(i)->setText(QString::fromStdString(row_labels[i])); + } + } + if (column_count != 0) + { + for (size_t i = 0; i < column_labels.size(); i++) + { + if (table->horizontalHeaderItem(i) == nullptr) + table->setHorizontalHeaderItem(i, new QTableWidgetItem()); + if (column_labels[i] != table->horizontalHeaderItem(i)->text()) + table->horizontalHeaderItem(i)->setText(QString::fromStdString(column_labels[i])); + } + } + for (size_t i = 0; i < table_data.size(); i++) + { + size_t row = i / column_count; + size_t column = i % column_count; + if (table->item(row, column) == nullptr) + { + table->setItem(row, column, new QTableWidgetItem()); + } + table->item(row, column)->setText(QString::fromStdString(table_data[i])); + } + table->resizeColumnsToContents(); + }); + m_process_lookup.insert("ui_can", [=, this](json &ui_item, QWidget *widget) { + auto can_ui = qobject_cast(widget); + if (can_ui == nullptr) + { + LOG_WARN("widget is not of type led"); + return; + } + + can_ui->clear(); + for (auto &send_item : ui_item.at("send_msgs")) + { + can_ui->add_send_item(send_item.at("id"), send_item.at("dlc"), send_item.at("payload")); + } + + for (auto &recvd_item : ui_item.at("rcvd_msgs")) + { + can_ui->add_receive_item(recvd_item.at("id"), recvd_item.at("dlc"), recvd_item.at("payload")); + } + }); } QWidget *UI_base::id_to_ui(size_t id) @@ -113,879 +851,7 @@ void UI_base::UI_item_parser(json &input) return; } - if (ui_item.at("type") == "ui_button") - { - process_ui_button(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_led") - { - process_ui_led(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_slider") - { - process_ui_slider(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_dial") - { - process_ui_dial(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_thermo") - { - process_ui_thermo(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_textbox") - { - process_ui_textbox(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_label") - { - process_ui_label(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_combobox") - { - process_ui_combobox(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_radiobutton") - { - process_ui_radiobutton(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_checkbox") - { - process_ui_checkbox(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_spinbox") - { - process_ui_spinbox(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_double_spinbox") - { - process_ui_double_spinbox(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_hex_spinbox") - { - process_ui_hex_spinbox(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_tab_widget") - { - process_ui_tab_widget(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_stacked_widget") - { - process_ui_stacked_widget(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_status_bar") - { - process_ui_status_bar(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_plot") - { - process_ui_plot(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_table") - { - process_ui_table(ui_item, widget); - continue; - } - if (ui_item.at("type") == "ui_can") - { - process_ui_can(ui_item, widget); - continue; - } - } -} - -void UI_base::process_ui_label(json &ui_item, QWidget *widget) -{ - auto label = qobject_cast(widget); - if (label == nullptr) - { - LOG_WARN("widget is not of type QLabel"); - return; - } - - QString value = QString::fromStdString(ui_item.at("text")); - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (label->isEnabled() != enabled) - { - label->setEnabled(enabled); - } - - if (label->isVisible() != visible) - { - label->setVisible(visible); - } - - if (label->text() != value) - { - label->setText(value); - } -} - -void UI_base::process_ui_slider(json &ui_item, QWidget *widget) -{ - QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); -#ifdef __WIN32 - if (!widget->inherits("QwtSlider")) - { - LOG_WARN("widget is not of type QwtSlider"); - return; - } - auto slider = static_cast(widget); -#else - auto slider = qobject_cast(widget); - if (slider == nullptr) - { - LOG_WARN("widget is not of type QwtSlider"); - return; - } -#endif - - double value = ui_item.at("value"); - bool enable = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - QString unit = QString::fromStdString(ui_item.at("unit")); - - if (label != nullptr) - { - label->setText(QString("%0 %1").arg(value).arg(unit)); - } - - if (slider->value() != value) - { - slider->setValue(value); - } - - if (slider->isReadOnly() != !enable) - { - slider->setReadOnly(!enable); - } - - if (slider->isVisible() != visible) - { - slider->setVisible(visible); - } - // TODO: Add color for scale - return; -} - -void UI_base::process_ui_dial(json &ui_item, QWidget *widget) -{ - QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); - -#ifdef __WIN32 - if (!widget->inherits("QwtDial")) - { - LOG_WARN("widget is not of type QwtDial"); - return; - } - auto dial = static_cast(widget); -#else - auto dial = qobject_cast(widget); - if (dial == nullptr) - { - LOG_WARN("widget is not of type QwtDial"); - return; - } -#endif - - double value = ui_item.at("value"); - bool visible = ui_item.at("visible"); - QString unit = QString::fromStdString(ui_item.at("unit")); - - if (label != nullptr) - { - label->setText(QString("%0 %1").arg(value).arg(unit)); - } - - if (dial->value() != value) - { - dial->setValue(value); - } - - if (dial->isVisible() != visible) - { - dial->setVisible(visible); - } - return; -} - -void UI_base::process_ui_thermo(json &ui_item, QWidget *widget) -{ - QLabel *label = id_to_label(id_to_ui(ui_item.at("id"))); - -#ifdef __WIN32 - if (!widget->inherits("QwtThermo")) - { - LOG_WARN("widget is not of type QwtThermo"); - return; - } - auto thermo = static_cast(widget); -#else - auto thermo = qobject_cast(widget); - if (thermo == nullptr) - { - LOG_WARN("widget is not of type QwtThermo"); - return; - } -#endif - - double value = ui_item.at("value"); - bool visible = ui_item.at("visible"); - QString unit = QString::fromStdString(ui_item.at("unit")); - - if (label != nullptr) - { - label->setText(QString("%0 %1").arg(value).arg(unit)); - } - - if (thermo->value() != value) - { - thermo->setValue(value); - } - - if (thermo->isVisible() != visible) - { - thermo->setVisible(visible); - } - return; -} - -void UI_base::process_ui_textbox(json &ui_item, QWidget *widget) -{ - auto line_edit = qobject_cast(widget); - if (line_edit == nullptr) - { - LOG_WARN("widget is not of type QLineEdit"); - return; - } - - bool visible = ui_item.at("visible"); - bool enabled = ui_item.at("enabled"); - QString hint = QString::fromStdString(ui_item.at("hint")); - QString text = QString::fromStdString(ui_item.at("text")); - - if (line_edit->isVisible() != visible) - { - line_edit->setVisible(visible); - } - - if (line_edit->isEnabled() != enabled) - { - line_edit->setEnabled(enabled); - } - if (line_edit->placeholderText() != hint) - { - line_edit->setPlaceholderText(hint); - } - if (line_edit->text() != text) - { - line_edit->setText(text); - } -} - -void UI_base::process_ui_combobox(json &ui_item, QWidget *widget) -{ - auto combobox = qobject_cast(widget); - if (combobox == nullptr) - { - LOG_WARN("widget is not of type QComboBox"); - return; - } - - size_t selected = ui_item.at("selected"); - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (combobox->isEnabled() != enabled) - { - combobox->setEnabled(enabled); - } - - if (combobox->isVisible() != visible) - { - combobox->setVisible(visible); - } - - for (json &item : ui_item.at("options")) - { - if (combobox->findText(QString::fromStdString(item)) == -1) - combobox->addItem(QString::fromStdString(item)); - } - - if (combobox->currentIndex() != selected) - { - combobox->setCurrentIndex(selected); - } -} - -void UI_base::process_ui_radiobutton(json &ui_item, QWidget *widget) -{ - LOG_CRITICAL("Not implemented"); -} - -void UI_base::process_ui_checkbox(json &ui_item, QWidget *widget) -{ - auto checkbox = qobject_cast(widget); - if (checkbox == nullptr) - { - LOG_WARN("widget is not of type QCheckbox"); - return; - } - - QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); - QColor fg_color = QColor(QString::fromStdString(ui_item.at("fg_color"))); - QString text = QString::fromStdString(ui_item.at("text")); - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - bool checked = ui_item.at("checked"); - - if (checkbox->isChecked() != checked) - { - checkbox->setChecked(checked); - } - - if (checkbox->text() != text) - { - checkbox->setText(text); - } - - if (checkbox->isEnabled() != enabled) - { - checkbox->setEnabled(enabled); - } - - if (checkbox->isVisible() != visible) - { - checkbox->setVisible(visible); - } - - if (bg_color == QColor(0x83, 0x91, 0x92)) - { - reset_widget_color(checkbox); - return; - } - - if (bg_color == QColor(0xFF, 0x00, 0x00)) - { - set_widget_color(checkbox, QString("black"), QString("darkred")); - return; - } - - if (bg_color == QColor(0x00, 0xFF, 0x00)) - { - set_widget_color(checkbox, QString("white"), QString("darkgreen")); - return; - } - - if (bg_color == QColor(0xFF8800)) - { - set_widget_color(checkbox, QString("darkred"), QString("darkorange")); - return; - } - - if (bg_color != widget_bg_color(checkbox) || fg_color != widget_color(checkbox)) - { - set_widget_color(checkbox, fg_color, bg_color); - } -} - -void UI_base::process_ui_button(json &ui_item, QWidget *widget) -{ - auto button = qobject_cast(widget); - if (button == nullptr) - { - LOG_WARN("widget is not of type QPushButton"); - return; - } - - QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); - QColor fg_color = QColor(QString::fromStdString(ui_item.at("fg_color"))); - QString text = QString::fromStdString(ui_item.at("text")); - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (button->isEnabled() != enabled) - { - button->setEnabled(enabled); - } - - if (button->isVisible() != visible) - { - button->setVisible(visible); - } - - if (button->text() != text) - { - button->setText(text); - } - - if (bg_color == QColor(0x83, 0x91, 0x92)) - { - reset_widget_color(button); - return; - } - - if (bg_color == QColor(0xFF, 0x00, 0x00)) - { - set_widget_color(button, QString("black"), QString("darkred")); - return; - } - - if (bg_color == QColor(0x00, 0xFF, 0x00)) - { - set_widget_color(button, QString("white"), QString("darkgreen")); - return; - } - - if (bg_color == QColor(0xFF8800)) - { - set_widget_color(button, QString("darkred"), QString("darkorange")); - return; - } - - if (bg_color != widget_bg_color(button) || fg_color != widget_color(button)) - { - set_widget_color(button, fg_color, bg_color); - } -} - -void UI_base::process_ui_led(json &ui_item, QWidget *widget) -{ - auto led = qobject_cast(widget); - if (led == nullptr) - { - LOG_WARN("widget is not of type led"); - return; - } - - QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); - QColor color = QColor(QString::fromStdString(ui_item.at("fg_color"))); - QString text = QString::fromStdString(ui_item.at("text")); - bool visible = ui_item.at("visible"); - - if (led->isVisible() != visible) - { - led->setVisible(visible); - } - - if (text != led->text()) - { - led->setText(text); - } - - if (bg_color == QColor(0x83, 0x91, 0x92)) - { - reset_widget_color(led); - return; - } - - if (bg_color == QColor(0xFF, 0x00, 0x00)) - { - set_widget_color(led, QString("black"), QString("darkred")); - return; - } - - if (bg_color == QColor(0x00, 0xFF, 0x00)) - { - set_widget_color(led, QString("white"), QString("darkgreen")); - return; - } - - if (bg_color == QColor(0xFF8800)) - { - set_widget_color(led, QString("darkred"), QString("darkorange")); - return; - } - - if (bg_color != widget_bg_color(led) || color != widget_color(led)) - { - set_widget_color(led, color, bg_color); - } -} - -void UI_base::process_ui_spinbox(json &ui_item, QWidget *widget) -{ - auto spinbox = qobject_cast(widget); - if (spinbox == nullptr) - { - LOG_WARN("widget is not of type QSpinBox"); - return; - } - - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - double min = ui_item.at("min"); - double max = ui_item.at("max"); - double value = ui_item.at("value"); - - if (enabled != spinbox->isEnabled()) - { - spinbox->setEnabled(enabled); - } - - if (visible != spinbox->isVisible()) - { - spinbox->setVisible(visible); - } - - if (min != spinbox->minimum()) - { - spinbox->setMinimum(min); - } - if (max != spinbox->maximum()) - { - spinbox->setMaximum(max); - } - if (value != spinbox->value()) - { - spinbox->setValue((int)value); - } -} - -void UI_base::process_ui_double_spinbox(json &ui_item, QWidget *widget) -{ - auto spinbox = qobject_cast(widget); - if (spinbox == nullptr) - { - LOG_WARN("widget is not of type QDoubleSpinBox"); - return; - } - - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - double min = ui_item.at("min"); - double max = ui_item.at("max"); - double value = ui_item.at("value"); - - if (enabled != spinbox->isEnabled()) - { - spinbox->setEnabled(enabled); - } - - if (visible != spinbox->isVisible()) - { - spinbox->setVisible(visible); - } - - if (min != spinbox->minimum()) - { - spinbox->setMinimum(min); - } - if (max != spinbox->maximum()) - { - spinbox->setMaximum(max); - } - if (value != spinbox->value()) - { - spinbox->setValue(value); - } -} - -void UI_base::process_ui_hex_spinbox(json &ui_item, QWidget *widget) -{ - auto spinbox = qobject_cast(widget); - if (spinbox == nullptr) - { - LOG_WARN("widget is not of type HexSpinBox"); - return; - } - - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - double min = ui_item.at("min"); - double max = ui_item.at("max"); - double value = ui_item.at("value"); - - if (enabled != spinbox->isEnabled()) - { - spinbox->setEnabled(enabled); - } - - if (visible != spinbox->isVisible()) - { - spinbox->setVisible(visible); - } - - if (min != spinbox->minimum()) - { - spinbox->setMinimum(min); - } - if (max != spinbox->maximum()) - { - spinbox->setMaximum(max); - } - if (value != spinbox->value()) - { - spinbox->setValue((int)value); - } -} - -void UI_base::process_ui_tab_widget(json &ui_item, QWidget *widget) -{ - auto tab_widget = qobject_cast(widget); - if (tab_widget == nullptr) - { - LOG_WARN("widget is not of type QTabWidget"); - return; - } - - if (tab_widget->count() != ui_item.at("tab_names").size()) - { - LOG_ERROR("QTabWidget has not the same amount of tabs as the server"); - return; - } - - auto tab_names = ui_item.at("tab_names"); - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (tab_widget->isEnabled() != enabled) - { - tab_widget->setEnabled(enabled); - } - - if (tab_widget->isVisible() != visible) - { - tab_widget->setVisible(visible); - } - - for (size_t i = 0; i < tab_names.size(); i++) - { - if (tab_widget->tabText(i) != QString::fromStdString(tab_names[i])) - { - tab_widget->setTabText(i, QString::fromStdString(tab_names[i])); - } - if (tab_widget->isVisible() != ui_item.at("tab_visible")[i]) - { - tab_widget->setTabVisible(i, ui_item.at("tab_visible")[i]); - } - } - - if (tab_widget->currentIndex() != ui_item.at("selected")) - { - tab_widget->setCurrentIndex(ui_item.at("selected")); - } -} - -void UI_base::process_ui_stacked_widget(json &ui_item, QWidget *widget) -{ - auto stacked_widget = qobject_cast(widget); - if (stacked_widget == nullptr) - { - LOG_WARN("widget is not of type QStackedWidget"); - return; - } - - if (stacked_widget->count() != ui_item.at("tab_count")) - { - LOG_ERROR("QStackedWidget has not the same amount of tabs as the server"); - return; - } - - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (stacked_widget->isEnabled() != enabled) - { - stacked_widget->setEnabled(enabled); - } - - if (stacked_widget->isVisible() != visible) - { - stacked_widget->setVisible(visible); - } - - if (stacked_widget->currentIndex() != ui_item.at("current_tab")) - { - stacked_widget->setCurrentIndex(ui_item.at("current_tab")); - } -} - -void UI_base::process_ui_status_bar(json &ui_item, QWidget *widget) -{ - auto status_bar = qobject_cast(widget); - if (status_bar == nullptr) - { - LOG_WARN("widget is not of type QStatusBar"); - return; - } - - if (status_bar->currentMessage() != QString::fromStdString(ui_item.at("text"))) - { - status_bar->showMessage(QString::fromStdString(ui_item.at("text")), ui_item.at("timeout")); - } -} - -void UI_base::process_ui_plot(json &ui_item, QWidget *widget) -{ - auto plot = qobject_cast(widget); - if (plot == nullptr) - { - LOG_WARN("widget is not of type QwtPlot"); - return; - } - - QString text = QString::fromStdString(ui_item.at("text")); - QColor bg_color = QColor(QString::fromStdString(ui_item.at("bg_color"))); - QColor color = QColor(QString::fromStdString(ui_item.at("fg_color"))); - QString x_label = QString::fromStdString(ui_item.at("x_label")); - QString y_label = QString::fromStdString(ui_item.at("y_label")); - - std::vector x_vals = (std::vector)ui_item.at("x_vals"); - std::vector y_vals = (std::vector)ui_item.at("y_vals"); - - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (plot->isEnabled() != enabled) - { - plot->setEnabled(enabled); - } - - if (plot->isVisible() != visible) - { - plot->setVisible(visible); - } - - if (text != plot->title().text()) - { - plot->setTitle(text); - } - - if (x_label != plot->axisTitle(QwtAxis::XBottom).text()) - { - plot->setAxisTitle(QwtAxis::XBottom, x_label); - } - - if (y_label != plot->axisTitle(QwtAxis::YLeft).text()) - { - plot->setAxisTitle(QwtAxis::YLeft, y_label); - } - - if (color != plot->curve()->pen().color()) - { - plot->curve()->setPen(QPen(color)); - } - - if (color == QColor(0x83, 0x91, 0x92)) - { - plot->curve()->setPen(QPen(Qt::red)); - } - - plot->curve()->setSamples(x_vals.data(), y_vals.data(), x_vals.size()); - if (x_vals.size() > 0) - plot->setAxisScale(QwtAxis::XBottom, x_vals[0], x_vals[x_vals.size() - 1]); - plot->replot(); -} - -void UI_base::process_ui_table(json &ui_item, QWidget *widget) -{ - auto table = qobject_cast(widget); - if (table == nullptr) - { - LOG_WARN("widget is not of type QTableWidget"); - return; - } - - size_t row_count = ui_item.at("row_count"); - std::vector row_labels = (std::vector)ui_item.at("row_labels"); - size_t column_count = ui_item.at("column_count"); - std::vector column_labels = (std::vector)ui_item.at("column_labels"); - std::vector table_data = (std::vector)ui_item.at("table"); - bool enabled = ui_item.at("enabled"); - bool visible = ui_item.at("visible"); - - if (table->isEnabled() != enabled) - { - table->setEnabled(enabled); - } - - if (table->isVisible() != visible) - { - table->setVisible(visible); - } - - table->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers); - table->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); - - if (table->rowCount() != row_count) - { - table->setRowCount(row_count); - } - if (table->columnCount() != column_count) - { - table->setColumnCount(column_count); - } - if (row_count != 0) - { - for (size_t i = 0; i < row_labels.size(); i++) - { - if (table->verticalHeaderItem(i) == nullptr) - table->setVerticalHeaderItem(i, new QTableWidgetItem()); - if (row_labels[i] != table->verticalHeaderItem(i)->text()) - table->verticalHeaderItem(i)->setText(QString::fromStdString(row_labels[i])); - } - } - if (column_count != 0) - { - for (size_t i = 0; i < column_labels.size(); i++) - { - if (table->horizontalHeaderItem(i) == nullptr) - table->setHorizontalHeaderItem(i, new QTableWidgetItem()); - if (column_labels[i] != table->horizontalHeaderItem(i)->text()) - table->horizontalHeaderItem(i)->setText(QString::fromStdString(column_labels[i])); - } - } - for (size_t i = 0; i < table_data.size(); i++) - { - size_t row = i / column_count; - size_t column = i % column_count; - if (table->item(row, column) == nullptr) - { - table->setItem(row, column, new QTableWidgetItem()); - } - table->item(row, column)->setText(QString::fromStdString(table_data[i])); - } - table->resizeColumnsToContents(); -} - -void UI_base::process_ui_can(json &ui_item, QWidget *widget) -{ - auto can_ui = qobject_cast(widget); - if (can_ui == nullptr) - { - LOG_WARN("widget is not of type led"); - return; - } - - can_ui->clear(); - for (auto &send_item : ui_item.at("send_msgs")) - { - can_ui->add_send_item(send_item.at("id"), send_item.at("dlc"), send_item.at("payload")); - } - - for (auto &recvd_item : ui_item.at("rcvd_msgs")) - { - can_ui->add_receive_item(recvd_item.at("id"), recvd_item.at("dlc"), recvd_item.at("payload")); + m_process_lookup[ui_item.at("type")](ui_item, widget); } } diff --git a/client/ui_base.hpp b/client/ui_base.hpp index 95779d5..94019db 100644 --- a/client/ui_base.hpp +++ b/client/ui_base.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #ifdef INTERNAL @@ -96,7 +97,6 @@ class EXPORT UI_base : public QMainWindow * @param j The JSON command */ void on_cmd_cb(json &j); - /** * @brief Push a UI item to the internal list and setup its connections * The item have to be in the same order as from the server @@ -104,147 +104,12 @@ class EXPORT UI_base : public QMainWindow * @param item The UI item to push */ void push_ui_item(QWidget *item); - /** * @brief Parse UI items from JSON and update the UI accordingly * * @param input The JSON input containing UI items */ void UI_item_parser(json &input); - /** - * @brief Process a label UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_label(json &ui_item, QWidget *widget); - /** - * @brief Process a slider UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_slider(json &ui_item, QWidget *widget); - /** - * @brief Process a dial UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_dial(json &ui_item, QWidget *widget); - /** - * @brief Process a thermo UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_thermo(json &ui_item, QWidget *widget); - /** - * @brief Process a textbox UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_textbox(json &ui_item, QWidget *widget); - /** - * @brief Process a combobox UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_combobox(json &ui_item, QWidget *widget); - /** - * @brief Process a radiobutton UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_radiobutton(json &ui_item, QWidget *widget); - /** - * @brief Process a checkbox UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_checkbox(json &ui_item, QWidget *widget); - /** - * @brief Process a button UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_button(json &ui_item, QWidget *widget); - /** - * @brief Process a LED UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_led(json &ui_item, QWidget *widget); - /** - * @brief Process a spinbox UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_spinbox(json &ui_item, QWidget *widget); - /** - * @brief Process a double spinbox UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_double_spinbox(json &ui_item, QWidget *widget); - /** - * @brief Process a hex spinbox UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_hex_spinbox(json &ui_item, QWidget *widget); - /** - * @brief Process a tab widget UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_tab_widget(json &ui_item, QWidget *widget); - /** - * @brief Process a stacked widget UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_stacked_widget(json &ui_item, QWidget *widget); - /** - * @brief Process a status bar UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_status_bar(json &ui_item, QWidget *widget); - /** - * @brief Process a plot UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_plot(json &ui_item, QWidget *widget); - /** - * @brief Process a table UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_table(json &ui_item, QWidget *widget); - /** - * @brief Process a CAN UI item - * - * @param ui_item The JSON UI item - * @param widget The associated widget - */ - void process_ui_can(json &ui_item, QWidget *widget); - /** * @brief Setup a generic UI item * @@ -357,4 +222,5 @@ class EXPORT UI_base : public QMainWindow bool m_open = false; bool m_first_load = true; std::vector m_ui_lookup; + QMap> m_process_lookup; };