2
2
#include < QtNetwork>
3
3
4
4
#include < stdlib.h>
5
-
5
+ # include < QDebug >
6
6
#include " dialog.h"
7
7
#include " server.h"
8
8
9
9
Dialog::Dialog (QWidget *parent)
10
10
: QWidget(parent)
11
11
{
12
+ hostCombo = new QComboBox;
13
+ portLineEdit = new QLineEdit ();
14
+ setIpPortButton = new QPushButton (tr (" Set Interface" ));
15
+
12
16
messageLabel = new QLabel;
13
17
messageLabel->setWordWrap (true );
14
18
statusLabel = new QLabel;
@@ -18,51 +22,62 @@ Dialog::Dialog(QWidget *parent)
18
22
dqButton = new QPushButton (tr (" Save Data" ));
19
23
dqButton->setAutoDefault (false );
20
24
dqButton->setCheckable (true );
25
+ hostLabel = new QLabel (tr (" Network Interface:" ));
26
+ hostLabel->setBuddy (hostCombo);
27
+ portLabel = new QLabel (tr (" Server port:" ));
28
+ portLabel->setBuddy (portLineEdit);
21
29
22
- QString ipAddress;
30
+ hostCombo->setEditable (true );
31
+ // find out name of this machine
32
+ QString name = QHostInfo::localHostName ();
33
+
34
+ if (name != QLatin1String (" localhost" ))
35
+ hostCombo->addItem (QString (" localhost" ));
36
+ // find out IP addresses of this machine
23
37
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses ();
24
- // use the first non-localhost IPv4 address
38
+ // add non-localhost addresses
25
39
for (int i = 0 ; i < ipAddressesList.size (); ++i) {
26
40
if (ipAddressesList.at (i) != QHostAddress::LocalHost &&
27
41
ipAddressesList.at (i).toIPv4Address ()) {
28
- ipAddress = ipAddressesList.at (i).toString ();
29
- break ;
42
+ hostCombo->addItem (ipAddressesList.at (i).toString ());
30
43
}
31
- }
32
-
33
- QHostAddress ipAddressQ (ipAddress);
34
44
35
-
36
- if (!server.listen (ipAddressQ, 50005 )) {
37
- QMessageBox::critical (this , tr (" \u03B7 Net Server" ),
38
- tr (" Unable to start the server: %1." )
39
- .arg (server.errorString ()));
40
- this ->close ();
41
- return ;
42
45
}
46
+ // add localhost addresses
47
+ for (int i = 0 ; i < ipAddressesList.size (); ++i) {
48
+ if (ipAddressesList.at (i).isLoopback ())
49
+ hostCombo->addItem (ipAddressesList.at (i).toString ());
50
+ }
51
+ portLineEdit->setText (" 50005" );
52
+ setIpPortButton->setEnabled (!hostCombo->currentText ().isEmpty () &&
53
+ !portLineEdit->text ().isEmpty ());
43
54
44
- // if we did not find one, use IPv4 localhost
45
- if (ipAddress.isEmpty ())
46
- ipAddress = QHostAddress (QHostAddress::LocalHost).toString ();
47
- statusLabel->setText (tr (" The \u03B7 Net server is running on\n\n IP: %1\n port: %2\n\n "
48
- " Run the \u03B7 Net Clients now." )
49
- .arg (ipAddress).arg (server.serverPort ()));
50
- messageLabel->setText (tr (" Wait for data from \u03B7 Net clients..." ));
55
+ statusLabel->setText (tr (" The \u03B7 Net server ready to start." ));
56
+ messageLabel->setText (tr (" Please select the network interface." ));
51
57
52
- connect (quitButton, SIGNAL (clicked ()), this , SLOT (close ()));
58
+ connect (quitButton, &QAbstractButton::clicked, this , &Dialog::close );
59
+ connect (hostCombo, &QComboBox::currentTextChanged, this , &Dialog::networkInterface);
60
+ connect (setIpPortButton, &QAbstractButton::clicked, this , &Dialog::setNetworkInterface);
61
+ connect (portLineEdit, &QLineEdit::textChanged, this , &Dialog::networkInterface);
53
62
54
63
QHBoxLayout *buttonLayout = new QHBoxLayout;
55
64
buttonLayout->addStretch (1 );
65
+ buttonLayout->addWidget (setIpPortButton);
56
66
buttonLayout->addWidget (dqButton);
57
67
buttonLayout->addWidget (quitButton);
58
68
buttonLayout->addStretch (1 );
59
69
60
70
QVBoxLayout *mainLayout = new QVBoxLayout;
71
+ // QGridLayout *mainLayout = new QGridLayout;
61
72
mainLayout->addWidget (statusLabel);
62
73
mainLayout->addWidget (messageLabel);
74
+ mainLayout->addWidget (hostLabel);
75
+ mainLayout->addWidget (hostCombo);
76
+ mainLayout->addWidget (portLabel);
77
+ mainLayout->addWidget (portLineEdit);
63
78
mainLayout->addLayout (buttonLayout);
64
79
setLayout (mainLayout);
65
- setWindowTitle (tr (" \u03B7 Net Server" ));
80
+ setWindowTitle (tr (" \u03B7 Net Server v0.9.4 " ));
66
81
}
67
82
68
83
Dialog& Dialog::getInstance ()
@@ -81,3 +96,59 @@ bool Dialog::getDQButtonState(void)
81
96
qDebug () << " ButtonState: " << dqButton->isChecked ();
82
97
return dqButton->isChecked ();
83
98
}
99
+
100
+ void Dialog::networkInterface ()
101
+ {
102
+ if (!this ->server .isListening ())
103
+ {
104
+ setIpPortButton->setEnabled (!hostCombo->currentText ().isEmpty () &&
105
+ !portLineEdit->text ().isEmpty ());
106
+
107
+ }
108
+
109
+ }
110
+
111
+ void Dialog::setNetworkInterface ()
112
+ {
113
+ setIpPortButton->setEnabled (false );
114
+ hostCombo->setVisible (false );
115
+ portLineEdit->setVisible (false );
116
+ portLabel->setVisible (false );
117
+ hostLabel->setVisible (false );
118
+
119
+
120
+
121
+ QString ipAddress;
122
+
123
+ ipAddress = hostCombo->currentText ();
124
+ qDebug () << ipAddress;
125
+ QHostAddress ipAddressQ (ipAddress);
126
+
127
+
128
+ if (!this ->server .listen (ipAddressQ, portLineEdit->text ().toInt ())) {
129
+ QMessageBox::critical (this , tr (" \u03B7 Net Server" ),
130
+ tr (" Unable to start the server: %1." )
131
+ .arg (this ->server .errorString ()));
132
+ this ->close ();
133
+ return ;
134
+ }
135
+
136
+ // // if we did not find one, use IPv4 localhost
137
+ // if (ipAddress.isEmpty())
138
+ // ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
139
+ statusLabel->setText (tr (" The \u03B7 Net server is running on\n\n IP: %1\n port: %2\n\n "
140
+ " Run the \u03B7 Net Clients now." )
141
+ .arg (ipAddress).arg (server.serverPort ()));
142
+ messageLabel->setText (tr (" Wait for data from \u03B7 Net clients..." ));
143
+ }
144
+
145
+ void Dialog::close ()
146
+ {
147
+ hostCombo->deleteLater ();
148
+ portLineEdit->deleteLater ();
149
+ hostLabel->deleteLater ();
150
+ portLabel->deleteLater ();
151
+ QWidget::close ();
152
+ }
153
+
154
+
0 commit comments