From f13432a2feebe3dfa9616dc61feb73f7b2e9f0d7 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 30 Mar 2018 22:50:02 +0200 Subject: [PATCH] Added search by food group. --- gui/openrecipedialog.cpp | 38 +++++++ gui/openrecipedialog.h | 7 ++ gui/openrecipedialog.ui | 172 ++++++++++++++++++------------ model/database/recipedatabase.cpp | 33 ++++++ model/database/recipedatabase.h | 3 + 5 files changed, 185 insertions(+), 68 deletions(-) diff --git a/gui/openrecipedialog.cpp b/gui/openrecipedialog.cpp index 4c961cd..184ea18 100644 --- a/gui/openrecipedialog.cpp +++ b/gui/openrecipedialog.cpp @@ -15,12 +15,17 @@ OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) : SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, SLOT(onIngredientsListViewSelectionChanged(QItemSelection))); + QObject::connect(ui->tagsListView->selectionModel(), + SIGNAL(selectionChanged(QItemSelection,QItemSelection)), + this, + SLOT(onTagsListViewSelectionChanged(QItemSelection))); } OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){ this->recipeDB = recipeDB; this->populateIngredientsList(); this->populateTagsList(); + this->populateFoodGroupsList(); this->populateRecipesTable(this->recipeDB->retrieveAllRecipes()); } @@ -48,6 +53,13 @@ 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()){ @@ -81,6 +93,7 @@ void OpenRecipeDialog::on_recipeTableView_doubleClicked(const QModelIndex &index } void OpenRecipeDialog::onIngredientsListViewSelectionChanged(const QItemSelection &selection){ + Q_UNUSED(selection); vector ingredients; QModelIndexList indexes = ui->ingredientsListView->selectionModel()->selectedRows(); for (QModelIndex index : indexes){ @@ -89,3 +102,28 @@ void OpenRecipeDialog::onIngredientsListViewSelectionChanged(const QItemSelectio } this->populateRecipesTable(this->recipeDB->retrieveRecipesWithIngredients(ingredients)); } + +void OpenRecipeDialog::onTagsListViewSelectionChanged(const QItemSelection &selection){ + Q_UNUSED(selection); + vector tags; + QModelIndexList indexes = ui->tagsListView->selectionModel()->selectedRows(); + for (QModelIndex index : indexes){ + RecipeTag t = this->tagsModel.getTags().at(index.row()); + tags.push_back(t); + } + this->populateRecipesTable(this->recipeDB->retrieveRecipesWithTags(tags)); +} + +void OpenRecipeDialog::on_nameEdit_textChanged(const QString &arg1){ + Q_UNUSED(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)); +} diff --git a/gui/openrecipedialog.h b/gui/openrecipedialog.h index 753c206..8e63496 100644 --- a/gui/openrecipedialog.h +++ b/gui/openrecipedialog.h @@ -32,6 +32,12 @@ class OpenRecipeDialog : public QDialog void onIngredientsListViewSelectionChanged(const QItemSelection &selection); + void onTagsListViewSelectionChanged(const QItemSelection &selection); + + void on_nameEdit_textChanged(const QString &arg1); + + void on_foodGroupsListWidget_itemSelectionChanged(); + private: Ui::OpenRecipeDialog *ui; RecipeDatabase *recipeDB; @@ -44,6 +50,7 @@ class OpenRecipeDialog : public QDialog void populateRecipesTable(vector recipes); void populateIngredientsList(); void populateTagsList(); + void populateFoodGroupsList(); }; #endif // OPENRECIPEDIALOG_H diff --git a/gui/openrecipedialog.ui b/gui/openrecipedialog.ui index 0e51715..93cb94b 100644 --- a/gui/openrecipedialog.ui +++ b/gui/openrecipedialog.ui @@ -6,7 +6,7 @@ 0 0 - 1000 + 1064 480 @@ -25,73 +25,109 @@ - - - - - - Tag - - - - - - - - 0 - 0 - - - - QFrame::NoFrame - - - QAbstractItemView::ExtendedSelection - - - false - - - false - - - - - - - - - - - - - Ingredient - - - - - - - - 0 - 0 - - - - QFrame::NoFrame - - - QAbstractItemView::ExtendedSelection - - - false - - - true - - - - + + + + 290 + 0 + + + + QTabWidget::South + + + QTabWidget::Rounded + + + 2 + + + + Tags + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QAbstractItemView::ExtendedSelection + + + QAbstractItemView::ScrollPerPixel + + + false + + + true + + + + + + + + Ingredients + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QAbstractItemView::ExtendedSelection + + + QAbstractItemView::ScrollPerPixel + + + false + + + true + + + + + + + + Food Groups + + + + + + QFrame::NoFrame + + + QAbstractItemView::ExtendedSelection + + + QAbstractItemView::ScrollPerPixel + + + true + + + + + diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 7e8ab6c..c05355b 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -189,6 +189,39 @@ vector RecipeDatabase::retrieveRecipesWithIngredients(vector return this->readRecipesFromTable(t); } +vector RecipeDatabase::retrieveRecipesWithTags(vector tags){ + vector recipes; + if (tags.empty()){ + return recipes; + } + string filterList = surroundString(tags.at(0).getValue(), "'"); + for (unsigned int i = 1; i < tags.size(); i++){ + filterList += ", " + surroundString(tags[i].getValue(), "'"); + } + filterList = '(' + filterList + ')'; + ResultTable t = this->executeSQL("SELECT * FROM recipe WHERE recipeId IN (SELECT recipeId FROM recipeTag WHERE tagName IN "+filterList+" );"); + return this->readRecipesFromTable(t); +} + +vector RecipeDatabase::retrieveRecipesWithSubstring(string s){ + ResultTable t = this->executeSQL("SELECT * FROM recipe WHERE name LIKE '%"+s+"%' COLLATE NOCASE;"); + 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; diff --git a/model/database/recipedatabase.h b/model/database/recipedatabase.h index f58dd19..34c767d 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -35,6 +35,9 @@ class RecipeDatabase : public Database Recipe retrieveRandomRecipe(); vector retrieveAllRecipes(); vector retrieveRecipesWithIngredients(vector ingredients); + vector retrieveRecipesWithTags(vector tags); + vector retrieveRecipesWithSubstring(string s); + vector retrieveRecipesWithFoodGroups(vector groups); vector retrieveAllFoodGroups(); vector retrieveRecipeIngredients(int recipeId); vector retrieveAllIngredients();