From 8fee8c0cdbe904017de563613f7e899450c26c1d Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 29 Mar 2018 17:17:07 +0200 Subject: [PATCH] Updates saving and deleting instructions and images. --- gui/openrecipedialog.cpp | 2 +- main.cpp | 3 ++- model/database/recipedatabase.cpp | 2 ++ utils/fileutils.cpp | 14 ++++++++++++++ utils/fileutils.h | 5 +++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/gui/openrecipedialog.cpp b/gui/openrecipedialog.cpp index fb34703..5031f10 100644 --- a/gui/openrecipedialog.cpp +++ b/gui/openrecipedialog.cpp @@ -38,7 +38,7 @@ void OpenRecipeDialog::on_deleteRecipeButton_clicked(){ return; } vector rows; - QModelIndexList indexes = selectModel->selectedIndexes(); + QModelIndexList indexes = selectModel->selectedRows(); for (int i = 0; i < indexes.count(); i++){ rows.push_back(indexes.at(i).row()); } diff --git a/main.cpp b/main.cpp index 3d4bfdb..5d4d83f 100644 --- a/main.cpp +++ b/main.cpp @@ -4,10 +4,11 @@ #include "model/database/database.h" #include "model/database/recipedatabase.h" +#include "utils/fileutils.h" int main(int argc, char *argv[]) { - RecipeDatabase recipeDB("recipes"); + RecipeDatabase recipeDB(QString(FileUtils::appDataPath+"recipes.db").toStdString()); QApplication a(argc, argv); MainWindow w(&recipeDB); w.show(); diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 1650579..412db80 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -255,6 +255,8 @@ bool RecipeDatabase::deleteRecipe(int recipeId){ bool tagsDeleted = this->deleteFrom("recipeTag", "WHERE recipeId="+idString); bool recipeIngredientDeleted = this->deleteFrom("recipeIngredient", "WHERE recipeId="+idString); bool recipeDeleted = this->deleteFrom("recipe", "WHERE recipeId="+idString); + bool instructionDeleted = FileUtils::deleteInstruction(recipeId); + bool imageDeleted = FileUtils::deleteImage(recipeId); if (tagsDeleted && recipeIngredientDeleted && recipeDeleted){ this->executeSQL("COMMIT;"); return true; diff --git a/utils/fileutils.cpp b/utils/fileutils.cpp index a453c57..1e428b0 100644 --- a/utils/fileutils.cpp +++ b/utils/fileutils.cpp @@ -61,4 +61,18 @@ QImage loadImage(int nr){ return img; } +bool deleteInstruction(int nr){ + ensureAppDataFolderExists(); + QString filename = appDataPath + QString::fromStdString(std::to_string(nr)) +".html"; + QFile file(filename); + return file.remove(); +} + +bool deleteImage(int nr){ + ensureAppDataFolderExists(); + QString filename = appDataPath + QString::fromStdString(std::to_string(nr)) +".png"; + QFile file(filename); + return file.remove(); +} + } diff --git a/utils/fileutils.h b/utils/fileutils.h index b975215..322dd84 100644 --- a/utils/fileutils.h +++ b/utils/fileutils.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "model/recipe/instruction.h" @@ -19,9 +20,13 @@ namespace FileUtils{ Instruction loadInstruction(int nr); + bool deleteInstruction(int nr); + bool saveImage(int nr, QImage image); QImage loadImage(int nr); + + bool deleteImage(int nr); } #endif // FILEUTILS_H