-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdialog.cpp
More file actions
151 lines (119 loc) · 3.94 KB
/
Copy pathdialog.cpp
File metadata and controls
151 lines (119 loc) · 3.94 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
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
#include <QMessageBox>
#include "server.h"
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog),
m_timer(new QTimer(this)),
m_server(new Server(this)),
m_count(0)
{
ui->setupUi(this);
setWindowFlags(this->windowFlags() | Qt::WindowMinMaxButtonsHint);
ui->btnStopLoop->setDisabled(true);
ui->labelNum->setText(QString("%1").arg(m_count));
m_server = new Server(this);
m_server->listen(QHostAddress::Any, 8712);
connect(ui->btnSend, SIGNAL(clicked()), this, SLOT(sendDataSlot()));
connect(ui->btnClear, SIGNAL(clicked()), this, SLOT(clearData()));
connect(m_timer, SIGNAL(timeout()), this, SLOT(startLoopSend()));
connect(ui->btnStopLoop, SIGNAL(clicked(bool)), this, SLOT(stopLoopSend()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showConnection(int sockDesc)
{
m_count++;
/* add socket object that join in */
ui->comboBoxObj->addItem(QString("%1").arg(sockDesc), sockDesc);
/* change connect number while connection is connecting */
ui->labelNum->setText(QString("%1").arg(m_count));
}
void Dialog::showDisconnection(int sockDesc)
{
m_count--;
/* refresh combobox */
ui->comboBoxObj->clear();
int index = ui->comboBoxObj->findData(sockDesc);
ui->comboBoxObj->removeItem(index);
/* change connect number while connection is disconnecting */
ui->labelNum->setText(QString("%1").arg(m_count));
}
void Dialog::sendHexData(void)
{
QString allData = ui->lineEditMsg->text();
QByteArray data;
QStringList list = allData.split(" ");
for (const QString &hex : list) {
data.append(static_cast<char>(hex.toInt(Q_NULLPTR, 16)));
}
emit sendData(ui->comboBoxObj->currentText().toInt(), data);
}
void Dialog::sendDataSlot(void)
{
/* if send message is null return */
if (ui->lineEditMsg->text().isEmpty()) {
QMessageBox::information(Q_NULLPTR, QString("注意"), QString("发送内容不能为空!"), QMessageBox::Yes);
return ;
}
if (ui->checkBoxLoop->isChecked()) {
int time_period = ui->lineEditLoopTime->text().toInt();
m_timer->setInterval(time_period);
m_timer->start();
ui->btnSend->setDisabled(true);
ui->btnStopLoop->setDisabled(false);
return;
}
/* whether send hex data */
if (ui->checkBoxHex->isChecked()) {
sendHexData();
} else {
/* send original data */
emit sendData(ui->comboBoxObj->currentText().toInt(), ui->lineEditMsg->text().toLocal8Bit());
}
ui->lineEditMsg->setText("");
}
void Dialog::recvData(const QString &ip, const QByteArray &data)
{
QString msg;
/* add ip address string to displaying string */
if (!ui->checkBoxHideIP->isChecked()) {
msg += ip + QString(": ");
}
/* choose data format to display */
if (ui->checkBoxHex->isChecked()) {
QString dataString;
for (int i = 0; i < data.size(); i++) {
dataString += QString("0x%1 ").arg(static_cast<quint8>(data.at(i)), 2, 16, QChar('0'));
}
msg += dataString + "\n";
} else {
msg += QString::fromLocal8Bit(data) + "\n";
}
/* send back data */
if (ui->checkBoxCycle->isChecked()) {
emit sendData(ui->comboBoxObj->currentText().toInt(), data);
}
ui->textBrowser->append(msg);
}
void Dialog::clearData(void)
{
ui->textBrowser->clear();
}
void Dialog::startLoopSend(void)
{
if (ui->checkBoxHex->isChecked()) {
sendHexData();
} else {
emit sendData(ui->comboBoxObj->currentText().toInt(), ui->lineEditMsg->text().toLocal8Bit());
}
}
void Dialog::stopLoopSend(void)
{
m_timer->stop();
ui->btnSend->setDisabled(false);
ui->btnStopLoop->setDisabled(true);
}