app.cpp
6.32 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
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
#include "app.h"
#include "myhelper.h"
#ifdef __arm__
int App::LeftPanelWidth = 150;
int App::RightPanelWidth = 200;
QString App::PlatForm = "A9";
#else
int App::LeftPanelWidth = 120;
int App::RightPanelWidth = 170;
QString App::PlatForm = "WIN";
#endif
QString App::SendFileName = "send.txt";
QString App::DeviceFileName = "device.txt";
bool App::HexSendTcpServer = true;
bool App::HexReceiveTcpServer = true;
bool App::DebugTcpServer = false;
bool App::AutoSendTcpServer = false;
int App::IntervalTcpServer = 1000;
int App::TcpListenPort = 6000;
bool App::HexSendTcpClient = true;
bool App::HexReceiveTcpClient = true;
bool App::DebugTcpClient = false;
bool App::AutoSendTcpClient = false;
int App::IntervalTcpClient = 1000;
QString App::TcpServerIP = "127.0.0.1";
int App::TcpServerPort = 6000;
bool App::HexSendUdpServer = true;
bool App::HexReceiveUdpServer = true;
bool App::DebugUdpServer = false;
bool App::AutoSendUdpServer = false;
int App::IntervalUdpServer = 1000;
int App::UdpListenPort = 6000;
QString App::UdpClientIP = "127.0.0.1";
int App::UdpClientPort = 6000;
void App::ReadConfig()
{
if (!CheckConfig()) {
return;
}
QString fileName = QString("%1/%2_Config.ini").arg(AppPath).arg(AppName);
QSettings set(fileName, QSettings::IniFormat);
set.beginGroup("AppConfig");
App::LeftPanelWidth = set.value("LeftPanelWidth").toInt();
App::RightPanelWidth = set.value("RightPanelWidth").toInt();
App::PlatForm = set.value("PlatForm").toString();
App::SendFileName = set.value("SendFileName").toString();
App::DeviceFileName = set.value("DeviceFileName").toString();
set.endGroup();
set.beginGroup("TcpServerConfig");
App::HexSendTcpServer = set.value("HexSendTcpServer").toBool();
App::HexReceiveTcpServer = set.value("HexReceiveTcpServer").toBool();
App::DebugTcpServer = set.value("DebugTcpServer").toBool();
App::AutoSendTcpServer = set.value("AutoSendTcpServer").toBool();
App::IntervalTcpServer = set.value("IntervalTcpServer").toInt();
App::TcpListenPort = set.value("TcpListenPort").toInt();
set.endGroup();
set.beginGroup("TcpClientConfig");
App::HexSendTcpClient = set.value("HexSendTcpClient").toBool();
App::HexReceiveTcpClient = set.value("HexReceiveTcpClient").toBool();
App::DebugTcpClient = set.value("DebugTcpClient").toBool();
App::AutoSendTcpClient = set.value("AutoSendTcpClient").toBool();
App::IntervalTcpClient = set.value("IntervalTcpClient").toInt();
App::TcpServerIP = set.value("TcpServerIP").toString();
App::TcpServerPort = set.value("TcpServerPort").toInt();
set.endGroup();
set.beginGroup("UdpServerConfig");
App::HexSendUdpServer = set.value("HexSendUdpServer").toBool();
App::HexReceiveUdpServer = set.value("HexReceiveUdpServer").toBool();
App::DebugUdpServer = set.value("DebugUdpServer").toBool();
App::AutoSendUdpServer = set.value("AutoSendUdpServer").toBool();
App::IntervalUdpServer = set.value("IntervalUdpServer").toInt();
App::UdpListenPort = set.value("UdpListenPort").toInt();
App::UdpClientIP = set.value("UdpClientIP").toString();
App::UdpClientPort = set.value("UdpClientPort").toInt();
set.endGroup();
}
void App::WriteConfig()
{
QString fileName = QString("%1/%2_Config.ini").arg(AppPath).arg(AppName);
QSettings set(fileName, QSettings::IniFormat);
set.beginGroup("AppConfig");
set.setValue("LeftPanelWidth", App::LeftPanelWidth);
set.setValue("RightPanelWidth", App::RightPanelWidth);
set.setValue("PlatForm", App::PlatForm);
set.setValue("SendFileName", App::SendFileName);
set.setValue("DeviceFileName", App::DeviceFileName);
set.endGroup();
set.beginGroup("TcpServerConfig");
set.setValue("HexSendTcpServer", App::HexSendTcpServer);
set.setValue("HexReceiveTcpServer", App::HexReceiveTcpServer);
set.setValue("DebugTcpServer", App::DebugTcpServer);
set.setValue("AutoSendTcpServer", App::AutoSendTcpServer);
set.setValue("IntervalTcpServer", App::IntervalTcpServer);
set.setValue("TcpListenPort", App::TcpListenPort);
set.endGroup();
set.beginGroup("TcpClientConfig");
set.setValue("HexSendTcpClient", App::HexSendTcpClient);
set.setValue("HexReceiveTcpClient", App::HexReceiveTcpClient);
set.setValue("DebugTcpClient", App::DebugTcpClient);
set.setValue("AutoSendTcpClient", App::AutoSendTcpClient);
set.setValue("IntervalTcpClient", App::IntervalTcpClient);
set.setValue("TcpServerIP", App::TcpServerIP);
set.setValue("TcpServerPort", App::TcpServerPort);
set.endGroup();
set.beginGroup("UdpServerConfig");
set.setValue("HexSendUdpServer", App::HexSendUdpServer);
set.setValue("HexReceiveUdpServer", App::HexReceiveUdpServer);
set.setValue("DebugUdpServer", App::DebugUdpServer);
set.setValue("AutoSendUdpServer", App::AutoSendUdpServer);
set.setValue("IntervalUdpServer", App::IntervalUdpServer);
set.setValue("UdpListenPort", App::UdpListenPort);
set.setValue("UdpClientIP", App::UdpClientIP);
set.setValue("UdpClientPort", App::UdpClientPort);
set.endGroup();
}
void App::NewConfig()
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#endif
WriteConfig();
}
bool App::CheckConfig()
{
QString fileName = QString("%1/%2_Config.ini").arg(AppPath).arg(AppName);
//如果配置文件大小为0,则以初始值继续运行,并生成配置文件
QFile file(fileName);
if (file.size() == 0) {
NewConfig();
return false;
}
//如果配置文件不完整,则以初始值继续运行,并生成配置文件
if (file.open(QFile::ReadOnly)) {
bool ok = true;
while (!file.atEnd()) {
QString line = file.readLine();
line = line.replace("\r", "");
line = line.replace("\n", "");
QStringList list = line.split("=");
if (list.count() == 2) {
if (list.at(1) == "") {
ok = false;
break;
}
}
}
if (!ok) {
NewConfig();
return false;
}
} else {
NewConfig();
return false;
}
return true;
}
void App::WriteError(QString str)
{
QString fileName = QString("%1/%2_Error_%3.txt").arg(AppPath).arg(AppName).arg(QDATE);
QFile file(fileName);
file.open(QIODevice::WriteOnly | QIODevice::Append | QFile::Text);
QTextStream stream(&file);
stream << DATETIME << " " << str << "\n";
}
void App::NewDir(QString dirName)
{
//如果路径中包含斜杠字符则说明是绝对路径
//linux系统路径字符带有 / windows系统 路径字符带有 :/
if (!dirName.startsWith("/") && !dirName.contains(":/")) {
dirName = QString("%1/%2").arg(AppPath).arg(dirName);
}
QDir dir(dirName);
if (!dir.exists()) {
dir.mkpath(dirName);
}
}