From 43b2132f4d57fac69be698d26cc8f920967065e6 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sat, 2 Mar 2024 16:16:59 -0500 Subject: Render entries --- src/add_entry_form.cpp | 45 +++++++++++++++++++++++++++++++++++++ src/add_entry_form.h | 23 +++++++++++++++++++ src/add_entry_form.ui | 8 +++---- src/add_group_form.h | 2 +- src/add_group_form.ui | 8 +++---- src/assignmentList.cpp | 22 +++++++++++++++++-- src/assignmentList.h | 4 +++- src/backend/db_sqlite.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++- src/backend/db_sqlite.h | 3 ++- src/entry.cpp | 8 ++++++- src/entry.h | 2 +- src/group.cpp | 6 ++++- src/lib.cpp | 13 +++++++++++ src/lib.h | 7 ++++++ 14 files changed, 190 insertions(+), 17 deletions(-) create mode 100644 src/add_entry_form.cpp create mode 100644 src/add_entry_form.h create mode 100644 src/lib.cpp create mode 100644 src/lib.h (limited to 'src') diff --git a/src/add_entry_form.cpp b/src/add_entry_form.cpp new file mode 100644 index 0000000..b08fedc --- /dev/null +++ b/src/add_entry_form.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +#include "add_entry_form.h" +#include "backend/db_sqlite.h" + +AddEntryForm::AddEntryForm(int parent_id) : + parent_id(parent_id) +{ + // load uic + ui.setupUi(this); + + // widgets setup + ui.new_entry_due->setDate(QDate::currentDate()); +} + +void AddEntryForm::accept() { + QString desc_text = ui.new_entry_desc->text(); + QDateTime due_text = ui.new_entry_due->date().startOfDay(); + QString due_alt_text = ui.new_entry_due_alt->text(); + QString link_text = ui.new_entry_link->text(); + QString color_text = ui.new_entry_color->text(); + QString highlight_text = ui.new_entry_highlight->text(); + QMessageBox error_message; + BackendDB database; + int new_id; + + // Check that the new entry is not blank + if(desc_text.isEmpty()) { + error_message.setIcon(QMessageBox::Warning); + error_message.setWindowTitle("Error Message"); + error_message.setText("Description cannot be blank"); + error_message.exec(); + return; + } + + // replace due_text with null date if due_checkbox is unchecked + if(!ui.new_entry_due_checkbox->isChecked()) + due_text = QDateTime(); + + new_id = database.insertEntry(Entry(0, this->parent_id, desc_text, due_text, due_alt_text, link_text, color_text, highlight_text)); + + QDialog::accept(); +} diff --git a/src/add_entry_form.h b/src/add_entry_form.h new file mode 100644 index 0000000..863dc57 --- /dev/null +++ b/src/add_entry_form.h @@ -0,0 +1,23 @@ +#ifndef ADDENTRYFORM_H +#define ADDENTRYFORM_H + +#include + +#include "ui_add_entry_form.h" + +class AddEntryForm : public QDialog { + Q_OBJECT + + public: + AddEntryForm(int parent_id); + + private: + Ui::addEntryDialog ui; + + int parent_id; + + private slots: + void accept(); +}; + +#endif diff --git a/src/add_entry_form.ui b/src/add_entry_form.ui index 6f60175..7a33856 100644 --- a/src/add_entry_form.ui +++ b/src/add_entry_form.ui @@ -1,7 +1,7 @@ - Dialog - + addEntryDialog + 0 @@ -152,7 +152,7 @@ buttonBox accepted() - Dialog + addEntryDialog accept() @@ -168,7 +168,7 @@ buttonBox rejected() - Dialog + addEntryDialog reject() diff --git a/src/add_group_form.h b/src/add_group_form.h index 2206e34..3b9c381 100644 --- a/src/add_group_form.h +++ b/src/add_group_form.h @@ -10,7 +10,7 @@ class AddGroupForm : public QDialog { AddGroupForm(); private: - Ui::Dialog ui; + Ui::addGroupDialog ui; private slots: void accept(); diff --git a/src/add_group_form.ui b/src/add_group_form.ui index cda7d55..53b854b 100644 --- a/src/add_group_form.ui +++ b/src/add_group_form.ui @@ -1,7 +1,7 @@ - Dialog - + addGroupDialog + 0 @@ -98,7 +98,7 @@ buttonBox accepted() - Dialog + addGroupDialog accept() @@ -114,7 +114,7 @@ buttonBox rejected() - Dialog + addGroupDialog reject() diff --git a/src/assignmentList.cpp b/src/assignmentList.cpp index cf53fc3..4e6dff9 100644 --- a/src/assignmentList.cpp +++ b/src/assignmentList.cpp @@ -74,8 +74,7 @@ void AssignmentList::displayWidgets() { for(i = 0; i < groups.size(); ++i) { if(groups[i]->hidden) continue; - // TODO set right click behavior - // TODO add entries to this layout + groups[i]->addLayout(this->drawEntries(groups[i]->id)); // add entries to layout if(groups[i]->column.toLower() == "left") column_left->addLayout(groups[i]); else column_right->addLayout(groups[i]); } @@ -87,6 +86,25 @@ void AssignmentList::displayWidgets() { ui.groups_layout->addLayout(column_right, 0, 1); } +QVBoxLayout *AssignmentList::drawEntries(int parent_id) { + BackendDB database; + QList entries = database.loadEntries(parent_id); + QVBoxLayout *output = new QVBoxLayout; + int i; + + // styling + output->setContentsMargins(5, 0, 0, 0); + + for(i = 0; i < entries.size(); ++i) { + // skip if this entry is set to hidden + if(entries[i]->hidden) continue; + // TODO set right click behavior + output->addLayout(entries[i]); + } + + return output; +} + // Open the 'addGroup' form void AssignmentList::addGroup() { AddGroupForm create_new_group_dialog; diff --git a/src/assignmentList.h b/src/assignmentList.h index 9997ab5..d5da023 100644 --- a/src/assignmentList.h +++ b/src/assignmentList.h @@ -3,6 +3,7 @@ #include #include +#include #include "ui_assignmentList.h" @@ -13,6 +14,7 @@ class AssignmentList : public QMainWindow { QSettings configuration; AssignmentList(); + void displayWidgets(); private: Ui::MainWindow ui; @@ -20,7 +22,7 @@ class AssignmentList : public QMainWindow { void initializeSettings(); void initializeUI(); void displayDate(); - void displayWidgets(); + QVBoxLayout *drawEntries(int parent_id); void editEntry(int id); void toggleDoneEntry(int id); void removeEntry(int id); diff --git a/src/backend/db_sqlite.cpp b/src/backend/db_sqlite.cpp index 7176a95..f65cd8f 100644 --- a/src/backend/db_sqlite.cpp +++ b/src/backend/db_sqlite.cpp @@ -77,7 +77,36 @@ QList BackendDB::loadEntries() { QSqlDatabase database(this->openDB()); QSqlQuery query; - query.exec("SELECT * FROM groups"); + query.exec("SELECT * FROM entries"); + 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())); + } + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); + return output; +} + +QList BackendDB::loadEntries(int parent_id) { + QList output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("SELECT * FROM entries WHERE parent_id = ?"); + query.bindValue(0, parent_id); + query.exec(); while(query.next()) { output.append(new Entry( query.record().field("id").value().toInt(), @@ -142,6 +171,31 @@ int BackendDB::insertGroup(const Group &new_group) { return output; } +// insert group to the database (returns 0 if failed) +int BackendDB::insertEntry(const Entry &new_entry) { + int output; + + { + QSqlDatabase database(this->openDB()); + QSqlQuery query; + + query.prepare("INSERT INTO entries (parent_id, description, due_date, alt_due_date, link, color, highlight) VALUES (:p_id, :desc, :due, :alt_due, :link, :color, :highlight)"); + query.bindValue(":p_id", new_entry.parent_id); + query.bindValue(":desc", new_entry.desc); + query.bindValue(":due", new_entry.due.toString("yyyy-MM-dd")); + query.bindValue(":alt_due", new_entry.due_alt); + query.bindValue(":link", new_entry.link); + query.bindValue(":color", new_entry.color); + query.bindValue(":highlight", new_entry.highlight); + query.exec(); + + output = query.lastInsertId().toInt(); + } + + QSqlDatabase::removeDatabase("qt_sql_default_connection"); + return output; +} + QString BackendDB::getDBPath() { QSettings settings; settings.beginGroup("paths"); diff --git a/src/backend/db_sqlite.h b/src/backend/db_sqlite.h index bec534b..afaac8d 100644 --- a/src/backend/db_sqlite.h +++ b/src/backend/db_sqlite.h @@ -14,9 +14,10 @@ class BackendDB : QSqlDatabase { BackendDB(); QList loadGroups(); QList loadEntries(); + QList loadEntries(int parent_id); QList loadRules(); int insertGroup(const Group &new_group); - int insertEntry(int new_entry); // param datatype TBD + int insertEntry(const Entry &new_entry); int insertRule(int new_rule); // param datatype TBD void updateGroup(int group); // param datatype TBD void updateEntry(int entry); // param datatype TBD diff --git a/src/entry.cpp b/src/entry.cpp index ed602d0..cdaa69a 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -34,18 +34,20 @@ Entry::Entry(int id, int parent_id, QString desc, QDateTime due, QString due_alt // set conditional styling if(this->done) { bullet->setText("\u2713"); + /* bullet->setStyleSheet( "QLabel {" " color: green;" "}" ); + */ } else bullet->setText("- "); this->addWidget(bullet); if(!this->due.isNull()) - body->setText(this->due.toString("M/d/yyyy")); + body->setText(this->due.toString("MM/dd/yyyy: ")); else if(!this->due_alt.isEmpty()) body->setText(this->due_alt + ": "); @@ -63,13 +65,16 @@ Entry::Entry(int id, int parent_id, QString desc, QDateTime due, QString due_alt QFont body_font = body->font(); body_font.setStrikeOut(true); body->setFont(body_font); + /* body->setStyleSheet( "QLabel {" " color: green" "}" ); + */ } else { + /* body->setStyleSheet( "QLabel {" " color: " + (this->color.isEmpty() ? "default" : this->color) + ";" @@ -77,6 +82,7 @@ Entry::Entry(int id, int parent_id, QString desc, QDateTime due, QString due_alt " font-weight: " + (this->due.isValid() && this->due <= QDateTime::currentDateTime() ? "bold" : "normal") + ";" ";" ); + */ } this->addWidget(body); } diff --git a/src/entry.h b/src/entry.h index 6bee76c..32cfd1a 100644 --- a/src/entry.h +++ b/src/entry.h @@ -6,7 +6,7 @@ #include #include -class Entry : QHBoxLayout { +class Entry : public QHBoxLayout { public: int id; int parent_id; diff --git a/src/group.cpp b/src/group.cpp index 5bc6b7c..1757753 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -4,7 +4,9 @@ #include +#include "add_entry_form.h" #include "group.h" +#include "lib.h" Group::Group(int id, QString name, QString column, QString link, bool hidden) : id(id), @@ -50,7 +52,9 @@ void Group::showContextMenu() { } void Group::addEntry() { - qDebug() << "WIP"; + AddEntryForm create_new_entry_dialog(this->id); + if(create_new_entry_dialog.exec() == QDialog::Accepted) + getMainWindow()->displayWidgets(); } void Group::editGroup() { diff --git a/src/lib.cpp b/src/lib.cpp new file mode 100644 index 0000000..2815863 --- /dev/null +++ b/src/lib.cpp @@ -0,0 +1,13 @@ +#include +#include +#include + +#include "lib.h" + +// shamelessly ripped from https://stackoverflow.com/a/46456214 +AssignmentList *getMainWindow() { + foreach(QWidget *w, QApplication::topLevelWidgets()) + if(AssignmentList *mainWin = qobject_cast(w)) + return mainWin; + return nullptr; +} diff --git a/src/lib.h b/src/lib.h new file mode 100644 index 0000000..b9b876b --- /dev/null +++ b/src/lib.h @@ -0,0 +1,7 @@ +/* Helper functions that various classes utilize */ + +#include + +#include "assignmentList.h" + +AssignmentList *getMainWindow(); -- cgit