From: Shane Jaroch Date: Wed, 21 Jan 2026 20:10:30 +0000 (-0500) Subject: keep working on ui X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=98e0feb8820fc7bbfa7c612939507d4386b80dae;p=nutratech%2Fgui.git keep working on ui --- diff --git a/include/widgets/dailylogwidget.h b/include/widgets/dailylogwidget.h index 98a0b34..3d10eb7 100644 --- a/include/widgets/dailylogwidget.h +++ b/include/widgets/dailylogwidget.h @@ -2,6 +2,10 @@ #define DAILYLOGWIDGET_H #include +#include +#include +#include +#include #include #include #include @@ -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 diff --git a/src/widgets/dailylogwidget.cpp b/src/widgets/dailylogwidget.cpp index 978d11e..40b0cc5 100644 --- a/src/widgets/dailylogwidget.cpp +++ b/src/widgets/dailylogwidget.cpp @@ -1,8 +1,11 @@ #include "widgets/dailylogwidget.h" #include +#include #include #include +#include +#include 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::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 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((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() {