-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager.cpp
345 lines (319 loc) · 10.2 KB
/
assetmanager.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "assetmanager.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
AssetManager::AssetManager()
{
LIBS_FILEPATH = "/Users/jacobplaster/Documents/Able/libs";
TMP_USER_CFG_FILEPATH = "/tmp_user_cfg.txt";
}
void AssetManager::loadAssets()
{
isLoaded = true;
// load default font
int id1 = QFontDatabase::addApplicationFont(LIBS_FILEPATH + "/fonts/SourceCodePro-Regular.ttf");
srcCodeLightFont = QFontDatabase::applicationFontFamilies(id1).at(0);
int id2 = QFontDatabase::addApplicationFont(LIBS_FILEPATH + "/fonts/Muli-Light.ttf");
mainAppFont = QFontDatabase::applicationFontFamilies(id2).at(0);
// load css stylesheets
QFile styleFile(LIBS_FILEPATH + "/css/default.qss");
//QFile styleFile(LIBS_FILEPATH + "/css/dark.qss");
if (!styleFile.exists()) {
qDebug() << "File " << styleFile.fileName() << " does not exist.";
}
styleFile.open( QFile::ReadOnly );
// Apply the loaded stylesheet
defaultStyle = styleFile.readAll();
styleFile.close();
loadAllLanguageSupport();
loadAllStyleSheets();
}
void AssetManager::saveUserCfg(QStringList cfg)
{
QFile userCfg(LIBS_FILEPATH+TMP_USER_CFG_FILEPATH);
userCfg.open(QIODevice::WriteOnly | QIODevice::Text);
if (!userCfg.isOpen()) {
qDebug() << "Failed to open user cfg.";
}
QTextStream outStream(&userCfg);
for(int i = 0; i < cfg.length(); i++)
outStream << cfg[i] << "\n";
userCfg.close();
}
QStringList AssetManager::loadUserCfg()
{
QStringList returnList;
QFile userCfg(LIBS_FILEPATH+TMP_USER_CFG_FILEPATH);
if (userCfg.open(QIODevice::ReadOnly))
{
QTextStream in(&userCfg);
while (!in.atEnd())
{
returnList << in.readLine();
}
userCfg.close();
}
return returnList;
}
QString AssetManager::loadStyleSheetByFilename(QString filename)
{
// load css stylesheets
QFile styleFile(filename);
if (!styleFile.exists()) {
qDebug() << "File " << styleFile.fileName() << " does not exist.";
}
styleFile.open( QFile::ReadOnly );
// Apply the loaded stylesheet
QString style = styleFile.readAll();
styleFile.close();
return style;
}
QString AssetManager::getStyle(const std::string &style) const
{
if(isLoaded)
{
if(style == "MAIN_STYLE")
{
return defaultStyle;
}
}
return defaultStyle;
}
QFileInfoList AssetManager::getStyleSheets()
{
return styleSheets;
}
QFont AssetManager::getFont(const std::string &font) const
{
if(isLoaded)
{
if(font == "MAIN_CODE_FONT")
return srcCodeLightFont;
if(font == "MAIN_APP_FONT")
return mainAppFont;
qDebug() << "Success, loading font: ";
} else
{
qDebug() << "Error: Attempting to load un-loaded assets. With font: ";
}
return srcCodeLightFont;
}
void AssetManager::loadAllLanguageSupport()
{
QDir lsDir(LIBS_FILEPATH + "/language_support");
QFileInfoList allLs = lsDir.entryInfoList();
foreach (QFileInfo fileI, allLs){
// if is a file and is a cfg file
if (!fileI.isDir() && fileI.completeSuffix() == "cfg")
{
// open and begin reading each file
QFile file(fileI.filePath());
if (!file.exists()) {
QMessageBox::information(0, "Error", "Unable to load language support file: " + fileI.fileName());
continue;
}
file.open( QFile::ReadOnly );
QTextStream fileStream(&file);
// load into variables
loadLanguageSupportFile(fileStream, fileI.baseName());
file.close();
}
}
}
SyntaxHighlightingRuleSet * AssetManager::getLanguageSupportRuleSet(QString &language) const
{
for (int i=0; i < syntaxHighlightingRules.length(); i++)
{
for (int i2=0; i2 < syntaxHighlightingRules[i]->languageSupport.length(); i2++)
{
if(language == syntaxHighlightingRules[i]->languageSupport[i2])
return syntaxHighlightingRules[i];
}
}
return NULL;
}
SyntaxHighlightingRuleSet * AssetManager::getLanguageSupportByName(QString &name)
{
for(int i = 0; i < syntaxHighlightingRules.length(); i++)
{
if(syntaxHighlightingRules[i]->fileName == name)
return syntaxHighlightingRules[i];
}
}
QStringList AssetManager::getLoadedSupportFileNames()
{
QStringList list;
for(int i = 0; i < syntaxHighlightingRules.length(); i++)
{
list << syntaxHighlightingRules[i]->fileName;
}
return list;
}
void AssetManager::loadAllStyleSheets()
{
QDir cssDir(LIBS_FILEPATH + "/css");
QFileInfoList allCss = cssDir.entryInfoList();
foreach (QFileInfo fileI, allCss){
// if is a file and is a cfg file
if (!fileI.isDir() && fileI.completeSuffix() == "qss")
{
styleSheets << fileI;
}
}
}
// Parses the file into an object that the syntax highlighting algorithm can understand
void AssetManager::loadLanguageSupportFile(QTextStream &in, const QString &inFileName)
{
int state = -1;
SyntaxHighlightingRuleSet *sRuleSet = new SyntaxHighlightingRuleSet();
sRuleSet->fileName = inFileName;
QString currentColor;
QString prevColor;
QString prevPrevColor;
// read line by line
while(!in.atEnd()) {
QString line = in.readLine();
QTextCharFormat qtf;
if(line != "")
{
// progress state if equal to percent
if(line[0] == '%')
{
state++;
continue;
}
// sets the color for the next expressions
if(line[0] == '#')
{
prevPrevColor = prevColor;
prevColor = currentColor;
currentColor = line;
}
SyntaxHighlightingRuleSet::HighlightingRule rule;
// language support
if(state==0)
{
sRuleSet->languageSupport.append(line);
}
//comment_Start_Expression
if(state==1)
{
sRuleSet->commentStartExpression = QRegExp(line);
}
// comment_End_Expression
if(state==2)
{
sRuleSet->commentEndExpression = QRegExp(line);
}
// Operator_Format
if(state==3)
{
qtf.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtf;
sRuleSet->highlightingRules.append(rule);
}
// number_Format
if(state==4)
{
QTextCharFormat qtf;
qtf.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtf;
sRuleSet->highlightingRules.append(rule);
}
// Keyword formats
if(state==5)
{
// add line to autocompleter
sRuleSet->constantKeywords << line;
line = "\\b" + line + "\\b";
sRuleSet->keywordPatterns << line;
}
// function_Format
if(state==6)
{
qtf.setFontItalic(true);
qtf.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtf;
sRuleSet->highlightingRules.append(rule);
}
// class_Format
if(state==7)
{
// also load keyword format once stream has finished
qtf.setForeground(QColor(prevPrevColor));
qtf.setFontWeight(QFont::Bold);
// load all as seperate expressions
foreach (const QString &pattern, sRuleSet->keywordPatterns) {
rule.pattern = QRegExp(pattern);
rule.format = qtf;
sRuleSet->highlightingRules.append(rule);
}
// handle class format
qtf.setFontWeight(QFont::Bold);
qtf.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtf;
sRuleSet->highlightingRules.append(rule);
}
// Other_Format
if(state==8)
{
qtf.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtf;
sRuleSet->highlightingRules.append(rule);
}
// single_Line_Comment_Format
if(state==9)
{
QTextCharFormat qtfs;
qtfs.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtfs;
sRuleSet->highlightingRules.append(rule);
}
// multi_line_Comment_Format
if(state==10)
sRuleSet->multiLineCommentFormat.setForeground(QColor(currentColor));
// Quotation_Format
if(state==11)
{
QTextCharFormat qtfq;
qtfq.setFontItalic(true);
qtfq.setForeground(QColor(currentColor));
rule.pattern = QRegExp(line);
rule.format = qtfq;
sRuleSet->highlightingRules.append(rule);
}
if(state==12)
{
sRuleSet->autocorrectFormat = QRegExp(line);
}
if(state==13)
{
sRuleSet->autocorrectTrimFormat = QRegExp(line);
}
// Line highlight
if(state==14)
{
sRuleSet->lineHighlightColor = currentColor;
}
// Search background
if(state==15)
{
sRuleSet->searchHighlightColor = currentColor;
}
// Search foreground
if(state==16)
{
sRuleSet->searchHighlightColorForeground = currentColor;
}
}
}
// append parsed object file
syntaxHighlightingRules.append(sRuleSet);
}