From 61711323ccfc3438fdbd079f653f0ed867b7c7ba Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sat, 10 Mar 2018 10:42:22 +0100 Subject: [PATCH] Polished up adding ingredients to the recipes list. --- gui/mainwindow.ui | 8 ++- gui/newrecipedialog.cpp | 26 +++++-- gui/newrecipedialog.h | 8 ++- gui/newrecipedialog.ui | 67 ++++++++++++++++--- main.cpp | 6 +- .../ingredients/ingredientlistmodel.cpp | 37 +++++++++- .../recipe/ingredients/ingredientlistmodel.h | 6 ++ 7 files changed, 139 insertions(+), 19 deletions(-) diff --git a/gui/mainwindow.ui b/gui/mainwindow.ui index e93bbfd..ae24fae 100644 --- a/gui/mainwindow.ui +++ b/gui/mainwindow.ui @@ -462,6 +462,12 @@ QPushButton#browseButton:pressed{ + + + Noto Sans CJK KR Medium + PreferAntialias + + background-color: rgb(244, 244, 244); @@ -493,7 +499,7 @@ QPushButton#browseButton:pressed{ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Liberation Serif Bold'; font-size:11pt; font-weight:400; font-style:normal;"> +</style></head><body style=" font-family:'Noto Sans CJK KR Medium'; font-size:11pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; color:#00ff40;">This is some </span><span style=" font-family:'MS Shell Dlg 2'; font-size:16pt; color:#a33c3e;">colored text and </span><a href="https://www.google.com"><span style=" font-family:'MS Shell Dlg 2'; font-size:8pt; text-decoration: underline; color:#0000ff;">link</span></a></p></body></html> diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index 15364f0..7467d30 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -5,6 +5,8 @@ NewRecipeDialog::NewRecipeDialog(QWidget *parent) : QDialog(parent), ui(new Ui::NewRecipeDialog){ ui->setupUi(this); + + ui->ingredientsListView->setModel(&this->ingredientListModel); } NewRecipeDialog::NewRecipeDialog(RecipeDatabase *db, QWidget *parent) : NewRecipeDialog(parent){ @@ -19,10 +21,6 @@ NewRecipeDialog::~NewRecipeDialog(){ delete ui; } -void NewRecipeDialog::on_toolButton_clicked(){ - ui->instructionsTextEdit->setFontItalic(!ui->instructionsTextEdit->fontItalic()); -} - void NewRecipeDialog::populateIngredientsBox(){ this->ingredients = this->recipeDB->retrieveAllIngredients(); ui->ingredientNameBox->clear(); @@ -40,3 +38,23 @@ void NewRecipeDialog::populateUnitsBox(){ ui->unitComboBox->insertItem(i, s); } } + +void NewRecipeDialog::on_addIngredientButton_clicked(){ + //Construct a recipe ingredient from the supplied data. + Ingredient i = this->ingredients[ui->ingredientNameBox->currentIndex()]; + UnitOfMeasure u = this->units[ui->unitComboBox->currentIndex()]; + RecipeIngredient ri(i, ui->quantitySpinBox->value(), u, ui->commentsLineEdit->text().toStdString()); + this->ingredientListModel.addIngredient(ri); +} + +void NewRecipeDialog::on_italicsButton_clicked(){ + ui->instructionsTextEdit->setFontItalic(ui->italicsButton->isChecked()); +} + +void NewRecipeDialog::on_boldButton_clicked(){ + if (ui->boldButton->isChecked()){ + ui->instructionsTextEdit->setFontWeight(QFont::Bold); + } else { + ui->instructionsTextEdit->setFontWeight(QFont::Normal); + } +} diff --git a/gui/newrecipedialog.h b/gui/newrecipedialog.h index 81272e4..51322ed 100644 --- a/gui/newrecipedialog.h +++ b/gui/newrecipedialog.h @@ -5,6 +5,7 @@ #include #include "model/database/recipedatabase.h" +#include "model/recipe/ingredients/ingredientlistmodel.h" namespace Ui { class NewRecipeDialog; @@ -20,13 +21,18 @@ class NewRecipeDialog : public QDialog ~NewRecipeDialog(); private slots: - void on_toolButton_clicked(); + void on_addIngredientButton_clicked(); + + void on_italicsButton_clicked(); + + void on_boldButton_clicked(); private: Ui::NewRecipeDialog *ui; RecipeDatabase *recipeDB; vector ingredients; vector units; + IngredientListModel ingredientListModel; //Helper functions to fill fields. void populateIngredientsBox(); diff --git a/gui/newrecipedialog.ui b/gui/newrecipedialog.ui index 9be836e..91c523e 100644 --- a/gui/newrecipedialog.ui +++ b/gui/newrecipedialog.ui @@ -86,7 +86,7 @@ 0 1999 12 - 28 + 27 @@ -160,7 +160,7 @@ - + 1 @@ -335,6 +335,13 @@ + + + + New + + + @@ -389,14 +396,54 @@ - - - - italics - - - Qt::ToolButtonIconOnly - + + + + + + + + Liberation Serif + 12 + true + + + + I + + + true + + + Qt::ToolButtonIconOnly + + + + + + + + Liberation Serif + 12 + 75 + true + + + + B + + + true + + + false + + + false + + + + diff --git a/main.cpp b/main.cpp index 3f700e5..5d09adf 100644 --- a/main.cpp +++ b/main.cpp @@ -18,13 +18,17 @@ int main(int argc, char *argv[]) //TESTING CODE vector ri; ri.push_back(RecipeIngredient("flour", "grains", 3.0f, UnitOfMeasure("cup", "cups", "c", UnitOfMeasure::VOLUME, 1.0), "")); - ri.push_back(RecipeIngredient("Baking Powder", "Additives", 1.0f, UnitOfMeasure("Teaspoon", "Teaspoons", "Tsp", UnitOfMeasure::VOLUME, 1.0), "")); + ri.push_back(RecipeIngredient("baking powder", "Additives", 1.0f, UnitOfMeasure("teaspoon", "teaspoons", "tsp", UnitOfMeasure::VOLUME, 1.0), "")); Recipe rec("Example", ri, Instruction("BOLDiTaLiCs"), QImage(), vector(), QDate::currentDate(), QTime(0, 30), QTime(0, 25), 10.0f); bool success = recipeDB.storeRecipe(rec); printf("Storage successful: %d\n", success); + recipeDB.storeUnitOfMeasure(UnitOfMeasure("tablespoon", "tablespoons", "tbsp", UnitOfMeasure::VOLUME, 1.0)); + recipeDB.storeUnitOfMeasure(UnitOfMeasure("pinch", "pinches", "pch", UnitOfMeasure::VOLUME, 1.0)); + recipeDB.storeUnitOfMeasure(UnitOfMeasure("gram", "grams", "g", UnitOfMeasure::MASS, 1.0)); + Recipe reloadRec = recipeDB.retrieveRecipe("Example"); reloadRec.print(); diff --git a/model/recipe/ingredients/ingredientlistmodel.cpp b/model/recipe/ingredients/ingredientlistmodel.cpp index bd1a41e..1370bc7 100644 --- a/model/recipe/ingredients/ingredientlistmodel.cpp +++ b/model/recipe/ingredients/ingredientlistmodel.cpp @@ -18,7 +18,8 @@ QVariant IngredientListModel::data(const QModelIndex &index, int role) const{ //The quantity is an integer and should be casted. displayStr += std::to_string((int)i.getQuantity()); } else { - displayStr += std::to_string(i.getQuantity()); + float q = i.getQuantity(); + displayStr += toString(q); } displayStr += " " + i.getUnit().getAbbreviation() + " " + i.getName(); @@ -35,5 +36,37 @@ void IngredientListModel::setIngredients(vector ingredients){ this->ingredients = ingredients; QModelIndex index = createIndex(0, 0); QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0); - emit dataChanged(index, bottomIndex); + emit dataChanged(index, bottomIndex); +} + +bool IngredientListModel::addIngredient(RecipeIngredient ri){ + //Add only if it doesn't exist already. + for (unsigned int i = 0; i < this->ingredients.size(); i++){ + if (!this->ingredients[i].getName().compare(ri.getName())){ + return false; + } + } + this->ingredients.push_back(ri); + QModelIndex index = createIndex(this->ingredients.size()-1, 0); + QModelIndex bottomIndex = createIndex(this->ingredients.size()-1, 0); + emit dataChanged(index, bottomIndex); + return true; +} + +vector IngredientListModel::getIngredients(){ + return this->ingredients; +} + +string toString(float val){ + float decimal = std::fmod(val, 1.0f); + int places = 1; + while (std::fmod(decimal * 10, 1.0f) > 0){ + decimal *= 10; + places++; + } + char buffer[50]; + string arg = "%."+std::to_string(places)+"f"; + sprintf(buffer, arg.c_str(), val); + string s = buffer; + return s; } diff --git a/model/recipe/ingredients/ingredientlistmodel.h b/model/recipe/ingredients/ingredientlistmodel.h index 64ca72a..803c2ca 100644 --- a/model/recipe/ingredients/ingredientlistmodel.h +++ b/model/recipe/ingredients/ingredientlistmodel.h @@ -17,9 +17,15 @@ public: //Custom methods to handle ingredient data. void setIngredients(vector ingredients); + bool addIngredient(RecipeIngredient ri); private: vector ingredients; + + //Helper for printing. + }; +string toString(float val); + #endif // INGREDIENTLISTMODEL_H