-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartdrillermainwindow.cpp
241 lines (211 loc) · 6 KB
/
smartdrillermainwindow.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
#include "smartdrillermainwindow.h"
#include "questionsdatabase.h"
#include "ui_smartdrillermainwindow.h"
#include <QInputDialog>
#include <QtCore>
SmartDrillerMainWindow::SmartDrillerMainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::SmartDrillerMainWindow)
{
ui->setupUi(this);
database = new QuestionsDatabase;
QSettings settings("SmartDriller", "SmartDriller");
databaseFileName = settings.value("databaseLocation").value<QString>();
CreateDefaultDatabase();
settings.setValue("databaseLocation", databaseFileName);
database->LoadDatabase(databaseFileName);
statusBar()->showMessage(tr("Loaded %1 questions").arg(database->GetNumberOfQuestions()));
actualQuestionIndex = -1;
lastQuestionIndex = -1;
if (database->GetNumberOfQuestions() > 2)
{
NextQuestion();
}
}
SmartDrillerMainWindow::~SmartDrillerMainWindow()
{
delete ui;
delete database;
}
void SmartDrillerMainWindow::on_pushButton_random_question_clicked()
{
NextQuestion();
}
void SmartDrillerMainWindow::CreateDefaultDatabase()
{
if (databaseFileName.length() == 0)
{
QString defaultDir = QDir::homePath() + QDir::separator() + "smartDriller";
if (!QDir(defaultDir).exists())
{
QDir().mkdir(defaultDir);
}
databaseFileName = defaultDir + QDir::separator() + "database.txt";
}
}
void SmartDrillerMainWindow::SelectRandomQuestion()
{
int count = database->GetNumberOfQuestions();
// randomize randomness :-)
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
QuestionsDatabase::sQuestionData record;
int index = 0;
bool next = true;
if (count > 2)
{
do
{
index = qrand() % count;
record = database->GetQuestion(index);
int repeatPeriod = record.repeatPeriod;
if (!record.lastGood)
{
repeatPeriod /= 2;
if (repeatPeriod < 1) repeatPeriod = 1;
}
if (qrand() % repeatPeriod == 0 && index != actualQuestionIndex)
{
next = false;
}
} while (next);
lastQuestionIndex = actualQuestionIndex;
actualQuestionIndex = index;
}
}
void SmartDrillerMainWindow::NextQuestion()
{
SelectRandomQuestion();
QuestionsDatabase::sQuestionData record;
record = database->GetQuestion(actualQuestionIndex);
ui->label_question->setText(record.question);
QColor color(Qt::black);
if (record.repeatPeriod < 10)
color = QColor(255, 0, 0);
else if (record.repeatPeriod < 40)
color = QColor(255, 100, 0);
else if (record.repeatPeriod < 80)
color = QColor(255, 255, 0);
else if (record.repeatPeriod < 160)
color = QColor(255, 255, 255);
else if (record.repeatPeriod < 320)
color = QColor(0, 255, 0);
QPalette palette = ui->label_question->palette();
palette.setColor(ui->label_question->foregroundRole(), color);
ui->label_question->setPalette(palette);
ui->lineEdit_answer->clear();
}
void SmartDrillerMainWindow::UpdateStatistics()
{
int count = database->GetNumberOfQuestions();
int score = 0;
int good = 0;
int bad = 0;
int learnedCount = 0;
QuestionsDatabase::sQuestionData record;
for (int i = 0; i < count; i++)
{
record = database->GetQuestion(i);
score += record.repeatPeriod - 1;
good += record.goodAnswers;
bad += record.badAnswers;
if (record.lastGood) learnedCount++;
}
double percent = double(good) / (good + bad) * 100.0;
double learned = double(learnedCount) / count * 100.0;
QString summaryString = tr("%1% total good answers, %2% already learned sentences, score: %3")
.arg(percent, 0, 'g', 4)
.arg(learned, 0, 'g', 4)
.arg(score / 10);
statusBar()->showMessage(summaryString);
}
void SmartDrillerMainWindow::on_lineEdit_answer_returnPressed()
{
QString answerEntered = ui->lineEdit_answer->text();
QuestionsDatabase::sQuestionData record = database->GetQuestion(actualQuestionIndex);
QString correctAnswer = record.answer;
// compare and count wrong letters
int differentLetters = 0;
for (int i = 0; i < answerEntered.length(); i++)
{
if (i < correctAnswer.length())
{
if (answerEntered.at(i).toLower() != correctAnswer.at(i).toLower())
{
differentLetters++;
}
}
}
differentLetters += abs(correctAnswer.length() - answerEntered.length());
QString resultText;
if (differentLetters == 0)
{
resultText = tr("Correct answer!");
record.goodAnswers++;
record.repeatPeriod = record.repeatPeriod * 1.5 + 1.0;
record.lastGood = true;
}
else if (differentLetters == 1)
{
resultText = tr("Wrong, but you were so close");
record.badAnswers++;
record.repeatPeriod /= 1;
record.lastGood = false;
}
else if (differentLetters == 2)
{
resultText = tr("Wrong. Your answer was quite similar");
record.badAnswers++;
record.repeatPeriod /= 1.5;
record.lastGood = false;
}
else if (differentLetters == 3)
{
resultText = tr("Wrong. Your answer was too different");
record.badAnswers++;
record.repeatPeriod /= 2;
record.lastGood = false;
}
else if (differentLetters > 3)
{
resultText = tr("Wrong. Your answer was totally bad");
record.badAnswers++;
record.repeatPeriod /= 3;
record.lastGood = false;
}
if (record.repeatPeriod < 1) record.repeatPeriod = 1;
ui->label_result->setText(resultText);
if (differentLetters > 0)
{
ui->label_correct_answer->setText(tr("Should be: %1").arg(correctAnswer));
}
else
{
ui->label_correct_answer->setText("");
}
database->UpdateQuestion(record, actualQuestionIndex);
UpdateStatistics();
NextQuestion();
}
void SmartDrillerMainWindow::on_pushButton_add_question_clicked()
{
bool ok;
QString question = QInputDialog::getText(
this, tr("Adding new question"), tr("Question:"), QLineEdit::Normal, "", &ok);
if (ok && !question.isEmpty())
{
QString answer = QInputDialog::getText(
this, tr("Adding new question"), tr("Answer:"), QLineEdit::Normal, "", &ok);
if (ok && !answer.isEmpty())
{
QuestionsDatabase::sQuestionData record;
record.answer = answer;
record.question = question;
record.badAnswers = 0;
record.goodAnswers = 0;
record.lastGood = false;
record.repeatPeriod = 10;
database->AddQuestion(record);
statusBar()->showMessage(tr("Added question #%1").arg(database->GetNumberOfQuestions()));
}
}
}