diff --git a/RecipeDB.pro b/RecipeDB.pro index b170e61..1c8f716 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -33,7 +33,8 @@ SOURCES += model/recipe/instruction.cpp \ gui/newDialogs/newunitdialog.cpp \ utils/aspectratiopixmaplabel.cpp \ utils/stringutils.cpp \ - gui/openrecipedialog.cpp + gui/openrecipedialog.cpp \ + model/recipe/recipetablemodel.cpp HEADERS += model/recipe/instruction.h \ model/recipe/recipe.h \ @@ -56,7 +57,8 @@ HEADERS += model/recipe/instruction.h \ gui/newDialogs/newunitdialog.h \ utils/aspectratiopixmaplabel.h \ utils/stringutils.h \ - gui/openrecipedialog.h + gui/openrecipedialog.h \ + model/recipe/recipetablemodel.h LIBS += -ldl \ diff --git a/gui/openrecipedialog.cpp b/gui/openrecipedialog.cpp index 950a06c..67e3add 100644 --- a/gui/openrecipedialog.cpp +++ b/gui/openrecipedialog.cpp @@ -6,9 +6,22 @@ OpenRecipeDialog::OpenRecipeDialog(QWidget *parent) : ui(new Ui::OpenRecipeDialog) { ui->setupUi(this); + + ui->recipeTableView->setModel(&this->recipeTableModel); +} + +OpenRecipeDialog::OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent) : OpenRecipeDialog(parent){ + this->recipeDB = recipeDB; + this->populateRecipesTable(); } OpenRecipeDialog::~OpenRecipeDialog() { delete ui; } + +void OpenRecipeDialog::populateRecipesTable(){ + vector recipes = this->recipeDB->retrieveAllRecipes(); + this->recipeTableModel.setRecipes(recipes); + ui->recipeTableView->update(QModelIndex()); +} diff --git a/gui/openrecipedialog.h b/gui/openrecipedialog.h index ad95ad6..51c7a18 100644 --- a/gui/openrecipedialog.h +++ b/gui/openrecipedialog.h @@ -3,6 +3,9 @@ #include +#include "model/database/recipedatabase.h" +#include "model/recipe/recipetablemodel.h" + namespace Ui { class OpenRecipeDialog; } @@ -13,10 +16,15 @@ class OpenRecipeDialog : public QDialog public: explicit OpenRecipeDialog(QWidget *parent = 0); + OpenRecipeDialog(RecipeDatabase *recipeDB, QWidget *parent = 0); ~OpenRecipeDialog(); private: Ui::OpenRecipeDialog *ui; + RecipeDatabase *recipeDB; + RecipeTableModel recipeTableModel; + + void populateRecipesTable(); }; #endif // OPENRECIPEDIALOG_H diff --git a/model/database/recipedatabase.cpp b/model/database/recipedatabase.cpp index 5ab05fd..d1a8de0 100644 --- a/model/database/recipedatabase.cpp +++ b/model/database/recipedatabase.cpp @@ -155,6 +155,15 @@ Recipe RecipeDatabase::retrieveRecipe(string name){ return r; } +vector RecipeDatabase::retrieveAllRecipes(){ + ResultTable t = this->selectFrom("recipe", "name", "ORDER BY name"); + vector recipes; + for (unsigned int row = 0; row < t.rowCount(); row++){ + recipes.push_back(this->retrieveRecipe(t.valueAt(row, 0))); + } + return recipes; +} + vector RecipeDatabase::retrieveRecipeIngredients(int recipeId){ ResultTable t = this->executeSQL("SELECT ingredient.name, ingredient.foodGroup, "//0, 1 "recipeIngredient.quantity, recipeIngredient.unitName, recipeIngredient.comment,"//2, 3, 4 diff --git a/model/database/recipedatabase.h b/model/database/recipedatabase.h index 099c233..60d382d 100644 --- a/model/database/recipedatabase.h +++ b/model/database/recipedatabase.h @@ -32,6 +32,7 @@ class RecipeDatabase : public Database //Retrieval. Recipe retrieveRecipe(string name); + vector retrieveAllRecipes(); vector retrieveRecipeIngredients(int recipeId); vector retrieveAllIngredients(); vector retrieveAllUnitsOfMeasure(); diff --git a/model/recipe/recipetablemodel.cpp b/model/recipe/recipetablemodel.cpp new file mode 100644 index 0000000..db58738 --- /dev/null +++ b/model/recipe/recipetablemodel.cpp @@ -0,0 +1,58 @@ +#include "recipetablemodel.h" + +RecipeTableModel::RecipeTableModel() +{ + +} + +RecipeTableModel::RecipeTableModel(vector recipes){ + this->setRecipes(recipes); +} + +int RecipeTableModel::rowCount(const QModelIndex &parent) const{ + Q_UNUSED(parent); + return this->recipes.size(); +} + +int RecipeTableModel::columnCount(const QModelIndex &parent) const{ + Q_UNUSED(parent); + return 2;//FIX THIS TO BE MORE ADAPTIVE EVENTUALLY. +} + +QVariant RecipeTableModel::data(const QModelIndex &index, int role) const{ + int row = index.row(); + int col = index.column(); + Recipe r = this->recipes[row]; + + if (role == Qt::DisplayRole){ + switch(col){ + case 0: + return QString::fromStdString(r.getName()); + case 1: + return QString::fromStdString(r.getCreatedDate().toString().toStdString()); + } + } + return QVariant(); +} + +QVariant RecipeTableModel::headerData(int section, Qt::Orientation orientation, int role) const{ + if (role != Qt::DisplayRole){ + return QVariant(); + } + if (orientation == Qt::Horizontal){ + switch (section){ + case 0: + return "Name"; + case 1: + return "Created On"; + } + } else if (orientation == Qt::Vertical){ + return QString::fromStdString(std::to_string(section)); + } + return QVariant(); +} + +void RecipeTableModel::setRecipes(vector recipes){ + this->recipes = recipes; + emit dataChanged(createIndex(0, 0), createIndex(this->recipes.size()-1, 2)); +} diff --git a/model/recipe/recipetablemodel.h b/model/recipe/recipetablemodel.h new file mode 100644 index 0000000..04285f5 --- /dev/null +++ b/model/recipe/recipetablemodel.h @@ -0,0 +1,27 @@ +#ifndef RECIPETABLEMODEL_H +#define RECIPETABLEMODEL_H + +#include + +#include "model/recipe/recipe.h" + +class RecipeTableModel : public QAbstractTableModel +{ + public: + RecipeTableModel(); + RecipeTableModel(vector recipes); + + //Overridden methods. + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + //Normal methods. + void setRecipes(vector recipes); + + private: + vector recipes; +}; + +#endif // RECIPETABLEMODEL_H diff --git a/userInterface/mainwindow.cpp b/userInterface/mainwindow.cpp index d2825d3..e805751 100644 --- a/userInterface/mainwindow.cpp +++ b/userInterface/mainwindow.cpp @@ -77,3 +77,9 @@ void MainWindow::on_newButton_clicked(){ this->loadFromRecipe(r); } } + +void MainWindow::on_openButton_clicked(){ + OpenRecipeDialog d(this->recipeDB, this); + d.show(); + d.exec(); +} diff --git a/userInterface/mainwindow.h b/userInterface/mainwindow.h index b5bcdb5..515fddb 100644 --- a/userInterface/mainwindow.h +++ b/userInterface/mainwindow.h @@ -8,6 +8,7 @@ #include "model/recipe/recipe.h" #include "model/recipe/ingredients/ingredientlistmodel.h" #include "gui/newrecipedialog.h" +#include "gui/openrecipedialog.h" #include "utils/stringutils.h" using namespace std; @@ -30,6 +31,8 @@ public: private slots: void on_newButton_clicked(); + void on_openButton_clicked(); + private: Ui::MainWindow *ui; RecipeDatabase *recipeDB;