forked from kangear/NetAssistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
315 lines (273 loc) · 9.4 KB
/
mainwindow.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUdpSocket>
#include "udpclient.h"
#include <QLabel>
#include <QDate>
#include "commonhelper.h"
#include <QSettings>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
qDebug("%s", __func__);
ui->setupUi(this);
/* 读取配置文件 */
doSettings(false);
/* 设置默认通讯模式 */
ui->tcpclient_radioButton->setChecked(true);
/** 目前设置为UDP为默认方式 */
// ui->udp_radioButton->setChecked(true);
/** 设置远程主机IP地址 获取本机IP */
ui->remoteIP_lineEdit->setText(mRemoteIp);
/* 设置远程端口号 */
/* TODO: 将其设置为不能以0开头 */
ui->remoteport_spinBox->setRange(1024,99999);
ui->remoteport_spinBox->setValue(mRemotePort);
/* 设置本地端口号 */
ui->localport_spinBox->setRange(1024,99999);
ui->localport_spinBox->setValue(mLocalPort);
isConnect = false;
// 状态栏
statusLabel = new QLabel;
statusLabel->setMinimumSize(260, 20); // 设置标签最小大小
statusLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
statusLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影
ui->statusBar->addWidget(statusLabel);
statusLabel->setText("UDP通信停止");
statusLabel->setAlignment(Qt::AlignHCenter);
// 接收数量
receiveLabel = new QLabel;
receiveLabel->setMinimumSize(150, 20); // 设置标签最小大小
receiveLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
receiveLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影
ui->statusBar->addWidget(receiveLabel);
receiveLabel->setAlignment(Qt::AlignHCenter);
// 发送数量
sendLabel = new QLabel;
sendLabel->setMinimumSize(150, 20); // 设置标签最小大小
sendLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
sendLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影
ui->statusBar->addWidget(sendLabel);
sendLabel->setAlignment(Qt::AlignHCenter);
updateStateBar(QString(), 0, 0);
// 计数器清零 button
clearCounterButton = new QPushButton();
ui->statusBar->addWidget(clearCounterButton);
clearCounterButton->setText(tr("计数器清零"));
connect(clearCounterButton, SIGNAL(released()), this, SLOT(on_clearCounter_pushButton_released()));
// 时间 TODO:要进行更新
timeLabel = new QLabel;
timeLabel->setMinimumSize(90, 20); // 设置标签最小大小
timeLabel->setMaximumWidth(90);
timeLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
timeLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影
ui->statusBar->addWidget(timeLabel);
timeLabel->setText(QDate::currentDate().toString("yyyy-MM-dd"));
// 更新接收到的数据
connect(&client, SIGNAL(valueChanged(QString)), this, SLOT(updateReceiveText(QString)));
connect(&client,
SIGNAL(updateState(QString, QVariant, QVariant)),
this, SLOT(updateStateBar(QString, QVariant, QVariant)));
// tcp client
tcpClient = new QTcpSocket(this);
connect(tcpClient, &QTcpSocket::readyRead, this, &MainWindow::tcpReadyRead);
init();
mReceiveNum = mSendNum = 0;
}
void MainWindow::connectNet()
{
qDebug("%s", __func__);
mRemoteIp = ui->remoteIP_lineEdit->text();
mRemotePort = ui->remoteport_spinBox->text().toInt();
mLocalPort = ui->localport_spinBox->text().toInt();
updateStateBar("UDP通信 " + mRemoteIp + ":" + QString().number(mRemotePort),
QVariant(QVariant::Int), QVariant(QVariant::Int));
// No.1
isConnect = true;
// 将状态设置为 通
ui->state_label->setText("通");
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::blue);
ui->state_label->setPalette(pa);
// 将按钮设置为 断开网络
ui->connect_pushButton->setText("断开网络");
// 禁用远程端口,本地端口,远程IP
ui->remoteIP_lineEdit->setEnabled(false);
ui->remoteport_spinBox->setEnabled(false);
ui->localport_spinBox->setEnabled(false);
// 使能button
ui->handSend_pushButton->setEnabled(true);
if (ui->udp_radioButton->isChecked()){
client.udpStart(chelper.getLocalHostIP(), mLocalPort, QHostAddress(mRemoteIp), mRemotePort);
} else if (ui->tcpclient_radioButton->isChecked()){
tcpClient->abort();
tcpClient->connectToHost(QHostAddress(mRemoteIp), mRemotePort);
tcpClient->write("hello sansa~");
}
}
void MainWindow::updateReceiveText(const QString string)
{
QString oldString = ui->receive_textBrowser->toPlainText();
ui->receive_textBrowser->setText(oldString + string + "\n");
// 将光标移动到最后位置
QTextCursor tmpCursor = ui->receive_textBrowser->textCursor();
tmpCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor, 4);
ui->receive_textBrowser->setTextCursor(tmpCursor);
}
/**
* @brief MainWindow::updateStateBar
* @param state 状态
* @param inNum 接收数值
* @param outNum 发送数值
*/
void MainWindow::updateStateBar(QString state, QVariant inNum, QVariant outNum)
{
if(!state.isNull())
statusLabel->setText(state);
if(!inNum.isNull()) {
//累计
if(inNum.toInt() == 0)
mReceiveNum = 0;
else
mReceiveNum += inNum.toInt();
receiveLabel->setText("接收:" + QString::number(mReceiveNum));
}
if(!outNum.isNull()) {
//累计
if(outNum.toInt() == 0)
mSendNum = 0;
else
mSendNum += outNum.toInt();
sendLabel->setText("发送:" + QString::number(mSendNum));
}
}
/**
* 断开UDP时调用该函数
* @brief MainWindow::init
*/
void MainWindow::init()
{
qDebug("%s", __func__);
// No.1
isConnect = false;
// 将状态设置为 断
ui->state_label->setText("断");
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
ui->state_label->setPalette(pa);
// 将按钮设置为 连接网络
ui->connect_pushButton->setText("连接网络");
// 使能远程端口,本地端口,远程IP
ui->remoteIP_lineEdit->setEnabled(true);
ui->remoteport_spinBox->setEnabled(true);
ui->localport_spinBox->setEnabled(true);
// 禁用button
ui->handSend_pushButton->setEnabled(false);
//
client.udpStop(NULL, NULL, NULL);
tcpClient->abort();
updateStateBar("本地IP: " + chelper.getLocalHostIP().toString() + " 无连接",
QVariant(QVariant::Int), QVariant(QVariant::Int));
}
/**
* 断开UDP时调用该函数
* @brief MainWindow::disConnectNet
*/
void MainWindow::disConnectNet()
{
qDebug("%s", __func__);
// No.1
isConnect = false;
// 将状态设置为 断
ui->state_label->setText("断");
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
ui->state_label->setPalette(pa);
// 将按钮设置为 连接网络
ui->connect_pushButton->setText("连接网络");
// 使能远程端口,本地端口,远程IP
ui->remoteIP_lineEdit->setEnabled(true);
ui->remoteport_spinBox->setEnabled(true);
ui->localport_spinBox->setEnabled(true);
// 禁用button
ui->handSend_pushButton->setEnabled(false);
//
client.udpStop(NULL, NULL, NULL);
tcpClient->abort();
updateStateBar(tr("UDP通信停止"), QVariant(QVariant::Int), QVariant(QVariant::Int));
}
void MainWindow::doSettings(bool isWrite)
{
QSettings settings("Yzs_think", "Application");
const QString REMOTE_IP = "remoteip";
const QString REMOTE_PORT = "remoteport";
const QString LOCAL_PORT = "localport";
if(isWrite) {
settings.setValue(REMOTE_IP, mRemoteIp);
settings.setValue(REMOTE_PORT, mRemotePort);
settings.setValue(LOCAL_PORT, mLocalPort);
} else {
mRemoteIp = settings.value(REMOTE_IP, chelper.getLocalHostIP().toString()).toString();
mRemotePort = settings.value(REMOTE_PORT, 1234).toInt();
mLocalPort = settings.value(LOCAL_PORT, 2468).toInt();
}
}
MainWindow::~MainWindow()
{
doSettings(true);
delete ui;
}
/**
* @brief MainWindow::on_clearCounter_pushButton_released
* 该函数是将计数器置零
*/
void MainWindow::on_clearCounter_pushButton_released()
{
qDebug("%s", __func__);
updateStateBar(QString(), 0, 0);
}
void MainWindow::on_clearReceive_pushButton_released()
{
ui->receive_textBrowser->clear();
}
void MainWindow::on_clearSend_pushButton_released()
{
ui->send_plainTextEdit->clear();
}
/**
* @brief MainWindow::on_connect_pushButton_released
* 该函数是在连接/断开网络时调用
*/
void MainWindow::on_connect_pushButton_released()
{
qDebug("%s", __func__);
// 如果当前网络是连接状态 调用断开连接函数
if(isConnect) {
disConnectNet();
} else { // 否则调用连接函数
connectNet();
}
}
/**
* 当用户点击 发送 时调用该函数
* @brief MainWindow::on_handSend_pushButton_released
*/
void MainWindow::on_handSend_pushButton_released()
{
// 获取 rmeote ip/ port 和内容
QString string = ui->send_plainTextEdit->toPlainText();
if(string.length() != 0) {
client.sendData(string, mRemoteIp, mRemotePort);
}
}
void MainWindow::on_quit_pushButton_released()
{
QApplication::quit();
}
void MainWindow::tcpReadyRead()
{
QByteArray data = tcpClient->readAll();
QString string = QString::fromUtf8(data);
ui->receive_textBrowser->setText(string + "\n");
}