2018-03-11 07:57:57 +00:00
|
|
|
#include "openrecipedialog.h"
|
|
|
|
#include "ui_openrecipedialog.h"
|
|
|
|
|
|
|
|
OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::OpenRecipeDialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2018-03-11 11:53:30 +00:00
|
|
|
|
|
|
|
ui->recipeTableView->setModel(&this->recipeTableModel);
|
2018-03-30 12:33:48 +00:00
|
|
|
ui->tagsListView->setModel(&this->tagsModel);
|
2018-03-30 14:20:09 +00:00
|
|
|
|
2018-03-30 20:50:02 +00:00
|
|
|
QObject::connect(ui->tagsListView->selectionModel(),
|
|
|
|
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
|
|
|
|
this,
|
|
|
|
SLOT(onTagsListViewSelectionChanged(QItemSelection)));
|
2018-03-11 11:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){
|
|
|
|
this->recipeDB = recipeDB;
|
2018-03-30 12:33:48 +00:00
|
|
|
this->populateTagsList();
|
2018-03-30 18:57:14 +00:00
|
|
|
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
|
2018-03-11 07:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenRecipeDialog::~OpenRecipeDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2018-03-11 11:53:30 +00:00
|
|
|
|
2018-03-29 14:09:58 +00:00
|
|
|
Recipe OpenRecipeDialog::getSelectedRecipe(){
|
|
|
|
return this->selectedRecipe;
|
|
|
|
}
|
|
|
|
|
2018-03-30 18:57:14 +00:00
|
|
|
void OpenRecipeDialog::populateRecipesTable(vector<Recipe> recipes){
|
2018-03-29 14:09:58 +00:00
|
|
|
this->recipeTableModel.clear();
|
2018-03-11 11:53:30 +00:00
|
|
|
this->recipeTableModel.setRecipes(recipes);
|
2018-03-29 14:09:58 +00:00
|
|
|
ui->recipeTableView->resizeColumnsToContents();
|
2018-03-29 07:58:25 +00:00
|
|
|
ui->recipeTableView->show();
|
2018-03-11 11:53:30 +00:00
|
|
|
}
|
2018-03-29 14:09:58 +00:00
|
|
|
|
2018-03-30 12:33:48 +00:00
|
|
|
void OpenRecipeDialog::populateTagsList(){
|
|
|
|
this->tagsModel.setTags(this->recipeDB->retrieveAllTags());
|
|
|
|
}
|
|
|
|
|
2018-03-29 14:09:58 +00:00
|
|
|
void OpenRecipeDialog::on_deleteRecipeButton_clicked(){
|
|
|
|
QItemSelectionModel *selectModel = ui->recipeTableView->selectionModel();
|
|
|
|
if (!selectModel->hasSelection()){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vector<int> rows;
|
2018-03-29 15:17:07 +00:00
|
|
|
QModelIndexList indexes = selectModel->selectedRows();
|
2018-03-29 14:09:58 +00:00
|
|
|
for (int i = 0; i < indexes.count(); i++){
|
|
|
|
rows.push_back(indexes.at(i).row());
|
|
|
|
}
|
|
|
|
string recipePlural = (rows.size() == 1) ? "recipe" : "recipes";
|
|
|
|
QString title = QString::fromStdString("Delete " + recipePlural);
|
2018-03-31 12:01:57 +00:00
|
|
|
QString content = QString::fromStdString("Are you sure you wish to delete the selected "+recipePlural+"?\nAll deleted recipes are permanently deleted.");
|
2018-03-29 14:09:58 +00:00
|
|
|
QMessageBox::StandardButton reply = QMessageBox::question(this, title, content);
|
|
|
|
if (reply == QMessageBox::Yes){
|
|
|
|
for (int row : rows){
|
|
|
|
Recipe r = this->recipeTableModel.getRecipeAt(row);
|
|
|
|
bool success = this->recipeDB->deleteRecipe(r.getName());
|
|
|
|
if (!success){
|
|
|
|
QMessageBox::critical(this, QString::fromStdString("Unable to Delete"), QString::fromStdString("Could not delete recipe "+r.getName()));
|
2018-03-30 18:57:14 +00:00
|
|
|
} else {
|
|
|
|
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
|
2018-03-29 14:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenRecipeDialog::on_recipeTableView_doubleClicked(const QModelIndex &index){
|
|
|
|
this->selectedRecipe = this->recipeTableModel.getRecipeAt(index.row());
|
|
|
|
this->close();
|
|
|
|
}
|
2018-03-30 14:20:09 +00:00
|
|
|
|
2018-03-30 18:57:14 +00:00
|
|
|
void OpenRecipeDialog::onIngredientsListViewSelectionChanged(const QItemSelection &selection){
|
2018-03-30 20:50:02 +00:00
|
|
|
Q_UNUSED(selection);
|
2018-03-30 14:20:09 +00:00
|
|
|
vector<Ingredient> ingredients;
|
2018-03-30 18:57:14 +00:00
|
|
|
QModelIndexList indexes = ui->ingredientsListView->selectionModel()->selectedRows();
|
|
|
|
for (QModelIndex index : indexes){
|
2018-03-30 14:20:09 +00:00
|
|
|
Ingredient i = this->ingredientsModel.getIngredients().at(index.row());
|
|
|
|
ingredients.push_back(i);
|
|
|
|
}
|
2018-03-30 18:57:14 +00:00
|
|
|
this->populateRecipesTable(this->recipeDB->retrieveRecipesWithIngredients(ingredients));
|
2018-03-30 14:20:09 +00:00
|
|
|
}
|
2018-03-30 20:50:02 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2018-03-31 10:53:31 +00:00
|
|
|
|
|
|
|
void OpenRecipeDialog::on_clearSearchButton_clicked(){
|
|
|
|
ui->nameEdit->clear();
|
|
|
|
ui->foodGroupsListWidget->selectionModel()->clearSelection();
|
|
|
|
ui->tagsListView->selectionModel()->clearSelection();
|
|
|
|
ui->ingredientsListView->selectionModel()->clearSelection();
|
|
|
|
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
|
|
|
|
}
|
2018-03-31 12:01:57 +00:00
|
|
|
|
|
|
|
void OpenRecipeDialog::on_exitButton_clicked(){
|
|
|
|
this->close();
|
|
|
|
}
|