Skip to content

Commit a59aaa4

Browse files
committed
add testing client
1 parent 037a75b commit a59aaa4

22 files changed

+2018
-307
lines changed

client/design_patterns.pro

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@ CONFIG += c++17
1010

1111
SOURCES += \
1212
base64.cpp \
13+
installation_of_correspondence.cpp \
1314
main.cpp \
1415
mainwindow.cpp \
16+
one_of_four.cpp \
1517
popup.cpp \
16-
tinyxml2.cpp
18+
some_of_four.cpp \
19+
test.cpp \
20+
test_widget.cpp \
21+
tinyxml2.cpp \
22+
write_answer.cpp
1723

1824
HEADERS += \
1925
base64.hpp \
26+
installation_of_correspondence.h \
2027
mainwindow.h \
28+
one_of_four.h \
2129
popup.h \
22-
tinyxml2.h
30+
question_base.h \
31+
some_of_four.h \
32+
test.h \
33+
test_widget.h \
34+
tinyxml2.h \
35+
write_answer.h
2336

2437
FORMS += \
2538
mainwindow.ui \
26-
popup.ui
39+
popup.ui \
40+
test_widget.ui
2741

2842
RESOURCES += res.qrc
2943

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "installation_of_correspondence.h"
2+
3+
void Installation_of_correspondence::input(QTextStream& file_stream)
4+
{
5+
this->question = file_stream.readLine();
6+
for (size_t i = 0; i < 4; i++)
7+
{
8+
this->answers[i] = file_stream.readLine();
9+
}
10+
for (size_t i = 0; i < 4; i++)
11+
{
12+
this->correct_answer[i] = file_stream.readLine();
13+
}
14+
}
15+
16+
std::pair<std::array<QString, 4>, std::array<QString, 4>> Installation_of_correspondence::get_answers()
17+
{
18+
auto correct{this->correct_answer};
19+
std::random_shuffle(correct.begin(), correct.end());
20+
21+
return std::make_pair(this->answers, correct);
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef INSTALLATION_OF_CORRESPONDENCE_H
2+
#define INSTALLATION_OF_CORRESPONDENCE_H
3+
4+
#include "question_base.h"
5+
6+
class Installation_of_correspondence : public Question_base
7+
{
8+
public:
9+
Installation_of_correspondence() = default;
10+
11+
void input(QTextStream& file_stream) override;
12+
13+
std::array<QString, 4> get_current_answer() const {return current_answer;}
14+
void set_current_answer(std::array<QString, 4> i) {this->current_answer = i;}
15+
16+
const std::array<QString, 4>& get_correct_answer(){return correct_answer;}
17+
/*answer, answer*/
18+
std::pair<std::array<QString, 4>, std::array<QString, 4>> get_answers();
19+
20+
private:
21+
std::array<QString, 4> answers;
22+
std::array<QString, 4> correct_answer;
23+
std::array<QString, 4> current_answer;
24+
25+
};
26+
27+
#endif // INSTALLATION_OF_CORRESPONDENCE_H

client/mainwindow.cpp

Lines changed: 143 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ MainWindow::MainWindow(QWidget *parent)
1313
{
1414
ui->setupUi(this);
1515
this->popup = new Popup(this);
16+
this->test_widget = new Test_widget(this);
17+
this->ui->test_layout->addWidget(this->test_widget);
18+
this->validator = new QRegularExpressionValidator(QRegularExpression("[A-Za-z0-9_]+"), this);
19+
this->ui->reg_login->setValidator(this->validator);
20+
this->ui->reg_pas->setValidator(this->validator);
21+
22+
for (size_t i = 0; i < this->test_widget->back.size(); i++)
23+
{
24+
connect(this->test_widget->back[i], &QPushButton::clicked, [this] { this->back_to_list(); });
25+
}
26+
connect(this->test_widget->result_done, &QPushButton::clicked, [this] { this->check_test(); });
1627

1728
if (!fileExists(QDir::currentPath() + "/main.db"))
1829
{
@@ -59,13 +70,11 @@ void MainWindow::on_login_2_clicked()
5970

6071
void MainWindow::slotReadyRead()
6172
{
62-
qDebug() << "Ready to read!";
6373
try
6474
{
6575
tinyxml2::XMLDocument doc;
6676
QString data = this->recv();
6777
doc.Parse(data.toUtf8());
68-
qDebug() << data;
6978

7079
switch (this->forward)
7180
{
@@ -113,10 +122,16 @@ void MainWindow::slotReadyRead()
113122
{
114123
this->fill_db_result_test(doc.FirstChildElement("body"));
115124
this->fill_result_test_form();
125+
126+
this->popup->set_title("Успешно");
127+
this->popup->set_description("Данные успешно скачаны с сервера!");
128+
this->popup->exec();
116129
}
117130
else
118131
{
119-
throw "Неверный логин или пароль!";
132+
this->popup->set_title("Ошибка");
133+
this->popup->set_description("Неверный логин или пароль!");
134+
this->popup->exec();
120135
}
121136
break;
122137
}
@@ -126,11 +141,15 @@ void MainWindow::slotReadyRead()
126141
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
127142
if (type->GetText() == QString("correct"))
128143
{
129-
qDebug() << "upload correct";
144+
this->popup->set_title("Успешно");
145+
this->popup->set_description("Данные успешно загружены на сервер!");
146+
this->popup->exec();
130147
}
131148
else
132149
{
133-
throw "Неверный логин или пароль!";
150+
this->popup->set_title("Ошибка");
151+
this->popup->set_description("Неверный логин или пароль!");
152+
this->popup->exec();
134153
}
135154
break;
136155
}
@@ -139,6 +158,10 @@ void MainWindow::slotReadyRead()
139158
{
140159
this->fill_db_patterns(doc.FirstChildElement("body"));
141160
this->fill_patterns_list_form();
161+
162+
this->popup->set_title("Успешно");
163+
this->popup->set_description("Список паттернов обновлен!");
164+
this->popup->exec();
142165
break;
143166
}
144167
}
@@ -170,13 +193,16 @@ void MainWindow::slotReadyRead()
170193

171194
void MainWindow::slotError(QAbstractSocket::SocketError)
172195
{
173-
qDebug() << "socket error!";
196+
this->popup->set_title("Произошла ошибка");
197+
this->popup->set_description("Ошибка соединения с сервером");
198+
this->popup->exec();
199+
this->socket->close();
200+
socket->connectToHost(this->host, this->port);
174201
}
175202

176203
void MainWindow::slotConnected()
177204
{
178205
user_info_t info;
179-
qDebug() << "host connected";
180206
switch (this->forward)
181207
{
182208
case (type_forward::login):
@@ -240,14 +266,14 @@ void MainWindow::create_db()
240266
"code TEXT NOT NULL, "
241267
"path_to_image VARCHAR(256) NOT NULL); ");
242268
query.exec("CREATE TABLE Pattern_test ( "
243-
"id_pattern INTEGER NOT NULL, "
244-
"question TEXT NOT NULL, "
245-
"id_type INTEGER NOT NULL, "
246-
"a1 VARCHAR(45) NOT NULL DEFAULT 'NULL',"
247-
"a2 VARCHAR(45) NOT NULL DEFAULT 'NULL',"
248-
"a3 VARCHAR(45) NOT NULL DEFAULT 'NULL',"
249-
"a4 VARCHAR(45) NOT NULL DEFAULT 'NULL',"
250-
"correct_answer VARCHAR(45) NOT NULL); " );
269+
"pattern_name VARCHAR(45) NOT NULL, "
270+
"question TEXT NOT NULL, "
271+
"id_type INTEGER NOT NULL, "
272+
"a1 VARCHAR(45) NULL DEFAULT 'NULL', "
273+
"a2 VARCHAR(45) NULL DEFAULT 'NULL', "
274+
"a3 VARCHAR(45) NULL DEFAULT 'NULL', "
275+
"a4 VARCHAR(45) NULL DEFAULT 'NULL', "
276+
"correct_answer VARCHAR(45) NOT NULL); " );
251277
query.exec("CREATE TABLE User ( "
252278
"login VARCHAR(45) NOT NULL UNIQUE, "
253279
"password VARCHAR(45) NOT NULL UNIQUE, "
@@ -340,8 +366,6 @@ void MainWindow::send_test_result(const QString &login, const QString &password)
340366
tinyxml2::XMLPrinter printer;
341367
doc.Print(&printer);
342368

343-
qDebug() << printer.CStr();
344-
345369
this->send(printer.CStr());
346370
}
347371

@@ -409,8 +433,10 @@ void MainWindow::quit()
409433
QFile::remove(QDir::currentPath() + "/main.db");
410434
QDir image_dir(QDir::currentPath() + "/images");
411435
image_dir.removeRecursively();
412-
QDir dir(QDir::currentPath());
413-
dir.mkdir("images");
436+
QDir dir1(QDir::currentPath());
437+
dir1.mkdir("images");
438+
QDir dir2(QDir::currentPath() + "/images");
439+
dir2.mkdir("patterns");
414440
this->ui->login_2->setEnabled(false);
415441
this->ui->register_2->setEnabled(false);
416442
}
@@ -449,7 +475,14 @@ void MainWindow::fill_db_result_test(tinyxml2::XMLElement *body)
449475

450476
void MainWindow::fill_db_patterns(tinyxml2::XMLElement *body)
451477
{
452-
QSqlQuery query("DELETE FROM Pattern");
478+
QSqlQuery query1("DELETE FROM Pattern");
479+
QSqlQuery query2("DELETE FROM Pattern_test");
480+
QDir image_dir(QDir::currentPath() + "/images/patterns");
481+
image_dir.removeRecursively();
482+
QDir dir1(QDir::currentPath());
483+
dir1.mkdir("images");
484+
QDir dir2(QDir::currentPath() + "/images");
485+
dir2.mkdir("patterns");
453486

454487
tinyxml2::XMLElement * patterns = body->FirstChildElement("pattern_list");
455488
tinyxml2::XMLElement * pattern = patterns->FirstChildElement("pattern");
@@ -461,16 +494,50 @@ void MainWindow::fill_db_patterns(tinyxml2::XMLElement *body)
461494
QString img_64 {pattern->FirstChildElement("img")->GetText()};
462495
QString img_type {pattern->FirstChildElement("img_type")->GetText()};
463496

497+
QSqlQuery insert("INSERT INTO Pattern (name, description, code, path_to_image)"
498+
" VALUES ('" + name + "', '" + description + "', '" + code + "', 'temp')");
464499

465500
QSqlQuery id_query("SELECT id FROM Pattern ORDER BY id DESC");
466501
id_query.next();
467502
QString id{QString::number(id_query.value(0).toInt() + 1)};
468503

469-
QString path_to_image {QDir::currentPath() + "/images/" + id + "." + img_type};
504+
QString path_to_image {QDir::currentPath() + "/images/patterns/" + id + "." + img_type};
470505
this->save_img_to_file(path_to_image, img_64);
506+
QSqlQuery update("UPDATE Pattern SET path_to_image = '" + path_to_image + "' WHERE name = '" + name + "'");
507+
508+
509+
tinyxml2::XMLElement * tests = pattern->FirstChildElement("test_list");
510+
if (tests)
511+
{
512+
tinyxml2::XMLElement * test = tests->FirstChildElement("test");
513+
while(test)
514+
{
515+
QString question {test->FirstChildElement("question")->GetText()};
516+
QString correct_answer {test->FirstChildElement("correct_answer")->GetText()};
517+
518+
int type {test->FirstChildElement("id_description")->IntText()};
519+
if (type == 1)
520+
{
521+
QString a1 {test->FirstChildElement("a1")->GetText()};
522+
QString a2 {test->FirstChildElement("a2")->GetText()};
523+
QString a3 {test->FirstChildElement("a3")->GetText()};
524+
QString a4 {test->FirstChildElement("a4")->GetText()};
525+
526+
QSqlQuery insert("INSERT INTO Pattern_test (pattern_name, question, id_type, a1, a2, a3, a4, correct_answer) "
527+
"VALUES ('" + name + "', '" + question + "', '" + QString::number(type) + "', "
528+
"'" + a1 + "', '" + a2 + "', '" + a3 + "', '" + a4 + "',"
529+
" '" + correct_answer + "')");
530+
}
531+
else if (type == 2)
532+
{
533+
QSqlQuery insert("INSERT INTO Pattern_test (pattern_name, question, id_type, correct_answer) "
534+
"VALUES ('" + name + "', '" + question + "', '" + QString::number(type) + "', '" + correct_answer + "')");
535+
}
536+
537+
test = test->NextSiblingElement("test");
538+
}
539+
}
471540

472-
QSqlQuery insert("INSERT INTO Pattern (name, description, code, path_to_image)"
473-
" VALUES ('" + name + "', '" + description + "', '" + code + "', '" + path_to_image + "')");
474541

475542
pattern = pattern->NextSiblingElement("pattern");
476543
}
@@ -616,8 +683,7 @@ std::vector<QString> MainWindow::get_patterns_name()
616683
pattern_info_t MainWindow::get_pattern_info(QString name)
617684
{
618685
QSqlQuery query;
619-
if (!query.exec("SELECT name, description, code, path_to_image FROM Pattern WHERE name = '" + name + "'"))
620-
qDebug() << "ERROR";
686+
query.exec("SELECT name, description, code, path_to_image FROM Pattern WHERE name = '" + name + "'");
621687

622688
query.next();
623689

@@ -654,9 +720,61 @@ void MainWindow::on_more_btn_clicked(const QString& name)
654720

655721
void MainWindow::on_to_test_btn_clicked(const QString &name)
656722
{
723+
QFile file("tmp.txt");
724+
file.open(QIODevice::WriteOnly | QIODevice::Text);
725+
726+
QTextStream questions(&file);
727+
QSqlQuery select("SELECT * FROM Pattern_test WHERE pattern_name = '" + name + "'");
728+
QSqlQuery count("SELECT COUNT(*) FROM Pattern_test WHERE pattern_name = '" + name + "'");
729+
count.next();
730+
731+
int counter {count.value(0).toInt()};
732+
questions << counter << "\n";
733+
while (select.next())
734+
{
735+
int type {select.value(2).toInt()};
736+
737+
if (type == 1)
738+
{
739+
questions << "1\n";
740+
questions << select.value(1).toString() << "\n"; /*вопрос*/
741+
742+
questions << select.value(3).toString() << "\n"; /*ответы*/
743+
questions << select.value(4).toString() << "\n";
744+
questions << select.value(5).toString() << "\n";
745+
questions << select.value(6).toString() << "\n";
746+
747+
questions << select.value(7).toString() << "\n"; /*правильный ответ*/
748+
}
749+
else if (type == 2)
750+
{
751+
questions << "3\n";
752+
questions << select.value(1).toString() << "\n"; /*вопрос*/
753+
754+
questions << select.value(7).toString() << "\n"; /*правильный ответ*/
755+
}
756+
}
757+
file.close();
758+
759+
this->test_widget->init("tmp.txt");
760+
this->test_widget->pattern = name;
761+
QFile::remove("tmp.txt");
657762
this->ui->screen_stacked->setCurrentIndex((int)screen::test_pattern);
658763
}
659764

765+
void MainWindow::back_to_list()
766+
{
767+
this->test_widget->stop_timer();
768+
this->ui->screen_stacked->setCurrentIndex((int)screen::patterns);
769+
}
770+
771+
void MainWindow::check_test()
772+
{
773+
QSqlQuery delete_query("DELETE FROM User_test WHERE pattern = '" + this->test_widget->pattern + "'");
774+
QSqlQuery insert("INSERT INTO User_test (pattern, count_corrent) VALUES ('" + this->test_widget->pattern + "', " + QString::number(this->test_widget->count_correct) + ")");
775+
this->fill_result_test_form();
776+
}
777+
660778
void MainWindow::on_to_register_clicked()
661779
{
662780
this->ui->screen_stacked->setCurrentIndex((int)screen::registration);

0 commit comments

Comments
 (0)