diff --git a/RecipeDB.pro b/RecipeDB.pro index 235d559..cfc7f09 100644 --- a/RecipeDB.pro +++ b/RecipeDB.pro @@ -19,7 +19,8 @@ SOURCES += SQLite/sqlite3.c \ model/recipe/recipeingredient.cpp \ userInterface/mainwindow.cpp \ main.cpp \ - model/database/database.cpp + model/database/database.cpp \ + model/recipe/ingredientlistmodel.cpp HEADERS += SQLite/sqlite3.h \ SQLite/sqlite3ext.h \ @@ -28,12 +29,12 @@ HEADERS += SQLite/sqlite3.h \ model/recipe/recipe.h \ model/recipe/recipeingredient.h \ userInterface/mainwindow.h \ - model/database/database.h + model/database/database.h \ + model/recipe/ingredientlistmodel.h FORMS += gui/mainwindow.ui DISTFILES += \ .gitignore -RESOURCES += \ - gui/menubuttonstylesheet.qrc +RESOURCES += diff --git a/gui/mainwindow.ui b/gui/mainwindow.ui index 365f358..2dcf446 100644 --- a/gui/mainwindow.ui +++ b/gui/mainwindow.ui @@ -2,6 +2,9 @@ MainWindow + + true + 0 @@ -11,7 +14,7 @@ - + 0 0 @@ -26,177 +29,473 @@ 1.000000000000000 - - - - 0 - 0 - 150 - 500 - - - - false - - - background-color: rgb(0, 0, 104) - - - - - 0 - -1 - 151 - 51 - - - - - Source Sans Pro Light - 24 - 75 - true - - - - false - - - color: rgb(255, 255, 255); - - - Recipe DB - - - Qt::RichText - - - Qt::AlignCenter - - - - - - 0 - 53 - 151 - 451 - - - - - 6 - - - QLayout::SetDefaultConstraint - - - - - - Source Sans Pro Light - 16 - - - - false - - - background-color: rgb(255, 255, 255); -color: rgb(0, 0, 104); - - - New - - - false - - - false - - - false - - - false - - - - - - - - Source Sans Pro Light - 16 - - - - background-color: rgb(255, 255, 255); -color: rgb(0, 0, 104); - - - Open - - - false - - - - - - - - Source Sans Pro Light - 16 - - - - background-color: rgb(255, 255, 255); -color: rgb(0, 0, 104); - - - Browse - - - - - - - - - - 149 - -1 - 861 - 511 - - - - background-color: qlineargradient(spread:pad, x1:0, y1:0.466, x2:1, y2:0.534, stop:0 rgba(0, 55, 104, 255), stop:1 rgba(0, 0, 0, 255)); - - - QFrame::Plain - - + + 0 - - Qt::ScrollBarAsNeeded + + 0 - - false + + 0 - - - - 0 - 0 - 859 - 509 - - - - + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 150 + 0 + + + + false + + + background-color: rgb(210, 222, 255); + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 150 + 80 + + + + + Source Sans Pro Light + 20 + + + + background-color: rgb(83, 75, 255); + + + New + + + false + + + + + + + + 150 + 80 + + + + + Source Sans Pro Light + 20 + + + + background-color: rgb(112, 105, 255); + + + Open + + + + + + + + 150 + 80 + + + + + Source Sans Pro Light + 20 + + + + background-color: rgb(137, 131, 255); + + + Browse + + + + + + + + + + + + + + 0 + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 0 + 80 + + + + + Source Sans Pro Light + 24 + + + + background-color: rgb(219, 216, 216); + + + Recipe Name + + + Qt::AlignCenter + + + + + + + + 0 + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 250 + 16777215 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 0 + 40 + + + + + Source Sans Pro Light + 18 + + + + background-color: rgb(194, 196, 233); + + + Ingredients + + + Qt::AlignCenter + + + + + + + + 0 + 0 + + + + + Source Sans Pro Light + 16 + + + + background-color: rgb(232, 232, 232); + + + QFrame::NoFrame + + + QFrame::Plain + + + 0 + + + Qt::ScrollBarAlwaysOff + + + true + + + QListView::SinglePass + + + true + + + + + + + + + + + 0 + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 0 + 40 + + + + + Source Sans Pro Light + 18 + + + + background-color: rgb(218, 219, 234); + + + Instructions + + + Qt::AlignCenter + + + + + + + background-color: rgb(244, 244, 244); + + + QFrame::NoFrame + + + QFrame::Plain + + + 0 + + + Qt::ScrollBarAlwaysOff + + + QAbstractScrollArea::AdjustToContents + + + QTextEdit::WidgetWidth + + + true + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" color:#00ff40;">This is some </span><span style=" font-size:16pt; color:#a33c3e;">colored text and </span><a href="https://www.google.com"><span style=" font-size:8pt; text-decoration: underline; color:#0000ff;">link</span></a></p></body></html> + + + Qt::LinksAccessibleByMouse + + + + + + + + + + + 0 + 0 + + + + + 200 + 0 + + + + background-color: rgb(255, 255, 255); + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + + diff --git a/main.cpp b/main.cpp index 4ec32ba..4f87ea1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,7 @@ #include "userInterface/mainwindow.h" #include -#include -#include "SQLite/sqlite3.h" -static int callback(void* data, int rows, char** argv, char** azColName){ - int i; - fprintf(stderr, "%s: ", (const char*)data); - for(i=0; ifilename = filename; + openConnection(); + if (tableExists("ingredients")){ + printf("Ingredients table already exists.\n"); + } else { + printf("Couldn't find the ingredients table.\n"); + } +} +Database::~Database(){ + closeConnection(); } void Database::openConnection(){ this->returnCode = sqlite3_open(this->filename.c_str(), &this->db); - if (this->returnCode){ + if (this->returnCode || this->db == NULL){ + this->dbIsOpen = false; fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); exit(EXIT_FAILURE); + } else { + this->dbIsOpen = true; } } + +void Database::closeConnection(){ + this->returnCode = sqlite3_close(this->db); + this->dbIsOpen = false; +} + +bool Database::tableExists(string tableName){ + if (this->dbIsOpen){ + this->sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"';"; + const char* str = this->sql.c_str(); + this->returnCode = sqlite3_exec(this->db, str, NULL, 0, &this->errorMsg); + if (this->returnCode == SQLITE_ERROR){ + fprintf(stderr, "Unable to select name from master table list: %s\n", this->errorMsg); + return false; + } else { + return true; + } + } + return false; +} diff --git a/model/database/database.h b/model/database/database.h index a0c5548..12a294e 100644 --- a/model/database/database.h +++ b/model/database/database.h @@ -13,17 +13,26 @@ class Database { public: Database(string filename); + ~Database(); void insertIngredient(Ingredient); - vector getIngredients(); + private: + //SQL Instance variables. string filename; sqlite3* db; + bool dbIsOpen; int returnCode; string sql; char* errorMsg; void openConnection(); + void closeConnection(); + //Guarantees that all tables from the schema exist. + void ensureTablesExist(); + + //Utility methods. + bool tableExists(string tableName); }; #endif // DATABASE_H diff --git a/model/recipe/ingredientlistmodel.cpp b/model/recipe/ingredientlistmodel.cpp new file mode 100644 index 0000000..d837125 --- /dev/null +++ b/model/recipe/ingredientlistmodel.cpp @@ -0,0 +1,17 @@ +#include "model/recipe/ingredientlistmodel.h" + +IngredientListModel::IngredientListModel(){ + this->ingredients = vector(); +} + +int IngredientListModel::rowCount(const QModelIndex &parent){ + return this->ingredients.size(); +} + +QVariant IngredientListModel::data(const QModelIndex &index, int role){ + +} + +void IngredientListModel::setIngredients(vector ingredients){ + this->ingredients = ingredients; +} diff --git a/model/recipe/ingredientlistmodel.h b/model/recipe/ingredientlistmodel.h new file mode 100644 index 0000000..b71d21c --- /dev/null +++ b/model/recipe/ingredientlistmodel.h @@ -0,0 +1,24 @@ +#ifndef INGREDIENTLISTMODEL_H +#define INGREDIENTLISTMODEL_H + +#include + +#include "model/recipe/ingredient.h" + +class IngredientListModel : public QAbstractListModel +{ +public: + IngredientListModel(); + + //Overridden methods. + int rowCount(const QModelIndex &parent = QModelIndex()); + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole); + + //Custom methods to handle ingredient data. + void setIngredients(vector ingredients); + +private: + vector ingredients; +}; + +#endif // INGREDIENTLISTMODEL_H diff --git a/model/recipe/recipe.cpp b/model/recipe/recipe.cpp index e510e52..948e231 100644 --- a/model/recipe/recipe.cpp +++ b/model/recipe/recipe.cpp @@ -1,6 +1,25 @@ -#include "headers/recipe.h" - -Recipe::Recipe() -{ +#include "model/recipe/recipe.h" +Recipe::Recipe(){ + this->name = "NULL"; + this->ingredients = vector(); + this->instructions = vector(); +} + +Recipe::Recipe(string name, vector ingredients, vector instructions){ + this->name = name; + this->ingredients = ingredients; + this->instructions = instructions; +} + +string Recipe::getName(){ + return this->name; +} + +vector Recipe::getIngredients(){ + return this->ingredients; +} + +vector Recipe::getInstructions(){ + return this->instructions; } diff --git a/model/recipe/recipe.h b/model/recipe/recipe.h index bcdee54..4cf3b9f 100644 --- a/model/recipe/recipe.h +++ b/model/recipe/recipe.h @@ -5,8 +5,8 @@ #include #include -#include "headers/ingredient.h" -#include "headers/instruction.h" +#include "model/recipe/ingredient.h" +#include "model/recipe/instruction.h" using namespace std; @@ -14,13 +14,17 @@ class Recipe { public: Recipe(); + Recipe(string name, vector ingredients, vector instructions); string getName(); + vector getIngredients(); + vector getInstructions(); private: string name; vector tags; vector ingredients; vector instructions; + }; #endif // RECIPE_H diff --git a/userInterface/mainwindow.cpp b/userInterface/mainwindow.cpp index 3e2f80b..895af29 100644 --- a/userInterface/mainwindow.cpp +++ b/userInterface/mainwindow.cpp @@ -3,12 +3,28 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) -{ + ui(new Ui::MainWindow){ ui->setupUi(this); } -MainWindow::~MainWindow() -{ +MainWindow::~MainWindow(){ delete ui; } + +void MainWindow::loadFromRecipe(Recipe recipe){ + setRecipeName(recipe.getName()); + setInstructions(recipe.getInstructions()); + setIngredients(recipe.getIngredients()); +} + +void MainWindow::setRecipeName(string name){ + ui->recipeNameLabel->setText(QString::fromStdString(name)); +} + +void MainWindow::setInstructions(vector instructions){ + +} + +void MainWindow::setIngredients(vector ingredients){ + ui->listView +} diff --git a/userInterface/mainwindow.h b/userInterface/mainwindow.h index a3948a9..eae9892 100644 --- a/userInterface/mainwindow.h +++ b/userInterface/mainwindow.h @@ -2,6 +2,11 @@ #define MAINWINDOW_H #include +#include + +#include "model/recipe/recipe.h" + +using namespace std; namespace Ui { class MainWindow; @@ -15,8 +20,15 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + //Loads all data from a recipe into the GUI components. + void loadFromRecipe(Recipe recipe); private: Ui::MainWindow *ui; + + //Hidden manipulation methods. + void setRecipeName(string name); + void setInstructions(vector instructions); + void setIngredients(vector ingredients); }; #endif // MAINWINDOW_H