summaryrefslogtreecommitdiff
path: root/src/assignmentList.cpp
blob: 09ef6e341589bc7e97ff9a7307cc411bc4d92795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <QAction>
#include <QApplication>
#include <QCoreApplication>
#include <QDate>
#include <QFile>
#include <QMessageBox>
#include <QObject>
#include <QStandardPaths>
#include <QUiLoader>

#include <QDebug>
#include <QErrorMessage>
#include <QSettings>

#include "addGroupForm.h"
#include "assignmentList.h"
#include "backend/db_sqlite.h"
#include "entryLayout.h"
#include "groupLayout.h"
#include "preferencesDialog.h"
#include "settings.h"

AssignmentList::AssignmentList() {
	// set QSettings information
	QCoreApplication::setOrganizationName("assignment-list-qt"); // TODO grab this from a config.h type file
	QCoreApplication::setApplicationName("assignment-list-qt"); // TODO grab this from a config.h type file

	// ensure QSettings location exists
	this->initializeSettings();

	// load uic
	ui.setupUi(this);
	this->initializeUI();
}

void AssignmentList::initializeSettings() {
	QSettings settings;
	QFile path = settings.fileName();

	if(!path.exists()) {
		qDebug() << "Creating Config";
		Settings::createConfig();
	}
}

void AssignmentList::initializeUI() {
	// create menu connections
	QObject::connect(ui.actionPreferences, &QAction::triggered, this, &AssignmentList::preferences);
	QObject::connect(ui.actionReload, &QAction::triggered, this, &AssignmentList::reload);
	QObject::connect(ui.actionExit, &QAction::triggered, this, QApplication::quit);
	QObject::connect(ui.actionAdd_Group, &QAction::triggered, this, &AssignmentList::addGroup);
	QObject::connect(ui.actionClean_Hidden, &QAction::triggered, this, &AssignmentList::cleanHidden);
	QObject::connect(ui.actionAbout, &QAction::triggered, this, &AssignmentList::aboutDialog);

	// create toolbar
	ui.toolBar->addAction(ui.actionAdd_Group);

	this->displayDate();
	this->displayWidgets();
	this->show();
}

void AssignmentList::displayDate() {
	QDate today = QDate::currentDate();
	ui.title->setText(today.toString("dddd, MMM d yyyy"));
}

void AssignmentList::displayWidgets() {
	QVBoxLayout *column_left = new QVBoxLayout();
	QVBoxLayout *column_right = new QVBoxLayout();
	BackendDB database;
	QList<Group *> groups = database.loadGroups();
	GroupLayout *new_group_layout;
	int i;

	// clear out old layouts if they exist
	this->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);
		else column_right->addLayout(new_group_layout);
	}

	column_left->addStretch();
	column_right->addStretch();

	ui.groups_layout->addLayout(column_left, 0, 0);
	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(new EntryLayout(*entries[i]));
	}

	return output;
}

// Open the 'addGroup' form
void AssignmentList::addGroup() {
	AddGroupForm create_new_group_dialog;
	if(create_new_group_dialog.exec() == QDialog::Accepted)
		this->displayWidgets();
}

void AssignmentList::preferences() {
	PreferencesDialog preferences_dialog;
	if(preferences_dialog.exec() == QDialog::Accepted)
		this->displayWidgets();
}

void AssignmentList::reload() {
	this->displayWidgets();
}

void AssignmentList::cleanHidden() {
	BackendDB database;
	database.cleanHidden();
}

void AssignmentList::aboutDialog() {
	QMessageBox about;
	about.about(this, "About Assignment List", "Created by Louie S. - 2023");
}

void AssignmentList::recursiveClear(QLayout *layout) {
	QLayoutItem *child;
	while((child = layout->takeAt(0)) != nullptr) {
		if(child->layout()) this->recursiveClear(child->layout());
		delete child->widget();
		delete child;
	}
}