-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconnectdialogc.cpp
More file actions
74 lines (55 loc) · 2.65 KB
/
connectdialogc.cpp
File metadata and controls
74 lines (55 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "connectdialogc.h"
#include "ui_connectdialog.h"
#include "MainApplication.h"
#include <QDialog>
#include <QString>
#include <QRegExpValidator>
#include <QPushButton>
#include <QMessageBox>
ConnectDialogClass::ConnectDialogClass(QWidget* parent):
QDialog(parent),
ui_dialog(new Ui::ConnectDialogClass)
{
ui_dialog->setupUi(this);
// Set the IP address validator
QLineEdit* ipLine = ui_dialog->ipLineEdit;
QString ipAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$";
QRegExpValidator* ipAddressValidator = new QRegExpValidator(QRegExp(ipAddressRegex), this);
ipLine->setValidator(ipAddressValidator);
ipLine->setCursorPosition(0);
// Set the port validator
QLineEdit* portLine = ui_dialog->portLineEdit;
QString portRegex = "^[0-9]{5}$";
QRegExpValidator* portValidator = new QRegExpValidator(QRegExp(portRegex), this);
portLine->setValidator(portValidator);
portLine->setCursorPosition(0);
// Set the default radio button
QRadioButton *serverRadio = ui_dialog->serverRadioButton;
serverRadio->setChecked(true);
// Connect the connect button to the connectPressed slot
connect(ui_dialog->connectButton, &QPushButton::clicked, this, &ConnectDialogClass::connectPressed);
}
ConnectDialogClass::~ConnectDialogClass()
{
delete ui_dialog;
}
void ConnectDialogClass::connectPressed() {
// Check if the server or client radio button is checked
bool isServer = ui_dialog->serverRadioButton->isChecked();
// Get the IP address and port number from the dialog
quint16 portAddress = ui_dialog->portLineEdit->text().toUInt();
QString ipAddress = ui_dialog->ipLineEdit->text();
// Check if the IP address and port number are valid
QRegularExpression regex("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$");
QRegularExpressionMatch matchip = regex.match(ipAddress);
if (matchip.hasMatch() && portAddress <= 65535 && portAddress >= 49152) {
(isServer) ? emit implementServer(ipAddress, portAddress) :emit implementClient(ipAddress, portAddress);
close();
}
else if(!matchip.hasMatch()){
QMessageBox::information(this, "Invalid IP address", "The IP address entered is in the wrong format. Please enter a valid IP address.");
}
else if (portAddress > 65535 || portAddress < 49152) {
QMessageBox::information(this, "Invalid Port number", "The port address entered is wrong. Please enter a port number between 49152 & 65535");
}
}