-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "generatelistwindow.h" | ||
#include <QMessageBox> | ||
|
||
GenerateGroceryListWindow::GenerateGroceryListWindow(QWidget *parent) | ||
: QDialog(parent) { | ||
setupUI(); | ||
} | ||
|
||
GenerateGroceryListWindow::~GenerateGroceryListWindow() {} | ||
|
||
void GenerateGroceryListWindow::setupUI() { | ||
setWindowTitle("Generate Grocery List"); | ||
resize(500, 400); | ||
|
||
QVBoxLayout *mainLayout = new QVBoxLayout(this); | ||
|
||
recipeScrollArea = new QScrollArea(this); | ||
QWidget *scrollAreaWidget = new QWidget(recipeScrollArea); | ||
scrollAreaLayout = new QVBoxLayout(scrollAreaWidget); | ||
recipeScrollArea->setWidget(scrollAreaWidget); | ||
recipeScrollArea->setWidgetResizable(true); | ||
|
||
generateListButton = new QPushButton("Generate List", this); | ||
connect(generateListButton, &QPushButton::clicked, this, &GenerateGroceryListWindow::generateGroceryList); | ||
|
||
mainLayout->addWidget(recipeScrollArea); | ||
mainLayout->addWidget(generateListButton); | ||
|
||
loadRecipes(); | ||
} | ||
|
||
void GenerateGroceryListWindow::loadRecipes() { | ||
std::vector<Recipe> recipes = SQLiteHelper().fetchRecipes(); | ||
|
||
for (Recipe recipe : recipes) { | ||
QWidget *recipeWidget = new QWidget(this); | ||
QHBoxLayout *recipeLayout = new QHBoxLayout(recipeWidget); | ||
|
||
QLabel *recipeLabel = new QLabel(recipe.title, recipeWidget); | ||
QSpinBox *quantitySpinBox = new QSpinBox(recipeWidget); | ||
quantitySpinBox->setRange(0, 100); | ||
|
||
recipeLayout->addWidget(recipeLabel); | ||
recipeLayout->addWidget(quantitySpinBox); | ||
|
||
scrollAreaLayout->addWidget(recipeWidget); | ||
|
||
recipeSpinBoxes[recipe.id] = quantitySpinBox; | ||
} | ||
} | ||
|
||
void GenerateGroceryListWindow::generateGroceryList() { | ||
QMap<QString, float> aggregatedIngredients; | ||
|
||
for (auto it = recipeSpinBoxes.begin(); it != recipeSpinBoxes.end(); it++) { | ||
int recipeId = it.key(); | ||
int quantity = it.value()->value(); | ||
|
||
if (quantity > 0) { | ||
std::vector<std::tuple<Ingredient, float, Unit>> recipeIngredients = | ||
SQLiteHelper().fetchRecipeIngredients(recipeId); | ||
|
||
for (auto &[ingredient, measure, unit] : recipeIngredients){ | ||
QString key = QString("%1 (%2)").arg(ingredient.name).arg(unit.name); | ||
aggregatedIngredients[key] += measure * quantity; | ||
} | ||
} | ||
} | ||
|
||
QStringList output; | ||
for (auto it = aggregatedIngredients.begin(); it != aggregatedIngredients.end(); it++){ | ||
output.append(QString("%1: %2").arg(it.key()).arg(it.value())); | ||
} | ||
|
||
QMessageBox::information(this, "Grocery List", output.join("\n")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "SQLiteHelper.h" | ||
#include <QDialog> | ||
#include <QLabel> | ||
#include <QLineEdit> | ||
#include <QListWidget> | ||
#include <QMainWindow> | ||
#include <QMap> | ||
#include <QPushButton> | ||
#include <QScrollArea> | ||
#include <QSpinBox> | ||
#include <QTextEdit> | ||
#include <QVBoxLayout> | ||
#include <QWidget> | ||
#include <vector> | ||
|
||
class GenerateGroceryListWindow : public QDialog { | ||
Q_OBJECT | ||
|
||
public: | ||
GenerateGroceryListWindow(QWidget *parent = nullptr); | ||
~GenerateGroceryListWindow(); | ||
|
||
private: | ||
QScrollArea *recipeScrollArea; | ||
QVBoxLayout *scrollAreaLayout; | ||
QPushButton *generateListButton; | ||
QMap<int, QSpinBox *> recipeSpinBoxes; | ||
|
||
void loadRecipes(); | ||
void generateGroceryList(); | ||
void setupUI(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters