Added search by food group.

This commit is contained in:
Andrew Lalis 2018-03-30 22:50:02 +02:00
parent 4170df7fc8
commit f13432a2fe
5 changed files with 185 additions and 68 deletions

View File

@ -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<Ingredient> 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<RecipeTag> 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<string> 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));
}

View File

@ -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<Recipe> recipes);
void populateIngredientsList();
void populateTagsList();
void populateFoodGroupsList();
};
#endif // OPENRECIPEDIALOG_H

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1000</width>
<width>1064</width>
<height>480</height>
</rect>
</property>
@ -25,15 +25,27 @@
<widget class="QWidget" name="searchPanel" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item alignment="Qt::AlignLeft">
<widget class="QWidget" name="tagsSearchpanel" native="true">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="tagLabel">
<property name="text">
<string>Tag</string>
<widget class="QTabWidget" name="tabWidget">
<property name="minimumSize">
<size>
<width>290</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<property name="tabPosition">
<enum>QTabWidget::South</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="tagsTab">
<attribute name="title">
<string>Tags</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QListView" name="tagsListView">
<property name="sizePolicy">
@ -48,27 +60,24 @@
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="isWrapping" stdset="0">
<bool>false</bool>
</property>
<property name="wordWrap">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item alignment="Qt::AlignLeft">
<widget class="QWidget" name="ingredientSearchPanel" native="true">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QLabel" name="ingredientLabel">
<property name="text">
<string>Ingredient</string>
</property>
</widget>
</item>
<widget class="QWidget" name="ingredientsTab">
<attribute name="title">
<string>Ingredients</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QListView" name="ingredientsListView">
<property name="sizePolicy">
@ -83,6 +92,9 @@
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="isWrapping" stdset="0">
<bool>false</bool>
</property>
@ -93,6 +105,30 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Food Groups</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QListWidget" name="foodGroupsListWidget">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QWidget" name="nameSearchPanel" native="true">

View File

@ -189,6 +189,39 @@ vector<Recipe> RecipeDatabase::retrieveRecipesWithIngredients(vector<Ingredient>
return this->readRecipesFromTable(t);
}
vector<Recipe> RecipeDatabase::retrieveRecipesWithTags(vector<RecipeTag> tags){
vector<Recipe> 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<Recipe> RecipeDatabase::retrieveRecipesWithSubstring(string s){
ResultTable t = this->executeSQL("SELECT * FROM recipe WHERE name LIKE '%"+s+"%' COLLATE NOCASE;");
return this->readRecipesFromTable(t);
}
vector<Recipe> RecipeDatabase::retrieveRecipesWithFoodGroups(vector<string> groups){
vector<Recipe> 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<string> RecipeDatabase::retrieveAllFoodGroups(){
ResultTable t = this->executeSQL("SELECT DISTINCT foodGroup FROM ingredient ORDER BY foodGroup;");
vector<string> foodGroups;

View File

@ -35,6 +35,9 @@ class RecipeDatabase : public Database
Recipe retrieveRandomRecipe();
vector<Recipe> retrieveAllRecipes();
vector<Recipe> retrieveRecipesWithIngredients(vector<Ingredient> ingredients);
vector<Recipe> retrieveRecipesWithTags(vector<RecipeTag> tags);
vector<Recipe> retrieveRecipesWithSubstring(string s);
vector<Recipe> retrieveRecipesWithFoodGroups(vector<string> groups);
vector<string> retrieveAllFoodGroups();
vector<RecipeIngredient> retrieveRecipeIngredients(int recipeId);
vector<Ingredient> retrieveAllIngredients();