diff --git a/source/RobotAPI/gui-plugins/GuiHealthClient/GuiHealthClientWidgetController.cpp b/source/RobotAPI/gui-plugins/GuiHealthClient/GuiHealthClientWidgetController.cpp index d976d77e6742b63152b172b7a55107929405302d..4868ebda8519f9d571c0a62a17ccbecd66e5a18d 100644 --- a/source/RobotAPI/gui-plugins/GuiHealthClient/GuiHealthClientWidgetController.cpp +++ b/source/RobotAPI/gui-plugins/GuiHealthClient/GuiHealthClientWidgetController.cpp @@ -28,6 +28,7 @@ #include <qrgb.h> #include <qtablewidget.h> #include <string> +#include <iomanip> #include <QLabel> #include <QPushButton> @@ -110,6 +111,24 @@ namespace armarx robotHealthTopicPrx->heartbeat(getName(), now); } + + std::string to_string_rounded(float value, int decimals = 100) + { + std::stringstream ss; + ss << std::fixed << std::setprecision(3) << value; + return ss.str(); + } + + + QTableWidgetItem* make_item(const std::string& text, + Qt::AlignmentFlag horAlignment = Qt::AlignLeft) + { + auto* item = new QTableWidgetItem(QString::fromStdString(text)); + item->setTextAlignment(horAlignment | Qt::AlignVCenter); + return item; + } + + void GuiHealthClientWidgetController::updateSummaryTimerClb() { @@ -147,64 +166,63 @@ namespace armarx const auto& entry = summaryVals.at(i); std::string stateRepr; - QColor color; + QColor stateColor; switch(entry.state) { case HealthOK: stateRepr = "yes"; - color.setRgb(0, 255, 0); // green + stateColor.setRgb(0, 255, 0); // green break; case HealthWarning: stateRepr = "yes"; - color.setRgb(255, 165, 0); // orange + stateColor.setRgb(255, 165, 0); // orange break; case HealthError: stateRepr = "no"; - color.setRgb(255, 0, 0); // red + stateColor.setRgb(255, 0, 0); // red break; } const std::string hostname = entry.lastReferenceTimestamp.hostname; - const std::string timeSinceLastArrivalRepr = std::to_string(entry.timeSinceLastArrival.microSeconds / 1000); - const std::string timeToLastReferenceRepr = std::to_string(entry.timeSinceLastUpdateReference.microSeconds / 1000); + const std::string timeSinceLastArrivalRepr = to_string_rounded(entry.timeSinceLastArrival.microSeconds / 1000.0); + const std::string timeToLastReferenceRepr = to_string_rounded(entry.timeSinceLastUpdateReference.microSeconds / 1000.0); const std::string tagsRepr = serializeList(entry.tags); - const long syncErrorMilliSeconds = std::abs(entry.timeSinceLastArrival.microSeconds - entry.timeSinceLastUpdateReference.microSeconds) / 1000; - + const float syncErrorMilliSeconds = std::abs(entry.timeSinceLastArrival.microSeconds - entry.timeSinceLastUpdateReference.microSeconds) / 1000.0; tableWidget->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(entry.identifier))); - - auto* requiredItem = new QTableWidgetItem(QString::fromStdString(entry.required ? "yes" : "no")); - requiredItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); - tableWidget->setItem(i, 1, requiredItem); - - auto* stateItem = new QTableWidgetItem(QString::fromStdString(stateRepr)); - stateItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); - tableWidget->setItem(i, 2, stateItem); - tableWidget->item(i, 2)->setBackgroundColor(color); + tableWidget->setItem(i, 1, make_item(entry.required ? "yes" : "no", Qt::AlignHCenter)); + + { + auto* item = make_item(stateRepr, Qt::AlignHCenter); + item->setBackgroundColor(stateColor); + tableWidget->setItem(i, 2, item); + } tableWidget->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(tagsRepr))); - - tableWidget->setItem(i, 4, new QTableWidgetItem(QString::fromStdString(timeSinceLastArrivalRepr))); - tableWidget->setItem(i, 5, new QTableWidgetItem(QString::fromStdString(timeToLastReferenceRepr))); - - tableWidget->setItem(i, 6, new QTableWidgetItem(QString::fromStdString(std::to_string(syncErrorMilliSeconds)))); + tableWidget->setItem(i, 4, make_item(timeSinceLastArrivalRepr, Qt::AlignRight)); + tableWidget->setItem(i, 5, make_item(timeToLastReferenceRepr, Qt::AlignRight)); - if(syncErrorMilliSeconds > 20) { + auto* item = make_item(to_string_rounded(syncErrorMilliSeconds), Qt::AlignRight); + QColor timeSyncColor; - timeSyncColor.setRgb(255, 0, 0); - tableWidget->item(i, 6)->setBackgroundColor(timeSyncColor); - }else { - QColor timeSyncColor; - timeSyncColor.setRgb(0, 255, 0); - tableWidget->item(i, 6)->setBackgroundColor(timeSyncColor); + if (syncErrorMilliSeconds > 20.) + { + timeSyncColor.setRgb(255, 0, 0); + } + else + { + timeSyncColor.setRgb(0, 255, 0); + } + item->setBackgroundColor(timeSyncColor); + + tableWidget->setItem(i, 6, item); } - - tableWidget->setItem(i, 7, new QTableWidgetItem(QString::fromStdString(std::to_string(entry.maximumCycleTimeWarning.microSeconds / 1000)))); - tableWidget->setItem(i, 8, new QTableWidgetItem(QString::fromStdString(std::to_string(entry.maximumCycleTimeError.microSeconds / 1000)))); + tableWidget->setItem(i, 7, make_item(to_string_rounded(entry.maximumCycleTimeWarning.microSeconds / 1000.), Qt::AlignRight)); + tableWidget->setItem(i, 8, make_item(to_string_rounded(entry.maximumCycleTimeError.microSeconds / 1000.), Qt::AlignRight)); tableWidget->setItem(i, 9, new QTableWidgetItem(QString::fromStdString(hostname))); }