summaryrefslogtreecommitdiff
path: root/src/rulesDialog.cpp
blob: 7d5705ee6ea73d88ef113b20e68a115be7fa724c (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
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>

#include <QDebug>

#include "backend/db_sqlite.h"
#include "lib.h"
#include "ruleLayout.h"
#include "rulesDialog.h"

RulesDialog::RulesDialog(const Entry &entry) {
	BackendDB database;

	this->entry_id = entry.id;

	// load uic
	ui.setupUi(this);

	// load rules into object member
	this->rules = database.loadRules(entry_id);

	// display widgets
	this->drawRules();

	// set connections
	QObject::connect(ui.add_rule_button, &QPushButton::released, this, &RulesDialog::addRule);
}

// for maintaining values when adding/removing rules
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();
	}
}

void RulesDialog::drawRules() {
	RuleLayout *new_layout;
	int i;


	// remove all children from layout
	recursiveClear(ui.rules_layout);

	// Draw each rule
	for(i = 0; i < this->rules.size(); ++i) {
		new_layout = new RuleLayout(rules[i]);

		// connect delete button
		QObject::connect(new_layout->del_button, &QPushButton::released, this, [=](){ this->deleteRule(i); });

		ui.rules_layout->addLayout(new_layout);
	}
}

void RulesDialog::addRule() {
	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);

	// redraw rules
	this->updateRulesList();
	this->drawRules();
}

void RulesDialog::deleteRule(int i) {
	if(i < 0) return;
	if(i >= this->rules.size()) return;

	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]);
	this->rules.removeAt(i);
	this->drawRules();
}

void RulesDialog::accept() {
	int i;
	BackendDB database;

	this->updateRulesList();

	// 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]);
		// update - this is an existing rule
		else
			database.updateRule(this->rules[i]);
	}

	// delete deleted rules in database
	for(i = 0; i < this->deleted_rules.size(); ++i) {
		database.removeRule(this->deleted_rules[i]);
	}

	QDialog::accept();
}