Skip to content

Commit 18f1b5c

Browse files
committed
add settings: url and port
1 parent 32206dc commit 18f1b5c

File tree

7 files changed

+792
-87
lines changed

7 files changed

+792
-87
lines changed

client/connection.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

client/mainwindow.cpp

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ MainWindow::MainWindow(QWidget *parent)
1818
this->validator = new QRegularExpressionValidator(QRegularExpression("[A-Za-z0-9_]+"), this);
1919
this->ui->reg_login->setValidator(this->validator);
2020
this->ui->reg_pas->setValidator(this->validator);
21-
QFile file(":/connection.txt");
21+
22+
if (!fileExists(QDir::currentPath() + "/connection.txt"))
23+
{
24+
QFile file("connection.txt");
25+
file.open(QIODevice::WriteOnly | QIODevice::Text);
26+
file.write("localhost\n");
27+
file.write("20002");
28+
file.close();
29+
}
30+
QFile file("connection.txt");
2231
file.open(QIODevice::ReadOnly | QIODevice::Text);
2332
this->host = file.readLine();
24-
this->host = this->host.sliced(0, this->host.size() - 1);
33+
this->host = this->host.simplified();
2534
this->port = file.readLine().toInt();
35+
file.close();
2636

2737
QDir dir1(QDir::currentPath());
2838
dir1.mkdir("images");
@@ -40,6 +50,7 @@ MainWindow::MainWindow(QWidget *parent)
4050
this->ui->screen_stacked->setCurrentIndex((int)screen::login);
4151
this->forward = type_forward::login;
4252

53+
this->is_auth = false;
4354
this->socket.reset(new QTcpSocket());
4455
socket->connectToHost(this->host, this->port);
4556
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
@@ -48,6 +59,7 @@ MainWindow::MainWindow(QWidget *parent)
4859
}
4960
else
5061
{
62+
this->is_auth = true;
5163
this->db = QSqlDatabase::addDatabase("QSQLITE");
5264

5365

@@ -66,6 +78,12 @@ MainWindow::MainWindow(QWidget *parent)
6678

6779
MainWindow::~MainWindow()
6880
{
81+
QFile file("connection.txt");
82+
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
83+
qDebug() << "not open to write!";
84+
file.write(QString(this->host + "\n").toUtf8());
85+
file.write(QString::number(this->port).toUtf8());
86+
file.close();
6987
delete ui;
7088
}
7189

@@ -228,6 +246,9 @@ void MainWindow::recv_data_handler(const QString &data)
228246
this->create_db();
229247
this->fill_db_login(doc.FirstChildElement("body"));
230248
this->fill_personal_area();
249+
this->is_auth = true;
250+
251+
this->socket.reset();
231252
}
232253
else
233254
{
@@ -248,6 +269,8 @@ void MainWindow::recv_data_handler(const QString &data)
248269

249270
this->base64_file = "";
250271
this->file_type = "";
272+
this->is_auth = true;
273+
this->socket.reset();
251274
}
252275
else
253276
{
@@ -258,13 +281,6 @@ void MainWindow::recv_data_handler(const QString &data)
258281

259282
case (type_forward::load_result):
260283
{
261-
if (!data.size())
262-
{
263-
this->popup->set_title("Успешно");
264-
this->popup->set_description("Данные успешно скачаны с сервера! Пройдено тестов: 0");
265-
this->popup->exec();
266-
break;
267-
}
268284
auto type = doc.FirstChildElement("body")->FirstChildElement("answer");
269285
if (type->GetText() == QString("correct"))
270286
{
@@ -274,6 +290,7 @@ void MainWindow::recv_data_handler(const QString &data)
274290
this->popup->set_title("Успешно");
275291
this->popup->set_description("Данные успешно скачаны с сервера!");
276292
this->popup->exec();
293+
this->socket.reset();
277294
}
278295
else
279296
{
@@ -292,6 +309,7 @@ void MainWindow::recv_data_handler(const QString &data)
292309
this->popup->set_title("Успешно");
293310
this->popup->set_description("Данные успешно загружены на сервер!");
294311
this->popup->exec();
312+
this->socket.reset();
295313
}
296314
else
297315
{
@@ -310,6 +328,7 @@ void MainWindow::recv_data_handler(const QString &data)
310328
this->popup->set_title("Успешно");
311329
this->popup->set_description("Список паттернов обновлен!");
312330
this->popup->exec();
331+
this->socket.reset();
313332
break;
314333
}
315334
}
@@ -956,6 +975,7 @@ void MainWindow::on_reg_img_clicked()
956975

957976
void MainWindow::on_quit_clicked()
958977
{
978+
this->is_auth = false;
959979
this->quit();
960980
}
961981

@@ -1017,3 +1037,43 @@ void MainWindow::on_to_pattern_list_clicked()
10171037
{
10181038
this->ui->screen_stacked->setCurrentIndex((int)screen::patterns);
10191039
}
1040+
1041+
void MainWindow::on_to_settings_clicked()
1042+
{
1043+
this->ui->screen_stacked->setCurrentIndex((int)screen::settings);
1044+
1045+
this->ui->adress->setText(this->host);
1046+
this->ui->port->setValue(this->port);
1047+
}
1048+
1049+
void MainWindow::on_to_back_clicked()
1050+
{
1051+
this->ui->screen_stacked->setCurrentIndex((int)(this->is_auth?screen::personal_area : screen::login));
1052+
}
1053+
1054+
void MainWindow::on_settings_save_clicked()
1055+
{
1056+
this->host = this->ui->adress->text();
1057+
this->port = this->ui->port->value();
1058+
1059+
if (this->socket)
1060+
{
1061+
this->ui->login_2->setEnabled(false);
1062+
this->ui->register_2->setEnabled(false);
1063+
this->socket.reset(new QTcpSocket());
1064+
socket->connectToHost(this->host, this->port);
1065+
connect(socket.get(), SIGNAL(connected()), SLOT(slotConnected()));
1066+
connect(socket.get(), SIGNAL(readyRead()), SLOT(slotReadyRead()));
1067+
connect(socket.get(), SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError)));
1068+
}
1069+
}
1070+
1071+
void MainWindow::on_to_settings_register_clicked()
1072+
{
1073+
this->on_to_settings_clicked();
1074+
}
1075+
1076+
void MainWindow::on_to_settings_login_clicked()
1077+
{
1078+
this->on_to_settings_clicked();
1079+
}

client/mainwindow.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ private slots:
102102

103103
void on_to_pattern_list_clicked();
104104

105+
void on_to_settings_clicked();
106+
107+
void on_to_back_clicked();
108+
109+
void on_settings_save_clicked();
110+
111+
void on_to_settings_register_clicked();
112+
113+
void on_to_settings_login_clicked();
114+
105115
private:
106116
enum class screen
107117
{
@@ -111,7 +121,8 @@ private slots:
111121
patterns = 3,
112122
result_test = 4,
113123
more_pattern = 5,
114-
test_pattern = 6
124+
test_pattern = 6,
125+
settings = 7
115126
};
116127
enum class type_forward
117128
{
@@ -148,6 +159,7 @@ private slots:
148159
std::unique_ptr<QTcpSocket> socket;
149160
QString host;
150161
quint16 port;
162+
bool is_auth{true};
151163

152164
void send(const QString& data);
153165
// QString recv();

0 commit comments

Comments
 (0)