/
addassignmentdialog.cpp
489 lines (425 loc) · 14.4 KB
/
addassignmentdialog.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "addassignmentdialog.h"
#include <QMessageBox>
#include <QLabel>
#include <vector>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QString>
#include <QIntValidator>
#include <QDoubleValidator>
/**********************AssignmentDialogBase Functions**************************/
AssignmentDialogBase::AssignmentDialogBase(QWidget *parent)
:QDialog(parent),
titleLabel(new QLabel("Title Label", this)),
boxOneLabel(new QLabel("Box One Label:", this)),
boxTwoLabel(new QLabel("Box Two Label:", this)),
boxOne(new QLineEdit(this)),
boxTwo(new QLineEdit(this)),
submitButton(new QPushButton("Submit", this)),
cancelButton(new QPushButton("Cancel", this)),
topHorLayout(new QHBoxLayout),
midHorLayout(new QHBoxLayout),
botHorLayout(new QHBoxLayout),
vertLayout(new QVBoxLayout),
messageWin(new QMessageBox(this))
{
titleLabel->setAlignment(Qt::AlignHCenter);
boxOneLabel->setAlignment(Qt::AlignHCenter);
boxTwoLabel->setAlignment(Qt::AlignHCenter);
topHorLayout->addWidget(boxOneLabel);
topHorLayout->addWidget(boxOne);
midHorLayout->addStretch(1);
midHorLayout->addWidget(boxTwoLabel);
midHorLayout->addWidget(boxTwo);
botHorLayout->addWidget(submitButton);
botHorLayout->addWidget(cancelButton);
vertLayout->addWidget(titleLabel);
vertLayout->addLayout(topHorLayout);
vertLayout->addLayout(midHorLayout);
vertLayout->addLayout(botHorLayout);
setLayout(vertLayout);
}
/*****************************************************************************/
AddAssignmentDialog::AddAssignmentDialog(Gradebook *book, QWidget *parent)
:AssignmentDialogBase(parent),
gradebook(book)
{
setWindowTitle("Add Assignment All");
titleLabel->setFrameStyle(QFrame::Box);
titleLabel->setLineWidth(1);
titleLabel->setText("<font size=4><b>Add Assignment All</b></font>");
boxOneLabel->setText("Assignment name:");
boxTwoLabel->setText("Point value:");
submitButton->setEnabled(false);
boxTwo->setValidator(new QDoubleValidator(this));
boxTwo->setMaxLength(5);
connectWidgets();
}
void AddAssignmentDialog::changeGradebook(Gradebook *book)
{
gradebook = book;
}
void AddAssignmentDialog::connectWidgets()
{
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(boxOne, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
connect(boxTwo, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
connect(submitButton, SIGNAL(clicked()), this, SLOT(addAssignment()));
}
// Adds an assignment to each Student in the Gradebook, each with a 0 for points earned
void AddAssignmentDialog::addAssignment()
{
QString assignName(boxOne->text());
QString pointValue(boxTwo->text());
std::string assignNameStr(assignName.toAscii());
double pointValueDouble(pointValue.toDouble());
if(assignNameStr == "INVALID ASSIGNMENT")
{
QMessageBox *messageBox = new QMessageBox(this);
messageBox->setText("You cannot name an assignment \"INVALID ASSIGNMENT\"");
messageBox->exec();
boxOne->clear();
boxTwo->clear();
return;
}
Assignment newAssignment(pointValueDouble, 0.0, assignNameStr);
std::vector<Student> studentsCopy(gradebook->getAllStudents());
if(gradebook->addAssignmentAll(newAssignment))
{
messageWin->setText("Assignment added!");
emit assignmentAdded();
}
else
{
messageWin->setText("Assignment was not added successfully.");
}
messageWin->setWindowTitle("Confirmation");
messageWin->exec();
close();
}
// The argument 'text' isn't actually used, it's only there so it takes the same
// number/type of arguments as textChanged from QLineEdit and can be used as a
// slot.
void AddAssignmentDialog::enableSubmitButton(const QString &text)
{
QString assignmentName(boxOne->text());
QString pointValue(boxTwo->text());
if(!assignmentName.isEmpty() && !pointValue.isEmpty())
{
submitButton->setEnabled(true);
}
else
{
submitButton->setEnabled(false);
}
}
void AddAssignmentDialog::close()
{
boxOne->clear();
boxTwo->clear();
AssignmentDialogBase::close();
}
/********************** AddIndvAssignmentDialog Functions**********************/
AddIndvAssignmentDialog::AddIndvAssignmentDialog(Student *stu, QWidget *parent)
:AssignmentDialogBase(parent),
student(stu),
earnedPointsBox(new QLineEdit(this)),
earnedPointsLabel(new QLabel("Points earned:", this))
{
earnedPointsLabel->setAlignment(Qt::AlignHCenter);
setWindowTitle("Add Individual Assignment");
titleLabel->setText("<font size=4><b>Add Assignment</b></font>");
titleLabel->setFrameStyle(QFrame::Box);
titleLabel->setLineWidth(1);
boxOneLabel->setText("Assignment name:");
boxTwoLabel->setText("Points possible:");
QHBoxLayout *newHorLayout = new QHBoxLayout;
newHorLayout->addStretch(1);
newHorLayout->addWidget(earnedPointsLabel);
newHorLayout->addWidget(earnedPointsBox);
vertLayout->insertLayout(3, newHorLayout);
// Set the tab order for the Widget
setTabOrder(boxOne, boxTwo);
setTabOrder(boxTwo, earnedPointsBox);
setTabOrder(earnedPointsBox, submitButton);
setTabOrder(submitButton, cancelButton);
// Set validators and connect widgets
submitButton->setEnabled(false);
boxTwo->setValidator(new QDoubleValidator(this));
boxTwo->setMaxLength(5);
earnedPointsBox->setValidator(new QDoubleValidator(this));
earnedPointsBox->setMaxLength(5);
connectWidgets();
}
void AddIndvAssignmentDialog::changeStudent(Student *stu)
{
student = stu;
}
void AddIndvAssignmentDialog::connectWidgets()
{
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(submitButton, SIGNAL(clicked()), this, SLOT(addAssignment()));
connect(boxOne, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
connect(boxTwo, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
connect(earnedPointsBox, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
}
void AddIndvAssignmentDialog::addAssignment()
{
QString assignName(boxOne->text());
QString pointValue(boxTwo->text());
QString earnedPoints(earnedPointsBox->text());
std::string assignNameStr(assignName.toAscii());
double pointValueDouble(pointValue.toDouble());
double earnedPointsDouble(earnedPoints.toDouble());
if(assignNameStr == "INVALID ASSIGNMENT")
{
QMessageBox *messageBox = new QMessageBox(this);
messageBox->setText("You cannot name an assignment \"INVALID ASSIGNMENT\"");
messageBox->exec();
boxOne->clear();
boxTwo->clear();
return;
}
Assignment newAssignment(pointValueDouble, earnedPointsDouble, assignNameStr);
messageWin->setWindowTitle("Confirmation");
if(student->addAssignment(newAssignment))
{
messageWin->setText("Assignment added!");
emit assignmentAdded();
}
else
{
messageWin->setText("Assignment was not added successfully.");
}
messageWin->exec();
close();
}
void AddIndvAssignmentDialog::enableSubmitButton(const QString &text)
{
QString assignmentName(boxOne->text());
QString pointValue(boxTwo->text());
QString earnedPoints(earnedPointsBox->text());
if(!assignmentName.isEmpty() && !pointValue.isEmpty() && !earnedPoints.isEmpty())
{
submitButton->setEnabled(true);
}
else
{
submitButton->setEnabled(false);
}
}
void AddIndvAssignmentDialog::close()
{
boxOne->clear();
boxTwo->clear();
earnedPointsBox->clear();
AssignmentDialogBase::close();
}
/*********************** EditIndvAssignmentDialog *****************************/
EditIndvAssignmentDialog::EditIndvAssignmentDialog(Student *stu, const std::string &name,
QWidget *parent)
:AssignmentDialogBase(parent),
student(stu),
assignmentName(name),
confirmWin(new QMessageBox(this)),
earnedPointsBox(new QLineEdit(this)),
earnedPointsLabel(new QLabel("Points earned:", this)),
origName(new QLabel("", this)),
origPoints(new QLabel("", this))
{
titleLabel->setText("<font size=4><b>Edit Assignment</b></font>");
titleLabel->setFrameStyle(QFrame::Box);
titleLabel->setLineWidth(1);
boxOneLabel->setText("Assignment name:");
boxTwoLabel->setText("Points possible:");
origName->setText("<b>Current name:</b> ");
origPoints->setText("<b>Current points:<b> ");
origName->setAlignment(Qt::AlignHCenter);
origPoints->setAlignment(Qt::AlignHCenter);
QHBoxLayout *newHorLayout = new QHBoxLayout;
newHorLayout->addStretch(1);
newHorLayout->addWidget(earnedPointsLabel);
newHorLayout->addWidget(earnedPointsBox);
vertLayout->insertLayout(3, newHorLayout);
vertLayout->insertWidget(1, origName);
vertLayout->insertWidget(2, origPoints);
vertLayout->insertSpacing(1, 15);
vertLayout->insertSpacing(4, 15);
// Set the tab order for the Widget
setTabOrder(boxOne, boxTwo);
setTabOrder(boxTwo, earnedPointsBox);
setTabOrder(earnedPointsBox, submitButton);
setTabOrder(submitButton, cancelButton);
submitButton->setEnabled(false);
boxTwo->setValidator(new QDoubleValidator(this));
boxTwo->setMaxLength(5);
earnedPointsBox->setValidator(new QDoubleValidator(this));
earnedPointsBox->setMaxLength(5);
connectWidgets();
resize(sizeHint());
}
// Change the Student and assignment name on which to make changes
void EditIndvAssignmentDialog::changeAssignment(Student *stu, const std::string &name)
{
student = stu;
assignmentName = name;
displayOrigData(); // Reload the original data displayed for the un-edited assignment
}
void EditIndvAssignmentDialog::displayOrigData()
{
// Get the assignment before it's been edited
Assignment currentAssignment(student->getAssignment(assignmentName));
double pointsPossible(currentAssignment.getPointsPossible());
double pointsEarned(currentAssignment.getPointsEarned());
QString points(QString::number(pointsEarned) + "/" + QString::number(pointsPossible));
// Set the display labels with the original assignment name and point values
origName->setText(QString("<b>Current name:</b> ") + QString(assignmentName.c_str()));
origPoints->setText(QString("<b>Current points:</b> ") + points);
boxOne->insert(QString(assignmentName.c_str()));
boxTwo->insert(QString::number(pointsPossible));
earnedPointsBox->insert(QString::number(pointsEarned));
// Resize the window if the new data is very long
resize(sizeHint());
}
void EditIndvAssignmentDialog::connectWidgets()
{
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(submitButton, SIGNAL(clicked()), this, SLOT(editAssignment()));
connect(boxOne, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
connect(boxTwo, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
connect(earnedPointsBox, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
}
void EditIndvAssignmentDialog::enableSubmitButton(const QString &text)
{
// Get the text currently in all three input boxes
QString assignmentName(boxOne->text());
QString pointValue(boxTwo->text());
QString earnedPoints(earnedPointsBox->text());
// Are any of the boxes empty?
if(!assignmentName.isEmpty() && !pointValue.isEmpty() && !earnedPoints.isEmpty())
{
submitButton->setEnabled(true); // If not empty, enable the submit button
}
else
{
submitButton->setEnabled(false); // If empty, disable the submit button
}
}
void EditIndvAssignmentDialog::editAssignment()
{
// Get the current values of each input box as a QString
QString assignName(boxOne->text());
QString pointValue(boxTwo->text());
QString earnedPoints(earnedPointsBox->text());
// Convert the QString values to useable data types
std::string assignNameStr(assignName.toAscii());
double pointValueDouble(pointValue.toDouble());
double earnedPointsDouble(earnedPoints.toDouble());
// Guard against the name "INVALID ASSIGNMENT" for assignments(it's reserved)
if(assignNameStr == "INVALID ASSIGNMENT")
{
QMessageBox *messageBox = new QMessageBox(this);
messageBox->setText("You cannot name an assignment \"INVALID ASSIGNMENT\"");
messageBox->exec();
boxOne->clear();
boxTwo->clear();
return;
}
// Make a copy of the new assignment as the old assignment for comparison
Assignment newAssignment(pointValueDouble, earnedPointsDouble, assignNameStr);
Assignment currentAssignment(student->getAssignment(assignmentName));
// Set the text on the confirmation window
confirmWin->setWindowTitle("Confirm Edit");
confirmWin->setText("Are you sure you want to edit this assignment?");
confirmWin->setStandardButtons(QMessageBox::Yes|QMessageBox::No);
// If the user confirms the confirmation dialog
if(confirmWin->exec() == QMessageBox::Yes)
{
// Edit the points earned/possible
student->editAssignmentGrade(currentAssignment.getName(), pointValueDouble, earnedPointsDouble);
// If the name has been changed
if(assignNameStr != currentAssignment.getName())
{
// Get the assignment number to edit the assignment name
int assignNum = student->getAssignmentNumber(currentAssignment.getName());
student->editAssignmentName(assignNum, assignNameStr);
}
emit assignmentEdited();
}
else // If the user selects no on the confirmation dialog
{
return;
}
close();
}
void EditIndvAssignmentDialog::close()
{
boxOne->clear();
boxTwo->clear();
earnedPointsBox->clear();
AssignmentDialogBase::close();
}
/********************* AddExtraPointsDialog **********************************/
AddExtraPointsDialog::AddExtraPointsDialog(Student *stu, QWidget *parent)
:AssignmentDialogBase(parent),
student(stu)
{
boxOneLabel->setText("Extra points:");
boxTwoLabel->hide();
boxTwo->hide();
submitButton->setEnabled(false);
boxOne->setValidator(new QDoubleValidator(this));
connectWidgets();
}
void AddExtraPointsDialog::changeStudent(Student *stu)
{
student = stu;
}
void AddExtraPointsDialog::connectWidgets()
{
connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(submitButton, SIGNAL(clicked()), this, SLOT(addExtraPoints()));
connect(boxOne, SIGNAL(textChanged(QString)), this, SLOT(enableSubmitButton(QString)));
}
void AddExtraPointsDialog::close()
{
boxOne->clear();
AssignmentDialogBase::close();
}
void AddExtraPointsDialog::addExtraPoints()
{
double pointsToAdd(boxOne->text().toInt());
if(pointsToAdd <= 0)
{
messageWin->setWindowTitle("Invalid Action");
messageWin->setText("You must add a positive amount of extra points.");
messageWin->exec();
boxOne->clear();
return;
}
if(!student->addExtraPoints(pointsToAdd))
{
throw std::runtime_error("Error adding extra points to a student.");
}
else
{
messageWin->setText("Extra points have been successfully added!");
messageWin->exec();
emit extraPointsAdded();
close();
}
}
void AddExtraPointsDialog::enableSubmitButton(const QString &text)
{
if(!text.isEmpty())
{
submitButton->setEnabled(true);
}
else
{
submitButton->setEnabled(false);
}
}