-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.cpp
More file actions
349 lines (283 loc) · 8.14 KB
/
Copy pathcalendar.cpp
File metadata and controls
349 lines (283 loc) · 8.14 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <QInputDialog>
#include <QMessageBox>
#include <QMutex>
#include <string>
#include <vector>
#include <algorithm>
#include "ui_calendar.h"
#include "calendar.h"
#include "calevent.h"
#include "caleventoption.h"
#include "editeventdialog.h"
#include "alert.h"
#include<QDebug>
Calendar::Calendar(QWidget *parent) :
QDialog(parent),
ui(new Ui::Calendar)
{
ui->setupUi(this);
ui->pushButton->setEnabled(false);
ui->dateTimeEdit->setMinimumDateTime(QDateTime::currentDateTime());
ui->dateTimeEdit->setDisplayFormat("dd-MM-yyyy hh:mm:ss ap");
}
Calendar::~Calendar()
{
delete ui;
}
void Calendar::setRef(Calendar* c)
{
ref = c;
}
void Calendar::setAQ(std::vector<Alert*>* alertsQueue)
{
aQ = alertsQueue;
}
void Calendar::setMutex(QMutex* m)
{
mtx = m;
}
void Calendar::setCurrentCItem(QListWidgetItem* cItem)
{
currentCItem = cItem;
}
Alert* Calendar::getAlert(CalEvent* cEvent)
{
auto it = eventAlertMap.find(cEvent);
if (it != eventAlertMap.end()){
return it->second;
}
return nullptr;
}
CalEvent* Calendar::getEvent(QListWidgetItem* cItem)
{
auto it = eventWidgetMap.find(cItem);
if (it != eventWidgetMap.end()){
return it->second;
}
return nullptr;
}
bool Calendar::createEvent()
{
bool ok;
QString inputdescr = QInputDialog::getText(0,"New Event Description",
"Enter event description below", QLineEdit::Normal, QString(), &ok);
if (!ok || inputdescr == ""){
return false;
}
std::string inputdescr_str = inputdescr.toStdString();
CalEvent *cEvent = new CalEvent;
cEvent->setStarttime(newEventTime);
cEvent->setDescription(inputdescr_str);
QListWidgetItem* listEvent = new QListWidgetItem;
listEvent->setText(inputdescr);
ui->listWidget->addItem(listEvent);
Alert* newAlert = new Alert(cEvent->getDescription(), cEvent->getStarttime());
bool acquired = false;
while(!acquired)
{
if(mtx->try_lock())
{
acquired = true;
aQ->push_back(newAlert);
mtx->unlock();
}
}
eventAlertMap.insert({cEvent, newAlert});
eventWidgetMap.insert({listEvent, cEvent});
QMessageBox::StandardButton reply = QMessageBox::question(this, "Lock Event",
"Do you want to lock this event?", QMessageBox::Yes | QMessageBox:: No);
if (reply == QMessageBox::Yes)
{
QString inputpswd = QInputDialog::getText(0,"Create password",
"Enter password for this event:", QLineEdit::Normal, QString(), &ok);
if (ok)
{
std::string inputpswd_str = inputpswd.toStdString();
cEvent->lock(inputpswd_str);
}
}
return true;
}
bool Calendar::deleteEvent(CalEvent* cEvent, QListWidgetItem* cItem)
{
if(cEvent->isLocked())
{
bool ok;
QString inputpswd = QInputDialog::getText(0,"Event Locked","Enter password:",
QLineEdit::Normal, QString(), &ok);
if (!ok)
{
ui->pushButton->setEnabled(false);
return false;
}
std::string inputpswd_str = inputpswd.toStdString();
if (!(cEvent->unlock(inputpswd_str))) {
QMessageBox::information(0,"","Incorrect Password. Event not deleted.");
ui->pushButton->setEnabled(false);
return false;
}
}
Alert* oldAlert = getAlert(cEvent);
bool acquired = false;
while(!acquired)
{
if(mtx->try_lock())
{
acquired = true;
aQ->erase(std::find(aQ->begin(), aQ->end(), oldAlert));
mtx->unlock();
}
}
eventAlertMap.erase(cEvent);
eventWidgetMap.erase(cItem);
cItem->setHidden(true);
delete oldAlert;
delete cEvent;
delete cItem;
QMessageBox::information(0,"","Event deleted.");
return true;
}
bool Calendar::editEvent(CalEvent* cEvent, QListWidgetItem* cItem)
{
bool ok;
bool relock_after_edit = false;
std::string pswd;
if(cEvent->isLocked())
{
relock_after_edit = true;
QString inputpswd = QInputDialog::getText(0,"Event Locked","Enter password:",
QLineEdit::Normal, QString(), &ok);
std::string inputpswd_str = inputpswd.toStdString();
if (!ok)
{
ui->pushButton->setEnabled(false);
return false;
}
if (!(cEvent->unlock(inputpswd_str))) {
QMessageBox::information(0,"","Incorrect Password. Event not updated.");
ui->pushButton->setEnabled(false);
return false;
}
}
QString inputdescr = QInputDialog::getText(0,"Edit Event Description",
"Enter event description below", QLineEdit::Normal, QString(), &ok);
if (!ok || inputdescr == "")
{
ui->pushButton->setEnabled(false);
return false;
}
std::string inputdescr_str = inputdescr.toStdString();
EditEventDialog editDateTime(this);
editDateTime.exec();
cEvent->setStarttime(newEventTime);
cEvent->setDescription(inputdescr_str);
cItem->setText(inputdescr);
Alert* newAlert = new Alert(cEvent->getDescription(), cEvent->getStarttime());
Alert* oldAlert = getAlert(cEvent);
bool acquired = false;
while(!acquired){
if(mtx->try_lock()){
acquired = true;
aQ->erase(std::find(aQ->begin(), aQ->end(), oldAlert));
delete oldAlert;
aQ->push_back(newAlert);
mtx->unlock();
}
}
eventAlertMap.erase(cEvent);
eventAlertMap.insert({cEvent, newAlert});
QMessageBox::information(0,"","Event updated.");
if(relock_after_edit)
{
cEvent->lock();
}
return true;
}
void Calendar::on_dateTimeEdit_dateTimeChanged(const QDateTime &dateTime)
{
ui->pushButton->setEnabled(true);
newEventTime = dateTime;
}
void Calendar::on_calendarWidget_clicked(const QDate &date)
{
newEventTime.setDate(date);
ui->dateTimeEdit->setDate(date);
ui->pushButton->setEnabled(true);
}
void Calendar::on_pushButton_clicked()
{
QDateTime now = QDateTime::currentDateTime();
QDateTime event;
if (now.secsTo(newEventTime) < 0)
{
QMessageBox::information(0,"Error","Cannot create an event in the past.");
ui->pushButton->setEnabled(false);
return;
}
ref->createEvent();
}
void Calendar::on_listWidget_itemActivated(QListWidgetItem *item)
{
QDateTime t = getAlert(getEvent(item))->getAlertTime();
QMessageBox::information(0,"Event date:", t.toString());
CalEventOption option(this, item);
option.exec();
}
void Calendar::delete_selected(QListWidgetItem* cItem)
{
if (getEvent(cItem) == nullptr)
{
QMessageBox::information(0,"Error","Event not found.");
ui->pushButton->setEnabled(false);
return;
}
ref->deleteEvent(getEvent(cItem), cItem);
}
void Calendar::edit_selected(QListWidgetItem* cItem)
{
ui->pushButton->setEnabled(false);
if (getEvent(cItem) == nullptr)
{
QMessageBox::information(0,"Error","Event not found.");
ui->pushButton->setEnabled(false);
return;
}
ref->editEvent(getEvent(cItem), cItem);
}
void Calendar::remove_cItem(Alert* a)
{
CalEvent* remove_cEvent = nullptr;
QListWidgetItem* remove_CItem = nullptr;
bool found = false;
for (auto it_1 = eventAlertMap.begin(); it_1 != eventAlertMap.end(); ++it_1){
if (it_1->second == a)
{
remove_cEvent = it_1->first;
for (auto it_2 = eventWidgetMap.begin(); it_2 != eventWidgetMap.end(); ++it_2)
{
remove_CItem = it_2->first;
found = true;
eventWidgetMap.erase(it_2->first);
break;
}
}
if (found) { break; }
}
delete a;
if (remove_cEvent != nullptr)
{
delete remove_cEvent;
}
if (remove_CItem != nullptr)
{
delete remove_CItem;
}
}
void Calendar::timeEdited(QDateTime* t)
{
newEventTime = *t;
}
void Calendar::voice_create_event(int time, QString title)
{
// create AlertTime from int* then create new event
}