From 844c5587265cc5ba8fc60351d8953dae59bd17fd Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Fri, 30 Mar 2018 14:33:48 +0200 Subject: [PATCH] Added query counter, list of all tags, ingredients to open recipe view. --- RecipeDB.pro | 11 +- gui/mainwindow.h | 4 +- gui/mainwindow.ui | 3 + gui/newDialogs/newfoodgroupdialog.cpp | 18 +++ gui/newDialogs/newfoodgroupdialog.h | 27 ++++ gui/newDialogs/newfoodgroupdialog.ui | 101 ++++++++++++ gui/newDialogs/newingredientdialog.cpp | 18 ++- gui/newDialogs/newingredientdialog.h | 8 + gui/newDialogs/newingredientdialog.ui | 3 + gui/newDialogs/newtagdialog.ui | 26 +++- gui/newDialogs/newunitdialog.ui | 47 +++++- gui/newrecipedialog.cpp | 4 +- gui/newrecipedialog.h | 4 +- gui/newrecipedialog.ui | 21 +++ gui/openrecipedialog.cpp | 12 ++ gui/openrecipedialog.h | 7 + gui/openrecipedialog.ui | 144 +++++++++++------- main.cpp | 2 + model/database/database.cpp | 8 +- model/database/database.h | 7 +- model/database/recipedatabase.cpp | 2 +- model/database/recipedatabase.h | 2 +- model/recipe/ingredients/ingredient.cpp | 6 +- model/recipe/ingredients/ingredient.h | 2 + .../ingredients/ingredientlistmodel.cpp | 10 +- .../recipe/ingredients/ingredientlistmodel.h | 8 +- model/recipe/ingredients/recipeingredient.cpp | 4 + model/recipe/ingredients/recipeingredient.h | 1 + .../ingredients/recipeingredientlistmodel.cpp | 53 +++++++ .../ingredients/recipeingredientlistmodel.h | 28 ++++ 30 files changed, 509 insertions(+), 82 deletions(-) create mode 100644 gui/newDialogs/newfoodgroupdialog.cpp create mode 100644 gui/newDialogs/newfoodgroupdialog.h create mode 100644 gui/newDialogs/newfoodgroupdialog.ui create mode 100644 model/recipe/ingredients/recipeingredientlistmodel.cpp create mode 100644 model/recipe/ingredients/recipeingredientlistmodel.h diff --git a/RecipeDB.pro b/RecipeDB.pro index 92ac7d6..4727193 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -34,7 +34,9 @@ SOURCES += model/recipe/instruction.cpp \ utils/stringutils.cpp \ gui/openrecipedialog.cpp \ model/recipe/recipetablemodel.cpp \ - gui/mainwindow.cpp + gui/mainwindow.cpp \ + gui/newDialogs/newfoodgroupdialog.cpp \ + model/recipe/ingredients/recipeingredientlistmodel.cpp HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ @@ -58,7 +60,9 @@ HEADERS += model/recipe/instruction.h \ utils/stringutils.h \ gui/openrecipedialog.h \ model/recipe/recipetablemodel.h \ - gui/mainwindow.h + gui/mainwindow.h \ + gui/newDialogs/newfoodgroupdialog.h \ + model/recipe/ingredients/recipeingredientlistmodel.h LIBS += -ldl \ @@ -68,7 +72,8 @@ FORMS += gui/mainwindow.ui \ gui/newDialogs/newtagdialog.ui \ gui/newDialogs/newunitdialog.ui \ gui/openrecipedialog.ui \ - gui/mainwindow.ui + gui/mainwindow.ui \ + gui/newDialogs/newfoodgroupdialog.ui DISTFILES += \ .gitignore diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 2b62cc3..03e802a 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -6,7 +6,7 @@ #include #include "model/recipe/recipe.h" -#include "model/recipe/ingredients/ingredientlistmodel.h" +#include "model/recipe/ingredients/recipeingredientlistmodel.h" #include "gui/newrecipedialog.h" #include "gui/openrecipedialog.h" #include "utils/stringutils.h" @@ -39,7 +39,7 @@ public: private: Ui::MainWindow *ui; RecipeDatabase *recipeDB; - IngredientListModel ingredientModel; + RecipeIngredientListModel ingredientModel; TagListModel tagsListModel; //Hidden manipulation methods. diff --git a/gui/mainwindow.ui b/gui/mainwindow.ui index 01f4b80..4ac1f88 100644 --- a/gui/mainwindow.ui +++ b/gui/mainwindow.ui @@ -604,6 +604,9 @@ font: "Noto Sans CJK KR"; Qt::ScrollBarAlwaysOff + + QAbstractItemView::NoEditTriggers + QAbstractItemView::ExtendedSelection diff --git a/gui/newDialogs/newfoodgroupdialog.cpp b/gui/newDialogs/newfoodgroupdialog.cpp new file mode 100644 index 0000000..8e0277a --- /dev/null +++ b/gui/newDialogs/newfoodgroupdialog.cpp @@ -0,0 +1,18 @@ +#include "newfoodgroupdialog.h" +#include "ui_newfoodgroupdialog.h" + +newFoodGroupDialog::newFoodGroupDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::newFoodGroupDialog) +{ + ui->setupUi(this); +} + +newFoodGroupDialog::~newFoodGroupDialog() +{ + delete ui; +} + +string newFoodGroupDialog::getFoodGroup() const{ + return ui->lineEdit->text().toStdString(); +} diff --git a/gui/newDialogs/newfoodgroupdialog.h b/gui/newDialogs/newfoodgroupdialog.h new file mode 100644 index 0000000..20c3451 --- /dev/null +++ b/gui/newDialogs/newfoodgroupdialog.h @@ -0,0 +1,27 @@ +#ifndef NEWFOODGROUPDIALOG_H +#define NEWFOODGROUPDIALOG_H + +#include +#include + +using namespace std; + +namespace Ui { +class newFoodGroupDialog; +} + +class newFoodGroupDialog : public QDialog +{ + Q_OBJECT + + public: + explicit newFoodGroupDialog(QWidget *parent = 0); + ~newFoodGroupDialog(); + + string getFoodGroup() const; + + private: + Ui::newFoodGroupDialog *ui; +}; + +#endif // NEWFOODGROUPDIALOG_H diff --git a/gui/newDialogs/newfoodgroupdialog.ui b/gui/newDialogs/newfoodgroupdialog.ui new file mode 100644 index 0000000..bbd72d3 --- /dev/null +++ b/gui/newDialogs/newfoodgroupdialog.ui @@ -0,0 +1,101 @@ + + + newFoodGroupDialog + + + + 0 + 0 + 240 + 114 + + + + + 11 + + + + New Food Group + + + + :/images/images/icon.png:/images/images/icon.png + + + + + + + Noto Sans CJK KR Light + 12 + + + + Add New Food Group + + + Qt::AlignCenter + + + + + + + + Noto Sans CJK KR Light + 12 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + buttonBox + accepted() + newFoodGroupDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + newFoodGroupDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/gui/newDialogs/newingredientdialog.cpp b/gui/newDialogs/newingredientdialog.cpp index c7f3554..693dc1e 100644 --- a/gui/newDialogs/newingredientdialog.cpp +++ b/gui/newDialogs/newingredientdialog.cpp @@ -24,10 +24,26 @@ Ingredient NewIngredientDialog::getIngredient(){ void NewIngredientDialog::populateFoodGroupBox(){ vector foodGroups = this->recipeDB->retrieveAllFoodGroups(); - printf("Found %ld food Groups.\n", foodGroups.size()); ui->foodGroupBox->clear(); for (unsigned int i = 0; i < foodGroups.size(); i++){ QString s = QString::fromStdString(foodGroups[i]); ui->foodGroupBox->insertItem(i, s); } } + +void NewIngredientDialog::on_addFoodGroupButton_clicked(){ + newFoodGroupDialog d(this); + if (d.exec() == QDialog::Accepted){ + string s = d.getFoodGroup(); + if (!s.empty()){ + ui->foodGroupBox->addItem(QString::fromStdString(s)); + ui->foodGroupBox->setCurrentText(QString::fromStdString(s)); + } else { + QMessageBox::warning(this, "Empty Food Group", "The food group you entered is empty!"); + } + } +} + +void NewIngredientDialog::on_deleteFoodGroupButton_clicked(){ + ui->foodGroupBox->removeItem(ui->foodGroupBox->currentIndex()); +} diff --git a/gui/newDialogs/newingredientdialog.h b/gui/newDialogs/newingredientdialog.h index d9ad2b3..e922167 100644 --- a/gui/newDialogs/newingredientdialog.h +++ b/gui/newDialogs/newingredientdialog.h @@ -2,8 +2,11 @@ #define NEWINGREDIENTDIALOG_H #include +#include + #include "model/recipe/ingredients/ingredient.h" #include "model/database/recipedatabase.h" +#include "gui/newDialogs/newfoodgroupdialog.h" namespace Ui { class NewIngredientDialog; @@ -21,6 +24,11 @@ class NewIngredientDialog : public QDialog //Access values. Ingredient getIngredient(); + private slots: + void on_addFoodGroupButton_clicked(); + + void on_deleteFoodGroupButton_clicked(); + private: Ui::NewIngredientDialog *ui; RecipeDatabase *recipeDB; diff --git a/gui/newDialogs/newingredientdialog.ui b/gui/newDialogs/newingredientdialog.ui index b562b1f..792e239 100644 --- a/gui/newDialogs/newingredientdialog.ui +++ b/gui/newDialogs/newingredientdialog.ui @@ -70,6 +70,9 @@ 0 + + QComboBox::InsertAlphabetically + diff --git a/gui/newDialogs/newtagdialog.ui b/gui/newDialogs/newtagdialog.ui index fd77812..72ea9f2 100644 --- a/gui/newDialogs/newtagdialog.ui +++ b/gui/newDialogs/newtagdialog.ui @@ -7,16 +7,19 @@ 0 0 240 - 320 + 121 - Dialog + New Tag :/images/images/icon.png:/images/images/icon.png + + font: 25 "Noto Sans CJK KR Light"; + true @@ -26,6 +29,14 @@ + + + 13 + 3 + false + false + + New Tag @@ -35,7 +46,16 @@ - + + + + 12 + 3 + false + false + + + diff --git a/gui/newDialogs/newunitdialog.ui b/gui/newDialogs/newunitdialog.ui index 953be58..92fc161 100644 --- a/gui/newDialogs/newunitdialog.ui +++ b/gui/newDialogs/newunitdialog.ui @@ -6,8 +6,8 @@ 0 0 - 240 - 350 + 195 + 340 @@ -17,6 +17,9 @@ :/images/images/icon.png:/images/images/icon.png + + font: 25 "Noto Sans CJK KR Light"; + true @@ -26,6 +29,14 @@ + + + 12 + 3 + false + false + + Unit Name @@ -39,6 +50,14 @@ + + + 12 + 3 + false + false + + Plural Name @@ -52,6 +71,14 @@ + + + 12 + 3 + false + false + + Abbreviation @@ -86,6 +113,14 @@ 0 + + + 12 + 3 + false + false + + Type: @@ -109,6 +144,14 @@ + + + 12 + 3 + false + false + + Metric Coefficient diff --git a/gui/newrecipedialog.cpp b/gui/newrecipedialog.cpp index 47d6847..c105333 100644 --- a/gui/newrecipedialog.cpp +++ b/gui/newrecipedialog.cpp @@ -199,8 +199,8 @@ void NewRecipeDialog::on_newUnitButton_clicked(){ d.show(); if (d.exec() == QDialog::Accepted){ UnitOfMeasure u = d.getUnit(); - if (!this->recipeDB->storeUnitOfMeasure(u) || u.getName().empty() || u.getNamePlural().empty() || u.getAbbreviation().empty()){ - QMessageBox::critical(this, "Error", "Unable to store new unit."); + if (u.getName().empty() || u.getNamePlural().empty() || u.getAbbreviation().empty() || !this->recipeDB->storeUnitOfMeasure(u)){ + QMessageBox::critical(this, "Error", "Unable to store new unit. Make sure all the information is filled in!"); } else { this->populateUnitsBox(); ui->unitComboBox->setCurrentText(QString::fromStdString(u.getName())); diff --git a/gui/newrecipedialog.h b/gui/newrecipedialog.h index bfeae1f..181cea2 100644 --- a/gui/newrecipedialog.h +++ b/gui/newrecipedialog.h @@ -8,7 +8,7 @@ #include #include "model/database/recipedatabase.h" -#include "model/recipe/ingredients/ingredientlistmodel.h" +#include "model/recipe/ingredients/recipeingredientlistmodel.h" #include "model/recipe/tags/taglistmodel.h" #include "gui/newDialogs/newingredientdialog.h" @@ -67,7 +67,7 @@ class NewRecipeDialog : public QDialog vector ingredients; vector units; vector tags; - IngredientListModel ingredientListModel; + RecipeIngredientListModel ingredientListModel; TagListModel tagsListModel; QImage img; bool accepted = false; diff --git a/gui/newrecipedialog.ui b/gui/newrecipedialog.ui index ad84d1a..937ee33 100644 --- a/gui/newrecipedialog.ui +++ b/gui/newrecipedialog.ui @@ -273,6 +273,12 @@ 0 + + background-color: rgb(113, 119, 255); + + + QComboBox::InsertAlphabetically + @@ -459,12 +465,21 @@ 0 + + background-color: rgb(113, 119, 255); + false + + QComboBox::InsertAlphabetically + + + true + @@ -554,6 +569,12 @@ 0 + + background-color: rgb(113, 119, 255); + + + QComboBox::InsertAlphabetically + diff --git a/gui/openrecipedialog.cpp b/gui/openrecipedialog.cpp index 5031f10..716e354 100644 --- a/gui/openrecipedialog.cpp +++ b/gui/openrecipedialog.cpp @@ -8,10 +8,14 @@ OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) : ui->setupUi(this); ui->recipeTableView->setModel(&this->recipeTableModel); + ui->ingredientsListView->setModel(&this->ingredientsModel); + ui->tagsListView->setModel(&this->tagsModel); } OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){ this->recipeDB = recipeDB; + this->populateIngredientsList(); + this->populateTagsList(); this->populateRecipesTable(); } @@ -32,6 +36,14 @@ void OpenRecipeDialog::populateRecipesTable(){ ui->recipeTableView->show(); } +void OpenRecipeDialog::populateIngredientsList(){ + this->ingredientsModel.setIngredients(this->recipeDB->retrieveAllIngredients()); +} + +void OpenRecipeDialog::populateTagsList(){ + this->tagsModel.setTags(this->recipeDB->retrieveAllTags()); +} + void OpenRecipeDialog::on_deleteRecipeButton_clicked(){ QItemSelectionModel *selectModel = ui->recipeTableView->selectionModel(); if (!selectModel->hasSelection()){ diff --git a/gui/openrecipedialog.h b/gui/openrecipedialog.h index 974b6cf..d762d39 100644 --- a/gui/openrecipedialog.h +++ b/gui/openrecipedialog.h @@ -6,6 +6,8 @@ #include "model/database/recipedatabase.h" #include "model/recipe/recipetablemodel.h" +#include "model/recipe/ingredients/ingredientlistmodel.h" +#include "model/recipe/tags/taglistmodel.h" namespace Ui { class OpenRecipeDialog; @@ -33,7 +35,12 @@ class OpenRecipeDialog : public QDialog RecipeTableModel recipeTableModel; Recipe selectedRecipe; + IngredientListModel ingredientsModel; + TagListModel tagsModel; + void populateRecipesTable(); + void populateIngredientsList(); + void populateTagsList(); }; #endif // OPENRECIPEDIALOG_H diff --git a/gui/openrecipedialog.ui b/gui/openrecipedialog.ui index d689a16..0e51715 100644 --- a/gui/openrecipedialog.ui +++ b/gui/openrecipedialog.ui @@ -6,7 +6,7 @@ 0 0 - 640 + 1000 480 @@ -20,10 +20,80 @@ true - - + + - + + + + + + + + Tag + + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QAbstractItemView::ExtendedSelection + + + false + + + false + + + + + + + + + + + + + Ingredient + + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QAbstractItemView::ExtendedSelection + + + false + + + true + + + + + + @@ -40,38 +110,6 @@ - - - - - - - Tag - - - - - - - - - - - - - - - - Ingredient - - - - - - - - - @@ -86,26 +124,26 @@ - - - - - - - - - - - :/images/images/trash.png:/images/images/trash.png - - - - - - + + + + + + + + + + + :/images/images/trash.png:/images/images/trash.png + + + + + + diff --git a/main.cpp b/main.cpp index 462f772..2a70cec 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,8 @@ int main(int argc, char *argv[]) a.exec(); recipeDB.closeConnection(); + printf("Total queries: %lu\n", recipeDB.getQueryCount()); + return 0; } diff --git a/model/database/database.cpp b/model/database/database.cpp index 0d3b9a4..db53c3f 100644 --- a/model/database/database.cpp +++ b/model/database/database.cpp @@ -2,6 +2,7 @@ Database::Database(string filename){ this->filename = filename; + this->queryCount = 0; openConnection(); } @@ -22,6 +23,7 @@ ResultTable Database::executeSQL(string statement){ t.extractData(stmt); this->returnCode = sqlite3_finalize(stmt); + this->queryCount++; return t; } @@ -99,6 +101,10 @@ bool Database::tableExists(string tableName){ return !t.isEmpty(); } -int Database::getLastInsertedRowId(){ +int Database::getLastInsertedRowId() const{ return sqlite3_last_insert_rowid(this->db); } + +unsigned long Database::getQueryCount() const{ + return this->queryCount; +} diff --git a/model/database/database.h b/model/database/database.h index ab149d5..d0bd435 100644 --- a/model/database/database.h +++ b/model/database/database.h @@ -29,7 +29,9 @@ public: bool deleteFrom(string tableName, string conditions); bool tableExists(string tableName); - int getLastInsertedRowId(); + int getLastInsertedRowId() const; + + unsigned long getQueryCount() const; void closeConnection(); @@ -45,6 +47,9 @@ private: string sql; char* errorMsg; + //Data tracking. + unsigned long queryCount; + void openConnection(); std::string combineVector(std::vector strings, std::string mid); }; diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 24e5151..1eeebd3 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -234,7 +234,7 @@ vector RecipeDatabase::retrieveTags(int recipeId){ } vector RecipeDatabase::retrieveAllTags(){ - ResultTable t = this->selectFrom("recipeTag", "tagName", "ORDER BY tagName"); + ResultTable t = this->executeSQL("SELECT DISTINCT tagName FROM recipeTag ORDER BY tagName;"); vector tags; if (!t.isEmpty()){ for (TableRow row : t.rows()){ diff --git a/model/database/recipedatabase.h b/model/database/recipedatabase.h index e02c60a..79ff7bd 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -1,7 +1,7 @@ #ifndef RECIPEDATABASE_H #define RECIPEDATABASE_H - +#include #include "database.h" #include "model/recipe/recipe.h" diff --git a/model/recipe/ingredients/ingredient.cpp b/model/recipe/ingredients/ingredient.cpp index d0a4664..8942e11 100644 --- a/model/recipe/ingredients/ingredient.cpp +++ b/model/recipe/ingredients/ingredient.cpp @@ -23,5 +23,9 @@ void Ingredient::setName(string newName){ } void Ingredient::setFoodGroup(string newFoodGroup){ - this->foodGroup = newFoodGroup; + this->foodGroup = newFoodGroup; +} + +string Ingredient::toString(){ + return this->getName(); } diff --git a/model/recipe/ingredients/ingredient.h b/model/recipe/ingredients/ingredient.h index 5dbe08d..88a7ec7 100644 --- a/model/recipe/ingredients/ingredient.h +++ b/model/recipe/ingredients/ingredient.h @@ -23,6 +23,8 @@ public: //Setters void setName(string newName); void setFoodGroup(string newFoodGroup); + + string toString(); protected: string name; string foodGroup; diff --git a/model/recipe/ingredients/ingredientlistmodel.cpp b/model/recipe/ingredients/ingredientlistmodel.cpp index 916bbb0..054a4d4 100644 --- a/model/recipe/ingredients/ingredientlistmodel.cpp +++ b/model/recipe/ingredients/ingredientlistmodel.cpp @@ -1,7 +1,7 @@ #include "model/recipe/ingredients/ingredientlistmodel.h" IngredientListModel::IngredientListModel(){ - this->ingredients = vector(); + this->ingredients = vector(); } int IngredientListModel::rowCount(const QModelIndex &parent) const{ @@ -10,7 +10,7 @@ int IngredientListModel::rowCount(const QModelIndex &parent) const{ QVariant IngredientListModel::data(const QModelIndex &index, int role) const{ int row = index.row(); - RecipeIngredient i = this->ingredients[row]; + Ingredient i = this->ingredients[row]; string displayStr = i.toString(); @@ -22,14 +22,14 @@ QVariant IngredientListModel::data(const QModelIndex &index, int role) const{ return QVariant(); } -void IngredientListModel::setIngredients(vector ingredients){ +void IngredientListModel::setIngredients(vector ingredients){ this->ingredients = ingredients; QModelIndex index = createIndex(0, 0); QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0); emit dataChanged(index, bottomIndex); } -bool IngredientListModel::addIngredient(RecipeIngredient ri){ +bool IngredientListModel::addIngredient(Ingredient 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())){ @@ -48,6 +48,6 @@ void IngredientListModel::deleteIngredient(int index){ emit dataChanged(createIndex(0, 0), createIndex(this->ingredients.size()-1, 0)); } -vector IngredientListModel::getIngredients(){ +vector IngredientListModel::getIngredients(){ return this->ingredients; } diff --git a/model/recipe/ingredients/ingredientlistmodel.h b/model/recipe/ingredients/ingredientlistmodel.h index 738e570..31dd94d 100644 --- a/model/recipe/ingredients/ingredientlistmodel.h +++ b/model/recipe/ingredients/ingredientlistmodel.h @@ -16,13 +16,13 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; //Custom methods to handle ingredient data. - void setIngredients(vector ingredients); - bool addIngredient(RecipeIngredient ri); + void setIngredients(vector ingredients); + bool addIngredient(Ingredient ri); void deleteIngredient(int index); - vector getIngredients(); + vector getIngredients(); private: - vector ingredients; + vector ingredients; //Helper for printing. diff --git a/model/recipe/ingredients/recipeingredient.cpp b/model/recipe/ingredients/recipeingredient.cpp index 34de9d1..6e7d775 100644 --- a/model/recipe/ingredients/recipeingredient.cpp +++ b/model/recipe/ingredients/recipeingredient.cpp @@ -14,6 +14,10 @@ RecipeIngredient::RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure u setComment(comment); } +RecipeIngredient::RecipeIngredient(Ingredient &i) : RecipeIngredient(i, 0.0f, UnitOfMeasure("bleh"), "Fuck"){ + //Constructs recipe ingredient from ingredient which is a hidden recipe ingredient. +} + RecipeIngredient::RecipeIngredient(){ } diff --git a/model/recipe/ingredients/recipeingredient.h b/model/recipe/ingredients/recipeingredient.h index cd044a5..8c1dd45 100644 --- a/model/recipe/ingredients/recipeingredient.h +++ b/model/recipe/ingredients/recipeingredient.h @@ -21,6 +21,7 @@ public: RecipeIngredient(string name, string foodGroup, float quantity, UnitOfMeasure unit, string comment); //Constructor using data from a child ingredient. RecipeIngredient(Ingredient i, float quantity, UnitOfMeasure unit, string comment); + RecipeIngredient(Ingredient &i); RecipeIngredient(); //Getters diff --git a/model/recipe/ingredients/recipeingredientlistmodel.cpp b/model/recipe/ingredients/recipeingredientlistmodel.cpp new file mode 100644 index 0000000..26e98fd --- /dev/null +++ b/model/recipe/ingredients/recipeingredientlistmodel.cpp @@ -0,0 +1,53 @@ +#include "recipeingredientlistmodel.h" + +RecipeIngredientListModel::RecipeIngredientListModel(){ + this->ingredients = vector(); +} + +int RecipeIngredientListModel::rowCount(const QModelIndex &parent) const{ + return this->ingredients.size(); +} + +QVariant RecipeIngredientListModel::data(const QModelIndex &index, int role) const{ + int row = index.row(); + RecipeIngredient i = this->ingredients[row]; + + string displayStr = i.toString(); + + switch(role){ + case Qt::DisplayRole: + return QString::fromStdString(displayStr); + } + + return QVariant(); +} + +void RecipeIngredientListModel::setIngredients(vector ingredients){ + this->ingredients = ingredients; + QModelIndex index = createIndex(0, 0); + QModelIndex bottomIndex = createIndex(ingredients.size()-1, 0); + emit dataChanged(index, bottomIndex); +} + +bool RecipeIngredientListModel::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; +} + +void RecipeIngredientListModel::deleteIngredient(int index){ + this->ingredients.erase(this->ingredients.begin() + index); + emit dataChanged(createIndex(0, 0), createIndex(this->ingredients.size()-1, 0)); +} + +vector RecipeIngredientListModel::getIngredients(){ + return this->ingredients; +} diff --git a/model/recipe/ingredients/recipeingredientlistmodel.h b/model/recipe/ingredients/recipeingredientlistmodel.h new file mode 100644 index 0000000..5ba9da0 --- /dev/null +++ b/model/recipe/ingredients/recipeingredientlistmodel.h @@ -0,0 +1,28 @@ +#ifndef RECIPEINGREDIENTLISTMODEL_H +#define RECIPEINGREDIENTLISTMODEL_H + +#include + +#include "model/recipe/ingredients/recipeingredient.h" + +class RecipeIngredientListModel : public QAbstractListModel +{ + public: + RecipeIngredientListModel(); + + //Overridden methods. + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + + //Custom methods to handle ingredient data. + void setIngredients(vector ingredients); + bool addIngredient(RecipeIngredient ri); + void deleteIngredient(int index); + vector getIngredients(); + + private: + vector ingredients; + +}; + +#endif // RECIPEINGREDIENTLISTMODEL_H