Skip to content

Commit c8b7217

Browse files
committed
work on the client
1 parent 482bad7 commit c8b7217

File tree

6 files changed

+412
-13
lines changed

6 files changed

+412
-13
lines changed

client/default.png

2.14 KB
Loading

client/mainwindow.cpp

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ MainWindow::MainWindow(QWidget *parent)
3636
{
3737
qDebug() << this->db.lastError().text();
3838
}
39+
40+
this->fill_personal_area();
3941
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
4042
}
4143
}
@@ -98,6 +100,7 @@ void MainWindow::slotReadyRead()
98100
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
99101
this->create_db();
100102
this->fill_db_login(doc.FirstChildElement("body"));
103+
this->fill_personal_area();
101104
}
102105
else
103106
{
@@ -112,6 +115,11 @@ void MainWindow::slotReadyRead()
112115
{
113116
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
114117
this->create_db();
118+
this->fill_db_register();
119+
this->fill_personal_area();
120+
121+
this->base64_file = "";
122+
this->file_type = "";
115123
}
116124
else
117125
{
@@ -200,7 +208,7 @@ void MainWindow::create_db()
200208
"name VARCHAR(45) NOT NULL UNIQUE, "
201209
"description TEXT NOT NULL, "
202210
"code TEXT NOT NULL, "
203-
"path_to_image VARCHAR(45) NOT NULL); ");
211+
"path_to_image VARCHAR(256) NOT NULL); ");
204212
query.exec("CREATE TABLE Pattern_test ( "
205213
"id_pattern INTEGER NOT NULL, "
206214
"question TEXT NOT NULL, "
@@ -213,10 +221,10 @@ void MainWindow::create_db()
213221
query.exec("CREATE TABLE User ( "
214222
"login VARCHAR(45) NOT NULL UNIQUE, "
215223
"password VARCHAR(45) NOT NULL UNIQUE, "
216-
"path_to_image VARCHAR(45) NOT NULL UNIQUE); " );
224+
"path_to_image VARCHAR(256) NOT NULL UNIQUE);" );
217225
query.exec("CREATE TABLE User_test ( "
218226
"pattern VARCHAR(45) NOT NULL UNIQUE, "
219-
"count_corrent INTEGER NOT NULL); " );
227+
"count_corrent INTEGER NOT NULL); " );
220228
}
221229

222230
void MainWindow::fill_db_login(tinyxml2::XMLElement *body)
@@ -247,11 +255,23 @@ void MainWindow::fill_db_login(tinyxml2::XMLElement *body)
247255
}
248256
}
249257

258+
void MainWindow::fill_db_register()
259+
{
260+
QString login {this->ui->reg_login->text()};
261+
QString password {this->ui->reg_pas->text()};
262+
//qDebug() << "in reg: " + this->file_type;
263+
QString path_to_image {QDir::currentPath() + "/images/user." + this->file_type};
264+
265+
this->save_img_to_file(path_to_image, this->base64_file);
266+
267+
QSqlQuery insert;
268+
insert.exec("INSERT INTO User (login, password, path_to_image) VALUES ('" + login + "', '" + password + "', '" + path_to_image + "')");
269+
}
270+
250271
void MainWindow::save_img_to_file(const QString &path, const QString &img)
251272
{
252273
std::ofstream tmp_file(path.toUtf8());
253274
tmp_file.close();
254-
qDebug() << path;
255275

256276
std::ofstream file(path.toUtf8(), std::ios::out | std::ios::binary);
257277
auto data{ base64_decode(img.toStdString()) };
@@ -262,6 +282,50 @@ void MainWindow::save_img_to_file(const QString &path, const QString &img)
262282
file.close();
263283
}
264284

285+
void MainWindow::fill_personal_area()
286+
{
287+
user_info_t info{this->get_user_info()};
288+
289+
this->pic = QPixmap();
290+
this->pic.load(info.path_to_image);
291+
292+
//this->pic = this->pic.scaled(QSize(256,256), Qt::AspectRatioMode(), Qt::SmoothTransformation);
293+
this->ui->img_profile->setPixmap(this->pic);
294+
this->ui->show_login->setText("Логин: " + info.login);
295+
}
296+
297+
user_info_t MainWindow::get_user_info()
298+
{
299+
QSqlQuery query;
300+
query.exec("SELECT login, password, path_to_image FROM User");
301+
QString login;
302+
QString path_to_file;
303+
QString password;
304+
while (query.next())
305+
{
306+
login = query.value(0).toString();
307+
password = query.value(1).toString();
308+
path_to_file = query.value(2).toString();
309+
}
310+
311+
return {login, password, path_to_file};
312+
}
313+
314+
std::vector<result_test_t> MainWindow::get_result_test_info()
315+
{
316+
std::vector<result_test_t> res;
317+
QSqlQuery query;
318+
query.exec("SELECT pattern, count_corrent FROM User_test");
319+
while (query.next())
320+
{
321+
result_test_t tmp;
322+
tmp.name = query.value(0).toString();
323+
tmp.result = query.value(1).toString();
324+
res.push_back(std::move(tmp));
325+
}
326+
return res;
327+
}
328+
265329
void MainWindow::on_to_register_clicked()
266330
{
267331
this->ui->screen_stacked->setCurrentIndex((int)screen::registration);
@@ -309,12 +373,7 @@ void MainWindow::on_register_2_clicked()
309373
tinyxml2::XMLPrinter printer;
310374
doc.Print(&printer);
311375

312-
//qDebug() << printer.CStr();
313-
314376
this->send(printer.CStr());
315-
316-
this->base64_file = "";
317-
this->file_type = "";
318377
}
319378

320379
void MainWindow::on_reg_img_clicked()
@@ -330,3 +389,34 @@ void MainWindow::on_reg_img_clicked()
330389
this->base64_file = base64_encode(data).c_str();
331390
this->file_type = fileName.split(".").back();
332391
}
392+
393+
void MainWindow::on_quit_clicked()
394+
{
395+
this->socket.reset(new QTcpSocket());
396+
socket->connectToHost(this->host, this->port);
397+
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
398+
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
399+
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
400+
401+
this->ui->screen_stacked->setCurrentIndex((int)screen::login);
402+
this->forward = type_forward::login;
403+
404+
this->db.close();
405+
QFile::remove(QDir::currentPath() + "/main.db");
406+
QDir image_dir(QDir::currentPath() + "/images");
407+
image_dir.removeRecursively();
408+
QDir dir(QDir::currentPath());
409+
dir.mkdir("images");
410+
this->ui->login_2->setEnabled(false);
411+
this->ui->register_2->setEnabled(false);
412+
}
413+
414+
void MainWindow::on_to_result_test_clicked()
415+
{
416+
this->ui->screen_stacked->setCurrentIndex((int)screen::result_test);
417+
}
418+
419+
void MainWindow::on_to_personal_area_clicked()
420+
{
421+
this->ui->screen_stacked->setCurrentIndex((int)screen::personal_area);
422+
}

client/mainwindow.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <QTcpSocket>
99
#include <QFileDialog>
1010
#include <QtSql>
11+
#include <QPixmap>
12+
#include <QDir>
1113

1214
#include <memory>
1315
#include <vector>
@@ -21,6 +23,19 @@ QT_BEGIN_NAMESPACE
2123
namespace Ui { class MainWindow; }
2224
QT_END_NAMESPACE
2325

26+
struct user_info_t
27+
{
28+
QString login;
29+
QString password;
30+
QString path_to_image;
31+
};
32+
33+
struct result_test_t
34+
{
35+
QString name;
36+
QString result;
37+
};
38+
2439
class MainWindow : public QMainWindow
2540
{
2641
Q_OBJECT
@@ -44,13 +59,20 @@ private slots:
4459

4560
void on_reg_img_clicked();
4661

62+
void on_quit_clicked();
63+
64+
void on_to_result_test_clicked();
65+
66+
void on_to_personal_area_clicked();
67+
4768
private:
4869
enum class screen
4970
{
5071
login = 0,
5172
registration = 1,
5273
personal_area = 2,
53-
patterns = 3
74+
patterns = 3,
75+
result_test = 4
5476
};
5577
enum class type_forward
5678
{
@@ -60,6 +82,7 @@ private slots:
6082

6183
Ui::MainWindow *ui;
6284
Popup *popup;
85+
QPixmap pic;
6386
QSqlDatabase db;
6487
type_forward forward;
6588
QString base64_file;
@@ -74,6 +97,13 @@ private slots:
7497
void create_db();
7598

7699
void fill_db_login(tinyxml2::XMLElement* body);
100+
void fill_db_register();
101+
void fill_personal_area();
102+
77103
void save_img_to_file(const QString& path, const QString& img);
104+
105+
/*выборка из базы данных*/
106+
user_info_t get_user_info();
107+
std::vector<result_test_t> get_result_test_info();
78108
};
79109
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)