From: Shane Jaroch Date: Mon, 26 Jan 2026 07:16:07 +0000 (-0500) Subject: support multiple days in food log (switching) X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=206a722fa8c6be17634c511b95e9b35133db23d3;p=nutratech%2Fgui.git support multiple days in food log (switching) --- diff --git a/include/widgets/dailylogwidget.h b/include/widgets/dailylogwidget.h index 3204e95..24fc6a0 100644 --- a/include/widgets/dailylogwidget.h +++ b/include/widgets/dailylogwidget.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -21,6 +22,10 @@ public: public slots: void refresh(); + void prevDay(); + void nextDay(); + void setToday(); + void onDateChanged(); private: void setupUi(); @@ -34,6 +39,10 @@ private: QTableWidget* analysisTable; QSpinBox* scaleInput; + // Date Nav + QDate currentDate; + QLabel* dateLabel; + MealRepository m_mealRepo; FoodRepository m_foodRepo; diff --git a/src/widgets/dailylogwidget.cpp b/src/widgets/dailylogwidget.cpp index 7e20c12..c061148 100644 --- a/src/widgets/dailylogwidget.cpp +++ b/src/widgets/dailylogwidget.cpp @@ -9,7 +9,7 @@ DailyLogWidget::DailyLogWidget(QWidget* parent) : QWidget(parent) { setupUi(); - refresh(); + setToday(); } void DailyLogWidget::setupUi() { @@ -18,11 +18,43 @@ void DailyLogWidget::setupUi() { auto* splitter = new QSplitter(Qt::Vertical, this); mainLayout->addWidget(splitter); + // --- Top: Log Table --- // --- Top: Log Table --- auto* topWidget = new QWidget(this); auto* topLayout = new QVBoxLayout(topWidget); topLayout->setContentsMargins(0, 0, 0, 0); - topLayout->addWidget(new QLabel("Today's Food Log", this)); + + // Date Navigation Header + auto* navLayout = new QHBoxLayout(); + auto* prevBtn = new QPushButton("<", this); + prevBtn->setFixedWidth(30); + + dateLabel = new QLabel(this); + dateLabel->setAlignment(Qt::AlignCenter); + QFont font = dateLabel->font(); + font.setBold(true); + font.setPointSize(12); + dateLabel->setFont(font); + + auto* nextBtn = new QPushButton(">", this); + nextBtn->setFixedWidth(30); + + auto* todayBtn = new QPushButton("Today", this); + + navLayout->addWidget(prevBtn); + navLayout->addStretch(); + navLayout->addWidget(dateLabel); + navLayout->addStretch(); + navLayout->addWidget(nextBtn); + navLayout->addWidget(todayBtn); + + topLayout->addLayout(navLayout); + + connect(prevBtn, &QPushButton::clicked, this, &DailyLogWidget::prevDay); + connect(nextBtn, &QPushButton::clicked, this, &DailyLogWidget::nextDay); + connect(todayBtn, &QPushButton::clicked, this, &DailyLogWidget::setToday); + + topLayout->addWidget(new QLabel("Food Log", this)); logTable = new QTableWidget(this); logTable->setColumnCount(4); @@ -74,13 +106,39 @@ void DailyLogWidget::setupUi() { } void DailyLogWidget::refresh() { + onDateChanged(); +} + +void DailyLogWidget::prevDay() { + currentDate = currentDate.addDays(-1); + onDateChanged(); +} + +void DailyLogWidget::nextDay() { + currentDate = currentDate.addDays(1); + onDateChanged(); +} + +void DailyLogWidget::setToday() { + currentDate = QDate::currentDate(); + onDateChanged(); +} + +void DailyLogWidget::onDateChanged() { + // Update Label + QString text = currentDate.toString("ddd, MMM d, yyyy"); + if (currentDate == QDate::currentDate()) { + text += " (Today)"; + } + dateLabel->setText(text); + updateTable(); updateAnalysis(); } void DailyLogWidget::updateAnalysis() { std::map totals; // id -> amount - auto logs = m_mealRepo.getDailyLogs(QDate::currentDate()); + auto logs = m_mealRepo.getDailyLogs(currentDate); for (const auto& log : logs) { auto nutrients = m_foodRepo.getFoodNutrients(log.foodId); @@ -148,8 +206,8 @@ void DailyLogWidget::updateAnalysis() { void DailyLogWidget::updateTable() { logTable->setRowCount(0); - // Get logs for today - auto logs = m_mealRepo.getDailyLogs(QDate::currentDate()); + // Get logs for selected date + auto logs = m_mealRepo.getDailyLogs(currentDate); for (const auto& log : logs) { int row = logTable->rowCount();