From cd501d27cb7a394653beba0d36c28615adf7e194 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:02:08 +0200 Subject: [PATCH 01/13] Updated new recipe dialog, Ingredient class. --- gui/newrecipedialog.ui | 177 +----------------------- model/recipe/ingredients/ingredient.cpp | 27 +--- model/recipe/ingredients/ingredient.h | 17 +-- 3 files changed, 18 insertions(+), 203 deletions(-) diff --git a/gui/newrecipedialog.ui b/gui/newrecipedialog.ui index 9759320..d59410c 100644 --- a/gui/newrecipedialog.ui +++ b/gui/newrecipedialog.ui @@ -175,7 +175,7 @@ 0 1999 12 - 21 + 20 @@ -530,176 +530,7 @@ QPushButton#deleteTagButton:pressed{ - - - - 2 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - background-color: rgb(113, 119, 255); - - - false - - - - - - QComboBox::InsertAlphabetically - - - true - - - - - - - Create a new ingredient - - - - :/images/images/plus_icon.png:/images/images/plus_icon.png - - - - - - - Delete this ingredient - - - - - - - :/images/images/minus_icon.png:/images/images/minus_icon.png - - - - - - - - - - - 0 - 36 - - - - - 2 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 50 - false - false - - - - Amount - - - - - - - - 0 - 0 - - - - 10000.000000000000000 - - - 1.000000000000000 - - - - - - - - 0 - 0 - - - - background-color: rgb(113, 119, 255); - - - QComboBox::InsertAlphabetically - - - - - - - Create a new unit of measure - - - - :/images/images/plus_icon.png:/images/images/plus_icon.png - - - - - - - Delete this unit of measure - - - - - - - :/images/images/minus_icon.png:/images/images/minus_icon.png - - - - - - - - + 0 @@ -710,10 +541,10 @@ QPushButton#deleteTagButton:pressed{ false - Qt::AlignCenter + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - Comments + Write ingredient here... false diff --git a/model/recipe/ingredients/ingredient.cpp b/model/recipe/ingredients/ingredient.cpp index 8942e11..bf58fa6 100644 --- a/model/recipe/ingredients/ingredient.cpp +++ b/model/recipe/ingredients/ingredient.cpp @@ -1,31 +1,18 @@ #include "model/recipe/ingredients/ingredient.h" Ingredient::Ingredient(){ - setName("NULL"); - setFoodGroup("NULL"); + setContent("NULL"); } -Ingredient::Ingredient(string name, string foodGroup){ - setName(name); - setFoodGroup(foodGroup); +Ingredient::Ingredient(string content){ + setContent(content); } -string Ingredient::getName() const{ - return this->name; +string Ingredient::getContent() const{ + return this->content; } -string Ingredient::getFoodGroup() const{ - return this->foodGroup; +void Ingredient::setContent(string newContent){ + this->content = newContent; } -void Ingredient::setName(string newName){ - this->name = newName; -} - -void Ingredient::setFoodGroup(string newFoodGroup){ - this->foodGroup = newFoodGroup; -} - -string Ingredient::toString(){ - return this->getName(); -} diff --git a/model/recipe/ingredients/ingredient.h b/model/recipe/ingredients/ingredient.h index 88a7ec7..f83fc76 100644 --- a/model/recipe/ingredients/ingredient.h +++ b/model/recipe/ingredients/ingredient.h @@ -6,28 +6,25 @@ using namespace std; /** - * @brief The Ingredient class represents an ingredient, which is classified by a food group, and has a name and an ID. - * An ingredient cannot be included on its own in a recipe, and must be paired with a Unit in a RecipeIngredient Object. + * @brief The Ingredient class represents an ingredient, which is a string representing one component of a recipe. + * The user is free to compose a recipe string however they like. However, the program will restrict obviously + * invalid input, and try to be smart about determining if an ingredient is valid. */ class Ingredient { public: Ingredient(); - Ingredient(string name, string foodGroup); + Ingredient(string content); //Getters - string getName() const; - string getFoodGroup() const; + string getContent() const; //Setters - void setName(string newName); - void setFoodGroup(string newFoodGroup); + void setContent(string newContent); - string toString(); protected: - string name; - string foodGroup; + string content; }; #endif // INGREDIENT_H From a7901286463b43752219af1601378dc9f014e743 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:07:16 +0200 Subject: [PATCH 02/13] Updated ingredientlistmodel for new ingredients model. --- model/recipe/ingredients/ingredientlistmodel.cpp | 7 ++++--- model/recipe/ingredients/ingredientlistmodel.h | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/model/recipe/ingredients/ingredientlistmodel.cpp b/model/recipe/ingredients/ingredientlistmodel.cpp index bbafe66..e3b4f63 100644 --- a/model/recipe/ingredients/ingredientlistmodel.cpp +++ b/model/recipe/ingredients/ingredientlistmodel.cpp @@ -30,13 +30,14 @@ void IngredientListModel::setIngredients(vector ingredients){ emit dataChanged(index, bottomIndex); } -bool IngredientListModel::addIngredient(Ingredient ri){ +bool IngredientListModel::addIngredient(Ingredient i){ //Add only if it doesn't exist already. - for (unsigned int i = 0; i < this->ingredients.size(); i++){ - if (!this->ingredients[i].getName().compare(ri.getName())){ + for (Ingredient ing : this->ingredients){ + if (!ing.getContent().compare(i.getContent())){ return false; } } + //The ingredient doesn't exist already, so we'll add it. this->ingredients.push_back(ri); QModelIndex index = createIndex(this->ingredients.size()-1, 0); QModelIndex bottomIndex = createIndex(this->ingredients.size()-1, 0); diff --git a/model/recipe/ingredients/ingredientlistmodel.h b/model/recipe/ingredients/ingredientlistmodel.h index 31dd94d..32bc65d 100644 --- a/model/recipe/ingredients/ingredientlistmodel.h +++ b/model/recipe/ingredients/ingredientlistmodel.h @@ -3,8 +3,9 @@ #include #include +#include -#include "model/recipe/ingredients/recipeingredient.h" +#include "model/recipe/ingredients/ingredient.h" class IngredientListModel : public QAbstractListModel { @@ -17,7 +18,7 @@ public: //Custom methods to handle ingredient data. void setIngredients(vector ingredients); - bool addIngredient(Ingredient ri); + bool addIngredient(Ingredient i); void deleteIngredient(int index); vector getIngredients(); From 07774cc349e82379b6f096615f95074cadc01f5d Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:09:14 +0200 Subject: [PATCH 03/13] Removed recipeingredient files, and unitofmeasure. These are too restrictive. --- RecipeDB.pro | 5 +- model/recipe/ingredients/recipeingredient.cpp | 60 ------------------- .../ingredients/recipeingredientlistmodel.cpp | 54 ----------------- model/recipe/ingredients/unitofmeasure.cpp | 42 ------------- 4 files changed, 1 insertion(+), 160 deletions(-) delete mode 100644 model/recipe/ingredients/recipeingredient.cpp delete mode 100644 model/recipe/ingredients/recipeingredientlistmodel.cpp delete mode 100644 model/recipe/ingredients/unitofmeasure.cpp diff --git a/RecipeDB.pro b/RecipeDB.pro index fdcef14..a07dd60 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -16,10 +16,8 @@ SOURCES += model/recipe/instruction.cpp \ model/recipe/recipe.cpp \ main.cpp \ model/database/database.cpp \ - model/recipe/ingredients/unitofmeasure.cpp \ model/recipe/ingredients/ingredient.cpp \ model/recipe/ingredients/ingredientlistmodel.cpp \ - model/recipe/ingredients/recipeingredient.cpp \ model/recipe/tags/recipetag.cpp \ SQLite/sqlite3.c \ model/database/resulttable.cpp \ @@ -35,8 +33,7 @@ SOURCES += model/recipe/instruction.cpp \ gui/openrecipedialog.cpp \ model/recipe/recipetablemodel.cpp \ gui/mainwindow.cpp \ - gui/newDialogs/newfoodgroupdialog.cpp \ - model/recipe/ingredients/recipeingredientlistmodel.cpp + gui/newDialogs/newfoodgroupdialog.cpp HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ diff --git a/model/recipe/ingredients/recipeingredient.cpp b/model/recipe/ingredients/recipeingredient.cpp deleted file mode 100644 index 6e7d775..0000000 --- a/model/recipe/ingredients/recipeingredient.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "model/recipe/ingredients/recipeingredient.h" - -RecipeIngredient::RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit, string comment) : Ingredient(name, foodGroup){ - setQuantity(quantity); - setUnit(unit); - setComment(comment); -} - -RecipeIngredient::RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit, string comment){ - setName(i.getName()); - setFoodGroup(i.getFoodGroup()); - setQuantity(quantity); - setUnit(unit); - setComment(comment); -} - -RecipeIngredient::RecipeIngredient(Ingredient &i) : RecipeIngredient(i, 0.0f, UnitOfMeasure("bleh"), "Fuck"){ - //Constructs recipe ingredient from ingredient which is a hidden recipe ingredient. -} - -RecipeIngredient::RecipeIngredient(){ - -} - -float RecipeIngredient::getQuantity() const{ - return this->quantity; -} - -UnitOfMeasure RecipeIngredient::getUnit() const{ - return this->unit; -} - -string RecipeIngredient::getComment() const{ - return this->comment; -} - -void RecipeIngredient::setQuantity(float newQuantity){ - this->quantity = newQuantity; -} - -void RecipeIngredient::setUnit(UnitOfMeasure newUnit){ - this->unit = newUnit; -} - -void RecipeIngredient::setComment(string newComment){ - this->comment = newComment; -} - -string RecipeIngredient::toString(){ - string result; - if (std::ceil(this->getQuantity()) == this->getQuantity()){ - result += std::to_string((int)this->getQuantity()); - } else { - result += StringUtils::toString(this->getQuantity()); - } - result += " " + this->getUnit().getAbbreviation() + " " + this->getName(); - if (!this->getComment().empty()) result += " (" + this->getComment() + ")"; - - return result; -} diff --git a/model/recipe/ingredients/recipeingredientlistmodel.cpp b/model/recipe/ingredients/recipeingredientlistmodel.cpp deleted file mode 100644 index 18cdaa6..0000000 --- a/model/recipe/ingredients/recipeingredientlistmodel.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "recipeingredientlistmodel.h" - -RecipeIngredientListModel::RecipeIngredientListModel(){ - this->ingredients = vector(); -} - -int RecipeIngredientListModel::rowCount(const QModelIndex &parent) const{ - Q_UNUSED(parent); - return this->ingredients.size(); -} - -QVariant RecipeIngredientListModel::data(const QModelIndex &index, int role) const{ - int row = index.row(); - RecipeIngredient i = this->ingredients[row]; - - string displayStr = i.toString(); - - switch(role){ - case Qt::DisplayRole: - return QString::fromStdString(displayStr); - } - - return QVariant(); -} - -void RecipeIngredientListModel::setIngredients(vector ingredients){ - this->ingredients = ingredients; - QModelIndex index = createIndex(0, 0); - QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0); - emit dataChanged(index, bottomIndex); -} - -bool RecipeIngredientListModel::addIngredient(RecipeIngredient ri){ - //Add only if it doesn't exist already. - for (unsigned int i = 0; i < this->ingredients.size(); i++){ - if (!this->ingredients[i].getName().compare(ri.getName())){ - return false; - } - } - this->ingredients.push_back(ri); - QModelIndex index = createIndex(this->ingredients.size()-1, 0); - QModelIndex bottomIndex = createIndex(this->ingredients.size()-1, 0); - emit dataChanged(index, bottomIndex); - return true; -} - -void RecipeIngredientListModel::deleteIngredient(int index){ - this->ingredients.erase(this->ingredients.begin() + index); - emit dataChanged(createIndex(0, 0), createIndex(this->ingredients.size()-1, 0)); -} - -vector RecipeIngredientListModel::getIngredients(){ - return this->ingredients; -} diff --git a/model/recipe/ingredients/unitofmeasure.cpp b/model/recipe/ingredients/unitofmeasure.cpp deleted file mode 100644 index dedcb75..0000000 --- a/model/recipe/ingredients/unitofmeasure.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "unitofmeasure.h" - -UnitOfMeasure::UnitOfMeasure(string name, string plural, string abbreviation, int type, double coef){ - this->name = name; - this->plural = plural; - this->abbreviation = abbreviation; - this->type = type; - this->metricCoefficient = coef; -} - -UnitOfMeasure::UnitOfMeasure(string name){ - this->name = name; - this->plural = name + "s"; - this->abbreviation = "NULL"; - this->type = MISC; - this->metricCoefficient = 1; - ///TODO: Make actual guessing of this stuff. -} - -UnitOfMeasure::UnitOfMeasure() : UnitOfMeasure::UnitOfMeasure("", "", "", MISC, 1.0){ - //Default constructor initializes all fields to empty strings. -} - -string UnitOfMeasure::getName() const{ - return this->name; -} - -string UnitOfMeasure::getNamePlural() const{ - return this->plural; -} - -string UnitOfMeasure::getAbbreviation() const{ - return this->abbreviation; -} - -int UnitOfMeasure::getType() const{ - return this->type; -} - -double UnitOfMeasure::getMetricCoefficient() const{ - return this->metricCoefficient; -} From 3da05f71f7c46dd4d220aec59d7da49186dcd775 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:16:54 +0200 Subject: [PATCH 04/13] Removed recipeingredient headers, removed recipeingredient from recipe object. --- RecipeDB.pro | 5 +-- model/recipe/ingredients/recipeingredient.h | 43 ------------------- .../ingredients/recipeingredientlistmodel.h | 28 ------------ model/recipe/ingredients/unitofmeasure.h | 42 ------------------ model/recipe/recipe.h | 2 +- 5 files changed, 2 insertions(+), 118 deletions(-) delete mode 100644 model/recipe/ingredients/recipeingredient.h delete mode 100644 model/recipe/ingredients/recipeingredientlistmodel.h delete mode 100644 model/recipe/ingredients/unitofmeasure.h diff --git a/RecipeDB.pro b/RecipeDB.pro index a07dd60..eac8ff6 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -38,10 +38,8 @@ SOURCES += model/recipe/instruction.cpp \ HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ model/database/database.h \ - model/recipe/ingredients/unitofmeasure.h \ model/recipe/ingredients/ingredient.h \ model/recipe/ingredients/ingredientlistmodel.h \ - model/recipe/ingredients/recipeingredient.h \ model/recipe/tags/recipetag.h \ SQLite/sqlite3.h \ SQLite/sqlite3ext.h \ @@ -58,8 +56,7 @@ HEADERS += model/recipe/instruction.h \ gui/openrecipedialog.h \ model/recipe/recipetablemodel.h \ gui/mainwindow.h \ - gui/newDialogs/newfoodgroupdialog.h \ - model/recipe/ingredients/recipeingredientlistmodel.h + gui/newDialogs/newfoodgroupdialog.h LIBS += -ldl \ diff --git a/model/recipe/ingredients/recipeingredient.h b/model/recipe/ingredients/recipeingredient.h deleted file mode 100644 index 8c1dd45..0000000 --- a/model/recipe/ingredients/recipeingredient.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef RECIPEINGREDIENT_H -#define RECIPEINGREDIENT_H - -#include -#include - -#include "model/recipe/ingredients/ingredient.h" -#include "model/recipe/ingredients/unitofmeasure.h" -#include "utils/stringutils.h" - -using namespace std; - -/** - * @brief The RecipeIngredient class represents both an ingredient and a unit of measure, to be used in a recipe object. - */ - -class RecipeIngredient : public Ingredient -{ -public: - //Constructor for new RecipeIngredient without starting child ingredient. - RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit, string comment); - //Constructor using data from a child ingredient. - RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit, string comment); - RecipeIngredient(Ingredient &i); - RecipeIngredient(); - - //Getters - float getQuantity() const; - UnitOfMeasure getUnit() const; - string getComment() const; - - //Setters - void setQuantity(float newQuantity); - void setUnit(UnitOfMeasure newUnit); - void setComment(string newComment); - string toString(); -private: - float quantity; - UnitOfMeasure unit; - string comment; -}; - -#endif // RECIPEINGREDIENT_H diff --git a/model/recipe/ingredients/recipeingredientlistmodel.h b/model/recipe/ingredients/recipeingredientlistmodel.h deleted file mode 100644 index 5ba9da0..0000000 --- a/model/recipe/ingredients/recipeingredientlistmodel.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef RECIPEINGREDIENTLISTMODEL_H -#define RECIPEINGREDIENTLISTMODEL_H - -#include - -#include "model/recipe/ingredients/recipeingredient.h" - -class RecipeIngredientListModel : public QAbstractListModel -{ - public: - RecipeIngredientListModel(); - - //Overridden methods. - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - - //Custom methods to handle ingredient data. - void setIngredients(vector ingredients); - bool addIngredient(RecipeIngredient ri); - void deleteIngredient(int index); - vector getIngredients(); - - private: - vector ingredients; - -}; - -#endif // RECIPEINGREDIENTLISTMODEL_H diff --git a/model/recipe/ingredients/unitofmeasure.h b/model/recipe/ingredients/unitofmeasure.h deleted file mode 100644 index 96022fc..0000000 --- a/model/recipe/ingredients/unitofmeasure.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef UNITOFMEASURE_H -#define UNITOFMEASURE_H - -#include - -using namespace std; - -/** - * @brief The UnitOfMeasure class represents a way to measure an ingredient. It contains a name, an abbreviation, plural name, and some information on conversion. - */ - -class UnitOfMeasure -{ -public: - //Constants Declarations. - static const int MASS = 0; - static const int VOLUME = 1; - static const int LENGTH = 2; - static const int MISC = 3; - - //Full constructor. - UnitOfMeasure(string name, string plural, string abbreviation, int type, double coef); - //Attempt to guess unit from just a string. - UnitOfMeasure(string name); - //Constructor with default values. - UnitOfMeasure(); - - //Getters - string getName() const; - string getNamePlural() const; - string getAbbreviation() const; - int getType() const; - double getMetricCoefficient() const; -private: - string name; //The name of the unit of measure. - string plural; //The plural name. - string abbreviation; //A short version of the unit. - int type; //The type of unit, as one of the constants above. - double metricCoefficient; //The conversion from this unit to the standard metric unit. -}; - -#endif // UNITOFMEASURE_H diff --git a/model/recipe/recipe.h b/model/recipe/recipe.h index 4d8dca3..12143e0 100644 --- a/model/recipe/recipe.h +++ b/model/recipe/recipe.h @@ -8,7 +8,7 @@ #include #include -#include "model/recipe/ingredients/recipeingredient.h" +#include "model/recipe/ingredients/ingredient.h" #include "model/recipe/instruction.h" #include "model/recipe/tags/recipetag.h" From 021ec5b8159ac60c3f41dcb0829c03207c2349ad Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:24:36 +0200 Subject: [PATCH 05/13] 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. From 4ca4854a631b67fe446a3f8351b48c5c7e2571b4 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 21:28:18 +0200 Subject: [PATCH 06/13] Updated mainwindow to remove recipeingredient. --- gui/mainwindow.cpp | 2 +- gui/mainwindow.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 9f8c381..d7712ab 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -48,7 +48,7 @@ void MainWindow::setInstruction(Instruction instruction){ ui->instructionsTextEdit->setHtml(QString::fromStdString(instruction.getHTML())); } -void MainWindow::setIngredients(vector ingredients){ +void MainWindow::setIngredients(vector ingredients){ this->ingredientModel.setIngredients(ingredients); } diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 70ab7be..1a1bdbb 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -6,7 +6,7 @@ #include #include "model/recipe/recipe.h" -#include "model/recipe/ingredients/recipeingredientlistmodel.h" +#include "model/recipe/ingredients/ingredientlistmodel.h" #include "gui/newrecipedialog.h" #include "gui/openrecipedialog.h" #include "utils/stringutils.h" @@ -41,14 +41,14 @@ public: private: Ui::MainWindow *ui; RecipeDatabase *recipeDB; - RecipeIngredientListModel ingredientModel; + IngredientListModel ingredientModel; TagListModel tagsListModel; Recipe currentRecipe; //Hidden manipulation methods. void setRecipeName(string name); void setInstruction(Instruction instruction); - void setIngredients(vector ingredients); + void setIngredients(vector ingredients); void setImage(QImage img); void setPrepTime(QTime prepTime); void setCookTime(QTime cookTime); From 17797eeb5fdcbda352b078fb8850ed6354c68a1d Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 22:01:37 +0200 Subject: [PATCH 07/13] Removed food groups and ingredients from open recipe dialog. --- gui/openrecipedialog.cpp | 18 ------- gui/openrecipedialog.h | 7 --- gui/openrecipedialog.ui | 103 +-------------------------------------- 3 files changed, 2 insertions(+), 126 deletions(-) diff --git a/gui/openrecipedialog.cpp b/gui/openrecipedialog.cpp index 8f936da..6e3d474 100644 --- a/gui/openrecipedialog.cpp +++ b/gui/openrecipedialog.cpp @@ -8,13 +8,8 @@ OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) : ui->setupUi(this); ui->recipeTableView->setModel(&this->recipeTableModel); - ui->ingredientsListView->setModel(&this->ingredientsModel); ui->tagsListView->setModel(&this->tagsModel); - QObject::connect(ui->ingredientsListView->selectionModel(), - SIGNAL(selectionChanged(QItemSelection, QItemSelection)), - this, - SLOT(onIngredientsListViewSelectionChanged(QItemSelection))); QObject::connect(ui->tagsListView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, @@ -23,9 +18,7 @@ OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) : OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){ this->recipeDB = recipeDB; - this->populateIngredientsList(); this->populateTagsList(); - this->populateFoodGroupsList(); this->populateRecipesTable(this->recipeDB->retrieveAllRecipes()); } @@ -45,21 +38,10 @@ void OpenRecipeDialog::populateRecipesTable(vector recipes){ ui->recipeTableView->show(); } -void OpenRecipeDialog::populateIngredientsList(){ - this->ingredientsModel.setIngredients(this->recipeDB->retrieveAllIngredients()); -} - void OpenRecipeDialog::populateTagsList(){ this->tagsModel.setTags(this->recipeDB->retrieveAllTags()); } -void OpenRecipeDialog::populateFoodGroupsList(){ - for (string s : this->recipeDB->retrieveAllFoodGroups()){ - ui->foodGroupsListWidget->addItem(QString::fromStdString(s)); - } - //ui->foodGroupsListWidget->show(); -} - void OpenRecipeDialog::on_deleteRecipeButton_clicked(){ QItemSelectionModel *selectModel = ui->recipeTableView->selectionModel(); if (!selectModel->hasSelection()){ diff --git a/gui/openrecipedialog.h b/gui/openrecipedialog.h index c6cf23c..593cd2c 100644 --- a/gui/openrecipedialog.h +++ b/gui/openrecipedialog.h @@ -30,14 +30,10 @@ class OpenRecipeDialog : public QDialog void on_recipeTableView_doubleClicked(const QModelIndex &index); - void onIngredientsListViewSelectionChanged(const QItemSelection &selection); - void onTagsListViewSelectionChanged(const QItemSelection &selection); void on_nameEdit_textChanged(const QString &arg1); - void on_foodGroupsListWidget_itemSelectionChanged(); - void on_clearSearchButton_clicked(); void on_exitButton_clicked(); @@ -48,13 +44,10 @@ class OpenRecipeDialog : public QDialog RecipeTableModel recipeTableModel; Recipe selectedRecipe; - IngredientListModel ingredientsModel; TagListModel tagsModel; void populateRecipesTable(vector recipes); - void populateIngredientsList(); void populateTagsList(); - void populateFoodGroupsList(); }; #endif // OPENRECIPEDIALOG_H diff --git a/gui/openrecipedialog.ui b/gui/openrecipedialog.ui index 89ae5ea..7a1deef 100644 --- a/gui/openrecipedialog.ui +++ b/gui/openrecipedialog.ui @@ -48,7 +48,7 @@ 0 - + @@ -63,7 +63,7 @@ QTabWidget::Rounded - 2 + 0 @@ -119,105 +119,6 @@ - - - - :/images/images/ingredients.png:/images/images/ingredients.png - - - - - - Ingredients - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - QFrame::NoFrame - - - QAbstractItemView::ExtendedSelection - - - QAbstractItemView::ScrollPerPixel - - - false - - - true - - - - - - - - - :/images/images/foodPyramid.png:/images/images/foodPyramid.png - - - - - - Food Groups - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - QFrame::NoFrame - - - QAbstractItemView::ExtendedSelection - - - QAbstractItemView::ScrollPerPixel - - - true - - - - - From 491e5c19a47501d32ff5fd1a310089070345e882 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 22:13:37 +0200 Subject: [PATCH 08/13] Removed ingredients, units table from recipe database. --- model/database/recipedatabase.cpp | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index bae0eae..3909c3f 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -487,18 +487,6 @@ void RecipeDatabase::ensureTablesExist(){ this->executeSQL("PRAGMA foreign_keys = ON;"); this->beginTransaction(); - //Ingredients table. - this->executeSQL("CREATE TABLE IF NOT EXISTS ingredient(" - "ingredientId INTEGER PRIMARY KEY," - "foodGroup varchar," - "name varchar UNIQUE);"); - //Unit of Measure table. - this->executeSQL("CREATE TABLE IF NOT EXISTS unitOfMeasure(" - "name varchar UNIQUE PRIMARY KEY," - "plural varchar," - "abbreviation varchar," - "type int," - "metricCoefficient real);"); //Recipe table. Each recipe can have at most one instruction, and one image. this->executeSQL("CREATE TABLE IF NOT EXISTS recipe(" "recipeId INTEGER PRIMARY KEY," @@ -515,14 +503,9 @@ void RecipeDatabase::ensureTablesExist(){ "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); //RecipeIngredient table. this->executeSQL("CREATE TABLE IF NOT EXISTS recipeIngredient(" - "ingredientId int," "recipeId int," - "quantity real," - "unitName varchar," - "comment varchar," - "FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId)," - "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId)," - "FOREIGN KEY (unitName) REFERENCES unitOfMeasure(name));"); + "content," + "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId);"); this->commitTransaction(); } From 6cfd50d329f5d5559393518285b059bd3563861a Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 23:06:11 +0200 Subject: [PATCH 09/13] Updated recipe database to remove unneeded components. --- model/database/database.cpp | 9 ++ model/database/database.h | 4 + model/database/recipedatabase.cpp | 216 +++--------------------------- model/database/recipedatabase.h | 14 +- 4 files changed, 32 insertions(+), 211 deletions(-) diff --git a/model/database/database.cpp b/model/database/database.cpp index 6e914b0..ca23338 100644 --- a/model/database/database.cpp +++ b/model/database/database.cpp @@ -39,6 +39,15 @@ bool Database::insertInto(string tableName, vector columnNames, vectorexecuteSQL(query); + return (t.getReturnCode() == SQLITE_DONE); +} + ResultTable Database::selectFrom(string tableName, string columnNames, string conditions){ if (columnNames.size() == 0 || tableName.empty()){ return ResultTable(); diff --git a/model/database/database.h b/model/database/database.h index 0ccc9fd..d98aacb 100644 --- a/model/database/database.h +++ b/model/database/database.h @@ -24,8 +24,12 @@ public: //Executes an SQL string statement in a safe way and returns the result. ResultTable executeSQL(string statement); + //Inserts into a table. bool insertInto(string tableName, vector columnNames, vector values); + bool insertInto(string tableName, string columnName, string value); + //Selects from a table. ResultTable selectFrom(string tableName, string columnNames, string conditions); + //Deletes from a table. bool deleteFrom(string tableName, string conditions); bool tableExists(string tableName); diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 3909c3f..9cd1351 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -57,66 +57,18 @@ bool RecipeDatabase::storeRecipe(Recipe recipe){ return false; } -bool RecipeDatabase::storeRecipeIngredient(RecipeIngredient ri, int recipeId){ - int ingId = this->storeIngredient(ri); - if (ingId < 0) return false; - - if (!this->storeUnitOfMeasure(ri.getUnit())) return false; - +bool RecipeDatabase::storeRecipeIngredient(Ingredient i, int recipeId){ return this->insertInto("recipeIngredient", vector({ - "ingredientId", - "recipeId", - "quantity", - "unitName", - "comment" + "content", + "recipeId" }), vector({ - std::to_string(ingId), - std::to_string(recipeId), - std::to_string(ri.getQuantity()), - ri.getUnit().getName(), - ri.getComment() + i.getContent(), + std::to_string(recipeId) })); } -int RecipeDatabase::storeIngredient(Ingredient ingredient){ - ResultTable t = this->selectFrom("ingredient", "*", "WHERE name="+surroundString(ingredient.getName(), "'")); - if (t.isEmpty()){ - bool success = this->insertInto("ingredient", vector({"foodGroup", "name"}), vector({ingredient.getFoodGroup(), ingredient.getName()})); - if (success){ - return this->getLastInsertedRowId(); - } else { - return -1; - } - } else { - return std::stoi(t.at(0, 0)); - } -} - -bool RecipeDatabase::storeUnitOfMeasure(UnitOfMeasure u){ - ResultTable t = this->selectFrom("unitOfMeasure", "name", "WHERE name="+surroundString(u.getName(), "'")); - if (!t.isEmpty()){ - return true; - } - bool success = this->insertInto("unitOfMeasure", - vector({ - "name", - "plural", - "abbreviation", - "type", - "metricCoefficient" - }), - vector({ - u.getName(), - u.getNamePlural(), - u.getAbbreviation(), - std::to_string(u.getType()), - std::to_string(u.getMetricCoefficient()) - })); - return success; -} - bool RecipeDatabase::storeInstruction(Instruction instruction, int recipeId){ return FileUtils::saveInstruction(recipeId, instruction); } @@ -166,31 +118,6 @@ vector RecipeDatabase::retrieveAllRecipes(){ return this->readRecipesFromTable(t); } -vector RecipeDatabase::retrieveRecipesWithIngredients(vector ingredients){ - vector recipes; - if (ingredients.empty()){ - return recipes; - } - string filterList = surroundString(ingredients.at(0).getName(), "'"); - for (unsigned int i = 1; i < ingredients.size(); i++){ - filterList += ", " + surroundString(ingredients[i].getName(), "'"); - } - filterList = '(' + filterList + ')'; - ResultTable t = this->executeSQL("SELECT * " - "FROM recipe " - "WHERE recipeId IN (" - " SELECT recipeIngredient.recipeId " - " FROM recipeIngredient " - " INNER JOIN (" - " SELECT ingredientId " - " FROM ingredient " - " WHERE name IN "+filterList+"" - " ) filteredIngredients " - " ON recipeIngredient.ingredientId = filteredIngredients.ingredientId" - ") ORDER BY name;"); - return this->readRecipesFromTable(t); -} - vector RecipeDatabase::retrieveRecipesWithTags(vector tags){ vector recipes; if (tags.empty()){ @@ -210,53 +137,15 @@ vector RecipeDatabase::retrieveRecipesWithSubstring(string s){ return this->readRecipesFromTable(t); } -vector RecipeDatabase::retrieveRecipesWithFoodGroups(vector groups){ - vector recipes; - if (groups.empty()){ - return recipes; - } - string filterList = surroundString(groups.at(0), "'"); - for (unsigned int i = 1; i < groups.size(); i++){ - filterList += ", " + surroundString(groups.at(i), "'"); - } - filterList = '(' + filterList + ')'; - ResultTable t = this->executeSQL("SELECT * FROM recipe WHERE recipeId IN (SELECT recipeId FROM recipeIngredient WHERE ingredientId IN (SELECT ingredientId FROM ingredient WHERE foodGroup IN "+filterList+" ) ) ORDER BY name;"); - return this->readRecipesFromTable(t); -} - -vector RecipeDatabase::retrieveAllFoodGroups(){ - ResultTable t = this->executeSQL("SELECT DISTINCT foodGroup FROM ingredient ORDER BY foodGroup;"); - vector foodGroups; +vector RecipeDatabase::retrieveRecipeIngredients(int recipeId){ + ResultTable t = this->executeSQL("SELECT content " + "FROM recipeIngredient " + "WHERE recipeId = "+std::to_string(recipeId)+";"); + vector ingredients; for (TableRow row : t.rows()){ - foodGroups.push_back(row.at(0)); + ingredients.push_back(Ingredient(row.at(0))); } - return foodGroups; -} - -vector RecipeDatabase::retrieveRecipeIngredients(int recipeId){ - ResultTable t = this->executeSQL("SELECT ingredient.name, ingredient.foodGroup, "//0, 1 - "recipeIngredient.quantity, recipeIngredient.unitName, recipeIngredient.comment,"//2, 3, 4 - "unitOfMeasure.name, unitOfMeasure.plural, unitOfMeasure.abbreviation, unitOfMeasure.type, unitOfMeasure.metricCoefficient "//5, 6, 7, 8, 9 - "FROM ingredient " - "INNER JOIN recipeIngredient " - "ON ingredient.ingredientId = recipeIngredient.ingredientId " - "INNER JOIN unitOfMeasure " - "ON recipeIngredient.unitName = unitOfMeasure.name " - "WHERE recipeIngredient.recipeId = "+std::to_string(recipeId)+";"); - vector ings; - for (TableRow row : t.rows()){ - RecipeIngredient r(row.at(0), - row.at(1), - std::stof(row.at(2)), - UnitOfMeasure(row.at(5), row.at(6), row.at(7), std::stoi(row.at(8)), std::stod(row.at(9))), - row.at(4)); - ings.push_back(r); - } - return ings; -} - -int RecipeDatabase::retrieveIngredientId(string ingredientName){ - return std::stoi(this->selectFrom("ingredient", "ingredientId", "WHERE name = '"+ingredientName+"'").at(0, 0)); + return ingredients; } bool RecipeDatabase::deleteRecipeTags(int recipeId){ @@ -268,27 +157,14 @@ bool RecipeDatabase::deleteRecipeIngredients(int recipeId){ } vector RecipeDatabase::retrieveAllIngredients(){ - ResultTable t = this->selectFrom("ingredient", "name, foodGroup", "ORDER BY name"); + ResultTable t = this->selectFrom("recipeIngredient", "content", "ORDER BY content"); vector ings; for (TableRow row : t.rows()){ - Ingredient i(row.at(0), row.at(1)); - ings.push_back(i); + ings.push_back(Ingredient(row.at(0))); } return ings; } -vector RecipeDatabase::retrieveAllUnitsOfMeasure(){ - ResultTable t = this->selectFrom("unitOfMeasure", "name, plural, abbreviation, type, metricCoefficient", "ORDER BY name"); - vector units; - if (!t.isEmpty()){ - for (TableRow row : t.rows()){ - UnitOfMeasure u(row.at(0), row.at(1), row.at(2), std::stoi(row.at(3)), std::stod(row.at(4))); - units.push_back(u); - } - } - return units; -} - vector RecipeDatabase::retrieveTags(int recipeId){ ResultTable t = this->selectFrom("recipeTag", "tagName", "WHERE recipeId="+std::to_string(recipeId)+" ORDER BY tagName"); vector tags; @@ -358,14 +234,6 @@ bool RecipeDatabase::deleteIngredient(string name){ return this->deleteFrom("ingredient", "WHERE name='"+name+"'"); } -bool RecipeDatabase::deleteUnitOfMeasure(string name){ - ResultTable t = this->selectFrom("recipeIngredient", "recipeId", "WHERE unitName='"+name+"'"); - if (!t.isEmpty()){ - return false; - } - return this->deleteFrom("unitOfMeasure", "WHERE name='"+name+"'"); -} - bool RecipeDatabase::deleteTag(RecipeTag tag){ return this->deleteFrom("recipeTag", "WHERE tagName='"+tag.getValue()+"'"); } @@ -405,22 +273,16 @@ bool RecipeDatabase::updateRecipe(Recipe recipe, string originalName) { return false; } bool ingredientsSuccess = this->deleteRecipeIngredients(id); - for (RecipeIngredient ri : recipe.getIngredients()){ + for (Ingredient i : recipe.getIngredients()){ ingredientsSuccess = ingredientsSuccess && this->insertInto( "recipeIngredient", vector({ "recipeId", - "ingredientId", - "unitName", - "quantity", - "comment" + "content" }), vector({ idS, - std::to_string(this->retrieveIngredientId(ri.getName())), - ri.getUnit().getName(), - std::to_string(ri.getQuantity()), - ri.getComment() + i.getContent() })); } if (!ingredientsSuccess){ @@ -438,50 +300,6 @@ bool RecipeDatabase::updateRecipe(Recipe recipe, string originalName) { } } -bool RecipeDatabase::addBasicUnits(){ - this->beginTransaction(); - //Volume - this->storeUnitOfMeasure(UnitOfMeasure("Teaspoon", "Teaspoons", "tsp", UnitOfMeasure::VOLUME, 5.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Tablespoon", "Tablespoons", "tbsp", UnitOfMeasure::VOLUME, 15.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Fluid Ounce", "Fluid Ounces", "fl oz", UnitOfMeasure::VOLUME, 30.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Cup", "Cups", "c", UnitOfMeasure::VOLUME, 250.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Milliliter", "Milliliters", "mL", UnitOfMeasure::VOLUME, 1.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Liter", "Liters", "L", UnitOfMeasure::VOLUME, 1000.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Gallon", "Gallons", "gal", UnitOfMeasure::VOLUME, 3800.0)); - //Mass/Weight - this->storeUnitOfMeasure(UnitOfMeasure("Ounce", "Ounces", "oz", UnitOfMeasure::MASS, 28.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Pound", "Pounds", "lb", UnitOfMeasure::MASS, 454.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Gram", "Grams", "g", UnitOfMeasure::MASS, 1.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Milligram", "Milligrams", "mg", UnitOfMeasure::MASS, 0.001)); - this->storeUnitOfMeasure(UnitOfMeasure("Kilogram", "Kilograms", "kg", UnitOfMeasure::MASS, 1000.0)); - //Length - this->storeUnitOfMeasure(UnitOfMeasure("Inch", "Inches", "in", UnitOfMeasure::LENGTH, 2.54)); - this->storeUnitOfMeasure(UnitOfMeasure("Centimeter", "Centimeters", "cm", UnitOfMeasure::LENGTH, 1.0)); - //MISC - this->storeUnitOfMeasure(UnitOfMeasure("Piece", "Pieces", "pc", UnitOfMeasure::MISC, 1.0)); - this->storeUnitOfMeasure(UnitOfMeasure("Item", "Items", "", UnitOfMeasure::MISC, 1.0)); - this->commitTransaction(); - return true; -} - -bool RecipeDatabase::addBasicIngredients(){ - this->beginTransaction(); - this->storeIngredient(Ingredient("Flour", "grains")); - this->storeIngredient(Ingredient("Eggs", "eggs")); - this->storeIngredient(Ingredient("Milk", "dairy")); - this->storeIngredient(Ingredient("Cheese", "dairy")); - this->storeIngredient(Ingredient("Salt", "spices")); - this->storeIngredient(Ingredient("Sugar", "sugars")); - this->storeIngredient(Ingredient("Vegetable Oil", "oils")); - this->storeIngredient(Ingredient("Olive Oil", "oils")); - this->storeIngredient(Ingredient("Water", "water")); - this->storeIngredient(Ingredient("Bell Pepper", "vegetables")); - this->storeIngredient(Ingredient("Onion", "vegetables")); - this->storeIngredient(Ingredient("Garlic", "spices")); - this->commitTransaction(); - return true; -} - void RecipeDatabase::ensureTablesExist(){ //Make sure that foreign keys are enabled. this->executeSQL("PRAGMA foreign_keys = ON;"); diff --git a/model/database/recipedatabase.h b/model/database/recipedatabase.h index 2452e42..79c949a 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -23,21 +23,15 @@ class RecipeDatabase : public Database //SQL Helper methods. //Storage. - bool storeRecipeIngredient(RecipeIngredient ri, int recipeId); - int storeIngredient(Ingredient ingredient); - bool storeUnitOfMeasure(UnitOfMeasure u); + bool storeRecipeIngredient(Ingredient i, int recipeId); //Retrieval. Recipe retrieveRecipe(string name); Recipe retrieveRandomRecipe(); vector retrieveAllRecipes(); - vector retrieveRecipesWithIngredients(vector ingredients); vector retrieveRecipesWithTags(vector tags); vector retrieveRecipesWithSubstring(string s); - vector retrieveRecipesWithFoodGroups(vector groups); - vector retrieveAllFoodGroups(); vector retrieveAllIngredients(); - vector retrieveAllUnitsOfMeasure(); vector retrieveAllTags(); //Deletion. @@ -50,9 +44,6 @@ class RecipeDatabase : public Database //Updating. bool updateRecipe(Recipe recipe, string originalName); - //Adding basic information at start. - bool addBasicUnits(); - bool addBasicIngredients(); private: //Utility methods. @@ -68,8 +59,7 @@ class RecipeDatabase : public Database //Retrieval vector retrieveTags(int recipeId); - vector retrieveRecipeIngredients(int recipeId); - int retrieveIngredientId(string ingredientName); + vector retrieveRecipeIngredients(int recipeId); //Deletion bool deleteRecipeTags(int recipeId); From 55a222967bad8c5b07c88fcf34244b71efbf58a9 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 23:09:52 +0200 Subject: [PATCH 10/13] Removed test method from main, removed new unit dialog. --- RecipeDB.pro | 2 - gui/newDialogs/newunitdialog.h | 27 ---- gui/newDialogs/newunitdialog.ui | 228 -------------------------------- main.cpp | 25 +--- model/recipe/recipe.cpp | 10 -- 5 files changed, 1 insertion(+), 291 deletions(-) delete mode 100644 gui/newDialogs/newunitdialog.h delete mode 100644 gui/newDialogs/newunitdialog.ui diff --git a/RecipeDB.pro b/RecipeDB.pro index eac8ff6..f61213b 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -50,7 +50,6 @@ HEADERS += model/recipe/instruction.h \ model/recipe/tags/taglistmodel.h \ gui/newDialogs/newingredientdialog.h \ gui/newDialogs/newtagdialog.h \ - gui/newDialogs/newunitdialog.h \ utils/aspectratiopixmaplabel.h \ utils/stringutils.h \ gui/openrecipedialog.h \ @@ -64,7 +63,6 @@ FORMS += gui/mainwindow.ui \ gui/newrecipedialog.ui \ gui/newDialogs/newingredientdialog.ui \ gui/newDialogs/newtagdialog.ui \ - gui/newDialogs/newunitdialog.ui \ gui/openrecipedialog.ui \ gui/mainwindow.ui \ gui/newDialogs/newfoodgroupdialog.ui diff --git a/gui/newDialogs/newunitdialog.h b/gui/newDialogs/newunitdialog.h deleted file mode 100644 index 7221134..0000000 --- a/gui/newDialogs/newunitdialog.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef NEWUNITDIALOG_H -#define NEWUNITDIALOG_H - -#include - -#include "model/recipe/ingredients/unitofmeasure.h" - -namespace Ui { -class NewUnitDialog; -} - -class NewUnitDialog : public QDialog -{ - Q_OBJECT - - public: - explicit NewUnitDialog(QWidget *parent = 0); - ~NewUnitDialog(); - - UnitOfMeasure getUnit(); - private: - Ui::NewUnitDialog *ui; - - int getSelectedType(); -}; - -#endif // NEWUNITDIALOG_H diff --git a/gui/newDialogs/newunitdialog.ui b/gui/newDialogs/newunitdialog.ui deleted file mode 100644 index fd3ada6..0000000 --- a/gui/newDialogs/newunitdialog.ui +++ /dev/null @@ -1,228 +0,0 @@ - - - NewUnitDialog - - - - 0 - 0 - 195 - 340 - - - - New Unit - - - - :/images/images/icon.png:/images/images/icon.png - - - - - - true - - - - - - - - - - 12 - 50 - false - false - - - - Unit Name - - - Qt::AlignCenter - - - - - - - - - - - 12 - 50 - false - false - - - - Plural Name - - - Qt::AlignCenter - - - - - - - - - - - 12 - 50 - false - false - - - - Abbreviation - - - Qt::AlignCenter - - - - - - - - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - - 12 - 50 - false - false - - - - Type: - - - - - - - - 0 - 0 - - - - - - - - - - - - - - - 12 - 50 - false - false - - - - Metric Coefficient - - - Qt::AlignCenter - - - - - - - 1000.000000000000000 - - - 1.000000000000000 - - - - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - - buttonBox - accepted() - NewUnitDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - NewUnitDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/main.cpp b/main.cpp index bed054e..80b9310 100644 --- a/main.cpp +++ b/main.cpp @@ -8,34 +8,11 @@ #include "model/database/recipedatabase.h" #include "utils/fileutils.h" -void test(RecipeDatabase *recipeDB){ - vector ri; - ri.push_back(RecipeIngredient("flour", "grains", 3.0f, UnitOfMeasure("cup", "cups", "c", UnitOfMeasure::VOLUME, 1.0), "")); - ri.push_back(RecipeIngredient("baking powder", "additives", 1.0f, UnitOfMeasure("teaspoon", "teaspoons", "tsp", UnitOfMeasure::VOLUME, 1.0), "")); - - Recipe rec("Example", - "Andrew Lalis", - ri, - Instruction("Placeholder Text"), - QImage(), - vector({RecipeTag("testing"), - RecipeTag("fake")}), - QDate::currentDate(), - QTime(0, 30), - QTime(0, 25), - 10.0f); - - bool success = recipeDB->storeRecipe(rec); - printf("Storage successful: %d\n", success); - -} - Recipe checkForFirstRun(RecipeDatabase *recipeDB){ Recipe r = recipeDB->retrieveRandomRecipe(); if (r.isEmpty()){//There are no recipes in the database. //Add some basic units to the units, and some basic ingredients. - recipeDB->addBasicUnits(); - recipeDB->addBasicIngredients(); + } return r; } diff --git a/model/recipe/recipe.cpp b/model/recipe/recipe.cpp index f0e04d2..f73c05f 100644 --- a/model/recipe/recipe.cpp +++ b/model/recipe/recipe.cpp @@ -29,16 +29,6 @@ vector Recipe::getIngredients() const{ return this->ingredients; } -vector Recipe::getFoodGroups() const{ - vector foodGroups; - for (Ingredient i : this->ingredients){ - if (find(foodGroups.begin(), foodGroups.end(), i.getFoodGroup()) == foodGroups.end()){ - foodGroups.push_back(i.getFoodGroup()); - } - } - return foodGroups; -} - Instruction Recipe::getInstruction() const{ return this->instruction; } From b3393266d34818884d50eb948b3b0f05553f861a Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 23:11:52 +0200 Subject: [PATCH 11/13] Removed more dialogs for obsolete units and recipe ingredients. --- RecipeDB.pro | 13 +- gui/newDialogs/newfoodgroupdialog.cpp | 18 --- gui/newDialogs/newfoodgroupdialog.h | 27 ----- gui/newDialogs/newfoodgroupdialog.ui | 102 ---------------- gui/newDialogs/newingredientdialog.cpp | 49 -------- gui/newDialogs/newingredientdialog.h | 39 ------ gui/newDialogs/newingredientdialog.ui | 158 ------------------------- gui/newDialogs/newunitdialog.cpp | 31 ----- 8 files changed, 3 insertions(+), 434 deletions(-) delete mode 100644 gui/newDialogs/newfoodgroupdialog.cpp delete mode 100644 gui/newDialogs/newfoodgroupdialog.h delete mode 100644 gui/newDialogs/newfoodgroupdialog.ui delete mode 100644 gui/newDialogs/newingredientdialog.cpp delete mode 100644 gui/newDialogs/newingredientdialog.h delete mode 100644 gui/newDialogs/newingredientdialog.ui delete mode 100644 gui/newDialogs/newunitdialog.cpp diff --git a/RecipeDB.pro b/RecipeDB.pro index f61213b..3440cb6 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -25,15 +25,12 @@ SOURCES += model/recipe/instruction.cpp \ utils/fileutils.cpp \ gui/newrecipedialog.cpp \ model/recipe/tags/taglistmodel.cpp \ - gui/newDialogs/newingredientdialog.cpp \ gui/newDialogs/newtagdialog.cpp \ - gui/newDialogs/newunitdialog.cpp \ utils/aspectratiopixmaplabel.cpp \ utils/stringutils.cpp \ gui/openrecipedialog.cpp \ model/recipe/recipetablemodel.cpp \ - gui/mainwindow.cpp \ - gui/newDialogs/newfoodgroupdialog.cpp + gui/mainwindow.cpp HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ @@ -48,24 +45,20 @@ HEADERS += model/recipe/instruction.h \ utils/fileutils.h \ gui/newrecipedialog.h \ model/recipe/tags/taglistmodel.h \ - gui/newDialogs/newingredientdialog.h \ gui/newDialogs/newtagdialog.h \ utils/aspectratiopixmaplabel.h \ utils/stringutils.h \ gui/openrecipedialog.h \ model/recipe/recipetablemodel.h \ - gui/mainwindow.h \ - gui/newDialogs/newfoodgroupdialog.h + gui/mainwindow.h LIBS += -ldl \ FORMS += gui/mainwindow.ui \ gui/newrecipedialog.ui \ - gui/newDialogs/newingredientdialog.ui \ gui/newDialogs/newtagdialog.ui \ gui/openrecipedialog.ui \ - gui/mainwindow.ui \ - gui/newDialogs/newfoodgroupdialog.ui + gui/mainwindow.ui DISTFILES += \ .gitignore diff --git a/gui/newDialogs/newfoodgroupdialog.cpp b/gui/newDialogs/newfoodgroupdialog.cpp deleted file mode 100644 index 8e0277a..0000000 --- a/gui/newDialogs/newfoodgroupdialog.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "newfoodgroupdialog.h" -#include "ui_newfoodgroupdialog.h" - -newFoodGroupDialog::newFoodGroupDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::newFoodGroupDialog) -{ - ui->setupUi(this); -} - -newFoodGroupDialog::~newFoodGroupDialog() -{ - delete ui; -} - -string newFoodGroupDialog::getFoodGroup() const{ - return ui->lineEdit->text().toStdString(); -} diff --git a/gui/newDialogs/newfoodgroupdialog.h b/gui/newDialogs/newfoodgroupdialog.h deleted file mode 100644 index 20c3451..0000000 --- a/gui/newDialogs/newfoodgroupdialog.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef NEWFOODGROUPDIALOG_H -#define NEWFOODGROUPDIALOG_H - -#include -#include - -using namespace std; - -namespace Ui { -class newFoodGroupDialog; -} - -class newFoodGroupDialog : public QDialog -{ - Q_OBJECT - - public: - explicit newFoodGroupDialog(QWidget *parent = 0); - ~newFoodGroupDialog(); - - string getFoodGroup() const; - - private: - Ui::newFoodGroupDialog *ui; -}; - -#endif // NEWFOODGROUPDIALOG_H diff --git a/gui/newDialogs/newfoodgroupdialog.ui b/gui/newDialogs/newfoodgroupdialog.ui deleted file mode 100644 index 42d9030..0000000 --- a/gui/newDialogs/newfoodgroupdialog.ui +++ /dev/null @@ -1,102 +0,0 @@ - - - newFoodGroupDialog - - - - 0 - 0 - 240 - 114 - - - - - 11 - - - - New Food Group - - - - :/images/images/icon.png:/images/images/icon.png - - - - - - - - - - 12 - - - - Add New Food Group - - - Qt::AlignCenter - - - - - - - - 12 - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - - buttonBox - accepted() - newFoodGroupDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - newFoodGroupDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/gui/newDialogs/newingredientdialog.cpp b/gui/newDialogs/newingredientdialog.cpp deleted file mode 100644 index 693dc1e..0000000 --- a/gui/newDialogs/newingredientdialog.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "newingredientdialog.h" -#include "ui_newingredientdialog.h" - -NewIngredientDialog::NewIngredientDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::NewIngredientDialog) -{ - 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->foodGroupBox->currentText().toStdString()); -} - -void NewIngredientDialog::populateFoodGroupBox(){ - vector foodGroups = this->recipeDB->retrieveAllFoodGroups(); - ui->foodGroupBox->clear(); - for (unsigned int i = 0; i < foodGroups.size(); i++){ - QString s = QString::fromStdString(foodGroups[i]); - ui->foodGroupBox->insertItem(i, s); - } -} - -void NewIngredientDialog::on_addFoodGroupButton_clicked(){ - newFoodGroupDialog d(this); - if (d.exec() == QDialog::Accepted){ - string s = d.getFoodGroup(); - if (!s.empty()){ - ui->foodGroupBox->addItem(QString::fromStdString(s)); - ui->foodGroupBox->setCurrentText(QString::fromStdString(s)); - } else { - QMessageBox::warning(this, "Empty Food Group", "The food group you entered is empty!"); - } - } -} - -void NewIngredientDialog::on_deleteFoodGroupButton_clicked(){ - ui->foodGroupBox->removeItem(ui->foodGroupBox->currentIndex()); -} diff --git a/gui/newDialogs/newingredientdialog.h b/gui/newDialogs/newingredientdialog.h deleted file mode 100644 index e922167..0000000 --- a/gui/newDialogs/newingredientdialog.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef NEWINGREDIENTDIALOG_H -#define NEWINGREDIENTDIALOG_H - -#include -#include - -#include "model/recipe/ingredients/ingredient.h" -#include "model/database/recipedatabase.h" -#include "gui/newDialogs/newfoodgroupdialog.h" - -namespace Ui { -class NewIngredientDialog; -} - -class NewIngredientDialog : public QDialog -{ - Q_OBJECT - - public: - explicit NewIngredientDialog(QWidget *parent = 0); - NewIngredientDialog(RecipeDatabase *recipeDB, QWidget *parent = 0); - ~NewIngredientDialog(); - - //Access values. - Ingredient getIngredient(); - - private slots: - void on_addFoodGroupButton_clicked(); - - void on_deleteFoodGroupButton_clicked(); - - private: - Ui::NewIngredientDialog *ui; - RecipeDatabase *recipeDB; - - void populateFoodGroupBox(); -}; - -#endif // NEWINGREDIENTDIALOG_H diff --git a/gui/newDialogs/newingredientdialog.ui b/gui/newDialogs/newingredientdialog.ui deleted file mode 100644 index 30e9aed..0000000 --- a/gui/newDialogs/newingredientdialog.ui +++ /dev/null @@ -1,158 +0,0 @@ - - - NewIngredientDialog - - - - 0 - 0 - 367 - 228 - - - - New Ingredient - - - - :/images/images/icon.png:/images/images/icon.png - - - true - - - - - - true - - - - - - - - - Name - - - Qt::AlignCenter - - - - - - - - - - - - - - - - Food Group - - - Qt::AlignCenter - - - - - - - - - - - 0 - 0 - - - - QComboBox::InsertAlphabetically - - - - - - - - - - - :/images/images/plus_icon.png:/images/images/plus_icon.png - - - - - - - - - - - :/images/images/minus_icon.png:/images/images/minus_icon.png - - - - - - - - - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - - buttonBox - accepted() - NewIngredientDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - NewIngredientDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - - diff --git a/gui/newDialogs/newunitdialog.cpp b/gui/newDialogs/newunitdialog.cpp deleted file mode 100644 index e7be030..0000000 --- a/gui/newDialogs/newunitdialog.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "newunitdialog.h" -#include "ui_newunitdialog.h" - -NewUnitDialog::NewUnitDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::NewUnitDialog) -{ - ui->setupUi(this); - - ui->typeComboBox->clear(); - QStringList list({"Mass", "Volume", "Length", "Misc"}); - ui->typeComboBox->insertItems(0, list); - -} - -NewUnitDialog::~NewUnitDialog() -{ - delete ui; -} - -UnitOfMeasure NewUnitDialog::getUnit(){ - return UnitOfMeasure(ui->unitNameEdit->text().toLower().toStdString(), - ui->pluralNameEdit->text().toLower().toStdString(), - ui->abbreviationEdit->text().toStdString(), - this->getSelectedType(), - ui->coefficientSpinBox->value()); -} - -int NewUnitDialog::getSelectedType(){ - return ui->typeComboBox->currentIndex(); -} From 4554fdcd1e3b997f2192f829aa0c0c28c96c9aa9 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 23:21:15 +0200 Subject: [PATCH 12/13] Fixed compilation bugs. Program runs. --- gui/newrecipedialog.cpp | 2 -- gui/newrecipedialog.h | 2 -- gui/openrecipedialog.cpp | 22 ------------------- model/database/database.cpp | 5 ++++- model/database/recipedatabase.cpp | 2 +- .../ingredients/ingredientlistmodel.cpp | 4 ++-- 6 files changed, 7 insertions(+), 30 deletions(-) diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index 87afeed..1d9a6c5 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -15,8 +15,6 @@ NewRecipeDialog::NewRecipeDialog(QWidget *parent) : NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, QWidget *parent) : NewRecipeDialog(parent){ this->recipeDB = db; - this->populateIngredientsBox(); - this->populateUnitsBox(); this->populateTagsBox(); } diff --git a/gui/newrecipedialog.h b/gui/newrecipedialog.h index 909f80c..99d7af8 100644 --- a/gui/newrecipedialog.h +++ b/gui/newrecipedialog.h @@ -11,9 +11,7 @@ #include "model/recipe/ingredients/ingredientlistmodel.h" #include "model/recipe/tags/taglistmodel.h" -#include "gui/newDialogs/newingredientdialog.h" #include "gui/newDialogs/newtagdialog.h" -#include "gui/newDialogs/newunitdialog.h" namespace Ui { class NewRecipeDialog; diff --git a/gui/openrecipedialog.cpp b/gui/openrecipedialog.cpp index 6e3d474..7984736 100644 --- a/gui/openrecipedialog.cpp +++ b/gui/openrecipedialog.cpp @@ -74,17 +74,6 @@ void OpenRecipeDialog::on_recipeTableView_doubleClicked(const QModelIndex &index this->close(); } -void OpenRecipeDialog::onIngredientsListViewSelectionChanged(const QItemSelection &selection){ - Q_UNUSED(selection); - vector ingredients; - QModelIndexList indexes = ui->ingredientsListView->selectionModel()->selectedRows(); - for (QModelIndex index : indexes){ - Ingredient i = this->ingredientsModel.getIngredients().at(index.row()); - ingredients.push_back(i); - } - this->populateRecipesTable(this->recipeDB->retrieveRecipesWithIngredients(ingredients)); -} - void OpenRecipeDialog::onTagsListViewSelectionChanged(const QItemSelection &selection){ Q_UNUSED(selection); vector tags; @@ -101,20 +90,9 @@ void OpenRecipeDialog::on_nameEdit_textChanged(const QString &arg1){ this->populateRecipesTable(this->recipeDB->retrieveRecipesWithSubstring(ui->nameEdit->text().toStdString())); } -void OpenRecipeDialog::on_foodGroupsListWidget_itemSelectionChanged(){ - vector groups; - for (QModelIndex index : ui->foodGroupsListWidget->selectionModel()->selectedRows()){ - QListWidgetItem *item = ui->foodGroupsListWidget->item(index.row()); - groups.push_back(item->text().toStdString()); - } - this->populateRecipesTable(this->recipeDB->retrieveRecipesWithFoodGroups(groups)); -} - void OpenRecipeDialog::on_clearSearchButton_clicked(){ ui->nameEdit->clear(); - ui->foodGroupsListWidget->selectionModel()->clearSelection(); ui->tagsListView->selectionModel()->clearSelection(); - ui->ingredientsListView->selectionModel()->clearSelection(); this->populateRecipesTable(this->recipeDB->retrieveAllRecipes()); } diff --git a/model/database/database.cpp b/model/database/database.cpp index ca23338..b879d7c 100644 --- a/model/database/database.cpp +++ b/model/database/database.cpp @@ -15,7 +15,10 @@ ResultTable Database::executeSQL(string statement){ this->sql = statement; this->returnCode = sqlite3_prepare_v2(this->db, statement.c_str(), -1, &stmt, NULL); if (this->returnCode != SQLITE_OK){ - fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\n\tError Message: %s\n", this->returnCode, sqlite3_errmsg(this->db)); + fprintf(stderr, "Unable to successfully prepare SQL statement. Error code: %d\n\tError Message: %s\nSQL Statement: %s\n", + this->returnCode, + sqlite3_errmsg(this->db), + statement.c_str()); return ResultTable(this->returnCode); } ResultTable t(statement); diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 9cd1351..08939a8 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -323,7 +323,7 @@ void RecipeDatabase::ensureTablesExist(){ this->executeSQL("CREATE TABLE IF NOT EXISTS recipeIngredient(" "recipeId int," "content," - "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId);"); + "FOREIGN KEY (recipeId) REFERENCES recipe(recipeId));"); this->commitTransaction(); } diff --git a/model/recipe/ingredients/ingredientlistmodel.cpp b/model/recipe/ingredients/ingredientlistmodel.cpp index e3b4f63..66721d1 100644 --- a/model/recipe/ingredients/ingredientlistmodel.cpp +++ b/model/recipe/ingredients/ingredientlistmodel.cpp @@ -13,7 +13,7 @@ QVariant IngredientListModel::data(const QModelIndex &index, int role) const{ int row = index.row(); Ingredient i = this->ingredients[row]; - string displayStr = i.toString(); + string displayStr = i.getContent(); switch(role){ case Qt::DisplayRole: @@ -38,7 +38,7 @@ bool IngredientListModel::addIngredient(Ingredient i){ } } //The ingredient doesn't exist already, so we'll add it. - this->ingredients.push_back(ri); + this->ingredients.push_back(i); QModelIndex index = createIndex(this->ingredients.size()-1, 0); QModelIndex bottomIndex = createIndex(this->ingredients.size()-1, 0); emit dataChanged(index, bottomIndex); From 29a4ed0ad2e2e80864a5645f2c98728d71f8be2f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 22 May 2018 23:27:40 +0200 Subject: [PATCH 13/13] Added slot for return in add ingredients input field. --- gui/newrecipedialog.cpp | 4 ++++ gui/newrecipedialog.h | 2 ++ gui/newrecipedialog.ui | 5 ++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index 1d9a6c5..4937851 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -149,3 +149,7 @@ void NewRecipeDialog::on_removeTagButton_clicked(){ this->populateTagsBox(); } } + +void NewRecipeDialog::on_ingredientLineEdit_returnPressed(){ + this->on_addIngredientButton_clicked(); +} diff --git a/gui/newrecipedialog.h b/gui/newrecipedialog.h index 99d7af8..0c3e986 100644 --- a/gui/newrecipedialog.h +++ b/gui/newrecipedialog.h @@ -55,6 +55,8 @@ class NewRecipeDialog : public QDialog void on_removeTagButton_clicked(); + void on_ingredientLineEdit_returnPressed(); + private: Ui::NewRecipeDialog *ui; RecipeDatabase *recipeDB; diff --git a/gui/newrecipedialog.ui b/gui/newrecipedialog.ui index d59410c..068f8af 100644 --- a/gui/newrecipedialog.ui +++ b/gui/newrecipedialog.ui @@ -640,11 +640,14 @@ QPushButton#removeIngredientButton:pressed{ QFrame::NoFrame - QAbstractItemView::MultiSelection + QAbstractItemView::SingleSelection QAbstractItemView::ScrollPerPixel + + QAbstractItemView::ScrollPerPixel + 100