Improve/redesign ingredients #13
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <QMessageBox>
|
||||
|
||||
#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<Ingredient> ingredients;
|
||||
vector<UnitOfMeasure> units;
|
||||
vector<RecipeTag> tags;
|
||||
RecipeIngredientListModel ingredientListModel;
|
||||
IngredientListModel ingredientListModel;
|
||||
TagListModel tagsListModel;
|
||||
bool accepted = false;
|
||||
|
||||
//Helper functions to fill fields.
|
||||
void populateIngredientsBox();
|
||||
void populateUnitsBox();
|
||||
void populateTagsBox();
|
||||
};
|
||||
|
||||
|
|
|
@ -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<Ingredient> ingredients;
|
||||
|
||||
//Helper for printing.
|
||||
|
||||
};
|
||||
|
||||
#endif // INGREDIENTLISTMODEL_H
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "model/recipe/recipe.h"
|
||||
|
||||
Recipe::Recipe(string name, string author, vector<RecipeIngredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings){
|
||||
Recipe::Recipe(string name, string author, vector<Ingredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> 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<RecipeIngredient> ingredients,
|
|||
setServings(servings);
|
||||
}
|
||||
|
||||
Recipe::Recipe() : Recipe::Recipe("", "", vector<RecipeIngredient>(), Instruction(), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(), QTime(), 1.0f){
|
||||
Recipe::Recipe() : Recipe::Recipe("", "", vector<Ingredient>(), Instruction(), QImage(), vector<RecipeTag>(), 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<RecipeIngredient> Recipe::getIngredients() const{
|
||||
vector<Ingredient> Recipe::getIngredients() const{
|
||||
return this->ingredients;
|
||||
}
|
||||
|
||||
vector<string> Recipe::getFoodGroups() const{
|
||||
vector<string> 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<RecipeIngredient> ingredients){
|
||||
void Recipe::setIngredients(vector<Ingredient> ingredients){
|
||||
this->ingredients = ingredients;
|
||||
}
|
||||
|
||||
|
@ -91,7 +91,7 @@ void Recipe::setTags(vector<RecipeTag> 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<RecipeIngredient>::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<RecipeTag>::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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,14 +32,14 @@ class Recipe
|
|||
{
|
||||
public:
|
||||
//Full constructor
|
||||
Recipe(string name, string author, vector<RecipeIngredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings);
|
||||
Recipe(string name, string author, vector<Ingredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings);
|
||||
//Constructor with default values.
|
||||
Recipe();
|
||||
|
||||
//Getters
|
||||
string getName() const;
|
||||
string getAuthor() const;
|
||||
vector<RecipeIngredient> getIngredients() const;
|
||||
vector<Ingredient> getIngredients() const;
|
||||
vector<string> 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<RecipeIngredient> ingredients);
|
||||
void setIngredients(vector<Ingredient> ingredients);
|
||||
void setTags(vector<RecipeTag> 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<RecipeIngredient> ingredients; //The list of ingredients in the recipe.
|
||||
vector<Ingredient> ingredients; //The list of ingredients in the recipe.
|
||||
Instruction instruction; //The instruction HTML document.
|
||||
QImage image; //An image displayed alongside the recipe.
|
||||
//Auxiliary Information.
|
||||
|
|
Loading…
Reference in New Issue