From: Shane Jaroch Date: Mon, 26 Jan 2026 06:27:17 +0000 (-0500) Subject: add color to details UI X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=489214edf1450bda80e63331ea6d8e0ced5d6947;p=nutratech%2Fgui.git add color to details UI --- diff --git a/src/widgets/detailswidget.cpp b/src/widgets/detailswidget.cpp index d3041df..6a25834 100644 --- a/src/widgets/detailswidget.cpp +++ b/src/widgets/detailswidget.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include DetailsWidget::DetailsWidget(QWidget* parent) : QWidget(parent), currentFoodId(-1) { @@ -28,9 +29,10 @@ DetailsWidget::DetailsWidget(QWidget* parent) : QWidget(parent), currentFoodId(- // Nutrients Table nutrientsTable = new QTableWidget(this); nutrientsTable->setColumnCount(3); - nutrientsTable->setHorizontalHeaderLabels({"Nutrient", "Amount", "Unit"}); - nutrientsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + nutrientsTable->setHorizontalHeaderLabels({"Nutrient", "Progress", "Detail"}); + nutrientsTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); nutrientsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); + nutrientsTable->setSelectionMode(QAbstractItemView::NoSelection); layout->addWidget(nutrientsTable); } @@ -43,13 +45,55 @@ void DetailsWidget::loadFood(int foodId, const QString& foodName) { nutrientsTable->setRowCount(0); std::vector nutrients = repository.getFoodNutrients(foodId); + auto rdas = repository.getNutrientRdas(); + + // Mapping for easy lookup if needed, but vector iteration is fine + // We want to show ALL nutrients returned for the food? Or all nutrients tracked by RDA? + // The previous implementation showed all nutrients returned by getFoodNutrients. + // Let's stick to that, but enhance the ones that have RDAs. nutrientsTable->setRowCount(static_cast(nutrients.size())); for (int i = 0; i < static_cast(nutrients.size()); ++i) { const auto& nut = nutrients[i]; nutrientsTable->setItem(i, 0, new QTableWidgetItem(nut.description)); - nutrientsTable->setItem(i, 1, new QTableWidgetItem(QString::number(nut.amount))); - nutrientsTable->setItem(i, 2, new QTableWidgetItem(nut.unit)); + + double rda = 0; + if (rdas.count(nut.id) != 0U) { + rda = rdas[nut.id]; + } + + // Progress Bar + auto* bar = new QProgressBar(); + bar->setRange(0, 100); + int pct = 0; + if (rda > 0) pct = static_cast((nut.amount / rda) * 100.0); + bar->setValue(std::min(pct, 100)); + bar->setTextVisible(true); + bar->setFormat(QString("%1%").arg(pct)); + + // Color logic + QString color = "#bdc3c7"; // Grey if no RDA + if (rda > 0) { + color = "#3498db"; // Blue + if (pct < 50) + color = "#f1c40f"; // Yellow + else if (pct > 150) + color = "#8e44ad"; // Purple + else if (pct >= 100) + color = "#2ecc71"; // Green + } + bar->setStyleSheet(QString("QProgressBar::chunk { background-color: %1; }").arg(color)); + nutrientsTable->setCellWidget(i, 1, bar); + + // Detail + QString detail; + if (rda > 0) { + detail = + QString("%1 / %2 %3").arg(nut.amount, 0, 'f', 1).arg(rda, 0, 'f', 1).arg(nut.unit); + } else { + detail = QString("%1 %2").arg(nut.amount, 0, 'f', 1).arg(nut.unit); + } + nutrientsTable->setItem(i, 2, new QTableWidgetItem(detail)); } }