summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouie S <louie@example.com>2024-03-15 16:35:30 -0400
committerLouie S <louie@example.com>2024-03-15 16:35:30 -0400
commit6c9d7e079cceade194b5faa6c65e98920be697ef (patch)
tree5af2be170449822320b3b4f1a873a9dc1dde10eb
parent4c2c8bc81df55ab2f0112d16bfd1a9d236b51e6d (diff)
sorting implemented; functionality is now matching pyqt version
-rw-r--r--src/assignmentList.cpp23
-rw-r--r--src/backend/db_sqlite.cpp30
-rw-r--r--src/backend/db_sqlite.h10
-rw-r--r--src/entry.cpp17
-rw-r--r--src/entry.h3
-rw-r--r--src/entryLayout.cpp20
-rw-r--r--src/entryLayout.h2
-rw-r--r--src/rulesDialog.cpp24
-rw-r--r--src/rulesDialog.h2
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 <algorithm>
#include <QAction>
#include <QApplication>
#include <QCoreApplication>
@@ -6,12 +7,12 @@
#include <QFile>
#include <QMessageBox>
#include <QObject>
+#include <QSettings>
#include <QStandardPaths>
#include <QUiLoader>
#include <QDebug>
#include <QErrorMessage>
-#include <QSettings>
#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<Group *> groups = database.loadGroups();
+ QList<Group> 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<Entry *> entries = database.loadEntries(parent_id);
+ QList<Entry> 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<Group *> BackendDB::loadGroups() {
- QList<Group *> output;
+QList<Group> BackendDB::loadGroups() {
+ QList<Group> output;
{
QSqlDatabase database(this->openDB());
@@ -56,7 +56,7 @@ QList<Group *> 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<Group *> BackendDB::loadGroups() {
}
// load entries
-QList<Entry *> BackendDB::loadEntries() {
- QList<Entry *> output;
+QList<Entry> BackendDB::loadEntries() {
+ QList<Entry> output;
{
QSqlDatabase database(this->openDB());
@@ -79,7 +79,7 @@ QList<Entry *> 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<Entry *> BackendDB::loadEntries() {
return output;
}
-QList<Entry *> BackendDB::loadEntries(int parent_id) {
- QList<Entry *> output;
+QList<Entry> BackendDB::loadEntries(int parent_id) {
+ QList<Entry> output;
{
QSqlDatabase database(this->openDB());
@@ -108,7 +108,7 @@ QList<Entry *> 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<Entry *> BackendDB::loadEntries(int parent_id) {
}
// load entries
-QList<Rule *> BackendDB::loadRules() {
- QList<Rule *> output;
+QList<Rule> BackendDB::loadRules() {
+ QList<Rule> output;
{
QSqlDatabase database(this->openDB());
@@ -136,7 +136,7 @@ QList<Rule *> 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<Rule *> BackendDB::loadRules() {
}
// load entries
-QList<Rule *> BackendDB::loadRules(int entry_id) {
- QList<Rule *> output;
+QList<Rule> BackendDB::loadRules(int entry_id) {
+ QList<Rule> output;
{
QSqlDatabase database(this->openDB());
@@ -162,7 +162,7 @@ QList<Rule *> 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<Group *> loadGroups();
- QList<Entry *> loadEntries();
- QList<Entry *> loadEntries(int parent_id);
- QList<Rule *> loadRules();
- QList<Rule *> loadRules(int entry_id);
+ QList<Group> loadGroups();
+ QList<Entry> loadEntries();
+ QList<Entry> loadEntries(int parent_id);
+ QList<Rule> loadRules();
+ QList<Rule> 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<Rule *> rules = this->loadRules();
+ QList<Rule> 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<Rule *> EntryLayout::loadRules() {
+QList<Rule> 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<Rule *> loadRules();
+ QList<Rule> 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<RuleLayout *>(ui.rules_layout->children()[i])->when_widget->currentIndex());
- this->rules[i]->date = qobject_cast<RuleLayout *>(ui.rules_layout->children()[i])->date_widget->dateTime();
- this->rules[i]->color = qobject_cast<RuleLayout *>(ui.rules_layout->children()[i])->color_widget->text();
- this->rules[i]->highlight = qobject_cast<RuleLayout *>(ui.rules_layout->children()[i])->highlight_widget->text();
+ this->rules[i].when = Rule::When(qobject_cast<RuleLayout *>(ui.rules_layout->children()[i])->when_widget->currentIndex());
+ this->rules[i].date = qobject_cast<RuleLayout *>(ui.rules_layout->children()[i])->date_widget->dateTime();
+ this->rules[i].color = qobject_cast<RuleLayout *>(ui.rules_layout->children()[i])->color_widget->text();
+ this->rules[i].highlight = qobject_cast<RuleLayout *>(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<Rule *> rules;
+ QList<Rule> rules;
QList<Rule> deleted_rules;
void updateRulesList();