From 2d096f485a87cb52f5307ef978ea03910341239f Mon Sep 17 00:00:00 2001 From: "A.G. Lalis" Date: Tue, 13 Feb 2018 10:22:05 +0100 Subject: [PATCH] Added const modifier to getters, able to display ingredients in GUI. --- RecipeDB.pro | 1 - gui/mainwindow.ui | 2 +- model/database/database.cpp | 1 + model/recipe/ingredients/ingredient.cpp | 4 ++-- model/recipe/ingredients/ingredient.h | 4 ++-- .../ingredients/ingredientlistmodel.cpp | 12 ++++++++++- .../recipe/ingredients/ingredientlistmodel.h | 1 + model/recipe/ingredients/recipeingredient.cpp | 6 +++--- model/recipe/ingredients/recipeingredient.h | 6 +++--- model/recipe/ingredients/unitofmeasure.cpp | 6 +++--- model/recipe/ingredients/unitofmeasure.h | 6 +++--- model/recipe/instruction.cpp | 2 +- model/recipe/instruction.h | 2 +- model/recipe/recipe.cpp | 18 ++++++++--------- model/recipe/recipe.h | 20 +++++++++---------- userInterface/mainwindow.cpp | 5 +++-- userInterface/mainwindow.h | 1 + 17 files changed, 55 insertions(+), 42 deletions(-) diff --git a/RecipeDB.pro b/RecipeDB.pro index a84d67e..a717ed2 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -30,7 +30,6 @@ HEADERS += SQLite/sqlite3.h \ model/recipe/recipe.h \ userInterface/mainwindow.h \ model/database/database.h \ - model/recipe/ingredientlistmodel.h \ model/recipe/ingredients/unitofmeasure.h \ model/recipe/ingredients/ingredient.h \ model/recipe/ingredients/ingredientlistmodel.h \ diff --git a/gui/mainwindow.ui b/gui/mainwindow.ui index 8d7b0c5..1db1cc9 100644 --- a/gui/mainwindow.ui +++ b/gui/mainwindow.ui @@ -315,7 +315,7 @@ - + 0 diff --git a/model/database/database.cpp b/model/database/database.cpp index f81c2ee..23716ab 100644 --- a/model/database/database.cpp +++ b/model/database/database.cpp @@ -3,6 +3,7 @@ Database::Database(string filename){ this->filename = filename; openConnection(); + //TESTING CODE if (tableExists("ingredients")){ printf("Ingredients table already exists.\n"); } else { diff --git a/model/recipe/ingredients/ingredient.cpp b/model/recipe/ingredients/ingredient.cpp index 2ce1d2b..d0a4664 100644 --- a/model/recipe/ingredients/ingredient.cpp +++ b/model/recipe/ingredients/ingredient.cpp @@ -10,11 +10,11 @@ Ingredient::Ingredient(string name, string foodGroup){ setFoodGroup(foodGroup); } -string Ingredient::getName(){ +string Ingredient::getName() const{ return this->name; } -string Ingredient::getFoodGroup(){ +string Ingredient::getFoodGroup() const{ return this->foodGroup; } diff --git a/model/recipe/ingredients/ingredient.h b/model/recipe/ingredients/ingredient.h index 4444ad0..5dbe08d 100644 --- a/model/recipe/ingredients/ingredient.h +++ b/model/recipe/ingredients/ingredient.h @@ -17,8 +17,8 @@ public: Ingredient(string name, string foodGroup); //Getters - string getName(); - string getFoodGroup(); + string getName() const; + string getFoodGroup() const; //Setters void setName(string newName); diff --git a/model/recipe/ingredients/ingredientlistmodel.cpp b/model/recipe/ingredients/ingredientlistmodel.cpp index e9bae29..de88da5 100644 --- a/model/recipe/ingredients/ingredientlistmodel.cpp +++ b/model/recipe/ingredients/ingredientlistmodel.cpp @@ -5,13 +5,23 @@ IngredientListModel::IngredientListModel(){ } int IngredientListModel::rowCount(const QModelIndex &parent) const{ - + return this->ingredients.size(); } QVariant IngredientListModel::data(const QModelIndex &index, int role) const{ + int row = index.row(); + switch(role){ + case Qt::DisplayRole: + return QString::fromStdString(ingredients[row].getName()); + } + + return QVariant(); } void IngredientListModel::setIngredients(vector ingredients){ this->ingredients = ingredients; + QModelIndex index = createIndex(0, 0); + QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0); + emit dataChanged(index, bottomIndex); } diff --git a/model/recipe/ingredients/ingredientlistmodel.h b/model/recipe/ingredients/ingredientlistmodel.h index ae4fb5d..64ca72a 100644 --- a/model/recipe/ingredients/ingredientlistmodel.h +++ b/model/recipe/ingredients/ingredientlistmodel.h @@ -2,6 +2,7 @@ #define INGREDIENTLISTMODEL_H #include +#include #include "model/recipe/ingredients/recipeingredient.h" diff --git a/model/recipe/ingredients/recipeingredient.cpp b/model/recipe/ingredients/recipeingredient.cpp index e34d299..3e09886 100644 --- a/model/recipe/ingredients/recipeingredient.cpp +++ b/model/recipe/ingredients/recipeingredient.cpp @@ -12,15 +12,15 @@ RecipeIngredient::RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure u setUnit(unit); } -float RecipeIngredient::getQuantity(){ +float RecipeIngredient::getQuantity() const{ return this->quantity; } -UnitOfMeasure RecipeIngredient::getUnit(){ +UnitOfMeasure RecipeIngredient::getUnit() const{ return this->unit; } -string RecipeIngredient::getComment(){ +string RecipeIngredient::getComment() const{ return this->comment; } diff --git a/model/recipe/ingredients/recipeingredient.h b/model/recipe/ingredients/recipeingredient.h index ea3ceb5..54224b3 100644 --- a/model/recipe/ingredients/recipeingredient.h +++ b/model/recipe/ingredients/recipeingredient.h @@ -21,9 +21,9 @@ public: RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit); //Getters - float getQuantity(); - UnitOfMeasure getUnit(); - string getComment(); + float getQuantity() const; + UnitOfMeasure getUnit() const; + string getComment() const; //Setters void setQuantity(float newQuantity); diff --git a/model/recipe/ingredients/unitofmeasure.cpp b/model/recipe/ingredients/unitofmeasure.cpp index 77b395e..ffc0066 100644 --- a/model/recipe/ingredients/unitofmeasure.cpp +++ b/model/recipe/ingredients/unitofmeasure.cpp @@ -10,14 +10,14 @@ UnitOfMeasure::UnitOfMeasure() : UnitOfMeasure::UnitOfMeasure("", "", ""){ //Default constructor initializes all fields to empty strings. } -string UnitOfMeasure::getName(){ +string UnitOfMeasure::getName() const{ return this->name; } -string UnitOfMeasure::getNamePlural(){ +string UnitOfMeasure::getNamePlural() const{ return this->plural; } -string UnitOfMeasure::getAbbreviation(){ +string UnitOfMeasure::getAbbreviation() const{ return this->abbreviation; } diff --git a/model/recipe/ingredients/unitofmeasure.h b/model/recipe/ingredients/unitofmeasure.h index f835cec..2692e5a 100644 --- a/model/recipe/ingredients/unitofmeasure.h +++ b/model/recipe/ingredients/unitofmeasure.h @@ -18,9 +18,9 @@ public: UnitOfMeasure(); //Getters - string getName(); - string getNamePlural(); - string getAbbreviation(); + string getName() const; + string getNamePlural() const; + string getAbbreviation() const; private: string name; //The name of the unit of measure. string plural; //The plural name. diff --git a/model/recipe/instruction.cpp b/model/recipe/instruction.cpp index e77d872..9c3cb48 100644 --- a/model/recipe/instruction.cpp +++ b/model/recipe/instruction.cpp @@ -8,7 +8,7 @@ Instruction::Instruction(string text){ setHTML(text); } -string Instruction::getHTML(){ +string Instruction::getHTML() const{ return this->htmlText; } diff --git a/model/recipe/instruction.h b/model/recipe/instruction.h index 1749a74..53b33fc 100644 --- a/model/recipe/instruction.h +++ b/model/recipe/instruction.h @@ -16,7 +16,7 @@ public: Instruction(string text); //Getters - string getHTML(); + string getHTML() const; //Setters void setHTML(string newText); diff --git a/model/recipe/recipe.cpp b/model/recipe/recipe.cpp index 4f642f4..1c6754d 100644 --- a/model/recipe/recipe.cpp +++ b/model/recipe/recipe.cpp @@ -16,39 +16,39 @@ Recipe::Recipe() : Recipe::Recipe("Unnamed Recipe", vector(), //Set default values when none are specified. } -string Recipe::getName(){ +string Recipe::getName() const{ return this->name; } -vector Recipe::getIngredients(){ +vector Recipe::getIngredients() const{ return this->ingredients; } -Instruction Recipe::getInstruction(){ +Instruction Recipe::getInstruction() const{ return this->instruction; } -QImage Recipe::getImage(){ +QImage Recipe::getImage() const{ return this->image; } -QDate Recipe::getCreatedDate(){ +QDate Recipe::getCreatedDate() const{ return this->createdDate; } -QTime Recipe::getPrepTime(){ +QTime Recipe::getPrepTime() const{ return this->prepTime; } -QTime Recipe::getCookTime(){ +QTime Recipe::getCookTime() const{ return this->cookTime; } -QTime Recipe::getTotalTime(){ +QTime Recipe::getTotalTime() const{ return QTime(this->cookTime.hour() + this->prepTime.hour(), this->cookTime.minute() + this->prepTime.minute(), this->cookTime.second() + this->prepTime.second()); } -float Recipe::getServings(){ +float Recipe::getServings() const{ return this->servings; } diff --git a/model/recipe/recipe.h b/model/recipe/recipe.h index 84d5fad..1fef395 100644 --- a/model/recipe/recipe.h +++ b/model/recipe/recipe.h @@ -36,16 +36,16 @@ public: Recipe(); //Getters - string getName(); - vector getIngredients(); - Instruction getInstruction(); - QImage getImage(); - vector getTags(); - QDate getCreatedDate(); - QTime getPrepTime(); - QTime getCookTime(); - QTime getTotalTime(); //Derived method to add prep and cook times. - float getServings(); + string getName() const; + vector getIngredients() const; + Instruction getInstruction() const; + QImage getImage() const; + vector getTags() const; + QDate getCreatedDate() const; + QTime getPrepTime() const; + QTime getCookTime() const; + QTime getTotalTime() const; //Derived method to add prep and cook times. + float getServings() const; //Setters void setName(string newName); diff --git a/userInterface/mainwindow.cpp b/userInterface/mainwindow.cpp index a96635e..53aab11 100644 --- a/userInterface/mainwindow.cpp +++ b/userInterface/mainwindow.cpp @@ -6,11 +6,12 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow){ ui->setupUi(this); - ui->ingredientsPanel->setModel(); + ui->ingredientsListView->setModel(&this->ingredientModel); //TESTING CODE vector ri; ri.push_back(RecipeIngredient("flour", "grains", 3.0f, UnitOfMeasure("cup", "cups", "c"))); + ri.push_back(RecipeIngredient("Baking Powder", "Additives", 1.0f, UnitOfMeasure("Teaspoon", "Teaspoons", "Tsp"))); Recipe rec("Example", ri, Instruction("BOLDiTaLiCs"), QImage(), vector(), QDate::currentDate(), QTime(0, 30), QTime(0, 25), 10.0f); @@ -36,5 +37,5 @@ void MainWindow::setInstruction(Instruction instruction){ } void MainWindow::setIngredients(vector ingredients){ - ///TODO: Implement this. + this->ingredientModel.setIngredients(ingredients); } diff --git a/userInterface/mainwindow.h b/userInterface/mainwindow.h index ec2a15e..b1b50be 100644 --- a/userInterface/mainwindow.h +++ b/userInterface/mainwindow.h @@ -26,6 +26,7 @@ public: void loadFromRecipe(Recipe recipe); private: Ui::MainWindow *ui; + IngredientListModel ingredientModel; //Hidden manipulation methods. void setRecipeName(string name);