This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.cpp
317 lines (281 loc) · 10 KB
/
main.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
316
317
#include "mainwindow.h"
//#include "controlpanel.h"
#include <QApplication>
#include <QScreen>
#include <QProcess>
#include <QSharedMemory>
#include <QTimer>
#include <QTime>
#include <QFile>
#include <QTextCodec>
#include <QSizeF>
#include <QDir>
#include <QFile>
QSharedMemory mem("OctoBTT");
QByteArray Global_X_API_Key = "";//Test Key
int DebugFlat = 0;//Normal Flat : 0 ;Debug Mode : 1
QSizeF SizePercent = QSizeF(1.0,1.0);//Design Size 800:480
//QSize SizePercent(1,1);
//存放配置信息
QString strConfiguration = "";
int main(int argc, char *argv[])
{
//Application Encoding
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
//Program Statement Code
QApplication a(argc, argv);
//设置全局按键默认样式表---12.24
//判断config.conf文件在不在
if(!QFile("config.conf").exists())
{
//复制资源文件中的config.conf到用户本地文件夹里
QFile::copy(":/config.conf", "config.conf");
//设置权限
QFile::setPermissions("config.conf", QFileDevice::ReadOwner|QFileDevice::WriteOwner);
//设置默认按键的样式表
QFile file("config.conf");
file.open(QFile::ReadOnly);
if(file.isOpen())
{
strConfiguration = file.readAll();
//全局默认设置
//
// qApp->setStyleSheet(strConfiguration);
}
file.close();
}
else
{
QFile file(":/config.conf");
file.open(QFile::ReadOnly);
//获取用户本地的版本号
QString userNumber = file.readLine().trimmed();
QStringList userNumberList = userNumber.replace(QString("/*"),QString("")).replace(QString("*/"),QString("")).split(".");
//判断版本号是否更新,如果版本号大于旧的版本号,就更新,否则不变
bool upDateFlag = false;
QFile userFile("config.conf");
userFile.open(QFile::ReadOnly);
QString strUpDate = userFile.readLine().trimmed();
QStringList strUpDateList = strUpDate.replace(QString("/*"),QString("")).replace(QString("*/"),QString("")).split(".");
if(QString(strUpDateList.at(0)).toInt() > QString(userNumberList.at(0)).toInt())
upDateFlag = true;
else if(QString(strUpDateList.at(0)).toInt() == QString(userNumberList.at(0)).toInt())
{
if(QString(strUpDateList.at(1)).toInt() > QString(userNumberList.at(1)).toInt())
upDateFlag = true;
else if(QString(strUpDateList.at(1)).toInt() == QString(userNumberList.at(1)).toInt())
if(QString(strUpDateList.at(2)).toInt() > QString(userNumberList.at(2)).toInt())
upDateFlag = true;
}
if(upDateFlag)
{
file.close();
//删除旧版本并复制新版本到用户本地文件中
//配置文件修改函数=======================
// QFile::remove("/config.conf");
// QFile::copy(":/config.conf", "/config.conf");
//设置权限
QFile::setPermissions("config.conf", QFileDevice::ReadOwner|QFileDevice::WriteOwner);
file.open(QFile::ReadOnly);
if(file.isOpen())
{
strConfiguration = file.readAll();
// qApp->setStyleSheet(strConfiguration);
}
}
else
{
if(file.isOpen())
{
strConfiguration = file.readAll();
// qApp->setStyleSheet(strConfiguration);
}
}
file.close();
userFile.close();
}
//设置全局按键默认样式表---12.22
QString strStyleSheet = "";
//判断StyleSheet目录在不在
if(!QDir("StyleSheet").exists())
{
QDir().mkdir("StyleSheet");
//复制资源文件中的globalStyleSheet.qss到用户本地文件夹里
QFile::copy(":/StyleSheet/globalStyleSheet.qss", "StyleSheet/globalStyleSheet.qss");
//设置权限
QFile::setPermissions("StyleSheet/globalStyleSheet.qss", QFileDevice::ReadOwner|QFileDevice::WriteOwner);
//设置默认按键的样式表
QFile file("StyleSheet/globalStyleSheet.qss");
file.open(QFile::ReadOnly);
if(file.isOpen())
{
strStyleSheet = file.readAll();
//全局默认设置
qApp->setStyleSheet(strStyleSheet);
}
file.close();
}
else
{
QFile file("StyleSheet/globalStyleSheet.qss");
file.open(QFile::ReadOnly);
//获取用户本地的版本号
QString userNumber = file.readLine().trimmed();
QStringList userNumberList = userNumber.replace(QString("/*"),QString("")).replace(QString("*/"),QString("")).split(".");
//判断用户是否自定义了样式表
if(0 == QString(userNumberList.at(0)).toInt() && DebugFlat == 0)
{
//使用用户定义的样式表文件qss
if(file.isOpen())
{
strStyleSheet = file.readAll();
qApp->setStyleSheet(strStyleSheet);
}
}
//如果不是就判断版本号是否更新
else
{
//如果资源版本号大于旧的版本号,就更新,否则不变
bool upDateFlag = false;
QFile userFile(":/StyleSheet/globalStyleSheet.qss");
userFile.open(QFile::ReadOnly);
QString strUpDate = userFile.readLine().trimmed();
QStringList strUpDateList = strUpDate.replace(QString("/*"),QString("")).replace(QString("*/"),QString("")).split(".");
if(QString(strUpDateList.at(0)).toInt() > QString(userNumberList.at(0)).toInt())
upDateFlag = true;
else if(QString(strUpDateList.at(0)).toInt() == QString(userNumberList.at(0)).toInt())
{
if(QString(strUpDateList.at(1)).toInt() > QString(userNumberList.at(1)).toInt())
upDateFlag = true;
else if(QString(strUpDateList.at(1)).toInt() == QString(userNumberList.at(1)).toInt())
if(QString(strUpDateList.at(2)).toInt() > QString(userNumberList.at(2)).toInt())
upDateFlag = true;
}
if(upDateFlag || DebugFlat == 1)
{
file.close();
//删除旧版本并复制新版本到用户本地文件中
QFile::remove("StyleSheet/globalStyleSheet.qss");
QFile::copy(":/StyleSheet/globalStyleSheet.qss", "StyleSheet/globalStyleSheet.qss");
//设置权限
QFile::setPermissions("StyleSheet/globalStyleSheet.qss", QFileDevice::ReadOwner|QFileDevice::WriteOwner);
file.open(QFile::ReadOnly);
if(file.isOpen())
{
strStyleSheet = file.readAll();
qApp->setStyleSheet(strStyleSheet);
}
}
else
{
if(file.isOpen())
{
strStyleSheet = file.readAll();
qApp->setStyleSheet(strStyleSheet);
}
}
file.close();
userFile.close();
}
}
//Find Api Key
if(QSysInfo::productType() == "raspbian")
{
QFile configfile("/home/pi/.octoprint/config.yaml");
if(configfile.open(QIODevice::ReadOnly))
{
QTextStream txtInput(&configfile);
QStringList ConfigArgu = txtInput.readAll().split('\n');
configfile.close();
bool has_api = false;
foreach(QString Argu,ConfigArgu)
{
if(Argu.indexOf("api:")>=0)
{
has_api = true;
continue;
}
else if(has_api && Argu.indexOf("key:")>=0)
{
Global_X_API_Key = ((Argu.trimmed()).remove(0,(Argu.trimmed()).indexOf("key:")+4)).trimmed().toUtf8();
break;
}
}
}
else
{
DebugFlat = 1;
}
//Calculate the scale
QScreen *screen=QGuiApplication::primaryScreen();
QRect mm=screen->availableGeometry();
SizePercent.setWidth((float)mm.width()/(float)800);
SizePercent.setHeight((float)mm.height()/(float)480);
// SizePercent = QSize(mm.width()/800,mm.height()/480);
}
MainWindow w;
//Release Code
if(QSysInfo::productType() == "raspbian")
{
//Single Program//Need a life cycle as the program was accidenta
if (DebugFlat != 1 && !mem.create(1))
{
QApplication::exit();
QApplication::quit();
return 0;
}
w.setWindowState(Qt::WindowFullScreen);
// qDebug()<<mem.key();
}
//DeBug Code
// qDebug()<<mm.width()<<"@"<< mm.height();
// qDebug()<<QSysInfo::productType();
w.show();
//Boot Mode
int MCode = a.exec();
if(MCode == Base_Restart)
{
if (mem.isAttached())
{
mem.unlock();
mem.detach();
}
if(QSysInfo::productType() == "raspbian")
{
// QProcess P(0);
// P.start("startx");
// P.waitForStarted();
QProcess::startDetached("bash "+qApp->applicationDirPath()+"/start.sh");
}
else
{
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
}
QApplication::exit();
QApplication::quit();
return 0;
}
else if(MCode == Base_Shutdown)
{
QProcess P(0);
P.start("sudo shutdown now");
P.waitForStarted();
P.waitForFinished();
QApplication::exit();
QApplication::quit();
return 0;
}
else if(MCode == Base_OnlyExitApp)
{
if (mem.isAttached())
{
mem.unlock();
mem.detach();
}
QApplication::exit();
QApplication::quit();
return 0;
}
return MCode;
}