Updated UI, added default ingredients.

This commit is contained in:
Andrew Lalis 2018-03-31 15:02:35 +02:00
parent 9e3c59e415
commit c84370be11
10 changed files with 272 additions and 46 deletions

View File

@ -19,6 +19,10 @@ MainWindow::~MainWindow(){
}
void MainWindow::loadFromRecipe(Recipe recipe){
if (recipe.isEmpty()){
setRecipeName("No recipes found.");
setAuthorName("Click 'New' to get started.");
} else {
setRecipeName(recipe.getName());
setInstruction(recipe.getInstruction());
setIngredients(recipe.getIngredients());
@ -33,6 +37,7 @@ void MainWindow::loadFromRecipe(Recipe recipe){
setTags(recipe.getTags());
this->currentRecipe = recipe;
}
}
void MainWindow::setRecipeName(string name){
ui->recipeNameLabel->setText(QString::fromStdString(name));
@ -66,6 +71,10 @@ void MainWindow::setTags(vector<RecipeTag> tags){
this->tagsListModel.setTags(tags);
}
void MainWindow::setAuthorName(string name){
ui->authorLabel->setText(QString::fromStdString(name));
}
void MainWindow::on_newButton_clicked(){
NewRecipeDialog d(this->recipeDB, this);
d.show();

View File

@ -54,6 +54,7 @@ public:
void setCookTime(QTime cookTime);
void setServings(float servings);
void setTags(vector<RecipeTag> tags);
void setAuthorName(string name);
};
#endif // MAINWINDOW_H

View File

@ -364,7 +364,7 @@ font: &quot;Noto Sans CJK KR&quot;;</string>
<item alignment="Qt::AlignLeft|Qt::AlignBottom">
<widget class="QLabel" name="authorLabel">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">

View File

@ -23,6 +23,7 @@ NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, QWidget *parent) : NewRecip
NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, Recipe recipe, QWidget *parent) : NewRecipeDialog(db, parent){
ui->recipeNameEdit->setText(QString::fromStdString(recipe.getName()));
ui->authorNameEdit->setText(QString::fromStdString(recipe.getAuthor()));
ui->prepTimeEdit->setTime(recipe.getPrepTime());
ui->cookTimeEdit->setTime(recipe.getCookTime());
ui->servingsSpinBox->setValue((double)recipe.getServings());
@ -38,6 +39,7 @@ NewRecipeDialog::~NewRecipeDialog(){
Recipe NewRecipeDialog::getRecipe(){
Recipe r(ui->recipeNameEdit->text().toStdString(),
ui->authorNameEdit->text().toStdString(),
this->ingredientListModel.getIngredients(),
ui->instructionsTextEdit->toHtml().toStdString(),
ui->imageDisplayLabel->pixmap()->toImage(),//Image

View File

@ -96,15 +96,25 @@
<item>
<widget class="QWidget" name="recipeNamePanel" native="true">
<layout class="QVBoxLayout" name="verticalLayout_9">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="recipeNameEdit">
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>3</weight>
<italic>false</italic>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="frame">
<bool>false</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
@ -113,6 +123,30 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="authorNameEdit">
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>3</weight>
<italic>false</italic>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(245, 245, 255);</string>
</property>
<property name="frame">
<bool>false</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="placeholderText">
<string>Author</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -141,7 +175,7 @@
<second>0</second>
<year>1999</year>
<month>12</month>
<day>23</day>
<day>22</day>
</datetime>
</property>
<property name="currentSection">
@ -252,6 +286,9 @@
<item alignment="Qt::AlignTop">
<widget class="QWidget" name="newTagBoxPanel" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_7">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
@ -285,6 +322,9 @@
<property name="toolTip">
<string>Create a new tag</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/images/plus_icon.png</normaloff>:/images/images/plus_icon.png</iconset>
@ -296,6 +336,9 @@
<property name="toolTip">
<string>Permanently delete this tag</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/images/minus_icon.png</normaloff>:/images/images/minus_icon.png</iconset>
@ -308,6 +351,9 @@
<item>
<widget class="QWidget" name="tagsListControlPanel" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_8">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
@ -322,6 +368,27 @@
</property>
<item>
<widget class="QPushButton" name="addTagButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="toolTip">
<string>Add the above tag to the recipe</string>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#addTagButton {
background-color: rgb(235, 235, 255);
border: 0px;
}
QPushButton#addTagButton:hover{
background-color: rgb(245, 245, 255);
}
QPushButton#addTagButton:pressed{
background-color: rgb(255, 255, 255);
}</string>
</property>
<property name="text">
<string>Add</string>
</property>
@ -329,8 +396,29 @@
</item>
<item>
<widget class="QPushButton" name="deleteTagButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="toolTip">
<string>Remove this tag from the recipe</string>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#deleteTagButton {
background-color: rgb(225, 225, 255);
border: 0px;
}
QPushButton#deleteTagButton:hover{
background-color: rgb(235, 235, 255);
}
QPushButton#deleteTagButton:pressed{
background-color: rgb(245, 245, 255);
}</string>
</property>
<property name="text">
<string>Delete</string>
<string>Remove</string>
</property>
</widget>
</item>
@ -434,7 +522,7 @@
<item>
<widget class="QLabel" name="addIngredientLabel">
<property name="text">
<string>Add Ingredient</string>
<string>Ingredients</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
@ -497,6 +585,9 @@
</item>
<item>
<widget class="QPushButton" name="deleteIngredientButton">
<property name="toolTip">
<string>Delete this ingredient</string>
</property>
<property name="text">
<string/>
</property>
@ -581,6 +672,9 @@
</item>
<item alignment="Qt::AlignRight">
<widget class="QPushButton" name="newUnitButton">
<property name="toolTip">
<string>Create a new unit of measure</string>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/images/plus_icon.png</normaloff>:/images/images/plus_icon.png</iconset>
@ -589,6 +683,9 @@
</item>
<item>
<widget class="QPushButton" name="deleteUnitButton">
<property name="toolTip">
<string>Delete this unit of measure</string>
</property>
<property name="text">
<string/>
</property>
@ -626,6 +723,9 @@
<item>
<widget class="QWidget" name="ingredientsListControlPanel" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_9">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
@ -640,6 +740,27 @@
</property>
<item>
<widget class="QPushButton" name="addIngredientButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="toolTip">
<string>Add the above ingredient to the recipe</string>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#addIngredientButton {
background-color: rgb(235, 235, 255);
border: 0px;
}
QPushButton#addIngredientButton:hover{
background-color: rgb(245, 245, 255);
}
QPushButton#addIngredientButton:pressed{
background-color: rgb(255, 255, 255);
}</string>
</property>
<property name="text">
<string>Add</string>
</property>
@ -647,8 +768,29 @@
</item>
<item>
<widget class="QPushButton" name="removeIngredientButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="toolTip">
<string>Remove this ingredient from the recipe</string>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#removeIngredientButton {
background-color: rgb(225, 225, 255);
border: 0px;
}
QPushButton#removeIngredientButton:hover{
background-color: rgb(235, 235, 255);
}
QPushButton#removeIngredientButton:pressed{
background-color: rgb(245, 245, 255);
}</string>
</property>
<property name="text">
<string>Delete</string>
<string>Remove</string>
</property>
</widget>
</item>

View File

@ -8,20 +8,25 @@
void test(RecipeDatabase *recipeDB);
Recipe checkForFirstRun(RecipeDatabase *recipeDB){
Recipe r = recipeDB->retrieveRandomRecipe();
if (r.isEmpty()){//There are no recipes in the database.
//Add some basic units to the units, and some basic ingredients.
recipeDB->addBasicUnits();
recipeDB->addBasicIngredients();
}
return r;
}
int main(int argc, char *argv[])
{
RecipeDatabase recipeDB(QString(FileUtils::appDataPath+"recipes.db").toStdString());
QApplication a(argc, argv);
MainWindow w(&recipeDB);
w.loadFromRecipe(checkForFirstRun(&recipeDB));
w.show();
//TESTING CODE
//test(&recipeDB);
//END TESTING CODE.
w.loadFromRecipe(recipeDB.retrieveRandomRecipe());
a.exec();
recipeDB.closeConnection();
@ -34,6 +39,7 @@ void test(RecipeDatabase *recipeDB){
ri.push_back(RecipeIngredient("baking powder", "additives", 1.0f, UnitOfMeasure("teaspoon", "teaspoons", "tsp", UnitOfMeasure::VOLUME, 1.0), ""));
Recipe rec("Example",
"Andrew Lalis",
ri,
Instruction("Placeholder Text"),
QImage(),

View File

@ -12,7 +12,7 @@ bool RecipeDatabase::storeRecipe(Recipe recipe){
return false;
}
//Store a recipe, if it doesn't already exist. This first tries to create the recipe entry, then all subsequent supporting table entries.
this->executeSQL("BEGIN;");
this->beginTransaction();
ResultTable t = this->selectFrom("recipe", "*", "WHERE name="+surroundString(recipe.getName(), "'"));
if (!t.isEmpty()){
fprintf(stderr, "Error storing recipe: Recipe with name %s already exists.\n", recipe.getName().c_str());
@ -20,6 +20,7 @@ bool RecipeDatabase::storeRecipe(Recipe recipe){
bool success = this->insertInto("recipe",
vector<string>({
"name",
"authorName",
"createdDate",
"cookTime",
"prepTime",
@ -27,6 +28,7 @@ bool RecipeDatabase::storeRecipe(Recipe recipe){
}),
vector<string>({
recipe.getName(),
recipe.getAuthor(),
recipe.getCreatedDate().toString().toStdString(),
recipe.getCookTime().toString().toStdString(),
recipe.getPrepTime().toString().toStdString(),
@ -46,12 +48,12 @@ bool RecipeDatabase::storeRecipe(Recipe recipe){
this->storeInstruction(recipe.getInstruction(), recipeId) &&
this->storeImage(recipe.getImage(), recipeId) &&
this->storeTags(recipe.getTags(), recipeId)){
this->executeSQL("COMMIT;");
this->commitTransaction();
return true;
}
}
}
this->executeSQL("ROLLBACK;");
this->rollbackTransaction();
return false;
}
@ -158,7 +160,7 @@ Recipe RecipeDatabase::retrieveRandomRecipe(){
}
return this->readFromResultTable(t);
}
//TODO: Change this to be more efficient! One query per recipe is not good!
vector<Recipe> RecipeDatabase::retrieveAllRecipes(){
ResultTable t = this->executeSQL("SELECT * FROM recipe ORDER BY name;");
return this->readRecipesFromTable(t);
@ -326,7 +328,7 @@ bool RecipeDatabase::deleteRecipe(int recipeId){
printf("Cannot delete. No recipe with ID %d exists.\n", recipeId);
return false;
}
this->executeSQL("BEGIN;");
this->beginTransaction();
bool tagsDeleted = this->deleteFrom("recipeTag", "WHERE recipeId="+idString);
bool recipeIngredientDeleted = this->deleteFrom("recipeIngredient", "WHERE recipeId="+idString);
bool recipeDeleted = this->deleteFrom("recipe", "WHERE recipeId="+idString);
@ -335,10 +337,10 @@ bool RecipeDatabase::deleteRecipe(int recipeId){
Q_UNUSED(instructionDeleted);
Q_UNUSED(imageDeleted);
if (tagsDeleted && recipeIngredientDeleted && recipeDeleted){
this->executeSQL("COMMIT;");
this->commitTransaction();
return true;
} else {
this->executeSQL("ROLLBACK;");
this->rollbackTransaction();
return false;
}
}
@ -374,6 +376,7 @@ bool RecipeDatabase::updateRecipe(Recipe recipe, string originalName) {
this->beginTransaction();
ResultTable t = this->executeSQL("UPDATE recipe "
"SET name = '"+recipe.getName()+"', "
"authorName = '"+recipe.getAuthor()+"', "
"createdDate = '"+recipe.getCreatedDate().toString().toStdString()+"', "
"prepTime = '"+recipe.getPrepTime().toString().toStdString()+"', "
"cookTime = '"+recipe.getCookTime().toString().toStdString()+"', "
@ -435,11 +438,55 @@ bool RecipeDatabase::updateRecipe(Recipe recipe, string originalName) {
}
}
bool RecipeDatabase::addBasicUnits(){
this->beginTransaction();
//Volume
this->storeUnitOfMeasure(UnitOfMeasure("Teaspoon", "Teaspoons", "tsp", UnitOfMeasure::VOLUME, 5.0));
this->storeUnitOfMeasure(UnitOfMeasure("Tablespoon", "Tablespoons", "tbsp", UnitOfMeasure::VOLUME, 15.0));
this->storeUnitOfMeasure(UnitOfMeasure("Fluid Ounce", "Fluid Ounces", "fl oz", UnitOfMeasure::VOLUME, 30.0));
this->storeUnitOfMeasure(UnitOfMeasure("Cup", "Cups", "c", UnitOfMeasure::VOLUME, 250.0));
this->storeUnitOfMeasure(UnitOfMeasure("Milliliter", "Milliliters", "mL", UnitOfMeasure::VOLUME, 1.0));
this->storeUnitOfMeasure(UnitOfMeasure("Liter", "Liters", "L", UnitOfMeasure::VOLUME, 1000.0));
this->storeUnitOfMeasure(UnitOfMeasure("Gallon", "Gallons", "gal", UnitOfMeasure::VOLUME, 3800.0));
//Mass/Weight
this->storeUnitOfMeasure(UnitOfMeasure("Ounce", "Ounces", "oz", UnitOfMeasure::MASS, 28.0));
this->storeUnitOfMeasure(UnitOfMeasure("Pound", "Pounds", "lb", UnitOfMeasure::MASS, 454.0));
this->storeUnitOfMeasure(UnitOfMeasure("Gram", "Grams", "g", UnitOfMeasure::MASS, 1.0));
this->storeUnitOfMeasure(UnitOfMeasure("Milligram", "Milligrams", "mg", UnitOfMeasure::MASS, 0.001));
this->storeUnitOfMeasure(UnitOfMeasure("Kilogram", "Kilograms", "kg", UnitOfMeasure::MASS, 1000.0));
//Length
this->storeUnitOfMeasure(UnitOfMeasure("Inch", "Inches", "in", UnitOfMeasure::LENGTH, 2.54));
this->storeUnitOfMeasure(UnitOfMeasure("Centimeter", "Centimeters", "cm", UnitOfMeasure::LENGTH, 1.0));
//MISC
this->storeUnitOfMeasure(UnitOfMeasure("Piece", "Pieces", "pc", UnitOfMeasure::MISC, 1.0));
this->storeUnitOfMeasure(UnitOfMeasure("Item", "Items", "", UnitOfMeasure::MISC, 1.0));
this->commitTransaction();
return true;
}
bool RecipeDatabase::addBasicIngredients(){
this->beginTransaction();
this->storeIngredient(Ingredient("Flour", "grains"));
this->storeIngredient(Ingredient("Eggs", "eggs"));
this->storeIngredient(Ingredient("Milk", "dairy"));
this->storeIngredient(Ingredient("Cheese", "dairy"));
this->storeIngredient(Ingredient("Salt", "spices"));
this->storeIngredient(Ingredient("Sugar", "sugars"));
this->storeIngredient(Ingredient("Vegetable Oil", "oils"));
this->storeIngredient(Ingredient("Olive Oil", "oils"));
this->storeIngredient(Ingredient("Water", "water"));
this->storeIngredient(Ingredient("Bell Pepper", "vegetables"));
this->storeIngredient(Ingredient("Onion", "vegetables"));
this->storeIngredient(Ingredient("Garlic", "spices"));
this->commitTransaction();
return true;
}
void RecipeDatabase::ensureTablesExist(){
//Make sure that foreign keys are enabled.
this->executeSQL("PRAGMA foreign_keys = ON;");
this->executeSQL("BEGIN;");
this->beginTransaction();
//Ingredients table.
this->executeSQL("CREATE TABLE IF NOT EXISTS ingredient("
"ingredientId INTEGER PRIMARY KEY,"
@ -456,6 +503,7 @@ void RecipeDatabase::ensureTablesExist(){
this->executeSQL("CREATE TABLE IF NOT EXISTS recipe("
"recipeId INTEGER PRIMARY KEY,"
"name varchar UNIQUE,"
"authorName varchar,"
"createdDate date,"
"prepTime time,"
"cookTime time,"
@ -475,18 +523,19 @@ void RecipeDatabase::ensureTablesExist(){
"FOREIGN KEY (ingredientId) REFERENCES ingredient(ingredientId),"
"FOREIGN KEY (recipeId) REFERENCES recipe(recipeId),"
"FOREIGN KEY (unitName) REFERENCES unitOfMeasure(name));");
this->executeSQL("COMMIT;");
this->commitTransaction();
}
Recipe RecipeDatabase::readFromResultTable(ResultTable t, int tRow){
Recipe r;
TableRow row = t.rows().at(tRow);
int id = std::stoi(row.at(0));
r.setName(row.at(1));
r.setCreatedDate(QDate::fromString(QString::fromStdString(row.at(2))));
r.setPrepTime(QTime::fromString(QString::fromStdString(row.at(3))));
r.setCookTime(QTime::fromString(QString::fromStdString(row.at(4))));
r.setServings(std::stof(row.at(5)));
int id = std::stoi(row.at(0)); //id
r.setName(row.at(1)); //Name
r.setAuthor(row.at(2)); //author
r.setCreatedDate(QDate::fromString(QString::fromStdString(row.at(3)))); //createdDate
r.setPrepTime(QTime::fromString(QString::fromStdString(row.at(4)))); //prepTime
r.setCookTime(QTime::fromString(QString::fromStdString(row.at(5)))); //cookTime
r.setServings(std::stof(row.at(6))); //servings
r.setInstruction(FileUtils::loadInstruction(id));
r.setImage(FileUtils::loadImage(id));
r.setIngredients(this->retrieveRecipeIngredients(id));

View File

@ -49,6 +49,10 @@ class RecipeDatabase : public Database
//Updating.
bool updateRecipe(Recipe recipe, string originalName);
//Adding basic information at start.
bool addBasicUnits();
bool addBasicIngredients();
private:
//Utility methods.

View File

@ -1,7 +1,8 @@
#include "model/recipe/recipe.h"
Recipe::Recipe(string name, vector<RecipeIngredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings){
Recipe::Recipe(string name, string author, vector<RecipeIngredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings){
setName(name);
setAuthor(author);
setIngredients(ingredients);
setInstruction(instruction);
setImage(image);
@ -12,7 +13,7 @@ Recipe::Recipe(string name, vector<RecipeIngredient> ingredients, Instruction in
setServings(servings);
}
Recipe::Recipe() : Recipe::Recipe("", vector<RecipeIngredient>(), Instruction(), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(), QTime(), 1.0f){
Recipe::Recipe() : Recipe::Recipe("", "", vector<RecipeIngredient>(), Instruction(), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(), QTime(), 1.0f){
//Set default values when none are specified.
}
@ -20,6 +21,10 @@ string Recipe::getName() const{
return this->name;
}
string Recipe::getAuthor() const{
return this->authorName;
}
vector<RecipeIngredient> Recipe::getIngredients() const{
return this->ingredients;
}
@ -74,6 +79,10 @@ void Recipe::setName(string newName){
this->name = newName;
}
void Recipe::setAuthor(string newName){
this->authorName = newName;
}
void Recipe::setIngredients(vector<RecipeIngredient> ingredients){
this->ingredients = ingredients;
}
@ -111,8 +120,9 @@ void Recipe::setServings(float newServingsCount){
}
void Recipe::print(){
printf("Recipe: %s, Created on: %s, Prep time: %s, Cook time: %s, Serves: %f\n",
printf("Recipe: %s, Created on: %s, by %s, Prep time: %s, Cook time: %s, Serves: %f\n",
this->name.c_str(),
this->authorName.c_str(),
this->createdDate.toString().toStdString().c_str(),
this->prepTime.toString().toStdString().c_str(),
this->cookTime.toString().toStdString().c_str(),

View File

@ -32,12 +32,13 @@ class Recipe
{
public:
//Full constructor
Recipe(string name, vector<RecipeIngredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings);
Recipe(string name, string author, vector<RecipeIngredient> ingredients, Instruction instruction, QImage image, vector<RecipeTag> tags, QDate createdDate, QTime prepTime, QTime cookTime, float servings);
//Constructor with default values.
Recipe();
//Getters
string getName() const;
string getAuthor() const;
vector<RecipeIngredient> getIngredients() const;
vector<string> getFoodGroups() const;
Instruction getInstruction() const;
@ -52,6 +53,7 @@ public:
//Setters
void setName(string newName);
void setAuthor(string newName);
void setIngredients(vector<RecipeIngredient> ingredients);
void setTags(vector<RecipeTag> tags);
void addIngredient(RecipeIngredient newIngredient);
@ -66,6 +68,7 @@ public:
private:
//Main information.
string name; //The name of the recipe.
string authorName; //The name of the author of this recipe.
vector<RecipeIngredient> ingredients; //The list of ingredients in the recipe.
Instruction instruction; //The instruction HTML document.
QImage image; //An image displayed alongside the recipe.