RecipeDB/gui/openrecipedialog.cpp

92 lines
3.1 KiB
C++
Raw Normal View History

#include "openrecipedialog.h"
#include "ui_openrecipedialog.h"
OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::OpenRecipeDialog)
{
ui->setupUi(this);
ui->recipeTableView->setModel(&this->recipeTableModel);
ui->ingredientsListView->setModel(&this->ingredientsModel);
ui->tagsListView->setModel(&this->tagsModel);
2018-03-30 14:20:09 +00:00
QObject::connect(ui->ingredientsListView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
2018-03-30 14:20:09 +00:00
this,
SLOT(onIngredientsListViewSelectionChanged(QItemSelection)));
}
OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){
this->recipeDB = recipeDB;
this->populateIngredientsList();
this->populateTagsList();
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
}
OpenRecipeDialog::~OpenRecipeDialog()
{
delete ui;
}
Recipe OpenRecipeDialog::getSelectedRecipe(){
return this->selectedRecipe;
}
void OpenRecipeDialog::populateRecipesTable(vector<Recipe> recipes){
this->recipeTableModel.clear();
this->recipeTableModel.setRecipes(recipes);
ui->recipeTableView->resizeColumnsToContents();
ui->recipeTableView->show();
}
void OpenRecipeDialog::populateIngredientsList(){
this->ingredientsModel.setIngredients(this->recipeDB->retrieveAllIngredients());
}
void OpenRecipeDialog::populateTagsList(){
this->tagsModel.setTags(this->recipeDB->retrieveAllTags());
}
void OpenRecipeDialog::on_deleteRecipeButton_clicked(){
QItemSelectionModel *selectModel = ui->recipeTableView->selectionModel();
if (!selectModel->hasSelection()){
return;
}
vector<int> rows;
QModelIndexList indexes = selectModel->selectedRows();
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);
QString content = QString::fromStdString("Are you sure you wish to delete the selected "+recipePlural+"?");
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()));
} else {
this->populateRecipesTable(this->recipeDB->retrieveAllRecipes());
}
}
}
}
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
void OpenRecipeDialog::onIngredientsListViewSelectionChanged(const QItemSelection &selection){
2018-03-30 14:20:09 +00:00
vector<Ingredient> ingredients;
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);
}
this->populateRecipesTable(this->recipeDB->retrieveRecipesWithIngredients(ingredients));
2018-03-30 14:20:09 +00:00
}