From 9065df521a499c76b49db36113636f9978556a72 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 30 Mar 2018 12:56:42 +0200 Subject: [PATCH] Made adding ingredients, tags more secure with extra checks. --- gui/newDialogs/newingredientdialog.cpp | 17 ++++++++++- gui/newDialogs/newingredientdialog.h | 5 ++++ gui/newDialogs/newingredientdialog.ui | 41 ++++++++++++++++++++++++-- gui/newrecipedialog.cpp | 28 +++++++++++------- 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/gui/newDialogs/newingredientdialog.cpp b/gui/newDialogs/newingredientdialog.cpp index 5b7597a..c7f3554 100644 --- a/gui/newDialogs/newingredientdialog.cpp +++ b/gui/newDialogs/newingredientdialog.cpp @@ -8,11 +8,26 @@ NewIngredientDialog::NewIngredientDialog(QWidget *parent) : ui->setupUi(this); } +NewIngredientDialog::NewIngredientDialog(RecipeDatabase *recipeDB, QWidget *parent) : NewIngredientDialog(parent){ + this->recipeDB = recipeDB; + this->populateFoodGroupBox(); +} + NewIngredientDialog::~NewIngredientDialog() { delete ui; } Ingredient NewIngredientDialog::getIngredient(){ - return Ingredient(ui->nameEdit->text().toLower().toStdString(), ui->foodGroupEdit->text().toLower().toStdString()); + return Ingredient(ui->nameEdit->text().toLower().toStdString(), ui->foodGroupBox->currentText().toStdString()); +} + +void NewIngredientDialog::populateFoodGroupBox(){ + vector foodGroups = this->recipeDB->retrieveAllFoodGroups(); + printf("Found %ld food Groups.\n", foodGroups.size()); + ui->foodGroupBox->clear(); + for (unsigned int i = 0; i < foodGroups.size(); i++){ + QString s = QString::fromStdString(foodGroups[i]); + ui->foodGroupBox->insertItem(i, s); + } } diff --git a/gui/newDialogs/newingredientdialog.h b/gui/newDialogs/newingredientdialog.h index 4c94275..d9ad2b3 100644 --- a/gui/newDialogs/newingredientdialog.h +++ b/gui/newDialogs/newingredientdialog.h @@ -3,6 +3,7 @@ #include #include "model/recipe/ingredients/ingredient.h" +#include "model/database/recipedatabase.h" namespace Ui { class NewIngredientDialog; @@ -14,6 +15,7 @@ class NewIngredientDialog : public QDialog public: explicit NewIngredientDialog(QWidget *parent = 0); + NewIngredientDialog(RecipeDatabase *recipeDB, QWidget *parent = 0); ~NewIngredientDialog(); //Access values. @@ -21,6 +23,9 @@ class NewIngredientDialog : public QDialog private: Ui::NewIngredientDialog *ui; + RecipeDatabase *recipeDB; + + void populateFoodGroupBox(); }; #endif // NEWINGREDIENTDIALOG_H diff --git a/gui/newDialogs/newingredientdialog.ui b/gui/newDialogs/newingredientdialog.ui index 8f4f268..b562b1f 100644 --- a/gui/newDialogs/newingredientdialog.ui +++ b/gui/newDialogs/newingredientdialog.ui @@ -6,8 +6,8 @@ 0 0 - 240 - 320 + 367 + 228 @@ -60,7 +60,42 @@ - + + + + + + + 0 + 0 + + + + + + + + + + + + :/images/images/plus_icon.png:/images/images/plus_icon.png + + + + + + + + + + + :/images/images/minus_icon.png:/images/images/minus_icon.png + + + + + diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index f35b50d..47d6847 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -145,13 +145,17 @@ void NewRecipeDialog::on_deleteIngredientButton_clicked(){ } void NewRecipeDialog::on_newIngredientButton_clicked(){ - NewIngredientDialog d(this); + NewIngredientDialog d(this->recipeDB, this); d.show(); if (d.exec() == QDialog::Accepted){ Ingredient i = d.getIngredient(); - this->recipeDB->storeIngredient(i); - this->populateIngredientsBox(); - ui->ingredientNameBox->setCurrentText(QString::fromStdString(i.getName())); + if (!i.getName().empty() && !i.getFoodGroup().empty() && this->recipeDB->storeIngredient(i)){ + this->populateIngredientsBox(); + ui->ingredientNameBox->setCurrentText(QString::fromStdString(i.getName())); + } else { + QMessageBox::critical(this, "Error", "Unable to add ingredient."); + } + } } @@ -161,12 +165,16 @@ void NewRecipeDialog::on_newTagButton_clicked(){ if (d.exec() == QDialog::Accepted){ RecipeTag tag = d.getTag(); //Temporarily add this to the tags list, and it will be saved if the recipe is saved. - this->tags.push_back(tag); - this->tagsListModel.addTag(tag); - ui->tagsComboBox->clear(); - for (unsigned int i = 0; i < this->tags.size(); i++){ - QString s = QString::fromStdString(this->tags[i].getValue()); - ui->tagsComboBox->insertItem(i, s); + if (!tag.getValue().empty()){ + this->tags.push_back(tag); + this->tagsListModel.addTag(tag); + ui->tagsComboBox->clear(); + for (unsigned int i = 0; i < this->tags.size(); i++){ + QString s = QString::fromStdString(this->tags[i].getValue()); + ui->tagsComboBox->insertItem(i, s); + } + } else { + QMessageBox::warning(this, "Empty Tag", "The tag you entered is blank!"); } }