-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassessmentframe.cpp
283 lines (245 loc) · 8.99 KB
/
assessmentframe.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
/****************************************************************************
**
** Antonio Cardoso Martins ([email protected])
** Paulo Dias Costa ([email protected])
** João Silva Marques ([email protected])
**
** Copyright (c) 2009 All Rights Reserved
**
** Date: May 2009
**
** This file is part of simECG
**
** simECG is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** simECG is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Foobar. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include <QtGlobal>
#include <QMessageBox>
#include "assessmentframe.h"
#include "ui_assessmentframe.h"
AssessmentFrame::AssessmentFrame(QWidget *parent) : QFrame(parent),
m_ui(new Ui::AssessmentFrame)
{
m_ui->setupUi(this);
// Prepare the timer
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTimer()));
connect(m_ui->pushButtonBegin, SIGNAL(clicked()), this, SLOT(startAssessment()));
connect(m_ui->pushButtonNext, SIGNAL(clicked()), this, SLOT(nextQuestion()));
// Resets all variables and widgets
resetScore();
}
AssessmentFrame::~AssessmentFrame()
{
delete m_ui;
}
void AssessmentFrame::changeEvent(QEvent *e)
{
QFrame::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
void AssessmentFrame::startAssessment()
{
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Start assessment", "Dialog title"));
msgBox.setText(QString(tr("You are about to start an ECG assessment. You will be asked %1 questions during %2 seconds. "))
.arg(TOTALQUESTIONS).arg(TOTALTIME)
+ QString(tr("Each question will have an ECG signal displayed, and three possible answers. "))
+ QString(tr("You need to choose the correct one. After answering all the questions, you will get a final score. "))
+ QString(tr("Good luck!")));
msgBox.setInformativeText(tr("Ready to start?"));
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
int ret = msgBox.exec();
if (ret == QMessageBox::Cancel) return;
// Ask the first question
chooseQuestions();
m_ui->radioAnswer1->show();
m_ui->radioAnswer2->show();
m_ui->radioAnswer3->show();
started = true;
m_ui->pushButtonBegin->setDisabled(true);
emit assessmentRunning(true);
// Start counting time
timer->start(1000);
}
void AssessmentFrame::updateTimer()
{
// Decrease main counter (in seconds)
timeRemaining--;
// Update LCD display
m_ui->lcdTimeCounter->display(timeRemaining);
// Start another timing for the next second, or stops assessment
if (! timeRemaining) {
timer->stop();
endAssessment();
}
}
void AssessmentFrame::endAssessment()
{
int score = correctAnswers * timeRemaining;
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Assessment finished"));
if (timeRemaining) {
if (! score) {
msgBox.setText(QString(tr("Sorry. You took %1 seconds to complete the assessment, but your score was a lousy 0 points."))
.arg(TOTALTIME - timeRemaining));
} else {
msgBox.setText(QString(tr("Congratulations! Your score was %1 points.\nYou took %2 seconds to complete the assessment."))
.arg(score).arg(TOTALTIME - timeRemaining));
}
} else {
msgBox.setText(QString(tr("Sorry your assessment time has expired.\nYour score was %1 points.")).arg(score));
}
msgBox.exec();
resetScore();
}
void AssessmentFrame::nextQuestion()
{
int userAnswer = -1;
// If assessment is not started, next button does nothing
if (! started) return;
// Do not proceed if there is not a correct answer
Q_ASSERT(correctAnswer >= 0);
// Check what was the user answer
if (m_ui->radioAnswer1->isChecked()) {
userAnswer = question1;
} else if (m_ui->radioAnswer2->isChecked()) {
userAnswer = question2;
} else if (m_ui->radioAnswer3->isChecked()) {
userAnswer = question3;
} else {
qDebug("User has not checked any buttons.");
}
answeredQuestions++;
// Update score if the answer is correct
if (userAnswer == correctAnswer) {
qDebug("(%d/%d) The answer was correct!", answeredQuestions, TOTALQUESTIONS);
correctAnswers++;
} else {
qDebug("(%d/%d) The answer was wrong. Correct was %s.",
answeredQuestions, TOTALQUESTIONS,
answerList.at(correctAnswer)->text().toLatin1().constData());
}
// Update progress bar
m_ui->progressBar->setValue(answeredQuestions * 100 / TOTALQUESTIONS);
// Uncheck all answer radio buttons
m_ui->radioAnswer1->setAutoExclusive(false);
m_ui->radioAnswer2->setAutoExclusive(false);
m_ui->radioAnswer3->setAutoExclusive(false);
m_ui->radioAnswer1->setChecked(Qt::Unchecked);
m_ui->radioAnswer2->setChecked(Qt::Unchecked);
m_ui->radioAnswer3->setChecked(Qt::Unchecked);
m_ui->radioAnswer1->setAutoExclusive(true);
m_ui->radioAnswer2->setAutoExclusive(true);
m_ui->radioAnswer3->setAutoExclusive(true);
// Check if assessment has finished
if (answeredQuestions == TOTALQUESTIONS) {
timer->stop();
endAssessment();
} else {
chooseQuestions();
}
}
void AssessmentFrame::resetScore()
{
answeredQuestions = 0;
timeRemaining = TOTALTIME;
correctAnswers = 0;
// Choose invalid question indexes from the answerList
question1 = -1;
question2 = -1;
question3 = -1;
correctAnswer = -2;
started = false;
emit assessmentRunning(false);
timer->stop();
m_ui->pushButtonBegin->setDisabled(false);
m_ui->radioAnswer1->setAutoExclusive(false);
m_ui->radioAnswer2->setAutoExclusive(false);
m_ui->radioAnswer3->setAutoExclusive(false);
m_ui->radioAnswer1->setChecked(Qt::Unchecked);
m_ui->radioAnswer2->setChecked(Qt::Unchecked);
m_ui->radioAnswer3->setChecked(Qt::Unchecked);
m_ui->radioAnswer1->setAutoExclusive(true);
m_ui->radioAnswer2->setAutoExclusive(true);
m_ui->radioAnswer3->setAutoExclusive(true);
m_ui->radioAnswer1->hide();
m_ui->radioAnswer2->hide();
m_ui->radioAnswer3->hide();
m_ui->progressBar->setValue(0);
m_ui->lcdTimeCounter->display(TOTALTIME);
}
///////////////////////////////////////////////////////////////////////////////
//
// Choose 3 questions from the set of available ECG preset signals
//
void AssessmentFrame::chooseQuestions()
{
Q_ASSERT(!answerList.empty());
//QTime midnight(0, 0, 0);
//qsrand(midnight.secsTo(QTime::currentTime()));
do {
question1 = qrand() % answerList.size();
} while (!answerList.at(question1)->isEnabled());
m_ui->radioAnswer1->setText(answerList.at(question1)->text());
do {
question2 = qrand() % answerList.size();
} while (question2 == question1 || !answerList.at(question2)->isEnabled());
m_ui->radioAnswer2->setText(answerList.at(question2)->text());
do {
question3 = qrand() % answerList.size();
} while (question3 == question2 || question3 == question1 || !answerList.at(question3)->isEnabled());
m_ui->radioAnswer3->setText(answerList.at(question3)->text());
// Choose the question that will be correct answer
int random = qrand() % 3;
// Place the index of the selected correct answer (from answerList) in the correctAnswer variable
switch (random) {
case 0:
correctAnswer = question1;
case 1:
correctAnswer = question2;
case 2:
correctAnswer = question3;
break;
default:
// This should never happen
Q_ASSERT(false);
}
// display the correct ECG signal
emit questionChanged(answerList.at(correctAnswer)->text());
// Use only for testing purposes
/*
// How many presets do we have ??
qDebug("Number of ECG presets: %d", answerList.size());
// How many enabled presets do we have ??
int enabledPresets = 0;
for (int i = 0; i < answerList.size(); i++) {
if (answerList.at(i)->isEnabled()) enabledPresets++;
}
qDebug("Number of enabled ECG presets: %d", enabledPresets);
// Show all button names
QString presetName;
Q_ASSERT(answerList.size() > 0);
for (int i = 0; i < answerList.size(); i++) {
presetName = answerList.at(i)->text();
qDebug("Button text is %s", presetName.toAscii().constData());
}
*/
}