-
Notifications
You must be signed in to change notification settings - Fork 5
/
dialog.cpp
239 lines (201 loc) · 7.81 KB
/
dialog.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
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
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QPlainTextEdit>
#include <QMessageBox>
#include "dashboard.h"
#include "dialog.h"
#include "ui_dialog.h"
/*=================================================================================================================================*/
// class Dialog-Specific Methods
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Constructor |*/
/*'------------------------'*/
//PURPOSE: default constructor
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Destructor |*/
/*'------------------------'*/
//PURPOSE: default deconstructor.
Dialog::~Dialog()
{
delete ui;
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Mutators |*/
/*'------------------------'*/
//PURPOSE: used to pass in user from dashboard.
void Dialog::setUser(QString u)
{
myuser = u;
}
//PURPOSE: sets date edit text to chosen date
void Dialog::setDate(const QDate date)
{
ui->dateEdit->setDate(date);
this->date = date.toString();
}
//PURPOSE: sets value of groupModeSet
void Dialog::setMode(bool newMode)
{
groupModeSet=newMode;
}
//PURPOSE: sets value of groupID
void Dialog::setGroupID(QString newID)
{
groupID = newID;
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Accessors |*/
/*'------------------------'*/
//PURPOSE: returns date entered in date edit
const QDate Dialog::getDate()
{
return QDate::fromString(date, "ddd MMM d yyyy");
}
//PURPOSE: returns newly created event ID
QString Dialog::getNewEventID()
{
QSqlQuery *query = new QSqlQuery(myconn.db);
QString result;
query->prepare("SELECT MAX(ID) "
"FROM innodb.EVENTS");
query->exec();
query->first();
result = query->value(0).toString();
return result;
}
/*=================================================================================================================================*/
/*.------------------------.*/
/*| Virtual Methods |*/
/*'------------------------'*/
//PURPOSE: Determines what's done when user selects
// red "x-button" in dialog's corner
void Dialog::closeEvent(QCloseEvent *event)
{
if(this->result() == QDialog::Accepted)
accepted = true;
else
accepted = false;
event->accept();
}
/*=================================================================================================================================*/
// SLOTS
/*=================================================================================================================================*/
/* Purpose: Adds new event into database
* Preconditions: Values entered in text fields are valid
* Postconditions: New group or personal event are added into the
* DB based on values specified in text fields
* Assumptions: innodb.EVENTS.groupID = 0 if adding a personal event
* otherwise, is set to relevant group's ID
*/
void Dialog::on_buttonBox_accepted()
{
this->accept();
QString mydesc = ui->description->toPlainText();
date = ui->dateEdit->date().toString();
QString mystart = ui->start->text();
QString myend = ui->end->text();
QSqlQuery query;
QString newEventID;
// get the current dates week and year. then set that for a new event.
int week = ui->dateEdit->date().weekNumber(); // get the dates week. ex: 43
int year = ui->dateEdit->date().year(); // get the dates year. ex: 2017
QString yearweek = QString::number(year) + QString::number(week); // ex: 201743
/* If in group mode,
* Execute add group event query
* Else,
* Execute regular personal event query
*/
if(groupModeSet)
{
QSqlQuery selectMemberQ, addUserEventQ;
QString groupMember;
// Add new group event into EVENTS table
query.exec("INSERT INTO innodb.EVENTS (date, start, end, description, groupID, yearweek) "
"VALUES ('"+date+"','"+mystart+"', '"+myend+"','"+mydesc+"', '" +groupID+ "', '" +yearweek+ "')");
// Get newly created eventID and add new entry into USER_EVENTS
newEventID = getNewEventID();
selectMemberQ.prepare("SELECT username "
"FROM innodb.GROUP_MEMBERS "
"WHERE groupID = '" +groupID+ "'");
/* If select member query is successful
* Grab each member's uername
* Insert into database newly created event into user_event
* using member's username and new eventID
*/
if(selectMemberQ.exec() && selectMemberQ.first())
{
do
{
groupMember = selectMemberQ.value(0).toString();
addUserEventQ.exec("INSERT INTO innodb.USER_EVENTS(username, eventID) "
"VALUES('" +groupMember+ "', '" +newEventID+ "')");
}while(selectMemberQ.next());
}
}
else
{
// Add new event into EVENTS table
query.exec("INSERT INTO innodb.EVENTS (date, start, end, description, groupID, yearweek) "
"VALUES ('"+date+"','"+mystart+"', '"+myend+"','"+mydesc+"', 0, '" +yearweek+ "')");
newEventID = getNewEventID();
query.exec("INSERT INTO innodb.USER_EVENTS(username, eventID) "
"VALUES('" +myuser+ "', '" +newEventID+ "')");
}
if (query.isActive()) {
qDebug().noquote() << "Insertion of Event " + newEventID + " into database.";
}
else {
qDebug() << query.lastError().text();
}
accepted = true;
Dialog::close();
}
//PURPOSE: closes add events window when cancel is clicked.
void Dialog::on_buttonBox_rejected()
{
this->reject();
accepted = false;
Dialog::close();
}
/* Purpose: slot for when user types description when creating an event
* limits message length and updates remaining characters.
* Postconditions: message box is updated as user types into given text
* field
*/
void Dialog::on_description_textChanged()
{
int MAX = 50;
// get message length. then update display count.
QString lettercount = QString::number(ui->description->toPlainText().length());
ui->count->setText(lettercount + " / " + QString::number(MAX));
// validation check if user pastes in input that is over 50.
if (ui->description->toPlainText().length() > MAX + 1) {
QMessageBox MsgBox;
MsgBox.setWindowTitle("Error");
MsgBox.setText("Your input is over the character limit.");
MsgBox.exec();
ui->description->clear();
}
// if message is over 50, limit it
if (ui->description->toPlainText().length() > MAX) {
ui->description->textCursor().deletePreviousChar();
}
// if message is 50, change label to red.
if (ui->description->toPlainText().length() == MAX) {
ui->count->setStyleSheet("color: red;");
}
else {
ui->count->setStyleSheet("color: black;");
}
}