summaryrefslogtreecommitdiff
path: root/src/entryLayout.cpp
blob: f5698293b0a557a39d19dc3e3747f91a3aa0bfc1 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <QLabel>
#include <QMenu>

#include <QDebug>

#include "backend/db_sqlite.h"
#include "editEntryForm.h"
#include "entryLayout.h"
#include "lib.h"
#include "rulesDialog.h"

EntryLayout::EntryLayout(const Entry &e) :
	entry(e)
{
	QLabel *bullet = new QLabel();
	QLabel *body = new QLabel();
	int i;

	// set styling
	this->setContentsMargins(2, 2, 2, 2);

	bullet->setFont(QFont("Arial", 11));
	bullet->setMaximumWidth(15);

	body->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
	body->setFont(QFont("Arial", 11));
	body->setWordWrap(true);
	body->setToolTip("Right-Click for actions");
	body->setContextMenuPolicy(Qt::CustomContextMenu);

	// set context menu
	body->setContextMenuPolicy(Qt::CustomContextMenu);
	QObject::connect(body,
			SIGNAL(customContextMenuRequested(const QPoint &)),
			SLOT(showContextMenu()));

	// Check rules
	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())
		  ) {
			if(!rules[i].color.isEmpty())
				this->entry.color = rules[i].color;
			if(!rules[i].highlight.isEmpty())
				this->entry.highlight = rules[i].highlight;
		}
	}

	// set conditional styling
	if(this->entry.done) {
		bullet->setText("\u2713");
		bullet->setStyleSheet(
				"QLabel {"
				"	color: green;"
				"}"
				);
	}
	else
		bullet->setText("- ");
	this->addWidget(bullet);

	if(!this->entry.due.isNull())
		body->setText(this->entry.due.toString("MM/dd/yyyy: "));
	else if(!this->entry.due_alt.isEmpty())
		body->setText(this->entry.due_alt + ": ");

	if(!this->entry.link.isEmpty()) {
		body->setOpenExternalLinks(true);
		body->setText(body->text() + "<a href=\"" + this->entry.link.toString() + "\" " "style=\"color: ");
		if(this->entry.done)
			body->setText(body->text() + "green");
		else if(this->entry.color.isEmpty())
			body->setText(body->text() + "default");
		else
			body->setText(body->text() + this->entry.color);
		body->setText(body->text() + ";\">");
	}
	body->setText(body->text() + this->entry.desc);
	if(!this->entry.link.isEmpty()) {
		body->setText(body->text() + "</a>");
		body->setToolTip(this->entry.link.toString());
	}

	if(this->entry.done) {
		QFont body_font = body->font();
		body_font.setStrikeOut(true);
		body->setFont(body_font);
		body->setStyleSheet(
				"QLabel {"
				"	color: green"
				"}"
				);
	}
	else {
		body->setStyleSheet(
				"QLabel {"
				"	color: " + (this->entry.color.isEmpty() ? "default" : this->entry.color) + ";"
				"	background-color: " + (this->entry.highlight.isEmpty() ? "none" : this->entry.highlight) + ";"
				"	font-weight: " + (this->entry.due.isValid() && this->entry.due <= QDateTime::currentDateTime() ? "bold" : "normal") + ";"
				"}"
				);
	}

	this->addWidget(body);
}

QList<Rule> EntryLayout::loadRules() {
	BackendDB database;

	return database.loadRules(this->entry.id);
}

void EntryLayout::showContextMenu() {
	QMenu menu;

	QAction *edit_entry_act = new QAction("Edit Entry");
	QObject::connect(edit_entry_act, &QAction::triggered, this, &EntryLayout::editEntry);
	menu.addAction(edit_entry_act);

	QAction *set_rules_act = new QAction("Rules");
	QObject::connect(set_rules_act, &QAction::triggered, this, &EntryLayout::setRules);
	menu.addAction(set_rules_act);

	QAction *toggle_done_act = new QAction("Done");
	toggle_done_act->setCheckable(true);
	if(this->entry.done) toggle_done_act->setChecked(true);
	QObject::connect(toggle_done_act, &QAction::triggered, this, &EntryLayout::toggleDone);
	menu.addAction(toggle_done_act);

	QAction *del_entry_act = new QAction("Remove Entry");
	QObject::connect(del_entry_act, &QAction::triggered, this, &EntryLayout::removeEntry);
	menu.addAction(del_entry_act);

	menu.exec(QCursor::pos());
}

void EntryLayout::editEntry() {
	EditEntryForm edit_entry_dialog(this->entry);
	if(edit_entry_dialog.exec() == QDialog::Accepted)
		getMainWindow()->displayWidgets();
}

void EntryLayout::setRules() {
	RulesDialog rules_dialog(this->entry);
	if(rules_dialog.exec() == QDialog::Accepted)
		getMainWindow()->displayWidgets();
}

void EntryLayout::toggleDone() {
	BackendDB database;

	this->entry.done = !this->entry.done;
	database.updateEntry(this->entry);
	getMainWindow()->displayWidgets();
}

void EntryLayout::removeEntry() {
	BackendDB database;

	if(database.removeEntry(this->entry) > 0)
		getMainWindow()->displayWidgets();
}