summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouie S <louie@example.com>2024-03-02 16:16:59 -0500
committerLouie S <louie@example.com>2024-03-02 16:16:59 -0500
commit43b2132f4d57fac69be698d26cc8f920967065e6 (patch)
treeed92c38189796e7dbb533d4685bc5743cd3d3b80
parent74c9ba962ffe142b04f77fd831438a75eec7b46b (diff)
Render entries
-rw-r--r--CMakeLists.txt4
-rw-r--r--src/add_entry_form.cpp45
-rw-r--r--src/add_entry_form.h23
-rw-r--r--src/add_entry_form.ui8
-rw-r--r--src/add_group_form.h2
-rw-r--r--src/add_group_form.ui8
-rw-r--r--src/assignmentList.cpp22
-rw-r--r--src/assignmentList.h4
-rw-r--r--src/backend/db_sqlite.cpp56
-rw-r--r--src/backend/db_sqlite.h3
-rw-r--r--src/entry.cpp8
-rw-r--r--src/entry.h2
-rw-r--r--src/group.cpp6
-rw-r--r--src/lib.cpp13
-rw-r--r--src/lib.h7
15 files changed, 194 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 84fae38..62efce1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -31,6 +31,8 @@ foreach(X ${qt_components})
endforeach()
set(project_sources
+ "src/add_entry_form.cpp"
+ "src/add_entry_form.h"
"src/add_entry_form.ui"
"src/add_group_form.cpp"
"src/add_group_form.h"
@@ -44,6 +46,8 @@ set(project_sources
"src/entry.h"
"src/group.cpp"
"src/group.h"
+ "src/lib.cpp"
+ "src/lib.h"
"src/main.cpp"
"src/preferences_dialog.ui"
"src/rule.cpp"
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 <QDate>
+#include <QMessageBox>
+#include <QString>
+
+#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 <QObject>
+
+#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 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
- <class>Dialog</class>
- <widget class="QDialog" name="Dialog">
+ <class>addEntryDialog</class>
+ <widget class="QDialog" name="addEntryDialog">
<property name="geometry">
<rect>
<x>0</x>
@@ -152,7 +152,7 @@
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
- <receiver>Dialog</receiver>
+ <receiver>addEntryDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
@@ -168,7 +168,7 @@
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
- <receiver>Dialog</receiver>
+ <receiver>addEntryDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
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 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
- <class>Dialog</class>
- <widget class="QDialog" name="Dialog">
+ <class>addGroupDialog</class>
+ <widget class="QDialog" name="addGroupDialog">
<property name="geometry">
<rect>
<x>0</x>
@@ -98,7 +98,7 @@
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
- <receiver>Dialog</receiver>
+ <receiver>addGroupDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
@@ -114,7 +114,7 @@
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
- <receiver>Dialog</receiver>
+ <receiver>addGroupDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
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<Entry *> 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 <QMainWindow>
#include <QSettings>
+#include <QVBoxLayout>
#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<Entry *> 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<Entry *> BackendDB::loadEntries(int parent_id) {
+ QList<Entry *> 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<Group *> loadGroups();
QList<Entry *> loadEntries();
+ QList<Entry *> loadEntries(int parent_id);
QList<Rule *> 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 <QString>
#include <QUrl>
-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 <QDebug>
+#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 <QApplication>
+#include <QMainWindow>
+#include <QWidget>
+
+#include "lib.h"
+
+// shamelessly ripped from https://stackoverflow.com/a/46456214
+AssignmentList *getMainWindow() {
+ foreach(QWidget *w, QApplication::topLevelWidgets())
+ if(AssignmentList *mainWin = qobject_cast<AssignmentList*>(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 <QMainWindow>
+
+#include "assignmentList.h"
+
+AssignmentList *getMainWindow();