Made adding ingredients, tags more secure with extra checks.

This commit is contained in:
Andrew Lalis 2018-03-30 12:56:42 +02:00
parent 35982cd514
commit 9065df521a
4 changed files with 77 additions and 14 deletions

View File

@ -8,11 +8,26 @@ NewIngredientDialog::NewIngredientDialog(QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
} }
NewIngredientDialog::NewIngredientDialog(RecipeDatabase *recipeDB, QWidget *parent) : NewIngredientDialog(parent){
this->recipeDB = recipeDB;
this->populateFoodGroupBox();
}
NewIngredientDialog::~NewIngredientDialog() NewIngredientDialog::~NewIngredientDialog()
{ {
delete ui; delete ui;
} }
Ingredient NewIngredientDialog::getIngredient(){ Ingredient NewIngredientDialog::getIngredient(){
return Ingredient(ui->nameEdit->text().toLower().toStdString(), ui->foodGroupEdit->text().toLower().toStdString()); return Ingredient(ui->nameEdit->text().toLower().toStdString(), ui->foodGroupBox->currentText().toStdString());
}
void NewIngredientDialog::populateFoodGroupBox(){
vector<string> foodGroups = this->recipeDB->retrieveAllFoodGroups();
printf("Found %ld food Groups.\n", foodGroups.size());
ui->foodGroupBox->clear();
for (unsigned int i = 0; i < foodGroups.size(); i++){
QString s = QString::fromStdString(foodGroups[i]);
ui->foodGroupBox->insertItem(i, s);
}
} }

View File

@ -3,6 +3,7 @@
#include <QDialog> #include <QDialog>
#include "model/recipe/ingredients/ingredient.h" #include "model/recipe/ingredients/ingredient.h"
#include "model/database/recipedatabase.h"
namespace Ui { namespace Ui {
class NewIngredientDialog; class NewIngredientDialog;
@ -14,6 +15,7 @@ class NewIngredientDialog : public QDialog
public: public:
explicit NewIngredientDialog(QWidget *parent = 0); explicit NewIngredientDialog(QWidget *parent = 0);
NewIngredientDialog(RecipeDatabase *recipeDB, QWidget *parent = 0);
~NewIngredientDialog(); ~NewIngredientDialog();
//Access values. //Access values.
@ -21,6 +23,9 @@ class NewIngredientDialog : public QDialog
private: private:
Ui::NewIngredientDialog *ui; Ui::NewIngredientDialog *ui;
RecipeDatabase *recipeDB;
void populateFoodGroupBox();
}; };
#endif // NEWINGREDIENTDIALOG_H #endif // NEWINGREDIENTDIALOG_H

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>240</width> <width>367</width>
<height>320</height> <height>228</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -60,7 +60,42 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLineEdit" name="foodGroupEdit"/> <widget class="QWidget" name="foodGroupSelectionWidget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QComboBox" name="foodGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item alignment="Qt::AlignRight">
<widget class="QPushButton" name="addFoodGroupButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images.qrc">
<normaloff>:/images/images/plus_icon.png</normaloff>:/images/images/plus_icon.png</iconset>
</property>
</widget>
</item>
<item alignment="Qt::AlignRight">
<widget class="QPushButton" name="deleteFoodGroupButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images.qrc">
<normaloff>:/images/images/minus_icon.png</normaloff>:/images/images/minus_icon.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>

View File

@ -145,13 +145,17 @@ void NewRecipeDialog::on_deleteIngredientButton_clicked(){
} }
void NewRecipeDialog::on_newIngredientButton_clicked(){ void NewRecipeDialog::on_newIngredientButton_clicked(){
NewIngredientDialog d(this); NewIngredientDialog d(this->recipeDB, this);
d.show(); d.show();
if (d.exec() == QDialog::Accepted){ if (d.exec() == QDialog::Accepted){
Ingredient i = d.getIngredient(); Ingredient i = d.getIngredient();
this->recipeDB->storeIngredient(i); if (!i.getName().empty() && !i.getFoodGroup().empty() && this->recipeDB->storeIngredient(i)){
this->populateIngredientsBox(); this->populateIngredientsBox();
ui->ingredientNameBox->setCurrentText(QString::fromStdString(i.getName())); ui->ingredientNameBox->setCurrentText(QString::fromStdString(i.getName()));
} else {
QMessageBox::critical(this, "Error", "Unable to add ingredient.");
}
} }
} }
@ -161,12 +165,16 @@ void NewRecipeDialog::on_newTagButton_clicked(){
if (d.exec() == QDialog::Accepted){ if (d.exec() == QDialog::Accepted){
RecipeTag tag = d.getTag(); RecipeTag tag = d.getTag();
//Temporarily add this to the tags list, and it will be saved if the recipe is saved. //Temporarily add this to the tags list, and it will be saved if the recipe is saved.
this->tags.push_back(tag); if (!tag.getValue().empty()){
this->tagsListModel.addTag(tag); this->tags.push_back(tag);
ui->tagsComboBox->clear(); this->tagsListModel.addTag(tag);
for (unsigned int i = 0; i < this->tags.size(); i++){ ui->tagsComboBox->clear();
QString s = QString::fromStdString(this->tags[i].getValue()); for (unsigned int i = 0; i < this->tags.size(); i++){
ui->tagsComboBox->insertItem(i, s); QString s = QString::fromStdString(this->tags[i].getValue());
ui->tagsComboBox->insertItem(i, s);
}
} else {
QMessageBox::warning(this, "Empty Tag", "The tag you entered is blank!");
} }
} }