From: Shane Jaroch Date: Wed, 21 Jan 2026 21:02:29 +0000 (-0500) Subject: init preferences ui X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=ec99ee72bf1d1ee480cc666b8e89509a45546423;p=nutratech%2Fgui.git init preferences ui --- diff --git a/include/widgets/preferencesdialog.h b/include/widgets/preferencesdialog.h new file mode 100644 index 0000000..47921f0 --- /dev/null +++ b/include/widgets/preferencesdialog.h @@ -0,0 +1,35 @@ +#ifndef PREFERENCESDIALOG_H +#define PREFERENCESDIALOG_H + +#include + +class QLabel; +class QTabWidget; + +class PreferencesDialog : public QDialog { + Q_OBJECT + +public: + explicit PreferencesDialog(QWidget* parent = nullptr); + +private: + void setupUi(); + void loadStatistics(); + QString formatBytes(qint64 bytes) const; + + QTabWidget* tabWidget; + + // Stats labels + QLabel* lblFoodLogs; + QLabel* lblCustomFoods; + QLabel* lblRdaOverrides; + QLabel* lblRecipes; + QLabel* lblSnapshots; + + // Size labels + QLabel* lblUsdaSize; + QLabel* lblUserSize; + QLabel* lblBackupSize; +}; + +#endif // PREFERENCESDIALOG_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c326108..98205bb 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -13,6 +13,7 @@ #include #include "db/databasemanager.h" +#include "widgets/preferencesdialog.h" #include "widgets/rdasettingswidget.h" MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { @@ -51,8 +52,11 @@ void MainWindow::setupUi() { dlg.exec(); }); - QAction* settingsAction = editMenu->addAction("Settings"); - connect(settingsAction, &QAction::triggered, this, &MainWindow::onSettings); + QAction* preferencesAction = editMenu->addAction("Preferences"); + connect(preferencesAction, &QAction::triggered, this, [this]() { + PreferencesDialog dlg(this); + dlg.exec(); + }); // Help Menu auto* helpMenu = menuBar()->addMenu("&Help"); diff --git a/src/widgets/preferencesdialog.cpp b/src/widgets/preferencesdialog.cpp new file mode 100644 index 0000000..10e56db --- /dev/null +++ b/src/widgets/preferencesdialog.cpp @@ -0,0 +1,139 @@ +#include "widgets/preferencesdialog.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "db/databasemanager.h" + +PreferencesDialog::PreferencesDialog(QWidget* parent) : QDialog(parent) { + setWindowTitle("Preferences"); + setMinimumSize(450, 400); + setupUi(); + loadStatistics(); +} + +void PreferencesDialog::setupUi() { + auto* mainLayout = new QVBoxLayout(this); + + tabWidget = new QTabWidget(this); + + // === Usage Statistics Tab === + auto* statsWidget = new QWidget(); + auto* statsLayout = new QVBoxLayout(statsWidget); + + // --- Counts Group --- + auto* countsGroup = new QGroupBox("Data Counts"); + auto* countsLayout = new QFormLayout(countsGroup); + + lblFoodLogs = new QLabel("--"); + lblCustomFoods = new QLabel("--"); + lblRdaOverrides = new QLabel("--"); + lblRecipes = new QLabel("--"); + lblSnapshots = new QLabel("--"); + + countsLayout->addRow("Food Log Entries:", lblFoodLogs); + countsLayout->addRow("Custom Foods:", lblCustomFoods); + countsLayout->addRow("RDA Overrides:", lblRdaOverrides); + countsLayout->addRow("Recipes:", lblRecipes); + countsLayout->addRow("Snapshots/Backups:", lblSnapshots); + + // --- Sizes Group --- + auto* sizesGroup = new QGroupBox("Database Sizes"); + auto* sizesLayout = new QFormLayout(sizesGroup); + + lblUsdaSize = new QLabel("--"); + lblUserSize = new QLabel("--"); + lblBackupSize = new QLabel("--"); + + sizesLayout->addRow("USDA Database:", lblUsdaSize); + sizesLayout->addRow("User Database:", lblUserSize); + sizesLayout->addRow("Total Backup Size:", lblBackupSize); + + // --- Disclaimer --- + auto* disclaimerLabel = new QLabel( + "Note: The USDA database contains public domain data and is " + "read-only. Only your personal user data (logs, custom foods, " + "RDAs, recipes) is backed up.

" + "You are encouraged to periodically upload your snapshots to an " + "online drive, archive them in a Git repository, or save them to a " + "backup USB stick or external HDD."); + disclaimerLabel->setWordWrap(true); + disclaimerLabel->setStyleSheet("color: #666; padding: 10px;"); + + statsLayout->addWidget(countsGroup); + statsLayout->addWidget(sizesGroup); + statsLayout->addWidget(disclaimerLabel); + statsLayout->addStretch(); + + tabWidget->addTab(statsWidget, "Usage Statistics"); + + mainLayout->addWidget(tabWidget); +} + +void PreferencesDialog::loadStatistics() { + QSqlDatabase userDb = DatabaseManager::instance().userDatabase(); + QSqlDatabase usdaDb = DatabaseManager::instance().database(); + + // --- Counts --- + if (userDb.isOpen()) { + QSqlQuery q(userDb); + + q.exec("SELECT COUNT(*) FROM log_food"); + if (q.next()) lblFoodLogs->setText(QString::number(q.value(0).toInt())); + + q.exec("SELECT COUNT(*) FROM custom_food"); + if (q.next()) lblCustomFoods->setText(QString::number(q.value(0).toInt())); + + q.exec("SELECT COUNT(*) FROM rda"); + if (q.next()) lblRdaOverrides->setText(QString::number(q.value(0).toInt())); + } + + // --- Recipe Count (from filesystem) --- + QString recipePath = QDir::homePath() + "/.nutra/recipe"; + QDir recipeDir(recipePath); + int recipeCount = 0; + if (recipeDir.exists()) { + recipeCount = recipeDir.entryList(QDir::Files).count(); + } + lblRecipes->setText(QString::number(recipeCount)); + + // --- Snapshot Count and Size --- + QString backupPath = QDir::homePath() + "/.nutra/backups"; + QDir backupDir(backupPath); + int snapshotCount = 0; + qint64 totalBackupSize = 0; + if (backupDir.exists()) { + QFileInfoList files = backupDir.entryInfoList({"*.sql.gz"}, QDir::Files); + snapshotCount = files.count(); + for (const auto& fi : files) { + totalBackupSize += fi.size(); + } + } + lblSnapshots->setText(QString::number(snapshotCount)); + lblBackupSize->setText(formatBytes(totalBackupSize)); + + // --- Database Sizes --- + if (usdaDb.isOpen()) { + QFileInfo usdaInfo(usdaDb.databaseName()); + lblUsdaSize->setText(formatBytes(usdaInfo.size())); + } + + if (userDb.isOpen()) { + QFileInfo userInfo(userDb.databaseName()); + lblUserSize->setText(formatBytes(userInfo.size())); + } +} + +QString PreferencesDialog::formatBytes(qint64 bytes) const { + if (bytes < 1024) return QString("%1 B").arg(bytes); + if (bytes < 1024 * 1024) return QString("%1 KB").arg(bytes / 1024.0, 0, 'f', 1); + if (bytes < 1024 * 1024 * 1024) + return QString("%1 MB").arg(bytes / (1024.0 * 1024.0), 0, 'f', 2); + return QString("%1 GB").arg(bytes / (1024.0 * 1024.0 * 1024.0), 0, 'f', 2); +}