-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
169 lines (140 loc) · 4.57 KB
/
Copy pathmainwindow.cpp
File metadata and controls
169 lines (140 loc) · 4.57 KB
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
167
168
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "day.h"
#include "catdialog.h"
#include "eventdialog.h"
#include "home.h"
#include "stats.h"
/*
* Data types in the database
* event_list(
year INT,
month INT,
day INT,
start_min INT,
end_min INT,
cat TEXT,
event TEXT
);
*/
vector<Day*> MainWindow::daysHolder;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Connect to SQLite DB
QSqlDatabase sqldb = QSqlDatabase::addDatabase("QSQLITE");
// Windows testing: Copy the absolute path to the database. Use double slashes ("\\"), remember to add the file name.
//sqldb.setDatabaseName("../NWHacks2022\\time_management.db");
sqldb.setDatabaseName("C:\\Users\\pm\\OneDrive\\Documents\\GitHub\\NWHacks2022\\time_management.db");
// Mac testing: Copy the absolute path to the database.
// sqldb.setDatabaseName("/Users/jonathanYSA/Documents/GitHub/NWHacks2022/time_management.db");
sqldb.setDatabaseName("/Users/ryan/Projects/NWHacks2022/time_management.db");
//TODO: Relative path should be used here
//Display whether connected to the database
if(!sqldb.open()){
QMessageBox::information(this,"Not Connected", "Database not connected");
}else{
QMessageBox::information(this,"Connected", "Database connected");
//TODO: should Exit here?
}
//Read data in the database, push into events of each Day
QSqlQueryModel* readModel = new QSqlQueryModel;
readModel->setQuery("SELECT * FROM event_list");
for(int i = 0; i < readModel->rowCount();i++){
QSqlRecord item = readModel->record(i);
int year = item.value(0).toInt();
int month = item.value(1).toInt();
int day = item.value(2).toInt();
int start = item.value(3).toInt();
int end = item.value(4).toInt();
string name = item.value(6).toString().toStdString();
Category cat(item.value(5).toString().toStdString());
if(findDay(year,month,day) == -1){
daysHolder.push_back(new Day(year, month, day));
daysHolder.at(findDay(year,month,day))->addEvent(new Event(cat,name,start,end));
}else{
daysHolder.at(findDay(year,month,day))->addEvent(new Event(cat,name,start,end));
}
}
//fix piechart testing
/*
Day* temp(0);
temp->weekday = 0;
vector<Event> events(5);
*/
// Grouped Implementation
if(!daysHolder.empty()){
updateGroupedPiechart();
for(unsigned long long i = 0; i < daysHolder.at(0)->events.size(); i++) {
std::cout << daysHolder.at(0)->events.at(i)->toString() << std::endl;
}
}else{
std::cout << "DaysHolder is empty! QUIT." << std::endl;
//exit(2); // If not testing with the database, comment out this line.
}
}
MainWindow::~MainWindow()
{
delete ui;
}
/**
* @brief MainWindow::on_addCatButton_clicked
*/
void MainWindow::on_addCatButton_clicked()
{
CatDialog catDialog;
catDialog.setModal(true);
catDialog.exec();
}
/**
* @brief MainWindow::on_addEventButton_clicked
*/
void MainWindow::on_addEventButton_clicked()
{
EventDialog eventDialog;
eventDialog.setModal(true);
eventDialog.exec();
}
/**
* @brief MainWindow::updateGroupedPiechart set first day's data to the grouped piechart
*/
void MainWindow::updateGroupedPiechart(){
ui->piechartgrouped->setData(daysHolder.at(0));
}
/**
* @brief MainWindow::on_tabWidget_tabBarClicked
* @param index
*/
void MainWindow::on_tabWidget_tabBarClicked(int index)
{
QString avgText = "Average time spent: \n";
QString perText = "Percent time spent: \n";
int n = std::min(7, (int) daysHolder.size());
if (n == 0) {
ui->avgPanel->setText(avgText);
ui->perPanel->setText(perText);
return;
}
std::map<string, int> m;
for (int i = 0; i < n; i++) {
for (Event* e : daysHolder[i]->events) {
m[e->category.name] += e->length; // total up minutes for each category
}
}
for (auto const & c : m) {
avgText += QString::fromStdString(c.first).toUpper();
avgText += QString(": ");
avgText += QString::number(c.second / n);
avgText += QString("minutes\n");
}
for (auto const & c : m) {
perText += QString::fromStdString(c.first).toUpper();
perText += QString(": ");
perText += QString::number((int) ((double) c.second / (n * 1440) * 100));
perText += QString("%\n");
}
ui->avgPanel->setText(avgText);
ui->perPanel->setText(perText);
}