]> Nutra Git (v2) - nutratech/gui.git/commitdiff
keep working on ui
authorShane Jaroch <chown_tee@proton.me>
Wed, 21 Jan 2026 20:10:30 +0000 (15:10 -0500)
committerShane Jaroch <chown_tee@proton.me>
Wed, 21 Jan 2026 20:10:30 +0000 (15:10 -0500)
include/widgets/dailylogwidget.h
src/widgets/dailylogwidget.cpp

index 98a0b34a91411b9b4b9ab557426075165dd3e563..3d10eb7ada2e24fe61da615fe6849c1b348ccd07 100644 (file)
@@ -2,6 +2,10 @@
 #define DAILYLOGWIDGET_H
 
 #include <QDate>
+#include <QGroupBox>
+#include <QProgressBar>
+#include <QPushButton>
+#include <QSpinBox>
 #include <QTableWidget>
 #include <QVBoxLayout>
 #include <QWidget>
@@ -23,8 +27,22 @@ private:
     void updateTable();
 
     QTableWidget* logTable;
+
+    // Analysis UI
+    QGroupBox* analysisBox;
+    QVBoxLayout* analysisLayout;
+    QProgressBar* kcalBar;
+    QProgressBar* proteinBar;
+    QProgressBar* carbsBar;
+    QProgressBar* fatBar;
+    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 978d11e54346ffe47791832e0f1ccdd6bbc8fe47..40b0cc5f44f13e3d6e3ebd7ddb59a79ae349413d 100644 (file)
@@ -1,8 +1,11 @@
 #include "widgets/dailylogwidget.h"
 
 #include <QDebug>
+#include <QGroupBox>
 #include <QHeaderView>
 #include <QLabel>
+#include <QProgressBar>
+#include <QSplitter>
 
 DailyLogWidget::DailyLogWidget(QWidget* parent) : QWidget(parent) {
     setupUi();
@@ -10,20 +13,155 @@ DailyLogWidget::DailyLogWidget(QWidget* parent) : QWidget(parent) {
 }
 
 void DailyLogWidget::setupUi() {
-    auto* layout = new QVBoxLayout(this);
+    auto* mainLayout = new QVBoxLayout(this);
 
-    layout->addWidget(new QLabel("Today's Food Log", this));
+    QSplitter* splitter = new QSplitter(Qt::Vertical, this);
+    mainLayout->addWidget(splitter);
+
+    // --- Top: Log Table ---
+    QWidget* topWidget = new QWidget(this);
+    auto* topLayout = new QVBoxLayout(topWidget);
+    topLayout->setContentsMargins(0, 0, 0, 0);
+    topLayout->addWidget(new QLabel("Today's Food Log", this));
 
     logTable = new QTableWidget(this);
     logTable->setColumnCount(4);
     logTable->setHorizontalHeaderLabels({"Meal", "Food", "Amount", "Calories"});
     logTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
+    topLayout->addWidget(logTable);
+
+    splitter->addWidget(topWidget);
+
+    // --- Bottom: Analysis ---
+    QWidget* bottomWidget = new QWidget(this);
+    auto* bottomLayout = new QVBoxLayout(bottomWidget);
+    bottomLayout->setContentsMargins(0, 0, 0, 0);
+
+    QGroupBox* 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));
+
+    scaleInput = new QSpinBox(this);
+    scaleInput->setRange(0, 50000);
+    scaleInput->setValue(2000);  // Default Goal
+    scaleLayout->addWidget(scaleInput);
+
+    scaleLayout->addWidget(new QLabel("kcal", this));
+
+    // Add spacer
+    scaleLayout->addStretch();
+
+    analysisLayout->addLayout(scaleLayout);
 
-    layout->addWidget(logTable);
+    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
+
+    bottomLayout->addWidget(analysisBox);
+    splitter->addWidget(bottomWidget);
+
+    // Set initial sizes
+    splitter->setStretchFactor(0, 3);
+    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();
+}
+
+void DailyLogWidget::updateAnalysis() {
+    std::map<int, double> totals;  // id -> amount
+    auto logs = m_mealRepo.getDailyLogs(QDate::currentDate());
+
+    for (const auto& log : logs) {
+        auto nutrients = m_foodRepo.getFoodNutrients(log.foodId);
+        double scale = log.grams / 100.0;
+        for (const auto& nut : nutrients) {
+            totals[nut.id] += nut.amount * scale;
+        }
+    }
+
+    // Hardcoded RDAs for now (TODO: Fetch from FoodRepository/User Profile)
+    double goalKcal = scaleInput->value();  // Projection Target
+
+    // Calculate Multiplier
+    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."
+
+    double rdaKcal = goalKcal;  // The goal IS the RDA in this context usually
+    double rdaProtein = 150;
+    double rdaCarbs = 300;
+    double rdaFat = 80;
+
+    auto updateBar = [&](QProgressBar* bar, int nutrId, double rda) {
+        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 {
+            // Reset style (hacky, ideally use separate stylesheet)
+            // bar->setStyleSheet("");
+        }
+    };
+
+    updateBar(kcalBar, 208, rdaKcal);
+    updateBar(proteinBar, 203, rdaProtein);
+    updateBar(carbsBar, 205, rdaCarbs);
+    updateBar(fatBar, 204, rdaFat);
 }
 
 void DailyLogWidget::updateTable() {