Merge pull request #2 from andrewlalis/development

Improvements in Code Quality, all basic aspects of a recipe can be displayed.
This commit is contained in:
Andrew Lalis 2018-02-13 10:23:08 +01:00 committed by GitHub
commit 3ef03e2887
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 65 additions and 50 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

@ -1,17 +1,27 @@
#include "model/recipe/ingredients/ingredientlistmodel.h" #include "model/recipe/ingredients/ingredientlistmodel.h"
IngredientListModel::IngredientListModel(){ IngredientListModel::IngredientListModel(){
this->ingredients = vector<Ingredient>(); this->ingredients = vector<RecipeIngredient>();
} }
int IngredientListModel::rowCount(const QModelIndex &parent){ int IngredientListModel::rowCount(const QModelIndex &parent) const{
return this->ingredients.size(); return this->ingredients.size();
} }
QVariant IngredientListModel::data(const QModelIndex &index, int role){ QVariant IngredientListModel::data(const QModelIndex &index, int role) const{
int row = index.row();
switch(role){
case Qt::DisplayRole:
return QString::fromStdString(ingredients[row].getName());
} }
void IngredientListModel::setIngredients(vector<Ingredient> ingredients){ return QVariant();
}
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,8 +2,9 @@
#define INGREDIENTLISTMODEL_H #define INGREDIENTLISTMODEL_H
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QModelIndex>
#include "model/recipe/ingredients/ingredient.h" #include "model/recipe/ingredients/recipeingredient.h"
class IngredientListModel : public QAbstractListModel class IngredientListModel : public QAbstractListModel
{ {
@ -11,14 +12,14 @@ public:
IngredientListModel(); IngredientListModel();
//Overridden methods. //Overridden methods.
int rowCount(const QModelIndex &parent = QModelIndex()); int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole); QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
//Custom methods to handle ingredient data. //Custom methods to handle ingredient data.
void setIngredients(vector<Ingredient> ingredients); void setIngredients(vector<RecipeIngredient> ingredients);
private: private:
vector<Ingredient> ingredients; vector<RecipeIngredient> ingredients;
}; };
#endif // INGREDIENTLISTMODEL_H #endif // INGREDIENTLISTMODEL_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,9 +6,12 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow){ ui(new Ui::MainWindow){
ui->setupUi(this); ui->setupUi(this);
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);
@ -34,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

@ -6,6 +6,7 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include "model/recipe/recipe.h" #include "model/recipe/recipe.h"
#include "model/recipe/ingredients/ingredientlistmodel.h"
using namespace std; using namespace std;
@ -25,7 +26,7 @@ public:
void loadFromRecipe(Recipe recipe); void loadFromRecipe(Recipe recipe);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QAbstractListModel *ingredientsListModel; IngredientListModel ingredientModel;
//Hidden manipulation methods. //Hidden manipulation methods.
void setRecipeName(string name); void setRecipeName(string name);