From 6c9d7e079cceade194b5faa6c65e98920be697ef Mon Sep 17 00:00:00 2001 From: Louie S Date: Fri, 15 Mar 2024 16:35:30 -0400 Subject: sorting implemented; functionality is now matching pyqt version --- src/assignmentList.cpp | 23 +++++++++++++---------- src/backend/db_sqlite.cpp | 30 +++++++++++++++--------------- src/backend/db_sqlite.h | 10 +++++----- src/entry.cpp | 17 +++++++++++++++++ src/entry.h | 3 +++ src/entryLayout.cpp | 20 ++++++++++---------- src/entryLayout.h | 2 +- src/rulesDialog.cpp | 24 ++++++++++++------------ src/rulesDialog.h | 2 +- 9 files changed, 77 insertions(+), 54 deletions(-) diff --git a/src/assignmentList.cpp b/src/assignmentList.cpp index a883523..abd2bda 100644 --- a/src/assignmentList.cpp +++ b/src/assignmentList.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,12 +7,12 @@ #include #include #include +#include #include #include #include #include -#include #include "addGroupForm.h" #include "assignmentList.h" @@ -72,7 +73,7 @@ void AssignmentList::displayWidgets() { QVBoxLayout *column_left = new QVBoxLayout(); QVBoxLayout *column_right = new QVBoxLayout(); BackendDB database; - QList groups = database.loadGroups(); + QList groups = database.loadGroups(); GroupLayout *new_group_layout; int i; @@ -80,10 +81,10 @@ void AssignmentList::displayWidgets() { recursiveClear(ui.groups_layout); for(i = 0; i < groups.size(); ++i) { - if(groups[i]->hidden) continue; - new_group_layout = new GroupLayout(*groups[i]); - new_group_layout->addLayout(this->drawEntries(groups[i]->id)); // add entries to layout - if(groups[i]->column == Group::left) column_left->addLayout(new_group_layout); + if(groups[i].hidden) continue; + new_group_layout = new GroupLayout(groups[i]); + new_group_layout->addLayout(this->drawEntries(groups[i].id)); // add entries to layout + if(groups[i].column == Group::left) column_left->addLayout(new_group_layout); else column_right->addLayout(new_group_layout); } @@ -96,18 +97,20 @@ void AssignmentList::displayWidgets() { QVBoxLayout *AssignmentList::drawEntries(int parent_id) { BackendDB database; - QList entries = database.loadEntries(parent_id); + QList entries = database.loadEntries(parent_id); QVBoxLayout *output = new QVBoxLayout; int i; // styling output->setContentsMargins(5, 0, 0, 0); - // TODO sort entries + // sort entries + std::sort(entries.begin(), entries.end(), Entry::compare); + for(i = 0; i < entries.size(); ++i) { // skip if this entry is set to hidden - if(entries[i]->hidden) continue; - output->addLayout(new EntryLayout(*entries[i])); + if(entries[i].hidden) continue; + output->addLayout(new EntryLayout(entries[i])); } return output; diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 4e62153..f9b9a8e 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -47,8 +47,8 @@ BackendDB::BackendDB() { } // load groups -QList BackendDB::loadGroups() { - QList output; +QList BackendDB::loadGroups() { + QList output; { QSqlDatabase database(this->openDB()); @@ -56,7 +56,7 @@ QList BackendDB::loadGroups() { query.exec("SELECT * FROM groups"); while(query.next()) { - output.append(new Group( + output.append(Group( query.record().field("id").value().toInt(), query.record().field("name").value().toString(), Group::Column(query.record().field("column").value().toInt()), @@ -70,8 +70,8 @@ QList BackendDB::loadGroups() { } // load entries -QList BackendDB::loadEntries() { - QList output; +QList BackendDB::loadEntries() { + QList output; { QSqlDatabase database(this->openDB()); @@ -79,7 +79,7 @@ QList BackendDB::loadEntries() { query.exec("SELECT * FROM entries"); while(query.next()) { - output.append(new Entry( + output.append(Entry( query.record().field("id").value().toInt(), query.record().field("parent_id").value().toInt(), query.record().field("description").value().toString(), @@ -97,8 +97,8 @@ QList BackendDB::loadEntries() { return output; } -QList BackendDB::loadEntries(int parent_id) { - QList output; +QList BackendDB::loadEntries(int parent_id) { + QList output; { QSqlDatabase database(this->openDB()); @@ -108,7 +108,7 @@ QList BackendDB::loadEntries(int parent_id) { query.bindValue(0, parent_id); query.exec(); while(query.next()) { - output.append(new Entry( + output.append(Entry( query.record().field("id").value().toInt(), query.record().field("parent_id").value().toInt(), query.record().field("description").value().toString(), @@ -127,8 +127,8 @@ QList BackendDB::loadEntries(int parent_id) { } // load entries -QList BackendDB::loadRules() { - QList output; +QList BackendDB::loadRules() { + QList output; { QSqlDatabase database(this->openDB()); @@ -136,7 +136,7 @@ QList BackendDB::loadRules() { query.exec("SELECT * FROM rules"); while(query.next()) { - output.append(new Rule( + output.append(Rule( query.record().field("id").value().toInt(), query.record().field("entry_id").value().toInt(), (Rule::When) query.record().field("before_after").value().toInt(), @@ -151,8 +151,8 @@ QList BackendDB::loadRules() { } // load entries -QList BackendDB::loadRules(int entry_id) { - QList output; +QList BackendDB::loadRules(int entry_id) { + QList output; { QSqlDatabase database(this->openDB()); @@ -162,7 +162,7 @@ QList BackendDB::loadRules(int entry_id) { query.bindValue(0, entry_id); query.exec(); while(query.next()) { - output.append(new Rule( + output.append(Rule( query.record().field("id").value().toInt(), query.record().field("entry_id").value().toInt(), (Rule::When) query.record().field("before_after").value().toInt(), diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index 93ae050..b23f208 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -12,11 +12,11 @@ class BackendDB : QSqlDatabase { public: BackendDB(); - QList loadGroups(); - QList loadEntries(); - QList loadEntries(int parent_id); - QList loadRules(); - QList loadRules(int entry_id); + QList loadGroups(); + QList loadEntries(); + QList loadEntries(int parent_id); + QList loadRules(); + QList loadRules(int entry_id); int insertGroup(const Group &new_group); int insertEntry(const Entry &new_entry); int insertRule(const Rule &new_rule); diff --git a/src/entry.cpp b/src/entry.cpp index 4876641..1082c1e 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -13,3 +13,20 @@ Entry::Entry(int id, int parent_id, QString desc, QDateTime due, QString due_alt hidden(hidden) { } + +bool Entry::compare(Entry a, Entry b) { + // 1st level: sort not done before done + if(a.done != b.done) + return a.done < b.done; + // next level: sort on due date + if(a.due != b.due) + return a.due < b.due; + // next level: sort on alt due date + if(a.due_alt != b.due_alt) + return a.due_alt < b.due_alt; + // next level: sort on description + if(a.desc != b.desc) + return a.desc < b.desc; + // otherwise, sort on id + return a.id < b.id; +} diff --git a/src/entry.h b/src/entry.h index 8f632f3..61450f7 100644 --- a/src/entry.h +++ b/src/entry.h @@ -29,6 +29,9 @@ struct Entry { bool done = false, bool hidden = false ); + + // function for sorting algorithm + static bool compare(Entry a, Entry b); }; #endif diff --git a/src/entryLayout.cpp b/src/entryLayout.cpp index 349a102..b3e742f 100644 --- a/src/entryLayout.cpp +++ b/src/entryLayout.cpp @@ -35,18 +35,18 @@ EntryLayout::EntryLayout(const Entry &e) : SLOT(showContextMenu())); // Check rules - QList rules = this->loadRules(); + QList rules = this->loadRules(); for(i = 0; i < rules.size(); ++i) { if( - (rules[i]->when == Rule::before && - rules[i]->date > QDateTime::currentDateTime()) || - (rules[i]->when == Rule::after && - rules[i]->date <= QDateTime::currentDateTime()) + (rules[i].when == Rule::before && + rules[i].date > QDateTime::currentDateTime()) || + (rules[i].when == Rule::after && + rules[i].date <= QDateTime::currentDateTime()) ) { - if(!rules[i]->color.isEmpty()) - this->entry.color = rules[i]->color; - if(!rules[i]->highlight.isEmpty()) - this->entry.highlight = rules[i]->highlight; + if(!rules[i].color.isEmpty()) + this->entry.color = rules[i].color; + if(!rules[i].highlight.isEmpty()) + this->entry.highlight = rules[i].highlight; } } @@ -101,7 +101,7 @@ EntryLayout::EntryLayout(const Entry &e) : this->addWidget(body); } -QList EntryLayout::loadRules() { +QList EntryLayout::loadRules() { BackendDB database; return database.loadRules(this->entry.id); diff --git a/src/entryLayout.h b/src/entryLayout.h index df19a18..c38a51b 100644 --- a/src/entryLayout.h +++ b/src/entryLayout.h @@ -18,7 +18,7 @@ class EntryLayout : public QHBoxLayout { EntryLayout(const Entry &e); private: - QList loadRules(); + QList loadRules(); private slots: void showContextMenu(); diff --git a/src/rulesDialog.cpp b/src/rulesDialog.cpp index be8b9d3..7d5705e 100644 --- a/src/rulesDialog.cpp +++ b/src/rulesDialog.cpp @@ -32,10 +32,10 @@ void RulesDialog::updateRulesList() { int i; for(i = 0; i < ui.rules_layout->children().size(); ++i) { - this->rules[i]->when = Rule::When(qobject_cast(ui.rules_layout->children()[i])->when_widget->currentIndex()); - this->rules[i]->date = qobject_cast(ui.rules_layout->children()[i])->date_widget->dateTime(); - this->rules[i]->color = qobject_cast(ui.rules_layout->children()[i])->color_widget->text(); - this->rules[i]->highlight = qobject_cast(ui.rules_layout->children()[i])->highlight_widget->text(); + this->rules[i].when = Rule::When(qobject_cast(ui.rules_layout->children()[i])->when_widget->currentIndex()); + this->rules[i].date = qobject_cast(ui.rules_layout->children()[i])->date_widget->dateTime(); + this->rules[i].color = qobject_cast(ui.rules_layout->children()[i])->color_widget->text(); + this->rules[i].highlight = qobject_cast(ui.rules_layout->children()[i])->highlight_widget->text(); } } @@ -49,7 +49,7 @@ void RulesDialog::drawRules() { // Draw each rule for(i = 0; i < this->rules.size(); ++i) { - new_layout = new RuleLayout(*rules[i]); + new_layout = new RuleLayout(rules[i]); // connect delete button QObject::connect(new_layout->del_button, &QPushButton::released, this, [=](){ this->deleteRule(i); }); @@ -59,8 +59,8 @@ void RulesDialog::drawRules() { } void RulesDialog::addRule() { - Rule *new_rule = new Rule(0, this->entry_id, Rule::before); - RuleLayout *new_layout = new RuleLayout(*new_rule); + Rule new_rule = Rule(0, this->entry_id, Rule::before); + RuleLayout *new_layout = new RuleLayout(new_rule); // add new rule to the member variable this->rules.append(new_rule); @@ -76,8 +76,8 @@ void RulesDialog::deleteRule(int i) { this->updateRulesList(); // mark as needing to be deleted from database if id > 0 - if(this->rules[i]->id > 0) - this->deleted_rules.append(*this->rules[i]); + if(this->rules[i].id > 0) + this->deleted_rules.append(this->rules[i]); this->rules.removeAt(i); this->drawRules(); } @@ -91,11 +91,11 @@ void RulesDialog::accept() { // update rules in database for(i = 0; i < this->rules.size(); ++i) { // insert - this is a new rule - if(this->rules[i]->id == 0) - database.insertRule(*this->rules[i]); + if(this->rules[i].id == 0) + database.insertRule(this->rules[i]); // update - this is an existing rule else - database.updateRule(*this->rules[i]); + database.updateRule(this->rules[i]); } // delete deleted rules in database diff --git a/src/rulesDialog.h b/src/rulesDialog.h index 25125fb..7e74dc2 100644 --- a/src/rulesDialog.h +++ b/src/rulesDialog.h @@ -18,7 +18,7 @@ class RulesDialog : public QDialog { private: Ui::rulesDialog ui; int entry_id; - QList rules; + QList rules; QList deleted_rules; void updateRulesList(); -- cgit