forked from changfeng1050/SerialWizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertDataDialog.cpp
More file actions
171 lines (140 loc) · 5.24 KB
/
ConvertDataDialog.cpp
File metadata and controls
171 lines (140 loc) · 5.24 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// Created by chang on 2017-07-31.
//
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QtWidgets/QHBoxLayout>
#include <QDebug>
#include <QCheckBox>
#include <QComboBox>
#include <QPushButton>
#include <QTextEdit>
#include "ConvertDataDialog.h"
#include "ConvertDataDialog.h"
#include "global.h"
const QString GBK = "GBK";
const QString UTF8 = "UTF8";
ConvertDataDialog::ConvertDataDialog(QWidget *parent, Qt::WindowFlags f) {
createUi();
createConnect();
}
void ConvertDataDialog::createUi() {
inputTextEdit = new QTextEdit(this);
inputTextEdit->setMinimumSize(600, 200);
hexCheckBox = new QCheckBox(tr("HEX"), this);
hexCheckBox->setChecked(true);
fromCodecComboBox = new QComboBox(this);
fromCodecComboBox->addItems(QStringList() << UTF8 << GBK);
exchangeCodecButton = new QPushButton(tr("<<=>>"), this);
toCodecComboBox = new QComboBox(this);
toCodecComboBox->addItems(QStringList() << GBK << UTF8);
parseButton = new QPushButton(tr("解析"), this);
convertButton = new QPushButton(tr("转换"), this);
auto convertLayout = new QHBoxLayout;
convertLayout->addWidget(hexCheckBox);
convertLayout->addStretch();
convertLayout->addWidget(fromCodecComboBox);
convertLayout->addWidget(exchangeCodecButton);
convertLayout->addWidget(toCodecComboBox);
convertLayout->addStretch();
convertLayout->addWidget(parseButton);
convertLayout->addWidget(convertButton);
resultTextEdit = new QTextEdit(this);
resultTextEdit->setMinimumSize(600, 200);
auto layout = new QVBoxLayout;
layout->addWidget(inputTextEdit);
layout->addLayout(convertLayout);
layout->addWidget(resultTextEdit);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(layout);
setWindowTitle(tr("数据转换"));
}
void ConvertDataDialog::createConnect() {
connect(exchangeCodecButton, &QPushButton::clicked, [this] {
auto fromCodec = fromCodecComboBox->currentText();
auto toCodec = toCodecComboBox->currentText();
fromCodecComboBox->setCurrentText(toCodec);
toCodecComboBox->setCurrentText(fromCodec);
});
connect(parseButton, &QPushButton::clicked, [this] {
auto inputText = inputTextEdit->toPlainText();
auto hex = hexCheckBox->isChecked();
auto fromCodec = fromCodecComboBox->currentText();
auto toCodec = toCodecComboBox->currentText();
if (inputText.isEmpty()) {
showWarning("", QString(tr("请输入内容!")));
return;
}
if (hex) {
auto byteArray = dataFromHex(inputText);
QString resultText;
if (fromCodec == GBK) {
resultText = fromGbk(byteArray);
} else if (fromCodec == UTF8) {
resultText = fromUtf8(byteArray);
}
if (resultText.isEmpty()) {
showWarning("", QString(tr("请检查输入数据是否有误!")));
return;
}
resultTextEdit->setText(resultText);
} else {
const auto &text = inputText;
QString resultText;
if (fromCodec == GBK) {
resultText = toGbkByteArray(text).toHex(' ').toUpper();
} else {
resultText = toUtf8ByteArray(text).toHex(' ').toUpper();
}
if (resultText.isEmpty()) {
showWarning("", QString(tr("请检查输入数据是否有误!")));
return;
}
resultTextEdit->setText(resultText);
}
});
connect(convertButton, &QPushButton::clicked, [this] {
auto inputText = inputTextEdit->toPlainText();
auto hex = hexCheckBox->isChecked();
auto fromCodec = fromCodecComboBox->currentText();
auto toCodec = toCodecComboBox->currentText();
if (hex) {
auto byteArray = dataFromHex(inputText);
QString text;
if (fromCodec == GBK) {
text = fromGbk(byteArray);
} else if (fromCodec == UTF8) {
text = fromUtf8(byteArray);
}
if (text.isEmpty()) {
showWarning("", QString(tr("请检查输入数据是否有误!")));
return;
}
QString convertedText;
if (toCodec == GBK) {
convertedText = toGbkByteArray(text).toHex(' ').toUpper();
} else {
convertedText = toUtf8ByteArray(text).toHex(' ').toUpper();
}
if (convertedText.isEmpty()) {
showWarning("", QString(tr("请检查输入数据是否有误!")));
return;
}
resultTextEdit->setText(convertedText);
} else {
const auto &text = inputText;
QString resultText;
if (toCodec == GBK) {
resultText = toGbkByteArray(text).toHex(' ').toUpper();
} else {
resultText = toUtf8ByteArray(text).toHex(' ').toUpper();
}
if (resultText.isEmpty()) {
showWarning("", QString(tr("请检查输入数据是否有误!")));
return;
}
resultTextEdit->setText(resultText);
}
});
}