Skip to content

Commit 38a36d9

Browse files
committed
add request pattern client
1 parent 7269934 commit 38a36d9

File tree

3 files changed

+325
-48
lines changed

3 files changed

+325
-48
lines changed

client/mainwindow.cpp

Lines changed: 177 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void MainWindow::slotReadyRead()
111111
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
112112
if (type->GetText() == QString("correct"))
113113
{
114-
this->fill_result_test(doc.FirstChildElement("body"));
114+
this->fill_db_result_test(doc.FirstChildElement("body"));
115115
this->fill_result_test_form();
116116
}
117117
else
@@ -126,14 +126,21 @@ void MainWindow::slotReadyRead()
126126
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
127127
if (type->GetText() == QString("correct"))
128128
{
129-
qDebug() << "Загружено на сервер!";
129+
qDebug() << "upload correct";
130130
}
131131
else
132132
{
133133
throw "Неверный логин или пароль!";
134134
}
135135
break;
136136
}
137+
138+
case (type_forward::load_patterns):
139+
{
140+
this->fill_db_patterns(doc.FirstChildElement("body"));
141+
this->fill_patterns_list_form();
142+
break;
143+
}
137144
}
138145

139146

@@ -187,6 +194,10 @@ void MainWindow::slotConnected()
187194
info = this->get_user_info();
188195
this->send_test_result(info.login, info.password);
189196
break;
197+
198+
case (type_forward::load_patterns):
199+
this->send_patterns_request();
200+
break;
190201
}
191202
}
192203

@@ -223,7 +234,7 @@ void MainWindow::create_db()
223234
}
224235
QSqlQuery query;
225236
query.exec("CREATE TABLE Pattern ( "
226-
"id INTEGER NOT NULL UNIQUE PRIMARY KEY,"
237+
"id INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT,"
227238
"name VARCHAR(45) NOT NULL UNIQUE, "
228239
"description TEXT NOT NULL, "
229240
"code TEXT NOT NULL, "
@@ -334,23 +345,33 @@ void MainWindow::send_test_result(const QString &login, const QString &password)
334345
this->send(printer.CStr());
335346
}
336347

348+
void MainWindow::send_patterns_request()
349+
{
350+
QString request{"<?xml version=\"1.1\" encoding=\"UTF-8\"?><type>patterns</type>"};
351+
this->send(request);
352+
}
353+
337354
void MainWindow::fill_db_login(tinyxml2::XMLElement *body)
338355
{
339356
QString login {this->ui->login_lineEdit->text()};
340357
QString password {this->ui->password_lineEdit->text()};
341358
QString type_image {body->FirstChildElement("img_type")->GetText()};
342359
QString path_to_image {QDir::currentPath() + "/images/user." + type_image};
360+
343361
this->base64_file = body->FirstChildElement("img")->GetText();
344362
this->save_img_to_file(path_to_image, this->base64_file);
345363

346-
this->fill_result_test(body);
364+
QSqlQuery insert;
365+
insert.exec("INSERT INTO User (login, password, path_to_image) VALUES ('" + login + "', '" + password + "', '" + path_to_image + "')");
366+
367+
368+
this->fill_db_result_test(body);
347369
}
348370

349371
void MainWindow::fill_db_register()
350372
{
351373
QString login {this->ui->reg_login->text()};
352374
QString password {this->ui->reg_pas->text()};
353-
//qDebug() << "in reg: " + this->file_type;
354375
QString path_to_image {QDir::currentPath() + "/images/user." + this->file_type};
355376

356377
this->save_img_to_file(path_to_image, this->base64_file);
@@ -373,19 +394,39 @@ void MainWindow::save_img_to_file(const QString &path, const QString &img)
373394
file.close();
374395
}
375396

397+
void MainWindow::quit()
398+
{
399+
this->socket.reset(new QTcpSocket());
400+
socket->connectToHost(this->host, this->port);
401+
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
402+
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
403+
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
404+
405+
this->ui->screen_stacked->setCurrentIndex((int)screen::login);
406+
this->forward = type_forward::login;
407+
408+
this->db.close();
409+
QFile::remove(QDir::currentPath() + "/main.db");
410+
QDir image_dir(QDir::currentPath() + "/images");
411+
image_dir.removeRecursively();
412+
QDir dir(QDir::currentPath());
413+
dir.mkdir("images");
414+
this->ui->login_2->setEnabled(false);
415+
this->ui->register_2->setEnabled(false);
416+
}
417+
376418
void MainWindow::fill_personal_area()
377419
{
378420
user_info_t info{this->get_user_info()};
379421

380422
this->pic = QPixmap();
381423
this->pic.load(info.path_to_image);
382424

383-
//this->pic = this->pic.scaled(QSize(256,256), Qt::AspectRatioMode(), Qt::SmoothTransformation);
384425
this->ui->img_profile->setPixmap(this->pic);
385426
this->ui->show_login->setText("Логин: " + info.login);
386427
}
387428

388-
void MainWindow::fill_result_test(tinyxml2::XMLElement *body)
429+
void MainWindow::fill_db_result_test(tinyxml2::XMLElement *body)
389430
{
390431
QSqlQuery insert;
391432
insert.exec("DELETE FROM User_test");
@@ -406,39 +447,118 @@ void MainWindow::fill_result_test(tinyxml2::XMLElement *body)
406447
}
407448
}
408449

450+
void MainWindow::fill_db_patterns(tinyxml2::XMLElement *body)
451+
{
452+
QSqlQuery query("DELETE FROM Pattern");
453+
454+
tinyxml2::XMLElement * patterns = body->FirstChildElement("pattern_list");
455+
tinyxml2::XMLElement * pattern = patterns->FirstChildElement("pattern");
456+
while (pattern)
457+
{
458+
QString name {pattern->FirstChildElement("name")->GetText()};
459+
QString description {pattern->FirstChildElement("description")->GetText()};
460+
QString code {pattern->FirstChildElement("code")->GetText()};
461+
QString img_64 {pattern->FirstChildElement("img")->GetText()};
462+
QString img_type {pattern->FirstChildElement("img_type")->GetText()};
463+
464+
QString path_to_image {QDir::currentPath() + "/images/" + name + "." + img_type};
465+
this->save_img_to_file(path_to_image, img_64);
466+
467+
QSqlQuery inser("INSERT INTO Pattern (name, description, code, path_to_image)"
468+
" VALUES ('" + name + "', '" + description + "', '" + code + "', '" + path_to_image + "')");
469+
470+
471+
472+
473+
pattern = pattern->NextSiblingElement("pattern");
474+
}
475+
}
476+
409477
void MainWindow::fill_result_test_form()
410478
{
411479
auto data {this->get_result_test_info()};
412480

413481
/*clear*/
414482
size_t i = 0;
415-
for (size_t i = 0; i < results_test.size(); i++)
483+
for (size_t i = 0; i < label_test.size(); i++)
416484
{
417-
delete this->results_test[i];
418-
if (int(i) != int(results_test.size()) - 1)
485+
delete this->label_test[i];
486+
if (int(i) != int(label_test.size()) - 1)
419487
{
420-
delete this->lines[i];
488+
delete this->lines_test[i];
421489
}
422490
}
423-
this->results_test.clear();
424-
this->lines.clear();
491+
this->label_test.clear();
492+
this->lines_test.clear();
425493

426494
/*results*/
427495
i = 0;
428496
this->ui->result_layout->setSizeConstraint(QLayout::SetMinimumSize);
429497
for (i = 0; i < data.size(); i++)
430498
{
431-
this->results_test.push_back(new QLabel(this));
432-
this->results_test[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
433-
this->results_test[i]->setWordWrap(true);
434-
this->results_test[i]->setText(data[i].name + ": " + data[i].result + "/10");
435-
this->ui->result_layout->addWidget(this->results_test[i]);
499+
this->label_test.push_back(new QLabel(this));
500+
this->label_test[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
501+
this->label_test[i]->setWordWrap(true);
502+
this->label_test[i]->setText(data[i].name + ": " + data[i].result + "/10");
503+
this->ui->result_layout->addWidget(this->label_test[i]);
436504

437505
if (int(i) != int(data.size()) - 1)
438506
{
439-
this->lines.push_back(new QFrame());
440-
this->lines[i]->setFrameShape(QFrame::HLine);
441-
this->ui->result_layout->addWidget(this->lines[i]);
507+
this->lines_test.push_back(new QFrame());
508+
this->lines_test[i]->setFrameShape(QFrame::HLine);
509+
this->ui->result_layout->addWidget(this->lines_test[i]);
510+
}
511+
}
512+
}
513+
514+
void MainWindow::fill_patterns_list_form()
515+
{
516+
auto data{this->get_patterns_name()};
517+
518+
/*clear*/
519+
size_t i = 0;
520+
for (size_t i = 0; i < pattern_label_list.size(); i++)
521+
{
522+
delete this->pattern_label_list[i];
523+
delete this->pattern_btn_more_list[i];
524+
delete this->pattern_btn_test_list[i];
525+
delete this->pattern_laoyut_list[i];
526+
if (int(i) != int(pattern_label_list.size()) - 1)
527+
{
528+
delete this->pattern_frame_list[i];
529+
}
530+
}
531+
this->pattern_label_list.clear();
532+
this->pattern_btn_more_list.clear();
533+
this->pattern_btn_test_list.clear();
534+
this->pattern_laoyut_list.clear();
535+
this->pattern_frame_list.clear();
536+
537+
/*results*/
538+
i = 0;
539+
for (i = 0; i < data.size(); i++)
540+
{
541+
this->pattern_laoyut_list.push_back(new QHBoxLayout(this));
542+
543+
this->pattern_label_list.push_back(new QLabel(this));
544+
this->pattern_label_list[i]->setWordWrap(true);
545+
this->pattern_label_list[i]->setText(data[i]);
546+
this->pattern_laoyut_list[i]->addWidget(this->pattern_label_list[i]);
547+
548+
this->pattern_btn_more_list.push_back(new QPushButton(this));
549+
this->pattern_btn_more_list[i]->setText("Подробнее");
550+
this->pattern_laoyut_list[i]->addWidget(this->pattern_btn_more_list[i]);
551+
552+
this->pattern_btn_test_list.push_back(new QPushButton(this));
553+
this->pattern_btn_test_list[i]->setText("Тест");
554+
this->pattern_laoyut_list[i]->addWidget(this->pattern_btn_test_list[i]);
555+
556+
this->ui->pattern_list_2->addLayout(this->pattern_laoyut_list[i]);
557+
if (int(i) != int(data.size()) - 1)
558+
{
559+
this->pattern_frame_list.push_back(new QFrame());
560+
this->pattern_frame_list[i]->setFrameShape(QFrame::HLine);
561+
this->ui->pattern_list_2->addWidget(this->pattern_frame_list[i]);
442562
}
443563
}
444564
}
@@ -475,6 +595,19 @@ std::vector<result_test_t> MainWindow::get_result_test_info()
475595
return res;
476596
}
477597

598+
std::vector<QString> MainWindow::get_patterns_name()
599+
{
600+
std::vector<QString> res;
601+
QSqlQuery query;
602+
query.exec("SELECT name FROM Pattern");
603+
while (query.next())
604+
{
605+
res.push_back(query.value(0).toString());
606+
}
607+
608+
return res;
609+
}
610+
478611
void MainWindow::on_to_register_clicked()
479612
{
480613
this->ui->screen_stacked->setCurrentIndex((int)screen::registration);
@@ -541,23 +674,7 @@ void MainWindow::on_reg_img_clicked()
541674

542675
void MainWindow::on_quit_clicked()
543676
{
544-
this->socket.reset(new QTcpSocket());
545-
socket->connectToHost(this->host, this->port);
546-
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
547-
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
548-
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
549-
550-
this->ui->screen_stacked->setCurrentIndex((int)screen::login);
551-
this->forward = type_forward::login;
552-
553-
this->db.close();
554-
QFile::remove(QDir::currentPath() + "/main.db");
555-
QDir image_dir(QDir::currentPath() + "/images");
556-
image_dir.removeRecursively();
557-
QDir dir(QDir::currentPath());
558-
dir.mkdir("images");
559-
this->ui->login_2->setEnabled(false);
560-
this->ui->register_2->setEnabled(false);
677+
this->quit();
561678
}
562679

563680
void MainWindow::on_to_result_test_clicked()
@@ -591,3 +708,25 @@ void MainWindow::on_upload_result_clicked()
591708
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
592709
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
593710
}
711+
712+
void MainWindow::on_to_patterns_clicked()
713+
{
714+
this->fill_patterns_list_form();
715+
this->ui->screen_stacked->setCurrentIndex((int)screen::patterns);
716+
}
717+
718+
void MainWindow::on_to_personal_area_2_clicked()
719+
{
720+
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
721+
}
722+
723+
void MainWindow::on_update_patterns_clicked()
724+
{
725+
this->forward = type_forward::load_patterns;
726+
727+
this->socket.reset(new QTcpSocket());
728+
socket->connectToHost(this->host, this->port);
729+
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
730+
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
731+
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
732+
}

0 commit comments

Comments
 (0)