-
Notifications
You must be signed in to change notification settings - Fork 0
/
weeklyschedule.cpp
119 lines (97 loc) · 3.57 KB
/
weeklyschedule.cpp
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
#include <QVBoxLayout>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QComboBox>
#include <QLineEdit>
#include <QSpinBox>
#include <QPushButton>
#include <QJsonObject>
#include <QTimer>
#include <QHeaderView>
#include "weeklyschedule.h"
WeeklySchedule::WeeklySchedule(QWidget * parent) : Feature("WeeklySchedule"), QWidget(parent)
{
m_openDatabase();
for(int i = 0; i < __database.size(); i++)
{
activity a;
a.description = __database.at(i)["description"].toString();
a.day = __database.at(i)["day"].toInt();
a.hour = __database.at(i)["hour"].toInt();
__activities.append(a);
}
QTimer * timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=]() { updateSchedule(); });
timer->start(1000*60);
__dateTime = new QDateTime(QDateTime::currentDateTime());
}
void WeeklySchedule::v_display(QTabWidget * tabWidget)
{
QVBoxLayout * layout = new QVBoxLayout(this);
__tableWidget = new QTableWidget(DAY_END_HOUR - DAY_START_HOUR, 7, this);
__tableWidget->setHorizontalHeaderLabels( {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} );
QHeaderView * h = __tableWidget->horizontalHeader();
h->setSectionResizeMode(QHeaderView::Stretch);
QStringList verticalHeaderLabels;
for(int i = DAY_START_HOUR; i <= DAY_END_HOUR; i++)
verticalHeaderLabels.append( QString("%1:00").arg(QString::number(i)) );
__tableWidget->setVerticalHeaderLabels(verticalHeaderLabels);
for(int i = 0; i < DAY_END_HOUR - DAY_START_HOUR; i++)
{
for(int j = 0; j < 7; j++)
{
QTableWidgetItem * item = new QTableWidgetItem("");
item->setTextAlignment(Qt::AlignCenter);
__tableWidget->setItem(i, j, item);
}
}
for(activity & a : __activities)
{
QTableWidgetItem * item = new QTableWidgetItem(a.description);
item->setTextAlignment(Qt::AlignCenter);
__tableWidget->setItem(a.hour - DAY_START_HOUR, a.day, item);
}
connect(__tableWidget, &QTableWidget::cellChanged, this, &WeeklySchedule::newActivityTableWidget_changed);
updateSchedule();
layout->addWidget(__tableWidget);
this->setLayout(layout);
tabWidget->addTab(this, "Weekly Schedule");
}
void WeeklySchedule::newActivityTableWidget_changed(int row, int column)
{
activity newActivity;
newActivity.description = __tableWidget->item(row, column)->text();
newActivity.day = column;
newActivity.hour = row + DAY_START_HOUR;
if(newActivity.description == "") return;
QJsonObject jsonNewActivity;
jsonNewActivity["description"] = newActivity.description;
jsonNewActivity["day"] = newActivity.day;
jsonNewActivity["hour"] = newActivity.hour;
bool isOverlapping = false;
for(int i = 0; i < __activities.size(); i++)
{
if(__activities[i].day == column && __activities[i].hour == row)
{
isOverlapping = true;
__activities[i] = newActivity;
__database.replace(i, jsonNewActivity);
}
}
if(!isOverlapping)
{
__activities.append(newActivity);
__database.append(jsonNewActivity);
}
m_saveDatabase();
}
void WeeklySchedule::updateSchedule()
{
int day = __dateTime->date().dayOfWeek();
int hour = __dateTime->time().hour();
if(hour >= DAY_START_HOUR && hour < DAY_END_HOUR)
{
__tableWidget->item(hour - DAY_START_HOUR, day - 1)->setBackground(QBrush(QColor(57, 255, 20)));
__tableWidget->item(hour - DAY_START_HOUR, day - 1)->setForeground(QBrush(Qt::white));
}
}