Added table model, having trouble getting it to work.

This commit is contained in:
Andrew Lalis 2018-03-11 12:53:30 +01:00
parent cc802e5d0d
commit 17cbeb0d1c
9 changed files with 129 additions and 2 deletions

View File

@ -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 \

View File

@ -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<Recipe> recipes = this->recipeDB->retrieveAllRecipes();
this->recipeTableModel.setRecipes(recipes);
ui->recipeTableView->update(QModelIndex());
}

View File

@ -3,6 +3,9 @@
#include <QDialog>
#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

View File

@ -155,6 +155,15 @@ Recipe RecipeDatabase::retrieveRecipe(string name){
return r;
}
vector<Recipe> RecipeDatabase::retrieveAllRecipes(){
ResultTable t = this->selectFrom("recipe", "name", "ORDER BY name");
vector<Recipe> recipes;
for (unsigned int row = 0; row < t.rowCount(); row++){
recipes.push_back(this->retrieveRecipe(t.valueAt(row, 0)));
}
return recipes;
}
vector<RecipeIngredient> RecipeDatabase::retrieveRecipeIngredients(int recipeId){
ResultTable t = this->executeSQL("SELECT ingredient.name, ingredient.foodGroup, "//0, 1
"recipeIngredient.quantity, recipeIngredient.unitName, recipeIngredient.comment,"//2, 3, 4

View File

@ -32,6 +32,7 @@ class RecipeDatabase : public Database
//Retrieval.
Recipe retrieveRecipe(string name);
vector<Recipe> retrieveAllRecipes();
vector<RecipeIngredient> retrieveRecipeIngredients(int recipeId);
vector<Ingredient> retrieveAllIngredients();
vector<UnitOfMeasure> retrieveAllUnitsOfMeasure();

View File

@ -0,0 +1,58 @@
#include "recipetablemodel.h"
RecipeTableModel::RecipeTableModel()
{
}
RecipeTableModel::RecipeTableModel(vector<Recipe> 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<Recipe> recipes){
this->recipes = recipes;
emit dataChanged(createIndex(0, 0), createIndex(this->recipes.size()-1, 2));
}

View File

@ -0,0 +1,27 @@
#ifndef RECIPETABLEMODEL_H
#define RECIPETABLEMODEL_H
#include <QAbstractTableModel>
#include "model/recipe/recipe.h"
class RecipeTableModel : public QAbstractTableModel
{
public:
RecipeTableModel();
RecipeTableModel(vector<Recipe> 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<Recipe> recipes);
private:
vector<Recipe> recipes;
};
#endif // RECIPETABLEMODEL_H

View File

@ -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();
}

View File

@ -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;