diff options
author | Louie S <louie@example.com> | 2024-02-26 17:04:26 -0500 |
---|---|---|
committer | Louie S <louie@example.com> | 2024-02-26 17:04:26 -0500 |
commit | bdb594ada83058ae35edb5edf01703afa203e593 (patch) | |
tree | ba901a1da8bcd7cffc377cd5566b0afa28e0bff7 /src | |
parent | 3ef9931e59ef38d9bbf045b07a4dbab2fd52e3f5 (diff) |
Loading groups from db
Diffstat (limited to 'src')
-rw-r--r-- | src/assignmentList.cpp | 28 | ||||
-rw-r--r-- | src/assignmentList.h | 1 | ||||
-rw-r--r-- | src/backend/db_sqlite.cpp | 103 | ||||
-rw-r--r-- | src/backend/db_sqlite.h | 9 | ||||
-rw-r--r-- | src/group.h | 2 | ||||
-rw-r--r-- | 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<Group *> 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 <QFileInfo> #include <QSettings> #include <QSqlDatabase> +#include <QSqlField> #include <QSqlQuery> -#include <QVariant> +#include <QSqlRecord> #include <QSqlError> #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<Group *> BackendDB::loadGroups() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList<Group *> 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<Entry *> loadEntries() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList<Entry *> 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<Rule *> loadRules() { + QSqlDatabase database = openDB(); + QSqlQuery query; + QList<Rule *> 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 <QString> #include <QStringList> +#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<Group *> loadGroups(); + QList<Entry *> loadEntries(); + QList<Rule *> 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 <QString> #include <QVBoxLayout> -class Group : QVBoxLayout { +class Group : public QVBoxLayout { public: int id; QString name; @@ -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; |