-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.cpp
502 lines (456 loc) · 16.3 KB
/
questions.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
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "questions.h"
#include "ui_questions.h"
#include <test_wind.h>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QMessageBox>
#include <QTextCodec>
#include <settings.h>
#include <QThread>
#include <QMouseEvent>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QTextCodec>
#include <vector>
#include <algorithm>
//to read non-english letters
QTextCodec* codeck = QTextCodec::codecForName("UTF-8");
//reading all text in file into one BIG string
//+ findind actual text and putting in other BIG string
QString bigstring, substr;
//number of questin to orientate with arrows
int quest_index;
int quest_num = 1;
//findind borders to store question that is between thos borderd
//its my own markdowns (=symbols)
QString first_index, second_index;
int iqst, jqst, ansb, anse;
//progress bar that tells you your progress in questions
int progressValue, chunk;
QString progress;
int animate_progress = 0;
int var, temp;
//array of right answers (read from file)
int numb_of_ans;
int *right_answers;
QString ans_line, all;
//in order to read russian letters (and other than english)
QTextCodec* codec2 = QTextCodec::codecForName("UTF-8");
//user answers
int *user_ans;
//ответы в строках
std::vector<QString> right_ans_strings, user_ans_strings;
//ответы
QString v1, v2, v3, v4;
questions::questions(QWidget *parent) :
QDialog(parent),
ui(new Ui::questions)
{
extern QString file_name;
ui->setupUi(this);
ui->pass->hide();
ui->save->hide();
ui->okay->hide();
this->setWindowFlag(Qt::FramelessWindowHint, true);
//fade animation
setWindowOpacity(0.0);
QPropertyAnimation *a = new QPropertyAnimation(this,"windowOpacity");
a->setStartValue(0.0);
a->setDuration(400);
a->setEndValue(1.0);
a->setEasingCurve(QEasingCurve::InSine);
a->start(QPropertyAnimation::DeleteWhenStopped);
//quickly finding question
QFile file(file_name);
if(!file.open(QIODevice::ReadOnly))
QMessageBox::information(0, "info", file.errorString());
QTextStream in (&file);
qDebug() << "1 test: " << file_name;
while(!file.atEnd()) {
bigstring = QString::fromLocal8Bit((file.readAll()));
Replace(bigstring);
all = bigstring; //to find answers later
int i = bigstring.indexOf("?1?");
int j = bigstring.indexOf(".1.");
QString all_str = bigstring.mid(i+3, j);
qDebug() << "3 test: " << all_str;
int exp = all_str.indexOf("!1");
QString q_str = all_str.mid(0, exp);
int var1 = all_str.indexOf("!2");
v1 = all_str.mid(exp+2, (var1-exp-2));
ui->Readin_2->setText(v1);
int var2 = all_str.indexOf("!3");
v2 = all_str.mid((var1+2), (var2-var1-2));
ui->Readin_3->setText(v2);
int var3 = all_str.indexOf("!4");
v3 = all_str.mid((var2+2), (var3-var2-2));
ui->Readin_4->setText(v3);
int var4 = all_str.indexOf("!5");
v4 = all_str.mid((var3+2), (var4-var3-2));
ui->Readin_5->setText(v4);
bigstring = bigstring.mid(j);
qDebug() << "4 test: " << q_str;
ui->Questions->setText((q_str));
}
progress = bigstring.mid(bigstring.length()-2, 1);
progressValue = progress.toInt();
temp = progressValue;
qDebug() << "5 test: " << temp;
chunk = 100/progressValue;
progressValue = 100/progressValue;
animate_progress = chunk;
ui->quest_progress->setValue(chunk);
//store right ans
right_answers = new int [temp];
int num_of_ans = all.indexOf("[0]");
for(int i = 0; i < temp; i++) {
ans_line = all.mid(num_of_ans+4, 1);
right_answers[i] = ans_line.toInt();
//qDebug() << ans_line << "answer" << right_answers[i];
num_of_ans++;
//заполнить вектор чтобы изменить
user_ans_strings.push_back("0");
right_ans_strings.push_back("0");
//user ans
user_ans = new int [temp];
}
}
questions::~questions()
{
delete ui;
}
void questions::on_execute_clicked()
{
this->close();
}
void questions::on_swish_clicked()
{
quest_num = 1;
QPropertyAnimation *a = new QPropertyAnimation(this,"windowOpacity");
a->setStartValue(1.0);
a->setDuration(400);
a->setEndValue(0.0);
a->setEasingCurve(QEasingCurve::InSine);
a->start(QPropertyAnimation::DeleteWhenStopped);
this->parentWidget()->setWindowOpacity(1.0);
this->parentWidget()->move(geometry().x(),geometry().y());
this->parentWidget()->show();
connect(a,SIGNAL(finished()),this,SLOT(hide()));
}
void questions::on_right_clicked()
{
extern QString file_name;
QFile file(file_name);
if(!file.open(QIODevice::ReadOnly))
QMessageBox::information(0, "info", file.errorString());
QTextStream in (&file);
while(!file.atEnd()) {
quest_num++;
bigstring = QString::fromLocal8Bit((file.readAll()));
Replace(bigstring);
first_index = "?";
substr = QString::number(quest_num);
first_index = first_index.append(substr);
first_index = first_index.append("?");
iqst = bigstring.indexOf(first_index);
second_index = ".";
second_index = second_index.append(substr);
second_index = second_index.append(".");
jqst = bigstring.indexOf(second_index);
qDebug() << jqst << " ####### " << bigstring.size();
if(jqst < bigstring.size() && jqst > 0) {
substr = bigstring.mid(iqst+3, (jqst-3)-iqst);
int exp = substr.indexOf("!1");
QString q_str = substr.mid(0, exp-2);
int var1 = substr.indexOf("!2");
v1 = substr.mid(exp+2, (var1-exp-2));
ui->Readin_2->setText(v1);
int var2 = substr.indexOf("!3");
v2 = substr.mid((var1+2), (var2-var1-2));
ui->Readin_3->setText(v2);
int var3 = substr.indexOf("!4");
v3 = substr.mid((var2+2), (var3-var2-2));
ui->Readin_4->setText(v3);
int var4 = substr.indexOf("!5");
v4 = substr.mid((var3+2), (var4-var3-2));
ui->Readin_5->setText(v4);
ui->Questions->setText((q_str));
ui->quest_progress->setValue(10);
progressValue+=chunk;
while(animate_progress < progressValue) {
ui->quest_progress->setValue(animate_progress);
animate_progress+=2;
QThread::msleep(30);
}
animate_progress = progressValue;
//ui->quest_progress->setValue(progressValue);
//show button to save results
if(quest_num == temp) {
ui->pass->show();
}
if(right_answers[quest_num-1] == 1) {
right_ans_strings[quest_num-1] = v1;
}
if(right_answers[quest_num-1] == 2) {
right_ans_strings[quest_num-1] = v2;
}
if(right_answers[quest_num-1] == 3) {
right_ans_strings[quest_num-1] = v3;
}
if(right_answers[quest_num-1] == 4) {
right_ans_strings[quest_num-1] = v4;
}
}
else {
quest_num--;
}
}
}
void questions::on_left_clicked()
{
if(quest_num > 1) {
extern QString file_name;
QFile file(file_name);
if(!file.open(QIODevice::ReadOnly))
QMessageBox::information(0, "info", file.errorString());
QTextStream in (&file);
while(!file.atEnd()) {
quest_num--;
bigstring = QString::fromLocal8Bit((file.readAll()));
Replace(bigstring);
first_index = "?";
substr = QString::number(quest_num);
first_index = first_index.append(substr);
first_index = first_index.append("?");
iqst = bigstring.indexOf(first_index);
second_index = ".";
second_index = second_index.append(substr);
second_index = second_index.append(".");
jqst = bigstring.indexOf(second_index);
qDebug() << jqst << " ####### " << bigstring.size();
substr = bigstring.mid(iqst+3, (jqst-3)-iqst);
int exp = substr.indexOf("!1");
QString q_str = substr.mid(0, exp-2);
int var1 = substr.indexOf("!2");
v1 = substr.mid(exp+2, (var1-exp-2));
ui->Readin_2->setText(v1);
int var2 = substr.indexOf("!3");
v2 = substr.mid((var1+2), (var2-var1-2));
ui->Readin_3->setText(v2);
int var3 = substr.indexOf("!4");
v3 = substr.mid((var2+2), (var3-var2-2));
ui->Readin_4->setText(v3);
int var4 = substr.indexOf("!5");
v4 = substr.mid((var3+2), (var4-var3-2));
ui->Readin_5->setText(v4);
ui->Questions->setText((q_str));
progressValue-=chunk;
while(animate_progress > progressValue) {
ui->quest_progress->setValue(animate_progress);
animate_progress-=2;
QThread::msleep(30);
}
animate_progress = progressValue;
ui->quest_progress->setValue(progressValue);
}
}
}
void questions::mousePressEvent(QMouseEvent *event) {
m_nMouseClick_X_Coordinate = event->position().x();
m_nMouseClick_Y_Coordinate = event->position().y();
}
void questions::mouseMoveEvent(QMouseEvent *event) {
move(event->globalPosition().x()-m_nMouseClick_X_Coordinate,event->globalPosition().y()-m_nMouseClick_Y_Coordinate);
}
void questions::on_questions_accepted()
{
}
void questions::on_check1_clicked()
{
user_ans[quest_num-1] = 1;
user_ans_strings[quest_num-1] = v1;
//qDebug() << user_ans[quest_num-1] << " - " << right_answers[quest_num-1];
}
void questions::on_check2_clicked()
{
user_ans[quest_num-1] = 2;
user_ans_strings[quest_num-1] = v2;
//qDebug() << user_ans[quest_num-1] << " - " << right_answers[quest_num-1];
}
void questions::on_check3_clicked()
{
user_ans[quest_num-1] = 3;
user_ans_strings[quest_num-1] = v3;
}
void questions::on_check4_clicked()
{
user_ans[quest_num-1] = 4;
user_ans_strings[quest_num-1] = v4;
}
void questions::on_pass_clicked()
{
//animate it
QPropertyAnimation *a = new QPropertyAnimation(this,"windowOpacity");
a->setStartValue(1.0);
a->setDuration(400);
a->setEndValue(0.0);
a->setEasingCurve(QEasingCurve::InSine);
a->start(QPropertyAnimation::DeleteWhenStopped);
//connect(a,SIGNAL(finished()),this,SLOT(hide()));
QPropertyAnimation *a2 = new QPropertyAnimation(this,"windowOpacity");
a2->setStartValue(0.0);
a2->setDuration(400);
a2->setEndValue(1.0);
a2->setEasingCurve(QEasingCurve::InSine);
a2->start(QPropertyAnimation::DeleteWhenStopped);
QString temp_text;
QString temp_text1;
QString temp_text2;
QString temp_text3;
QString temp_text4;
int all;
//change button from sumbit to save
ui->pass->hide();
ui->save->show();
ui->okay->show();
ui->check1->hide();
ui->check2->hide();
ui->check3->hide();
ui->check4->hide();
ui->right->hide();
ui->left->hide();
ui->Readin_2->hide();
ui->Readin_3->hide();
ui->Readin_4->hide();
ui->Readin_5->hide();
qDebug() << user_ans << " - " << right_answers;
all = quest_num;
ui->Questions->setFixedHeight(500);
ui->Questions->setText("\t\tВаш результат:\n");
for(int i = 0; i < quest_num; i++) {
temp_text = QString::number(i+1);
temp_text1 = QString::number(user_ans[i]);
temp_text2 = QString::number(right_answers[i]);
if(user_ans[i] != right_answers[i]) {
ui->Questions->append("\n" + temp_text + " вопрос: " + user_ans_strings[i] + " - неверный ответ, правильный - " + right_ans_strings[i]);
all--;
}
else {
ui->Questions->append("\n" + temp_text + " вопрос: " + user_ans_strings[i] + " - верный ответ");
}
}
temp_text3 = QString::number(all);
temp_text4 = QString::number(quest_num);
ui->Questions->append("\nИтого: " + temp_text3 + " из " + temp_text4);
qDebug() << all;
if(all >= 6) {
ui->Questions->append(", тест пройден успешно");
}
else {
ui->Questions->append(", тест пройден не удовлетворительно");
}
}
void questions::on_save_clicked()
{
extern QString user_name;
QString temp_text;
QString temp_text1;
QString temp_text2;
QString temp_text3;
QString temp_text4;
int all;
all = quest_num;
QString file_path;
file_path = QFileDialog::getSaveFileName( this,
tr("Сохранить файл как"),
QDir::homePath(),
tr("Text file (*.txt)")
);
QFile file( file_path );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
stream.setEncoding( QStringConverter::Utf8 );
for(int i = 0; i < quest_num; i++) {
temp_text = QString::number(i+1);
temp_text1 = QString::number(user_ans[i]);
temp_text2 = QString::number(right_answers[i]);
if(user_ans[i] != right_answers[i]) {
stream << ("\n" + temp_text + " вопрос: " + user_ans_strings[i] + " - неверный ответ, правильный - " + right_ans_strings[i]);
all--;
}
else {
stream << ("\n" + temp_text + " вопрос: " + user_ans_strings[i] + " - верный ответ");
}
}
temp_text3 = QString::number(all);
temp_text4 = QString::number(quest_num);
stream << ("\n\n" + temp_text3 + " | " + temp_text4);
stream << ("\n\nВариант: " + user_name);
}
//clearing memory otherwise itll be...too bad!
delete[]user_ans;
delete[]right_answers;
QPropertyAnimation *a = new QPropertyAnimation(this,"windowOpacity");
a->setStartValue(1.0);
a->setDuration(400);
a->setEndValue(0.0);
a->setEasingCurve(QEasingCurve::InSine);
a->start(QPropertyAnimation::DeleteWhenStopped);
this->parentWidget()->setWindowOpacity(1.0);
this->parentWidget()->move(geometry().x(),geometry().y());
this->parentWidget()->show();
connect(a,SIGNAL(finished()),this,SLOT(close()));
}
void questions::on_okay_clicked()
{
delete[]user_ans;
delete[]right_answers;
QPropertyAnimation *a = new QPropertyAnimation(this,"windowOpacity");
a->setStartValue(1.0);
a->setDuration(400);
a->setEndValue(0.0);
a->setEasingCurve(QEasingCurve::InSine);
a->start(QPropertyAnimation::DeleteWhenStopped);
this->parentWidget()->setWindowOpacity(1.0);
this->parentWidget()->move(geometry().x(),geometry().y());
this->parentWidget()->show();
connect(a,SIGNAL(finished()),this,SLOT(close()));
}
void questions::Replace(QString &text) {
QMap<QString, QString> replace {
{"0", "?!*(#-=#"},
{"1", "!*!(#-#--"},
{"2", "*))!(#=@"},
{"3", "?*#)=#-=#"},
{"4", "-!(#-=!#="},
{"5", "=$:;==--"},
{"6", "Ap;l==#-$"},
{"7", "-#=#-??*"},
{"а", "###-=-@@@"},
{"б", "@@@-=-###"},
{"в", "###===@@$"},
{"г", "$##---@@@"},
{"д", "$##===-@@"},
{"е", "@-=-!##$#"},
{"ж", "#%=%@@@-#"},
{"з", "##====@@$"},
{"и", "##$$$=-=#"},
{"к", "#!!#$=$##"},
{"л", "#$#$#$=--"},
{"м", "$#$#$#!-!"},
{"н", "!#!#!#!-$"},
{"о", "!#!#!#!=$"},
{"п", "%#%#%#%-!"},
{"с", "$!$!-#=#!"},
{"у", "(#(#=-$!%"},
{"х", "##-(%-=#!"}
};
QMap<QString, QString>::const_iterator i = replace.constBegin();
while (i != replace.constEnd()) {
text.replace(i.value(), i.key());
++i;
}
}