From bdb594ada83058ae35edb5edf01703afa203e593 Mon Sep 17 00:00:00 2001 From: Louie S Date: Mon, 26 Feb 2024 17:04:26 -0500 Subject: Loading groups from db --- src/assignmentList.cpp | 28 +++++++++---- src/assignmentList.h | 1 - src/backend/db_sqlite.cpp | 103 +++++++++++++++++++++++++++++++++++++++------- src/backend/db_sqlite.h | 9 +++- src/group.h | 2 +- src/rule.h | 3 +- 6 files changed, 119 insertions(+), 27 deletions(-) diff --git a/src/assignmentList.cpp b/src/assignmentList.cpp index 17ec3cc..0ccda0e 100644 --- a/src/assignmentList.cpp +++ b/src/assignmentList.cpp @@ -51,24 +51,38 @@ void AssignmentList::initializeUI() { // create toolbar ui.toolBar->addAction(ui.actionAdd_Group); - this->setupDB(); + // setup database + BackendDB::init(); + this->displayDate(); this->displayWidgets(); this->show(); } -void AssignmentList::setupDB() { - BackendDB::init(); - qDebug() << "WIP"; -} - void AssignmentList::displayDate() { QDate today = QDate::currentDate(); ui.title->setText(today.toString("dddd, MMM d yyyy")); } void AssignmentList::displayWidgets() { - //this->drawGroups(); + QVBoxLayout *column_left = new QVBoxLayout(); + QVBoxLayout *column_right = new QVBoxLayout(); + QList groups = BackendDB::loadGroups(); + int i; + + for(i = 0; i < groups.size(); ++i) { + if(groups[i]->hidden) continue; + // TODO set right click behavior + // TODO add entries to this layout + if(groups[i]->column.toLower() == "left") column_left->addLayout(groups[i]); + else column_right->addLayout(groups[i]); + } + + column_left->addStretch(); + column_right->addStretch(); + + ui.groups_layout->addLayout(column_left, 0, 0); + ui.groups_layout->addLayout(column_right, 0, 1); } // Open the 'addGroup' form diff --git a/src/assignmentList.h b/src/assignmentList.h index 3e45351..6544dd0 100644 --- a/src/assignmentList.h +++ b/src/assignmentList.h @@ -19,7 +19,6 @@ class AssignmentList : public QMainWindow { void initializeSettings(); void initializeUI(); - void setupDB(); void displayDate(); void displayWidgets(); void editGroup(int id); diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 05dc702..9e4a6e7 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -4,21 +4,40 @@ #include #include #include +#include #include -#include +#include #include #include "db_sqlite.h" -QString BackendDB::getDBPath() { +QString getDBPath(); +QSqlDatabase openDB(); + +QString getDBPath() { QSettings settings; settings.beginGroup("paths"); return settings.value("db_path").toString(); } + +QSqlDatabase openDB() { + QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE"); + database.setDatabaseName(getDBPath()); + + database.open(); + if(database.isOpenError()) { + // FIXME end-user friendly error message + qDebug() << database.lastError(); + std::exit(1); + } + + return database; +} + void BackendDB::init() { - QString db_path(BackendDB::getDBPath()); + QString db_path(getDBPath()); int i; // Check if database already exists @@ -34,17 +53,7 @@ void BackendDB::init() { } // Create database - QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE"); - database.setDatabaseName(db_path); - - // TODO see if explicitly creating the parent directory is necessary - database.open(); - if(database.isOpenError()) { - // FIXME end-user friendly error message - qDebug() << database.lastError(); - std::exit(1); - } - + QSqlDatabase database = openDB(); QSqlQuery query; // Erase database contents so that we don't have duplicates @@ -59,3 +68,69 @@ void BackendDB::init() { database.close(); } + +// load groups +QList BackendDB::loadGroups() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList output; + + query.exec("SELECT * FROM groups"); + while(query.next()) { + output.append(new Group( + query.record().field("id").value().toInt(), + query.record().field("name").value().toString(), + query.record().field("column").value().toString(), + query.record().field("link").value().toString(), + query.record().field("hidden").value().toBool())); + } + + database.close(); + return output; +} + +// load entries +QList loadEntries() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList output; + + query.exec("SELECT * FROM groups"); + while(query.next()) { + output.append(new Entry( + query.record().field("id").value().toInt(), + query.record().field("parent_id").value().toInt(), + query.record().field("description").value().toString(), + query.record().field("due_date").value().toDateTime(), + query.record().field("alt_due_date").value().toString(), + query.record().field("link").value().toUrl(), + query.record().field("color").value().toString(), + query.record().field("highlight").value().toString(), + query.record().field("done").value().toBool(), + query.record().field("hidden").value().toBool())); + } + + database.close(); + return output; +} + +// load entries +QList loadRules() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList output; + + query.exec("SELECT * FROM groups"); + while(query.next()) { + output.append(new Rule( + query.record().field("id").value().toInt(), + query.record().field("entry_id").value().toInt(), + (Rule::When) query.record().field("before_after").value().toInt(), + query.record().field("date").value().toDateTime(), + query.record().field("color").value().toString(), + query.record().field("highlight").value().toString())); + } + + database.close(); + return output; +} diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index 8563223..6e94fdd 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -4,6 +4,10 @@ #include #include +#include "../entry.h" +#include "../group.h" +#include "../rule.h" + namespace BackendDB { const QStringList create_table_queries = { "CREATE TABLE groups (" @@ -35,9 +39,10 @@ namespace BackendDB { ")" }; - QString getDBPath(); void init(); - void load(); + QList loadGroups(); + QList loadEntries(); + QList loadRules(); void insertGroup(int new_group); // param datatype TBD void insertEntry(int new_entry); // param datatype TBD void insertRule(int new_rule); // param datatype TBD diff --git a/src/group.h b/src/group.h index 901a29f..5513f7c 100644 --- a/src/group.h +++ b/src/group.h @@ -4,7 +4,7 @@ #include #include -class Group : QVBoxLayout { +class Group : public QVBoxLayout { public: int id; QString name; diff --git a/src/rule.h b/src/rule.h index ecd2285..92e3356 100644 --- a/src/rule.h +++ b/src/rule.h @@ -9,9 +9,8 @@ // TODO determine whether an alternative approach is better (same goes for group and entry) class Rule : QHBoxLayout { - enum When { before, after }; - public: + enum When { before, after }; int id; int entry_id; When when; -- cgit