]> Nutra Git (v2) - nutratech/gui.git/commitdiff
wip add some features
authorShane Jaroch <chown_tee@proton.me>
Mon, 26 Jan 2026 05:23:36 +0000 (00:23 -0500)
committerShane Jaroch <chown_tee@proton.me>
Mon, 26 Jan 2026 05:23:36 +0000 (00:23 -0500)
include/db/foodrepository.h
include/widgets/dailylogwidget.h
src/db/foodrepository.cpp
src/widgets/dailylogwidget.cpp

index 3a27f47a5e76ad0e657c77d9276c47564f3d5290..eeed9374db78b8c96a56e5eea67389ad8d50b4b1 100644 (file)
@@ -48,8 +48,9 @@ public:
     std::map<int, double> getNutrientRdas();
     void updateRda(int nutrId, double value);
 
-    // Helper to get nutrient definition basics if needed
-    // QString getNutrientName(int nutrientId);
+    // Helper to get nutrient definition basics
+    QString getNutrientName(int nutrientId);
+    QString getNutrientUnit(int nutrientId);
 
 private:
     // Internal helper methods
@@ -60,6 +61,8 @@ private:
     // Cache stores basic food info
     std::vector<FoodItem> m_cache;
     std::map<int, double> m_rdas;
+    std::map<int, QString> m_nutrientNames;
+    std::map<int, QString> m_nutrientUnits;
 };
 
 #endif  // FOODREPOSITORY_H
index 3d10eb7ada2e24fe61da615fe6849c1b348ccd07..3204e95a16df8ee12e139a2681ac99ae3666698b 100644 (file)
@@ -31,18 +31,13 @@ private:
     // Analysis UI
     QGroupBox* analysisBox;
     QVBoxLayout* analysisLayout;
-    QProgressBar* kcalBar;
-    QProgressBar* proteinBar;
-    QProgressBar* carbsBar;
-    QProgressBar* fatBar;
+    QTableWidget* analysisTable;
     QSpinBox* scaleInput;
 
     MealRepository m_mealRepo;
     FoodRepository m_foodRepo;
 
     void updateAnalysis();
-    void createProgressBar(QVBoxLayout* layout, const QString& label, QProgressBar*& bar,
-                           const QString& color);
 };
 
 #endif  // DAILYLOGWIDGET_H
index 646d19587a4c19b76ddb56a4f47fc3d7a7bc3b88..93ab8b5caf7ed7ac74e3eb7e22fe5c9ca1407a8e 100644 (file)
@@ -36,6 +36,16 @@ void FoodRepository::ensureCacheLoaded() {
         nutrientCounts[countQuery.value(0).toInt()] = countQuery.value(1).toInt();
     }
 
+    // 3. Load Nutrient Definition Metadata
+    m_nutrientNames.clear();
+    m_nutrientUnits.clear();
+    QSqlQuery defQuery("SELECT id, nutr_desc, unit FROM nutr_def", db);
+    while (defQuery.next()) {
+        int id = defQuery.value(0).toInt();
+        m_nutrientNames[id] = defQuery.value(1).toString();
+        m_nutrientUnits[id] = defQuery.value(2).toString();
+    }
+
     while (query.next()) {
         FoodItem item;
         item.id = query.value(0).toInt();
@@ -263,3 +273,19 @@ void FoodRepository::updateRda(int nutrId, double value) {
         qCritical() << "Failed to update RDA:" << query.lastError().text();
     }
 }
+
+QString FoodRepository::getNutrientName(int nutrientId) {
+    ensureCacheLoaded();
+    if (m_nutrientNames.count(nutrientId) != 0U) {
+        return m_nutrientNames[nutrientId];
+    }
+    return QString("Unknown Nutrient (%1)").arg(nutrientId);
+}
+
+QString FoodRepository::getNutrientUnit(int nutrientId) {
+    ensureCacheLoaded();
+    if (m_nutrientUnits.count(nutrientId) != 0U) {
+        return m_nutrientUnits[nutrientId];
+    }
+    return "?";
+}
index e91ac80e8e5f3bb414cb50bbb125ebab0f1d0624..7e20c12c89235543c8f64997242f46b3423ceadb 100644 (file)
@@ -40,12 +40,6 @@ void DailyLogWidget::setupUi() {
     auto* analysisBox = new QGroupBox("Analysis (Projected)", this);
     auto* analysisLayout = new QVBoxLayout(analysisBox);
 
-    // Analysis UI
-    kcalBar = nullptr;
-    proteinBar = nullptr;
-    carbsBar = nullptr;
-    fatBar = nullptr;
-
     // Scale Controls
     auto* scaleLayout = new QHBoxLayout();
     scaleLayout->addWidget(new QLabel("Project to Goal:", this));
@@ -56,19 +50,20 @@ void DailyLogWidget::setupUi() {
     scaleLayout->addWidget(scaleInput);
 
     scaleLayout->addWidget(new QLabel("kcal", this));
-
-    // Add spacer
     scaleLayout->addStretch();
-
     analysisLayout->addLayout(scaleLayout);
 
     connect(scaleInput, QOverload<int>::of(&QSpinBox::valueChanged), this,
             &DailyLogWidget::updateAnalysis);
 
-    createProgressBar(analysisLayout, "Calories", kcalBar, "#3498db");    // Blue
-    createProgressBar(analysisLayout, "Protein", proteinBar, "#e74c3c");  // Red
-    createProgressBar(analysisLayout, "Carbs", carbsBar, "#f1c40f");      // Yellow
-    createProgressBar(analysisLayout, "Fat", fatBar, "#2ecc71");          // Green
+    // Analysis Table
+    analysisTable = new QTableWidget(this);
+    analysisTable->setColumnCount(3);
+    analysisTable->setHorizontalHeaderLabels({"Nutrient", "Progress", "Detail"});
+    analysisTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
+    analysisTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
+    analysisTable->setSelectionMode(QAbstractItemView::NoSelection);
+    analysisLayout->addWidget(analysisTable);
 
     bottomLayout->addWidget(analysisBox);
     splitter->addWidget(bottomWidget);
@@ -78,21 +73,6 @@ void DailyLogWidget::setupUi() {
     splitter->setStretchFactor(1, 2);
 }
 
-void DailyLogWidget::createProgressBar(QVBoxLayout* layout, const QString& label,
-                                       QProgressBar*& bar, const QString& color) {
-    auto* hLayout = new QHBoxLayout();
-    hLayout->addWidget(new QLabel(label + ":"));
-
-    bar = new QProgressBar();
-    bar->setRange(0, 100);
-    bar->setValue(0);
-    bar->setTextVisible(true);
-    bar->setStyleSheet(QString("QProgressBar::chunk { background-color: %1; }").arg(color));
-
-    hLayout->addWidget(bar);
-    layout->addLayout(hLayout);
-}
-
 void DailyLogWidget::refresh() {
     updateTable();
     updateAnalysis();
@@ -110,59 +90,59 @@ void DailyLogWidget::updateAnalysis() {
         }
     }
 
-    // Hardcoded RDAs for now (TODO: Fetch from FoodRepository/User Profile)
-    double goalKcal = scaleInput->value();  // Projection Target
-
-    // Calculate Multiplier
+    double goalKcal = scaleInput->value();
     double currentKcal = totals[208];
     double multiplier = 1.0;
     if (currentKcal > 0 && goalKcal > 0) {
         multiplier = goalKcal / currentKcal;
     }
 
-    // Use scaling for "What If" visualization?
-    // Actually, progress bars usually show % of RDA.
-    // If we project, we want to show: "If I ate this ratio until I hit 2000kcal, I would have X
-    // protein."
+    analysisTable->setRowCount(0);
 
-    double rdaKcal = goalKcal;  // The goal IS the RDA in this context usually
-    double rdaProtein = 150;
-    double rdaCarbs = 300;
-    double rdaFat = 80;
+    // Iterate over defined RDAs from repository
+    auto rdas = m_foodRepo.getNutrientRdas();
+    for (const auto& [nutrId, rda] : rdas) {
+        if (rda <= 0) continue;
 
-    auto updateBar = [&](QProgressBar* bar, int nutrId, double rda, const QString& normalColor) {
         double val = totals[nutrId];
         double projectedVal = val * multiplier;
-
-        int pct = 0;
-        if (rda > 0) pct = static_cast<int>((projectedVal / rda) * 100.0);
-
-        bar->setValue(std::min(pct, 100));
-
-        // Format: "Actual (Projected) / Target"
-        QString text =
-            QString("%1 (%2) / %3 g").arg(val, 0, 'f', 0).arg(projectedVal, 0, 'f', 0).arg(rda);
-        if (nutrId == 208)
-            text = QString("%1 (%2) / %3 kcal")
-                       .arg(val, 0, 'f', 0)
-                       .arg(projectedVal, 0, 'f', 0)
-                       .arg(rda);
-
-        bar->setFormat(text);
-
-        if (pct > 100) {
-            bar->setStyleSheet("QProgressBar::chunk { background-color: #8e44ad; }");
-        } else {
-            // Restore original color
-            bar->setStyleSheet(
-                QString("QProgressBar::chunk { background-color: %1; }").arg(normalColor));
-        }
-    };
-
-    updateBar(kcalBar, 208, rdaKcal, "#3498db");
-    updateBar(proteinBar, 203, rdaProtein, "#e74c3c");
-    updateBar(carbsBar, 205, rdaCarbs, "#f1c40f");
-    updateBar(fatBar, 204, rdaFat, "#2ecc71");
+        double pct = (projectedVal / rda) * 100.0;
+        QString unit = m_foodRepo.getNutrientUnit(nutrId);
+        QString name = m_foodRepo.getNutrientName(nutrId);
+
+        int row = analysisTable->rowCount();
+        analysisTable->insertRow(row);
+
+        // 1. Nutrient Name
+        analysisTable->setItem(row, 0, new QTableWidgetItem(name));
+
+        // 2. Progress Bar
+        auto* bar = new QProgressBar();
+        bar->setRange(0, 100);
+        bar->setValue(std::min(static_cast<int>(pct), 100));
+        bar->setTextVisible(true);
+        bar->setFormat(QString("%1%").arg(pct, 0, 'f', 1));
+
+        // Coloring logic based on CLI thresholds
+        QString color = "#3498db";  // Default Blue
+        if (pct < 50)
+            color = "#f1c40f";  // Yellow (Under)
+        else if (pct > 150)
+            color = "#8e44ad";  // Purple (Over)
+        else if (pct >= 100)
+            color = "#2ecc71";  // Green (Good)
+
+        bar->setStyleSheet(QString("QProgressBar::chunk { background-color: %1; }").arg(color));
+        analysisTable->setCellWidget(row, 1, bar);
+
+        // 3. Detail Text
+        QString detail = QString("%1 (%2) / %3 %4")
+                             .arg(val, 0, 'f', 1)
+                             .arg(projectedVal, 0, 'f', 1)
+                             .arg(rda, 0, 'f', 1)
+                             .arg(unit);
+        analysisTable->setItem(row, 2, new QTableWidgetItem(detail));
+    }
 }
 
 void DailyLogWidget::updateTable() {