Polished up adding ingredients to the recipes list.

This commit is contained in:
Andrew Lalis 2018-03-10 10:42:22 +01:00
parent ceab72177a
commit 61711323cc
7 changed files with 139 additions and 19 deletions

View File

@ -462,6 +462,12 @@ QPushButton#browseButton:pressed{
</item>
<item>
<widget class="QTextEdit" name="instructionsTextEdit">
<property name="font">
<font>
<family>Noto Sans CJK KR Medium</family>
<stylestrategy>PreferAntialias</stylestrategy>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(244, 244, 244);</string>
</property>
@ -493,7 +499,7 @@ QPushButton#browseButton:pressed{
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Liberation Serif Bold'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Noto Sans CJK KR Medium'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; color:#00ff40;&quot;&gt;This is some &lt;/span&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:16pt; color:#a33c3e;&quot;&gt;colored text and &lt;/span&gt;&lt;a href=&quot;https://www.google.com&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt; text-decoration: underline; color:#0000ff;&quot;&gt;link&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textInteractionFlags">

View File

@ -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);
}
}

View File

@ -5,6 +5,7 @@
#include <QTextCharFormat>
#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<Ingredient> ingredients;
vector<UnitOfMeasure> units;
IngredientListModel ingredientListModel;
//Helper functions to fill fields.
void populateIngredientsBox();

View File

@ -86,7 +86,7 @@
<second>0</second>
<year>1999</year>
<month>12</month>
<day>28</day>
<day>27</day>
</datetime>
</property>
<property name="currentSection">
@ -160,7 +160,7 @@
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="doubleSpinBox">
<widget class="QDoubleSpinBox" name="servingsSpinBox">
<property name="decimals">
<number>1</number>
</property>
@ -335,6 +335,13 @@
<item>
<widget class="QComboBox" name="unitComboBox"/>
</item>
<item>
<widget class="QPushButton" name="newUnitButton">
<property name="text">
<string>New</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -389,16 +396,56 @@
</property>
</widget>
</item>
<item alignment="Qt::AlignLeft|Qt::AlignTop">
<widget class="QWidget" name="textControlPanel" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QToolButton" name="toolButton">
<widget class="QToolButton" name="italicsButton">
<property name="font">
<font>
<family>Liberation Serif</family>
<pointsize>12</pointsize>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>italics</string>
<string>I</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonIconOnly</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="boldButton">
<property name="font">
<font>
<family>Liberation Serif</family>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>B</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="autoRaise">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTextEdit" name="instructionsTextEdit">
<property name="placeholderText">

View File

@ -18,13 +18,17 @@ int main(int argc, char *argv[])
//TESTING CODE
vector<RecipeIngredient> 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("<b>BOLD</b><i>iTaLiCs</i>"), QImage(), vector<RecipeTag>(), 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();

View File

@ -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();
@ -37,3 +38,35 @@ void IngredientListModel::setIngredients(vector<RecipeIngredient> ingredients){
QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0);
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<RecipeIngredient> 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;
}

View File

@ -17,9 +17,15 @@ public:
//Custom methods to handle ingredient data.
void setIngredients(vector<RecipeIngredient> ingredients);
bool addIngredient(RecipeIngredient ri);
private:
vector<RecipeIngredient> ingredients;
//Helper for printing.
};
string toString(float val);
#endif // INGREDIENTLISTMODEL_H