From 021ec5b8159ac60c3f41dcb0829c03207c2349ad Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:24:36 +0200 Subject: [PATCH] Removed recipe ingredient from the newrecipedialog methods. --- gui/newrecipedialog.cpp | 89 +------------------ gui/newrecipedialog.h | 18 ++-- .../recipe/ingredients/ingredientlistmodel.h | 8 +- model/recipe/recipe.cpp | 28 +++--- model/recipe/recipe.h | 11 +-- 5 files changed, 31 insertions(+), 123 deletions(-) diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index fc8657f..87afeed 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -15,7 +15,6 @@ NewRecipeDialog::NewRecipeDialog(QWidget *parent) : NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, QWidget *parent) : NewRecipeDialog(parent){ this->recipeDB = db; - this->populateIngredientsBox(); this->populateUnitsBox(); this->populateTagsBox(); @@ -55,24 +54,6 @@ bool NewRecipeDialog::isAccepted() const{ return this->accepted; } -void NewRecipeDialog::populateIngredientsBox(){ - this->ingredients = this->recipeDB->retrieveAllIngredients(); - ui->ingredientNameBox->clear(); - for (unsigned int i = 0; i < this->ingredients.size(); i++){ - QString s = QString::fromStdString(this->ingredients[i].getName()); - ui->ingredientNameBox->insertItem(i, s); - } -} - -void NewRecipeDialog::populateUnitsBox(){ - this->units = this->recipeDB->retrieveAllUnitsOfMeasure(); - ui->unitComboBox->clear(); - for (unsigned int i = 0; i < this->units.size(); i++){ - QString s = QString::fromStdString(this->units[i].getName()); - ui->unitComboBox->insertItem(i, s); - } -} - void NewRecipeDialog::populateTagsBox(){ this->tags = this->recipeDB->retrieveAllTags(); ui->tagsComboBox->clear(); @@ -83,12 +64,9 @@ void NewRecipeDialog::populateTagsBox(){ } void NewRecipeDialog::on_addIngredientButton_clicked(){ - //Construct a recipe ingredient from the supplied data. - Ingredient i = this->ingredients[ui->ingredientNameBox->currentIndex()]; - UnitOfMeasure u = this->units[ui->unitComboBox->currentIndex()]; - RecipeIngredient ri(i, ui->quantitySpinBox->value(), u, ui->commentsLineEdit->text().toStdString()); - this->ingredientListModel.addIngredient(ri); - ui->commentsLineEdit->clear(); + Ingredient ing(ui->ingredientLineEdit->text().toStdString()); + this->ingredientListModel.addIngredient(ing); + ui->ingredientLineEdit->clear(); } void NewRecipeDialog::on_italicsButton_clicked(){ @@ -113,7 +91,6 @@ void NewRecipeDialog::on_buttonBox_rejected(){ } void NewRecipeDialog::on_addTagButton_clicked(){ - //Add a tag to the list of those prepared to be added. this->tagsListModel.addTag(this->tags[ui->tagsComboBox->currentIndex()]); } @@ -140,37 +117,6 @@ void NewRecipeDialog::on_removeIngredientButton_clicked(){ } } -void NewRecipeDialog::on_deleteIngredientButton_clicked(){ - int index = ui->ingredientNameBox->currentIndex(); - Ingredient ing = this->ingredients.at(index); - QMessageBox::StandardButton reply = QMessageBox::question(this, - QString::fromStdString("Delete Ingredient"), - QString::fromStdString("Are you sure you want to delete the ingredient " + ing.getName() + "?")); - if (reply == QMessageBox::Yes){ - bool success = this->recipeDB->deleteIngredient(ing.getName()); - if (!success){ - QMessageBox::critical(this, QString::fromStdString("Error"), QString::fromStdString("Unable to delete ingredient: " + ing.getName() + ", some recipes use it!")); - } else { - this->populateIngredientsBox(); - } - } -} - -void NewRecipeDialog::on_newIngredientButton_clicked(){ - NewIngredientDialog d(this->recipeDB, this); - d.show(); - if (d.exec() == QDialog::Accepted){ - Ingredient i = d.getIngredient(); - 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."); - } - - } -} - void NewRecipeDialog::on_newTagButton_clicked(){ NewTagDialog d(this); d.show(); @@ -205,32 +151,3 @@ void NewRecipeDialog::on_removeTagButton_clicked(){ this->populateTagsBox(); } } - -void NewRecipeDialog::on_newUnitButton_clicked(){ - NewUnitDialog d(this); - d.show(); - if (d.exec() == QDialog::Accepted){ - UnitOfMeasure u = d.getUnit(); - if (u.getName().empty() || u.getNamePlural().empty() || u.getAbbreviation().empty() || !this->recipeDB->storeUnitOfMeasure(u)){ - QMessageBox::critical(this, "Error", "Unable to store new unit. Make sure all the information is filled in!"); - } else { - this->populateUnitsBox(); - ui->unitComboBox->setCurrentText(QString::fromStdString(u.getName())); - } - } -} - -void NewRecipeDialog::on_deleteUnitButton_clicked(){ - int index = ui->unitComboBox->currentIndex(); - UnitOfMeasure unit = this->units[index]; - QMessageBox::StandardButton reply = QMessageBox::question(this, - QString::fromStdString("Delete Unit Of Measure"), - QString::fromStdString("Are you sure you want to delete the unit " + unit.getName() + "?")); - if (reply == QMessageBox::Yes){ - if (!this->recipeDB->deleteUnitOfMeasure(unit.getName())){ - QMessageBox::critical(this, "Error", "Unable to delete unit. There may be recipes which still use it!"); - } else { - this->populateUnitsBox(); - } - } -} diff --git a/gui/newrecipedialog.h b/gui/newrecipedialog.h index 5198dd9..909f80c 100644 --- a/gui/newrecipedialog.h +++ b/gui/newrecipedialog.h @@ -8,7 +8,7 @@ #include #include "model/database/recipedatabase.h" -#include "model/recipe/ingredients/recipeingredientlistmodel.h" +#include "model/recipe/ingredients/ingredientlistmodel.h" #include "model/recipe/tags/taglistmodel.h" #include "gui/newDialogs/newingredientdialog.h" @@ -29,6 +29,7 @@ class NewRecipeDialog : public QDialog NewRecipeDialog(RecipeDatabase *db, Recipe recipe, QWidget *parent = 0); ~NewRecipeDialog(); + //Extracts a recipe from all the information entered in the ui. Recipe getRecipe(); bool isAccepted() const; private slots: @@ -38,8 +39,10 @@ class NewRecipeDialog : public QDialog void on_boldButton_clicked(); + //Sets the dialog as accepted, as in, the user accepts that they want to create the recipe. void on_buttonBox_accepted(); + //The user has rejected the creation of the recipe. void on_buttonBox_rejected(); void on_addTagButton_clicked(); @@ -50,31 +53,20 @@ class NewRecipeDialog : public QDialog void on_removeIngredientButton_clicked(); - void on_deleteIngredientButton_clicked(); - - void on_newIngredientButton_clicked(); - void on_newTagButton_clicked(); void on_removeTagButton_clicked(); - void on_newUnitButton_clicked(); - - void on_deleteUnitButton_clicked(); - private: Ui::NewRecipeDialog *ui; RecipeDatabase *recipeDB; vector ingredients; - vector units; vector tags; - RecipeIngredientListModel ingredientListModel; + IngredientListModel ingredientListModel; TagListModel tagsListModel; bool accepted = false; //Helper functions to fill fields. - void populateIngredientsBox(); - void populateUnitsBox(); void populateTagsBox(); }; diff --git a/model/recipe/ingredients/ingredientlistmodel.h b/model/recipe/ingredients/ingredientlistmodel.h index 32bc65d..02dbfab 100644 --- a/model/recipe/ingredients/ingredientlistmodel.h +++ b/model/recipe/ingredients/ingredientlistmodel.h @@ -7,6 +7,12 @@ #include "model/recipe/ingredients/ingredient.h" +/** + * @brief The IngredientListModel class + * + * The ingredient list model extends the QAbstractListModel and is used for lists of ingredients, + * whether they appear in the NewRecipe dialog or in the main recipe view. + */ class IngredientListModel : public QAbstractListModel { public: @@ -25,8 +31,6 @@ public: private: vector ingredients; - //Helper for printing. - }; #endif // INGREDIENTLISTMODEL_H diff --git a/model/recipe/recipe.cpp b/model/recipe/recipe.cpp index 8d123e7..f0e04d2 100644 --- a/model/recipe/recipe.cpp +++ b/model/recipe/recipe.cpp @@ -1,6 +1,6 @@ #include "model/recipe/recipe.h" -Recipe::Recipe(string name, string author, vector ingredients, Instruction instruction, QImage image, vector tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings){ +Recipe::Recipe(string name, string author, vector ingredients, Instruction instruction, QImage image, vector tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings){ setName(name); setAuthor(author); setIngredients(ingredients); @@ -13,7 +13,7 @@ Recipe::Recipe(string name, string author, vector ingredients, setServings(servings); } -Recipe::Recipe() : Recipe::Recipe("", "", vector(), Instruction(), QImage(), vector(), QDate::currentDate(), QTime(), QTime(), 1.0f){ +Recipe::Recipe() : Recipe::Recipe("", "", vector(), Instruction(), QImage(), vector(), QDate::currentDate(), QTime(), QTime(), 1.0f){ //Set default values when none are specified. } @@ -25,15 +25,15 @@ string Recipe::getAuthor() const{ return this->authorName; } -vector Recipe::getIngredients() const{ +vector Recipe::getIngredients() const{ return this->ingredients; } vector Recipe::getFoodGroups() const{ vector foodGroups; - for (RecipeIngredient ri : this->ingredients){ - if (find(foodGroups.begin(), foodGroups.end(), ri.getFoodGroup()) == foodGroups.end()){ - foodGroups.push_back(ri.getFoodGroup()); + for (Ingredient i : this->ingredients){ + if (find(foodGroups.begin(), foodGroups.end(), i.getFoodGroup()) == foodGroups.end()){ + foodGroups.push_back(i.getFoodGroup()); } } return foodGroups; @@ -83,7 +83,7 @@ void Recipe::setAuthor(string newName){ this->authorName = newName; } -void Recipe::setIngredients(vector ingredients){ +void Recipe::setIngredients(vector ingredients){ this->ingredients = ingredients; } @@ -91,7 +91,7 @@ void Recipe::setTags(vector tags){ this->tags = tags; } -void Recipe::addIngredient(RecipeIngredient newIngredient){ +void Recipe::addIngredient(Ingredient newIngredient){ this->ingredients.push_back(newIngredient); } @@ -129,17 +129,11 @@ void Recipe::print(){ this->servings); printf("\tInstruction: %s\n", this->instruction.getHTML().c_str()); printf("\tIngredients:\n"); - for (vector::iterator it = this->ingredients.begin(); it != this->ingredients.end(); ++it){ - RecipeIngredient ri = *it; - printf("\t\t%s, Food Group: %s, Quantity: %f, Unit: %s\n", - ri.getName().c_str(), - ri.getFoodGroup().c_str(), - ri.getQuantity(), - ri.getUnit().getName().c_str()); + for (Ingredient i : this->ingredients){ + printf("\t\t%s\n", i.getContent().c_str()); } printf("\tTags:\n"); - for (vector::iterator it = this->tags.begin(); it != this->tags.end(); ++it){ - RecipeTag t = *it; + for (RecipeTag t : this->tags){ printf("\t\t%s\n", t.getValue().c_str()); } } diff --git a/model/recipe/recipe.h b/model/recipe/recipe.h index 12143e0..ab17dda 100644 --- a/model/recipe/recipe.h +++ b/model/recipe/recipe.h @@ -32,14 +32,14 @@ class Recipe { public: //Full constructor - Recipe(string name, string author, vector ingredients, Instruction instruction, QImage image, vector tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings); + Recipe(string name, string author, vector ingredients, Instruction instruction, QImage image, vector tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings); //Constructor with default values. Recipe(); //Getters string getName() const; string getAuthor() const; - vector getIngredients() const; + vector getIngredients() const; vector getFoodGroups() const; Instruction getInstruction() const; QImage getImage() const; @@ -54,9 +54,9 @@ public: //Setters void setName(string newName); void setAuthor(string newName); - void setIngredients(vector ingredients); + void setIngredients(vector ingredients); void setTags(vector tags); - void addIngredient(RecipeIngredient newIngredient); + void addIngredient(Ingredient newIngredient); void setInstruction(Instruction newInstruction); void setImage(QImage newImage); void setCreatedDate(QDate newDate); @@ -64,12 +64,13 @@ public: void setCookTime(QTime newTime); void setServings(float newServingsCount); + //Prints information about the recipe to the console, for debugging. void print(); private: //Main information. string name; //The name of the recipe. string authorName; //The name of the author of this recipe. - vector ingredients; //The list of ingredients in the recipe. + vector ingredients; //The list of ingredients in the recipe. Instruction instruction; //The instruction HTML document. QImage image; //An image displayed alongside the recipe. //Auxiliary Information.