forked from changfeng1050/SerialWizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.cpp
More file actions
129 lines (103 loc) · 3.65 KB
/
global.cpp
File metadata and controls
129 lines (103 loc) · 3.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
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
//
// Created by chang on 2017-08-02.
//
#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QTime>
#include <QtWidgets/QWidget>
#include <QtWidgets/QMessageBox>
#include <QtNetwork/QHostInfo>
#include "global.h"
QTextCodec *gbk = QTextCodec::codecForName("GB18030");
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
QString utf82Gbk(const QString &inStr) {
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
// gbk->fromUnicode(utf8->toUnicode(inStr.toLatin1()));
return QString(gbk->fromUnicode(inStr));
// QString utf2gbk = gbk->toUnicode(inStr.toLocal8Bit());
// return utf2gbk;
}
//
//QString gbk2Utf8(const QString &inStr) {
// QTextCodec *gbk = QTextCodec::codecForName("GB18030");
// QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
//
// gbk->fromUnicode(utf8->toUnicode(inStr.toLatin1()));
//
// return QString(gbk->fromUnicode(inStr));
//}
QString fromUtf8(const QByteArray &data) {
qDebug() << "fromUtf8" << data.toHex();
return utf8->toUnicode(data);
}
QString fromGbk(const QByteArray &data) {
qDebug() << "fromGbk" << data.toHex();
return gbk->toUnicode(data);
}
QByteArray toGbkByteArray(const QString &text) {
qDebug() << "toGbkByteArray" << text;
return text.toLocal8Bit();
}
QByteArray toUtf8ByteArray(const QString &text) {
qDebug() << "toUtf8ByteArray" << text;
return text.toUtf8();
}
bool okToContinue(const QString &title, const QString &text, QWidget *parent) {
return QMessageBox::Yes == QMessageBox::warning(parent, title, text, QMessageBox::Yes | QMessageBox::No);
}
bool showQuestion(const QString &title, const QString &text, QWidget *parent) {
return QMessageBox::question(parent, title, text, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
}
void showError(const QString &title, const QString &text, QWidget *parent) {
return (void) QMessageBox::critical(parent, title, text, QMessageBox::Ok);
}
bool showWarning(const QString &title, const QString &text, QWidget *parent) {
return QMessageBox::warning(parent, title, text, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
}
void showMessage(const QString &title, const QString &text, QWidget *parent) {
return (void) QMessageBox::information(parent, title, text);
};
QString getTimestamp() {
auto time = QTime(QTime::currentTime());
return time.toString("hh:mm:ss.zzz");
}
QString getFileSuffix(const QString &filePath) {
auto index = filePath.lastIndexOf('.');
return filePath.right(filePath.count() - index - 1);
}
QString getFileDir(const QString &filePath) {
auto index = filePath.lastIndexOf('.');
return filePath.left(index + 1);
}
QString getIp() {
auto localHostName = QHostInfo::localHostName();
qDebug() << "local host name:" << localHostName;
auto ipAddress = QHostInfo::fromName(localHostName).addresses();
qDebug() << "ip address:" << ipAddress;
for (auto address:ipAddress) {
if (address.protocol() == QAbstractSocket::IPv4Protocol) {
return address.toString();
}
}
for (auto address:ipAddress) {
if (address.protocol() == QAbstractSocket::IPv6Protocol) {
return address.toString();
}
}
return "";
}
QString getAddressString(const QHostAddress &address) {
return address.toString();
}
QByteArray dataToHex(const QByteArray &data) {
QByteArray result = data.toHex().toUpper();
for (int i = 2; i < result.size(); i += 3)
result.insert(i, ' ');
return result;
}
QByteArray dataFromHex(const QString &hex) {
QByteArray line = hex.toLatin1();
line.replace(' ', QByteArray());
auto result = QByteArray::fromHex(line);
return result;
}