Skip to content

Commit 8250831

Browse files
committed
client code done
1 parent a59aaa4 commit 8250831

File tree

14 files changed

+333
-145
lines changed

14 files changed

+333
-145
lines changed

client/mainwindow.cpp

Lines changed: 199 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,143 @@ void MainWindow::on_login_2_clicked()
6969
}
7070

7171
void MainWindow::slotReadyRead()
72+
{
73+
if (this->msg_length == -1)
74+
{
75+
char buf[sizeof(int32_t)]{0};
76+
this->socket->read(buf, sizeof(int32_t));
77+
78+
int32_t length;
79+
std::memcpy(&length, buf, sizeof(int32_t));
80+
// qDebug() << "len buf: " <<length;
81+
82+
this->msg_length = length - 1;
83+
}
84+
85+
// qDebug() << "slot ready read, bytesAvailable: " << this->socket->bytesAvailable();
86+
87+
if (this->socket->bytesAvailable() >= this->msg_length)
88+
{
89+
this->msg_length = -1;
90+
this->recv_data_handler(this->socket->readAll());
91+
}
92+
93+
}
94+
95+
void MainWindow::slotError(QAbstractSocket::SocketError)
96+
{
97+
this->popup->set_title("Произошла ошибка");
98+
this->popup->set_description("Ошибка соединения с сервером");
99+
this->popup->exec();
100+
this->socket->close();
101+
socket->connectToHost(this->host, this->port);
102+
}
103+
104+
void MainWindow::slotConnected()
105+
{
106+
user_info_t info;
107+
switch (this->forward)
108+
{
109+
case (type_forward::login):
110+
case (type_forward::registration):
111+
this->ui->login_2->setEnabled(true);
112+
this->ui->register_2->setEnabled(true);
113+
break;
114+
115+
case (type_forward::load_result):
116+
info = this->get_user_info();
117+
this->send_auth(info.login, info.password);
118+
break;
119+
120+
case (type_forward::upload_result):
121+
info = this->get_user_info();
122+
this->send_test_result(info.login, info.password);
123+
break;
124+
125+
case (type_forward::load_patterns):
126+
this->send_patterns_request();
127+
break;
128+
}
129+
}
130+
131+
void MainWindow::send(const QString &data)
132+
{
133+
int32_t length = int32_t(data.toUtf8().length());
134+
length++;
135+
char buf[4];
136+
std::memcpy(buf, &length, 4);
137+
this->socket->write(buf, 4);
138+
139+
this->socket->write(data.toUtf8());
140+
}
141+
142+
//QString MainWindow::recv()
143+
//{
144+
// char buf[sizeof(int32_t)]{0};
145+
// this->socket->read(buf, sizeof(int32_t));
146+
147+
// int32_t length;
148+
// std::memcpy(&length, buf, sizeof(int32_t));
149+
// qDebug() << "len buf: " <<length;
150+
151+
// length--;
152+
// QString data;
153+
154+
// while (1)
155+
// {
156+
// data.append(this->socket->readAll());
157+
// if (data.size() >= length)
158+
// break;
159+
// }
160+
// qDebug() << data.length();
161+
162+
// return data;
163+
//}
164+
165+
void MainWindow::create_db()
166+
{
167+
this->db = QSqlDatabase::addDatabase("QSQLITE");
168+
this->db.setDatabaseName(QDir::currentPath() + "/main.db");
169+
170+
if (!this->db.open())
171+
{
172+
qDebug() << this->db.lastError().text();
173+
}
174+
QSqlQuery query;
175+
query.exec("CREATE TABLE Pattern ( "
176+
"id INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT,"
177+
"name VARCHAR(45) NOT NULL UNIQUE, "
178+
"description TEXT NOT NULL, "
179+
"code TEXT NOT NULL, "
180+
"path_to_image VARCHAR(256) NOT NULL); ");
181+
query.exec("CREATE TABLE Pattern_test ( "
182+
"pattern_name VARCHAR(45) NOT NULL, "
183+
"question TEXT NOT NULL, "
184+
"id_type INTEGER NOT NULL, "
185+
"a1 VARCHAR(45) NULL DEFAULT 'NULL', "
186+
"a2 VARCHAR(45) NULL DEFAULT 'NULL', "
187+
"a3 VARCHAR(45) NULL DEFAULT 'NULL', "
188+
"a4 VARCHAR(45) NULL DEFAULT 'NULL', "
189+
"correct_answer VARCHAR(45) NOT NULL); " );
190+
query.exec("CREATE TABLE User ( "
191+
"login VARCHAR(45) NOT NULL UNIQUE, "
192+
"password VARCHAR(45) NOT NULL UNIQUE, "
193+
"path_to_image VARCHAR(256) NOT NULL UNIQUE);" );
194+
query.exec("CREATE TABLE User_test ( "
195+
"pattern VARCHAR(45) NOT NULL UNIQUE, "
196+
"count_corrent INTEGER NOT NULL); " );
197+
}
198+
199+
void MainWindow::recv_data_handler(const QString &data)
72200
{
73201
try
74202
{
75203
tinyxml2::XMLDocument doc;
76-
QString data = this->recv();
204+
// QString data = this->recv();
77205
doc.Parse(data.toUtf8());
78206

207+
// qDebug() << data.size() << ", " << (doc.FirstChildElement("body") == nullptr);
208+
79209
switch (this->forward)
80210
{
81211
case (type_forward::login):
@@ -117,6 +247,13 @@ void MainWindow::slotReadyRead()
117247

118248
case (type_forward::load_result):
119249
{
250+
if (!data.size())
251+
{
252+
this->popup->set_title("Успешно");
253+
this->popup->set_description("Данные успешно скачаны с сервера! Пройдено тестов: 0");
254+
this->popup->exec();
255+
break;
256+
}
120257
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
121258
if (type->GetText() == QString("correct"))
122259
{
@@ -191,98 +328,6 @@ void MainWindow::slotReadyRead()
191328
}
192329
}
193330

194-
void MainWindow::slotError(QAbstractSocket::SocketError)
195-
{
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);
201-
}
202-
203-
void MainWindow::slotConnected()
204-
{
205-
user_info_t info;
206-
switch (this->forward)
207-
{
208-
case (type_forward::login):
209-
case (type_forward::registration):
210-
this->ui->login_2->setEnabled(true);
211-
this->ui->register_2->setEnabled(true);
212-
break;
213-
214-
case (type_forward::load_result):
215-
info = this->get_user_info();
216-
this->send_auth(info.login, info.password);
217-
break;
218-
219-
case (type_forward::upload_result):
220-
info = this->get_user_info();
221-
this->send_test_result(info.login, info.password);
222-
break;
223-
224-
case (type_forward::load_patterns):
225-
this->send_patterns_request();
226-
break;
227-
}
228-
}
229-
230-
void MainWindow::send(const QString &data)
231-
{
232-
int32_t length = int32_t(data.toUtf8().length());
233-
length++;
234-
char buf[4];
235-
std::memcpy(buf, &length, 4);
236-
this->socket->write(buf, 4);
237-
238-
this->socket->write(data.toUtf8());
239-
}
240-
241-
QString MainWindow::recv()
242-
{
243-
char buf[sizeof(int32_t)]{0};
244-
this->socket->read(buf, sizeof(int32_t));
245-
246-
int32_t length;
247-
std::memcpy(&length, buf, sizeof(int32_t));
248-
249-
return this->socket->readAll();
250-
}
251-
252-
void MainWindow::create_db()
253-
{
254-
this->db = QSqlDatabase::addDatabase("QSQLITE");
255-
this->db.setDatabaseName(QDir::currentPath() + "/main.db");
256-
257-
if (!this->db.open())
258-
{
259-
qDebug() << this->db.lastError().text();
260-
}
261-
QSqlQuery query;
262-
query.exec("CREATE TABLE Pattern ( "
263-
"id INTEGER NOT NULL UNIQUE PRIMARY KEY AUTOINCREMENT,"
264-
"name VARCHAR(45) NOT NULL UNIQUE, "
265-
"description TEXT NOT NULL, "
266-
"code TEXT NOT NULL, "
267-
"path_to_image VARCHAR(256) NOT NULL); ");
268-
query.exec("CREATE TABLE Pattern_test ( "
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); " );
277-
query.exec("CREATE TABLE User ( "
278-
"login VARCHAR(45) NOT NULL UNIQUE, "
279-
"password VARCHAR(45) NOT NULL UNIQUE, "
280-
"path_to_image VARCHAR(256) NOT NULL UNIQUE);" );
281-
query.exec("CREATE TABLE User_test ( "
282-
"pattern VARCHAR(45) NOT NULL UNIQUE, "
283-
"count_corrent INTEGER NOT NULL); " );
284-
}
285-
286331
void MainWindow::send_auth(const QString &login, const QString &password)
287332
{
288333
tinyxml2::XMLDocument doc;
@@ -447,6 +492,45 @@ void MainWindow::fill_personal_area()
447492

448493
this->profile_pic = QPixmap();
449494
this->profile_pic.load(info.path_to_image);
495+
int width{this->profile_pic.width()};
496+
int height{this->profile_pic.height()};
497+
bool is_width{ width >= height };
498+
int big{ width >= height ? width : height };
499+
500+
if (width == height)
501+
{
502+
this->profile_pic = this->profile_pic.scaled
503+
(
504+
512,
505+
512,
506+
507+
Qt::IgnoreAspectRatio, Qt::SmoothTransformation
508+
);
509+
}
510+
else if (big > 512)
511+
{
512+
double ratio {big / 512.0};
513+
514+
this->profile_pic = this->profile_pic.scaled
515+
(
516+
is_width ? 512 : int(this->profile_pic.width() / ratio),
517+
is_width ? int(this->profile_pic.height() / ratio) : 512,
518+
519+
Qt::IgnoreAspectRatio, Qt::SmoothTransformation
520+
);
521+
}
522+
else
523+
{
524+
double ratio {512.0 / big};
525+
526+
this->profile_pic = this->profile_pic.scaled
527+
(
528+
is_width ? 512 : int(this->profile_pic.width() * ratio),
529+
is_width ? int(this->profile_pic.height() * ratio) : 512,
530+
531+
Qt::IgnoreAspectRatio, Qt::SmoothTransformation
532+
);
533+
}
450534

451535
this->ui->img_profile->setPixmap(this->profile_pic);
452536
this->ui->show_login->setText("Логин: " + info.login);
@@ -568,7 +652,7 @@ void MainWindow::fill_result_test_form()
568652
this->label_test.push_back(new QLabel(this));
569653
this->label_test[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
570654
this->label_test[i]->setWordWrap(true);
571-
this->label_test[i]->setText(data[i].name + ": " + data[i].result + "/10");
655+
this->label_test[i]->setText(data[i].name + ": " + data[i].result + "/" + QString::number(this->get_count_question(data[i].name)));
572656
this->ui->result_layout->addWidget(this->label_test[i]);
573657

574658
if (int(i) != int(data.size()) - 1)
@@ -690,6 +774,16 @@ pattern_info_t MainWindow::get_pattern_info(QString name)
690774
return {query.value(0).toString(), query.value(1).toString(), query.value(2).toString(), query.value(3).toString()};
691775
}
692776

777+
int MainWindow::get_count_question(QString name)
778+
{
779+
QSqlQuery query;
780+
query.exec("SELECT COUNT(*) FROM Pattern_test WHERE pattern_name = '" + name + "'");
781+
782+
query.next();
783+
784+
return query.value(0).toInt();
785+
}
786+
693787
void MainWindow::on_more_btn_clicked(const QString& name)
694788
{
695789
this->ui->screen_stacked->setCurrentIndex((int)screen::more_pattern);
@@ -756,8 +850,8 @@ void MainWindow::on_to_test_btn_clicked(const QString &name)
756850
}
757851
file.close();
758852

759-
this->test_widget->init("tmp.txt");
760853
this->test_widget->pattern = name;
854+
this->test_widget->init("tmp.txt");
761855
QFile::remove("tmp.txt");
762856
this->ui->screen_stacked->setCurrentIndex((int)screen::test_pattern);
763857
}
@@ -790,7 +884,10 @@ void MainWindow::on_to_login_clicked()
790884
void MainWindow::on_register_2_clicked()
791885
{
792886
if (!this->ui->reg_login->text().size() || !this->ui->reg_pas->text().size() || !this->base64_file.size())
887+
{
888+
qDebug() << "not reg";
793889
return;
890+
}
794891

795892
tinyxml2::XMLDocument doc;
796893
tinyxml2::XMLDeclaration* decl = doc.NewDeclaration("xml version=\"1.1\" encoding=\"UTF-8\"");
@@ -829,9 +926,15 @@ void MainWindow::on_reg_img_clicked()
829926
{
830927
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"));
831928

929+
qDebug() << "file path: " << fileName;
832930
std::ifstream file(fileName.toUtf8(), std::ios::in | std::ios::binary);
833-
if (!file.is_open())
931+
if (!file.is_open())
932+
{
933+
this->popup->set_title("Ошибка");
934+
this->popup->set_description("Не удается открыть файл!");
935+
this->popup->exec();
834936
return;
937+
}
835938

836939
std::vector<uint8_t> data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
837940

client/mainwindow.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private slots:
132132
QString file_type;
133133
QValidator *validator;
134134
Test_widget* test_widget;
135+
int32_t msg_length{-1};
135136

136137
/*для результатов тестов*/
137138
std::vector<QLabel*> label_test;
@@ -149,9 +150,11 @@ private slots:
149150
const quint16 port{20002};
150151

151152
void send(const QString& data);
152-
QString recv();
153+
// QString recv();
153154
void create_db();
154155

156+
void recv_data_handler(const QString& data);
157+
155158
void send_auth(const QString& login, const QString& password);
156159
void send_test_result(const QString& login, const QString& password);
157160
void send_patterns_request();
@@ -176,5 +179,6 @@ private slots:
176179
std::vector<result_test_t> get_result_test_info();
177180
std::vector<QString> get_patterns_name();
178181
pattern_info_t get_pattern_info(QString name);
182+
int get_count_question(QString name);
179183
};
180184
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)