Added filtering by ingredient to search window.
This commit is contained in:
parent
e0dda64412
commit
4170df7fc8
|
@ -11,17 +11,17 @@ OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) :
|
||||||
ui->ingredientsListView->setModel(&this->ingredientsModel);
|
ui->ingredientsListView->setModel(&this->ingredientsModel);
|
||||||
ui->tagsListView->setModel(&this->tagsModel);
|
ui->tagsListView->setModel(&this->tagsModel);
|
||||||
|
|
||||||
connect(ui->ingredientsListView->selectionModel(),
|
QObject::connect(ui->ingredientsListView->selectionModel(),
|
||||||
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
|
SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
|
||||||
this,
|
this,
|
||||||
SLOT(on_ingredientsListView_selectionChanged(QItemSelection)));
|
SLOT(onIngredientsListViewSelectionChanged(QItemSelection)));
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){
|
OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){
|
||||||
this->recipeDB = recipeDB;
|
this->recipeDB = recipeDB;
|
||||||
this->populateIngredientsList();
|
this->populateIngredientsList();
|
||||||
this->populateTagsList();
|
this->populateTagsList();
|
||||||
this->populateRecipesTable();
|
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenRecipeDialog::~OpenRecipeDialog()
|
OpenRecipeDialog::~OpenRecipeDialog()
|
||||||
|
@ -33,9 +33,8 @@ Recipe OpenRecipeDialog::getSelectedRecipe(){
|
||||||
return this->selectedRecipe;
|
return this->selectedRecipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenRecipeDialog::populateRecipesTable(){
|
void OpenRecipeDialog::populateRecipesTable(vector<Recipe> recipes){
|
||||||
this->recipeTableModel.clear();
|
this->recipeTableModel.clear();
|
||||||
vector<Recipe> recipes = this->recipeDB->retrieveAllRecipes();
|
|
||||||
this->recipeTableModel.setRecipes(recipes);
|
this->recipeTableModel.setRecipes(recipes);
|
||||||
ui->recipeTableView->resizeColumnsToContents();
|
ui->recipeTableView->resizeColumnsToContents();
|
||||||
ui->recipeTableView->show();
|
ui->recipeTableView->show();
|
||||||
|
@ -69,9 +68,10 @@ void OpenRecipeDialog::on_deleteRecipeButton_clicked(){
|
||||||
bool success = this->recipeDB->deleteRecipe(r.getName());
|
bool success = this->recipeDB->deleteRecipe(r.getName());
|
||||||
if (!success){
|
if (!success){
|
||||||
QMessageBox::critical(this, QString::fromStdString("Unable to Delete"), QString::fromStdString("Could not delete recipe "+r.getName()));
|
QMessageBox::critical(this, QString::fromStdString("Unable to Delete"), QString::fromStdString("Could not delete recipe "+r.getName()));
|
||||||
|
} else {
|
||||||
|
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->populateRecipesTable();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,13 +80,12 @@ void OpenRecipeDialog::on_recipeTableView_doubleClicked(const QModelIndex &index
|
||||||
this->close();
|
this->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenRecipeDialog::on_ingredientsListView_selectionChanged(const QItemSelection &selection){
|
void OpenRecipeDialog::onIngredientsListViewSelectionChanged(const QItemSelection &selection){
|
||||||
printf("Selection changed!\n");
|
|
||||||
vector<Ingredient> ingredients;
|
vector<Ingredient> ingredients;
|
||||||
for (QModelIndex index : selection.indexes()){
|
QModelIndexList indexes = ui->ingredientsListView->selectionModel()->selectedRows();
|
||||||
|
for (QModelIndex index : indexes){
|
||||||
Ingredient i = this->ingredientsModel.getIngredients().at(index.row());
|
Ingredient i = this->ingredientsModel.getIngredients().at(index.row());
|
||||||
ingredients.push_back(i);
|
ingredients.push_back(i);
|
||||||
printf("Selected: %s\n", i.getName().c_str());
|
|
||||||
}
|
}
|
||||||
|
this->populateRecipesTable(this->recipeDB->retrieveRecipesWithIngredients(ingredients));
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ class OpenRecipeDialog : public QDialog
|
||||||
|
|
||||||
void on_recipeTableView_doubleClicked(const QModelIndex &index);
|
void on_recipeTableView_doubleClicked(const QModelIndex &index);
|
||||||
|
|
||||||
void on_ingredientsListView_selectionChanged(const QItemSelection &selection);
|
void onIngredientsListViewSelectionChanged(const QItemSelection &selection);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::OpenRecipeDialog *ui;
|
Ui::OpenRecipeDialog *ui;
|
||||||
|
@ -41,7 +41,7 @@ class OpenRecipeDialog : public QDialog
|
||||||
IngredientListModel ingredientsModel;
|
IngredientListModel ingredientsModel;
|
||||||
TagListModel tagsModel;
|
TagListModel tagsModel;
|
||||||
|
|
||||||
void populateRecipesTable();
|
void populateRecipesTable(vector<Recipe> recipes);
|
||||||
void populateIngredientsList();
|
void populateIngredientsList();
|
||||||
void populateTagsList();
|
void populateTagsList();
|
||||||
};
|
};
|
||||||
|
|
|
@ -158,14 +158,35 @@ Recipe RecipeDatabase::retrieveRandomRecipe(){
|
||||||
}
|
}
|
||||||
return this->readFromResultTable(t);
|
return this->readFromResultTable(t);
|
||||||
}
|
}
|
||||||
|
//TODO: Change this to be more efficient! One query per recipe is not good!
|
||||||
vector<Recipe> RecipeDatabase::retrieveAllRecipes(){
|
vector<Recipe> RecipeDatabase::retrieveAllRecipes(){
|
||||||
ResultTable t = this->selectFrom("recipe", "name", "ORDER BY name");
|
ResultTable t = this->executeSQL("SELECT * FROM recipe ORDER BY name;");
|
||||||
|
return this->readRecipesFromTable(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<Recipe> RecipeDatabase::retrieveRecipesWithIngredients(vector<Ingredient> ingredients){
|
||||||
vector<Recipe> recipes;
|
vector<Recipe> recipes;
|
||||||
for (TableRow row : t.rows()){
|
if (ingredients.empty()){
|
||||||
recipes.push_back(this->retrieveRecipe(row.at(0)));
|
return recipes;
|
||||||
}
|
}
|
||||||
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<string> RecipeDatabase::retrieveAllFoodGroups(){
|
vector<string> RecipeDatabase::retrieveAllFoodGroups(){
|
||||||
|
@ -360,3 +381,13 @@ Recipe RecipeDatabase::readFromResultTable(ResultTable t, int tRow){
|
||||||
r.setTags(this->retrieveTags(id));
|
r.setTags(this->retrieveTags(id));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Retrieves recipes from a table with the following format:
|
||||||
|
// id, name, createdDate, prepTime, cookTime, servings
|
||||||
|
vector<Recipe> RecipeDatabase::readRecipesFromTable(ResultTable t){
|
||||||
|
vector<Recipe> recipes;
|
||||||
|
for (unsigned int row = 0; row < t.rowCount(); row++){
|
||||||
|
recipes.push_back(readFromResultTable(t, row));
|
||||||
|
}
|
||||||
|
return recipes;
|
||||||
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ class RecipeDatabase : public Database
|
||||||
Recipe retrieveRecipe(string name);
|
Recipe retrieveRecipe(string name);
|
||||||
Recipe retrieveRandomRecipe();
|
Recipe retrieveRandomRecipe();
|
||||||
vector<Recipe> retrieveAllRecipes();
|
vector<Recipe> retrieveAllRecipes();
|
||||||
|
vector<Recipe> retrieveRecipesWithIngredients(vector<Ingredient> ingredients);
|
||||||
vector<string> retrieveAllFoodGroups();
|
vector<string> retrieveAllFoodGroups();
|
||||||
vector<RecipeIngredient> retrieveRecipeIngredients(int recipeId);
|
vector<RecipeIngredient> retrieveRecipeIngredients(int recipeId);
|
||||||
vector<Ingredient> retrieveAllIngredients();
|
vector<Ingredient> retrieveAllIngredients();
|
||||||
|
@ -53,6 +54,7 @@ class RecipeDatabase : public Database
|
||||||
void ensureTablesExist();
|
void ensureTablesExist();
|
||||||
//Read a recipe from a row of a result table.
|
//Read a recipe from a row of a result table.
|
||||||
Recipe readFromResultTable(ResultTable t, int row=0);
|
Recipe readFromResultTable(ResultTable t, int row=0);
|
||||||
|
vector<Recipe> readRecipesFromTable(ResultTable t);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // RECIPEDATABASE_H
|
#endif // RECIPEDATABASE_H
|
||||||
|
|
Loading…
Reference in New Issue