Added const modifier to getters, able to display ingredients in GUI.

This commit is contained in:
A.G. Lalis 2018-02-13 10:22:05 +01:00
parent 30c1328f3b
commit 2d096f485a
17 changed files with 55 additions and 42 deletions

View File

@ -30,7 +30,6 @@ HEADERS += SQLite/sqlite3.h \
model/recipe/recipe.h \ model/recipe/recipe.h \
userInterface/mainwindow.h \ userInterface/mainwindow.h \
model/database/database.h \ model/database/database.h \
model/recipe/ingredientlistmodel.h \
model/recipe/ingredients/unitofmeasure.h \ model/recipe/ingredients/unitofmeasure.h \
model/recipe/ingredients/ingredient.h \ model/recipe/ingredients/ingredient.h \
model/recipe/ingredients/ingredientlistmodel.h \ model/recipe/ingredients/ingredientlistmodel.h \

View File

@ -315,7 +315,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QListView" name="listView"> <widget class="QListView" name="ingredientsListView">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding"> <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>

View File

@ -3,6 +3,7 @@
Database::Database(string filename){ Database::Database(string filename){
this->filename = filename; this->filename = filename;
openConnection(); openConnection();
//TESTING CODE
if (tableExists("ingredients")){ if (tableExists("ingredients")){
printf("Ingredients table already exists.\n"); printf("Ingredients table already exists.\n");
} else { } else {

View File

@ -10,11 +10,11 @@ Ingredient::Ingredient(string name, string foodGroup){
setFoodGroup(foodGroup); setFoodGroup(foodGroup);
} }
string Ingredient::getName(){ string Ingredient::getName() const{
return this->name; return this->name;
} }
string Ingredient::getFoodGroup(){ string Ingredient::getFoodGroup() const{
return this->foodGroup; return this->foodGroup;
} }

View File

@ -17,8 +17,8 @@ public:
Ingredient(string name, string foodGroup); Ingredient(string name, string foodGroup);
//Getters //Getters
string getName(); string getName() const;
string getFoodGroup(); string getFoodGroup() const;
//Setters //Setters
void setName(string newName); void setName(string newName);

View File

@ -5,13 +5,23 @@ IngredientListModel::IngredientListModel(){
} }
int IngredientListModel::rowCount(const QModelIndex &parent) const{ int IngredientListModel::rowCount(const QModelIndex &parent) const{
return this->ingredients.size();
} }
QVariant IngredientListModel::data(const QModelIndex &index, int role) const{ 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<RecipeIngredient> ingredients){ void IngredientListModel::setIngredients(vector<RecipeIngredient> ingredients){
this->ingredients = ingredients; this->ingredients = ingredients;
QModelIndex index = createIndex(0, 0);
QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0);
emit dataChanged(index, bottomIndex);
} }

View File

@ -2,6 +2,7 @@
#define INGREDIENTLISTMODEL_H #define INGREDIENTLISTMODEL_H
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QModelIndex>
#include "model/recipe/ingredients/recipeingredient.h" #include "model/recipe/ingredients/recipeingredient.h"

View File

@ -12,15 +12,15 @@ RecipeIngredient::RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure u
setUnit(unit); setUnit(unit);
} }
float RecipeIngredient::getQuantity(){ float RecipeIngredient::getQuantity() const{
return this->quantity; return this->quantity;
} }
UnitOfMeasure RecipeIngredient::getUnit(){ UnitOfMeasure RecipeIngredient::getUnit() const{
return this->unit; return this->unit;
} }
string RecipeIngredient::getComment(){ string RecipeIngredient::getComment() const{
return this->comment; return this->comment;
} }

View File

@ -21,9 +21,9 @@ public:
RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit); RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit);
//Getters //Getters
float getQuantity(); float getQuantity() const;
UnitOfMeasure getUnit(); UnitOfMeasure getUnit() const;
string getComment(); string getComment() const;
//Setters //Setters
void setQuantity(float newQuantity); void setQuantity(float newQuantity);

View File

@ -10,14 +10,14 @@ UnitOfMeasure::UnitOfMeasure() : UnitOfMeasure::UnitOfMeasure("", "", ""){
//Default constructor initializes all fields to empty strings. //Default constructor initializes all fields to empty strings.
} }
string UnitOfMeasure::getName(){ string UnitOfMeasure::getName() const{
return this->name; return this->name;
} }
string UnitOfMeasure::getNamePlural(){ string UnitOfMeasure::getNamePlural() const{
return this->plural; return this->plural;
} }
string UnitOfMeasure::getAbbreviation(){ string UnitOfMeasure::getAbbreviation() const{
return this->abbreviation; return this->abbreviation;
} }

View File

@ -18,9 +18,9 @@ public:
UnitOfMeasure(); UnitOfMeasure();
//Getters //Getters
string getName(); string getName() const;
string getNamePlural(); string getNamePlural() const;
string getAbbreviation(); string getAbbreviation() const;
private: private:
string name; //The name of the unit of measure. string name; //The name of the unit of measure.
string plural; //The plural name. string plural; //The plural name.

View File

@ -8,7 +8,7 @@ Instruction::Instruction(string text){
setHTML(text); setHTML(text);
} }
string Instruction::getHTML(){ string Instruction::getHTML() const{
return this->htmlText; return this->htmlText;
} }

View File

@ -16,7 +16,7 @@ public:
Instruction(string text); Instruction(string text);
//Getters //Getters
string getHTML(); string getHTML() const;
//Setters //Setters
void setHTML(string newText); void setHTML(string newText);

View File

@ -16,39 +16,39 @@ Recipe::Recipe() : Recipe::Recipe("Unnamed Recipe", vector<RecipeIngredient>(),
//Set default values when none are specified. //Set default values when none are specified.
} }
string Recipe::getName(){ string Recipe::getName() const{
return this->name; return this->name;
} }
vector<RecipeIngredient> Recipe::getIngredients(){ vector<RecipeIngredient> Recipe::getIngredients() const{
return this->ingredients; return this->ingredients;
} }
Instruction Recipe::getInstruction(){ Instruction Recipe::getInstruction() const{
return this->instruction; return this->instruction;
} }
QImage Recipe::getImage(){ QImage Recipe::getImage() const{
return this->image; return this->image;
} }
QDate Recipe::getCreatedDate(){ QDate Recipe::getCreatedDate() const{
return this->createdDate; return this->createdDate;
} }
QTime Recipe::getPrepTime(){ QTime Recipe::getPrepTime() const{
return this->prepTime; return this->prepTime;
} }
QTime Recipe::getCookTime(){ QTime Recipe::getCookTime() const{
return this->cookTime; 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()); 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; return this->servings;
} }

View File

@ -36,16 +36,16 @@ public:
Recipe(); Recipe();
//Getters //Getters
string getName(); string getName() const;
vector<RecipeIngredient> getIngredients(); vector<RecipeIngredient> getIngredients() const;
Instruction getInstruction(); Instruction getInstruction() const;
QImage getImage(); QImage getImage() const;
vector<RecipeTag> getTags(); vector<RecipeTag> getTags() const;
QDate getCreatedDate(); QDate getCreatedDate() const;
QTime getPrepTime(); QTime getPrepTime() const;
QTime getCookTime(); QTime getCookTime() const;
QTime getTotalTime(); //Derived method to add prep and cook times. QTime getTotalTime() const; //Derived method to add prep and cook times.
float getServings(); float getServings() const;
//Setters //Setters
void setName(string newName); void setName(string newName);

View File

@ -6,11 +6,12 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow){ ui(new Ui::MainWindow){
ui->setupUi(this); ui->setupUi(this);
ui->ingredientsPanel->setModel(); ui->ingredientsListView->setModel(&this->ingredientModel);
//TESTING CODE //TESTING CODE
vector<RecipeIngredient> ri; vector<RecipeIngredient> ri;
ri.push_back(RecipeIngredient("flour", "grains", 3.0f, UnitOfMeasure("cup", "cups", "c"))); 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("<b>BOLD</b><i>iTaLiCs</i>"), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(0, 30), QTime(0, 25), 10.0f); Recipe rec("Example", ri, Instruction("<b>BOLD</b><i>iTaLiCs</i>"), QImage(), vector<RecipeTag>(), QDate::currentDate(), QTime(0, 30), QTime(0, 25), 10.0f);
@ -36,5 +37,5 @@ void MainWindow::setInstruction(Instruction instruction){
} }
void MainWindow::setIngredients(vector<RecipeIngredient> ingredients){ void MainWindow::setIngredients(vector<RecipeIngredient> ingredients){
///TODO: Implement this. this->ingredientModel.setIngredients(ingredients);
} }

View File

@ -26,6 +26,7 @@ public:
void loadFromRecipe(Recipe recipe); void loadFromRecipe(Recipe recipe);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
IngredientListModel ingredientModel;
//Hidden manipulation methods. //Hidden manipulation methods.
void setRecipeName(string name); void setRecipeName(string name);