@@ -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
8360void 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
161154void 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+
230256void 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
258268void 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+
297365user_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+ }
0 commit comments