Skip to content

Commit b53614c

Browse files
committed
add load result to client
1 parent c8b7217 commit b53614c

File tree

3 files changed

+164
-46
lines changed

3 files changed

+164
-46
lines changed

client/mainwindow.cpp

Lines changed: 121 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ MainWindow::MainWindow(QWidget *parent)
3838
}
3939

4040
this->fill_personal_area();
41+
this->fill_result_test_form();
4142
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
4243
}
4344
}
@@ -53,31 +54,7 @@ void MainWindow::on_login_2_clicked()
5354
if (!this->ui->login_lineEdit->text().size() || !this->ui->password_lineEdit->text().size())
5455
return;
5556

56-
tinyxml2::XMLDocument doc;
57-
tinyxml2::XMLDeclaration* decl = doc.NewDeclaration("xml version=\"1.1\" encoding=\"UTF-8\"");
58-
doc.InsertEndChild(decl);
59-
60-
tinyxml2::XMLElement* type = doc.NewElement("type");
61-
type->SetText("authorization");
62-
doc.InsertEndChild(type);
63-
64-
tinyxml2::XMLElement* body = doc.NewElement("body");
65-
doc.InsertEndChild(body);
66-
67-
tinyxml2::XMLElement* login = doc.NewElement("login");
68-
login->SetText(this->ui->login_lineEdit->text().toLocal8Bit().data());
69-
body->InsertEndChild(login);
70-
71-
tinyxml2::XMLElement* password = doc.NewElement("password");
72-
password->SetText(this->ui->password_lineEdit->text().toLocal8Bit().data());
73-
body->InsertEndChild(password);
74-
75-
tinyxml2::XMLPrinter printer;
76-
doc.Print(&printer);
77-
78-
qDebug() << printer.CStr();
79-
80-
this->send(printer.CStr());
57+
this->send_auth(this->ui->login_lineEdit->text(), this->ui->password_lineEdit->text());
8158
}
8259

8360
void MainWindow::slotReadyRead()
@@ -108,6 +85,7 @@ void MainWindow::slotReadyRead()
10885
}
10986
break;
11087
}
88+
11189
case (type_forward::registration):
11290
{
11391
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
@@ -127,6 +105,21 @@ void MainWindow::slotReadyRead()
127105
}
128106
break;
129107
}
108+
109+
case (type_forward::load_result):
110+
{
111+
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
112+
if (type->GetText() == QString("correct"))
113+
{
114+
this->fill_result_test(doc.FirstChildElement("body"));
115+
this->fill_result_test_form();
116+
}
117+
else
118+
{
119+
throw "Неверный логин или пароль!";
120+
}
121+
break;
122+
}
130123
}
131124

132125
this->socket->close();
@@ -160,6 +153,7 @@ void MainWindow::slotError(QAbstractSocket::SocketError)
160153

161154
void MainWindow::slotConnected()
162155
{
156+
user_info_t info;
163157
qDebug() << "host connected";
164158
switch (this->forward)
165159
{
@@ -168,6 +162,11 @@ void MainWindow::slotConnected()
168162
this->ui->login_2->setEnabled(true);
169163
this->ui->register_2->setEnabled(true);
170164
break;
165+
166+
case (type_forward::load_result):
167+
info = this->get_user_info();
168+
this->send_auth(info.login, info.password);
169+
break;
171170
}
172171
}
173172

@@ -227,6 +226,33 @@ void MainWindow::create_db()
227226
"count_corrent INTEGER NOT NULL); " );
228227
}
229228

229+
void MainWindow::send_auth(const QString &login, const QString &password)
230+
{
231+
tinyxml2::XMLDocument doc;
232+
tinyxml2::XMLDeclaration* decl = doc.NewDeclaration("xml version=\"1.1\" encoding=\"UTF-8\"");
233+
doc.InsertEndChild(decl);
234+
235+
tinyxml2::XMLElement* type = doc.NewElement("type");
236+
type->SetText("authorization");
237+
doc.InsertEndChild(type);
238+
239+
tinyxml2::XMLElement* body = doc.NewElement("body");
240+
doc.InsertEndChild(body);
241+
242+
tinyxml2::XMLElement* login_xml = doc.NewElement("login");
243+
login_xml->SetText(login.toLocal8Bit().data());
244+
body->InsertEndChild(login_xml);
245+
246+
tinyxml2::XMLElement* password_xml = doc.NewElement("password");
247+
password_xml->SetText(password.toLocal8Bit().data());
248+
body->InsertEndChild(password_xml);
249+
250+
tinyxml2::XMLPrinter printer;
251+
doc.Print(&printer);
252+
253+
this->send(printer.CStr());
254+
}
255+
230256
void MainWindow::fill_db_login(tinyxml2::XMLElement *body)
231257
{
232258
QString login {this->ui->login_lineEdit->text()};
@@ -236,23 +262,7 @@ void MainWindow::fill_db_login(tinyxml2::XMLElement *body)
236262
this->base64_file = body->FirstChildElement("img")->GetText();
237263
this->save_img_to_file(path_to_image, this->base64_file);
238264

239-
QSqlQuery insert;
240-
insert.exec("INSERT INTO User (login, password, path_to_image) VALUES ('" + login + "', '" + password + "', '" + path_to_image + "')");
241-
int length {body->FirstChildElement("count_test")->IntText()};
242-
if (length > 0)
243-
{
244-
tinyxml2::XMLElement * tests = body->FirstChildElement("tests");
245-
tinyxml2::XMLElement * test = tests->FirstChildElement("test");
246-
while (test)
247-
{
248-
QString name {test->FirstChildElement("name")->GetText()};
249-
QString result {test->FirstChildElement("result")->GetText()};
250-
251-
insert.exec("INSERT INTO User_test (pattern, count_corrent) VALUES ('" + name + "', " + result + ")");
252-
253-
test = test->NextSiblingElement("test");
254-
}
255-
}
265+
this->fill_result_test(body);
256266
}
257267

258268
void MainWindow::fill_db_register()
@@ -294,6 +304,64 @@ void MainWindow::fill_personal_area()
294304
this->ui->show_login->setText("Логин: " + info.login);
295305
}
296306

307+
void MainWindow::fill_result_test(tinyxml2::XMLElement *body)
308+
{
309+
QSqlQuery insert;
310+
insert.exec("DELETE FROM User_test");
311+
int length {body->FirstChildElement("count_test")->IntText()};
312+
if (length > 0)
313+
{
314+
tinyxml2::XMLElement * tests = body->FirstChildElement("tests");
315+
tinyxml2::XMLElement * test = tests->FirstChildElement("test");
316+
while (test)
317+
{
318+
QString name {test->FirstChildElement("name")->GetText()};
319+
QString result {test->FirstChildElement("result")->GetText()};
320+
321+
insert.exec("INSERT INTO User_test (pattern, count_corrent) VALUES ('" + name + "', " + result + ")");
322+
323+
test = test->NextSiblingElement("test");
324+
}
325+
}
326+
}
327+
328+
void MainWindow::fill_result_test_form()
329+
{
330+
auto data {this->get_result_test_info()};
331+
332+
/*clear*/
333+
size_t i = 0;
334+
for (size_t i = 0; i < results_test.size(); i++)
335+
{
336+
delete this->results_test[i];
337+
if (int(i) != int(results_test.size()) - 1)
338+
{
339+
delete this->lines[i];
340+
}
341+
}
342+
this->results_test.clear();
343+
this->lines.clear();
344+
345+
/*results*/
346+
i = 0;
347+
this->ui->result_layout->setSizeConstraint(QLayout::SetMinimumSize);
348+
for (i = 0; i < data.size(); i++)
349+
{
350+
this->results_test.push_back(new QLabel(this));
351+
this->results_test[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
352+
this->results_test[i]->setWordWrap(true);
353+
this->results_test[i]->setText(data[i].name + ": " + data[i].result + "/10");
354+
this->ui->result_layout->addWidget(this->results_test[i]);
355+
356+
if (int(i) != int(data.size()) - 1)
357+
{
358+
this->lines.push_back(new QFrame());
359+
this->lines[i]->setFrameShape(QFrame::HLine);
360+
this->ui->result_layout->addWidget(this->lines[i]);
361+
}
362+
}
363+
}
364+
297365
user_info_t MainWindow::get_user_info()
298366
{
299367
QSqlQuery query;
@@ -420,3 +488,14 @@ void MainWindow::on_to_personal_area_clicked()
420488
{
421489
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
422490
}
491+
492+
void MainWindow::on_load_result_clicked()
493+
{
494+
this->forward = type_forward::load_result;
495+
496+
this->socket.reset(new QTcpSocket());
497+
socket->connectToHost(this->host, this->port);
498+
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
499+
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
500+
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
501+
}

client/mainwindow.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <QtSql>
1111
#include <QPixmap>
1212
#include <QDir>
13+
#include <QLabel>
14+
#include <QWidget>
15+
#include <QVBoxLayout>
1316

1417
#include <memory>
1518
#include <vector>
@@ -65,6 +68,8 @@ private slots:
6568

6669
void on_to_personal_area_clicked();
6770

71+
void on_load_result_clicked();
72+
6873
private:
6974
enum class screen
7075
{
@@ -77,7 +82,9 @@ private slots:
7782
enum class type_forward
7883
{
7984
login,
80-
registration
85+
registration,
86+
load_result, /*загрузка результатов тестов с сервера*/
87+
upload_result, /*отгрузка данных с клиента на сервер*/
8188
};
8289

8390
Ui::MainWindow *ui;
@@ -88,6 +95,9 @@ private slots:
8895
QString base64_file;
8996
QString file_type;
9097

98+
std::vector<QLabel*> results_test;
99+
std::vector<QFrame*> lines;
100+
91101
std::unique_ptr<QTcpSocket> socket;
92102
const QString host{"localhost"};
93103
const quint16 port{20002};
@@ -96,9 +106,14 @@ private slots:
96106
QString recv();
97107
void create_db();
98108

109+
void send_auth(const QString& login, const QString& password);
110+
99111
void fill_db_login(tinyxml2::XMLElement* body);
100112
void fill_db_register();
101113
void fill_personal_area();
114+
void fill_result_test(tinyxml2::XMLElement* body);
115+
116+
void fill_result_test_form();
102117

103118
void save_img_to_file(const QString& path, const QString& img);
104119

client/mainwindow.ui

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@
449449
<property name="widgetResizable">
450450
<bool>true</bool>
451451
</property>
452-
<widget class="QWidget" name="scrollAreaWidgetContents">
452+
<widget class="QWidget" name="weqwdaf">
453453
<property name="geometry">
454454
<rect>
455455
<x>0</x>
@@ -458,6 +458,30 @@
458458
<height>395</height>
459459
</rect>
460460
</property>
461+
<property name="sizePolicy">
462+
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
463+
<horstretch>0</horstretch>
464+
<verstretch>0</verstretch>
465+
</sizepolicy>
466+
</property>
467+
<property name="minimumSize">
468+
<size>
469+
<width>0</width>
470+
<height>395</height>
471+
</size>
472+
</property>
473+
<layout class="QVBoxLayout" name="verticalLayout_3">
474+
<property name="sizeConstraint">
475+
<enum>QLayout::SetMinimumSize</enum>
476+
</property>
477+
<item>
478+
<layout class="QVBoxLayout" name="result_layout">
479+
<property name="sizeConstraint">
480+
<enum>QLayout::SetDefaultConstraint</enum>
481+
</property>
482+
</layout>
483+
</item>
484+
</layout>
461485
</widget>
462486
</widget>
463487
</item>
@@ -477,7 +501,7 @@
477501
<item>
478502
<layout class="QGridLayout" name="gridLayout_7">
479503
<item row="3" column="0">
480-
<widget class="QPushButton" name="pushButton">
504+
<widget class="QPushButton" name="upload_result">
481505
<property name="text">
482506
<string>Загрузить результаты на сервер</string>
483507
</property>
@@ -497,7 +521,7 @@
497521
</spacer>
498522
</item>
499523
<item row="2" column="0">
500-
<widget class="QPushButton" name="pushButton_3">
524+
<widget class="QPushButton" name="load_result">
501525
<property name="text">
502526
<string>Скачать результаты с сервера</string>
503527
</property>

0 commit comments

Comments
 (0)