From 6597efa646dbc5df0c98373e584c99aee0e14e4d Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 30 Mar 2018 23:25:28 +0200 Subject: [PATCH] Added ability to edit recipe (WIP). --- gui/mainwindow.cpp | 15 +++++++++++++++ gui/mainwindow.h | 3 +++ gui/mainwindow.ui | 15 +++++++++------ gui/newrecipedialog.cpp | 11 +++++++++++ gui/newrecipedialog.h | 1 + main.cpp | 22 +++++++++++----------- model/database/recipedatabase.cpp | 4 ++++ model/database/recipedatabase.h | 3 +++ 8 files changed, 57 insertions(+), 17 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 7db6bbe..d2de990 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -31,6 +31,7 @@ void MainWindow::loadFromRecipe(Recipe recipe){ setCookTime(recipe.getCookTime()); setServings(recipe.getServings()); setTags(recipe.getTags()); + this->currentRecipe = recipe; } void MainWindow::setRecipeName(string name){ @@ -91,3 +92,17 @@ void MainWindow::on_openButton_clicked(){ void MainWindow::on_exitButton_clicked(){ this->close(); } + +void MainWindow::on_editButton_clicked(){ + NewRecipeDialog d(this->recipeDB, this->currentRecipe, this); + d.show(); + d.exec(); + if (d.isAccepted()){ + Recipe r = d.getRecipe(); + if (!this->recipeDB->storeRecipe(r)){ + QMessageBox::critical(this, QString("Unable to Save Recipe"), QString("The program was not able to successfully save the recipe. Make sure to give the recipe a name, instructions, and some ingredients!")); + } else { + this->loadFromRecipe(r); + } + } +} diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 03e802a..3ce5cf4 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -36,11 +36,14 @@ public: void on_exitButton_clicked(); + void on_editButton_clicked(); + private: Ui::MainWindow *ui; RecipeDatabase *recipeDB; RecipeIngredientListModel ingredientModel; TagListModel tagsListModel; + Recipe currentRecipe; //Hidden manipulation methods. void setRecipeName(string name); diff --git a/gui/mainwindow.ui b/gui/mainwindow.ui index 4ac1f88..23922da 100644 --- a/gui/mainwindow.ui +++ b/gui/mainwindow.ui @@ -180,9 +180,9 @@ QPushButton#openButton:pressed{ - + - false + true @@ -201,19 +201,19 @@ QPushButton#openButton:pressed{ false - QPushButton#browseButton { + QPushButton#editButton { background-color: rgb(215, 215, 255); border: 0px; } -QPushButton#browseButton:hover{ +QPushButton#editButton:hover{ background-color: rgb(225, 225, 255); } -QPushButton#browseButton:pressed{ +QPushButton#editButton:pressed{ background-color: rgb(255, 255, 255); } - Browse + Edit false @@ -363,6 +363,9 @@ font: "Noto Sans CJK KR"; + + false + 0 diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index c105333..3825ad1 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -21,6 +21,17 @@ NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, QWidget *parent) : NewRecip this->populateTagsBox(); } +NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, Recipe recipe, QWidget *parent) : NewRecipeDialog(db, parent){ + ui->recipeNameEdit->setText(QString::fromStdString(recipe.getName())); + ui->prepTimeEdit->setTime(recipe.getPrepTime()); + ui->cookTimeEdit->setTime(recipe.getCookTime()); + ui->servingsSpinBox->setValue((double)recipe.getServings()); + ui->instructionsTextEdit->setHtml(QString::fromStdString(recipe.getInstruction().getHTML())); + ui->imageDisplayLabel->setPixmap(QPixmap::fromImage(recipe.getImage())); + this->tagsListModel.setTags(recipe.getTags()); + this->ingredientListModel.setIngredients(recipe.getIngredients()); +} + NewRecipeDialog::~NewRecipeDialog(){ delete ui; } diff --git a/gui/newrecipedialog.h b/gui/newrecipedialog.h index 181cea2..105cd37 100644 --- a/gui/newrecipedialog.h +++ b/gui/newrecipedialog.h @@ -26,6 +26,7 @@ class NewRecipeDialog : public QDialog public: explicit NewRecipeDialog(QWidget *parent = 0); NewRecipeDialog(RecipeDatabase *db, QWidget *parent = 0); + NewRecipeDialog(RecipeDatabase *db, Recipe recipe, QWidget *parent = 0); ~NewRecipeDialog(); Recipe getRecipe(); diff --git a/main.cpp b/main.cpp index 2a70cec..940cd9d 100644 --- a/main.cpp +++ b/main.cpp @@ -48,18 +48,18 @@ void test(RecipeDatabase *recipeDB){ bool success = recipeDB->storeRecipe(rec); printf("Storage successful: %d\n", success); - vector foodGroups = recipeDB->retrieveAllFoodGroups(); - printf("Food Groups:\n"); - for (string s : foodGroups){ - printf("\t%s\n", s.c_str()); - } +// vector foodGroups = recipeDB->retrieveAllFoodGroups(); +// printf("Food Groups:\n"); +// for (string s : foodGroups){ +// printf("\t%s\n", s.c_str()); +// } //Get food groups from recipe. - Recipe r = recipeDB->retrieveRecipe("Pannenkoeken"); - vector foodGroupsR = r.getFoodGroups(); - printf("Pannenkoeken Food Groups:\n"); - for (string s : foodGroupsR){ - printf("\t%s\n", s.c_str()); - } +// Recipe r = recipeDB->retrieveRecipe("Pannenkoeken"); +// vector foodGroupsR = r.getFoodGroups(); +// printf("Pannenkoeken Food Groups:\n"); +// for (string s : foodGroupsR){ +// printf("\t%s\n", s.c_str()); +// } } diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index c05355b..1a9f83a 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -356,6 +356,10 @@ bool RecipeDatabase::deleteTag(RecipeTag tag){ return this->deleteFrom("recipeTag", "WHERE tagName='"+tag.getValue()+"'"); } +bool RecipeDatabase::updateRecipe(Recipe recipe){ + +} + 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 34c767d..c68b0c9 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -51,6 +51,9 @@ class RecipeDatabase : public Database bool deleteIngredient(string name); bool deleteUnitOfMeasure(string name); bool deleteTag(RecipeTag tag); + + //Updating. + bool updateRecipe(Recipe recipe); private: //Utility methods.