-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
308 lines (266 loc) · 6.12 KB
/
util.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
#include "util.h"
#include <QFile>
#include <QDir>
#include <QStringList>
#include <QString>
#include <QTextStream>
#include <model/devisersettings.h>
Util::Util()
{
}
QString
Util::guessPlural(const QString &s)
{
QString str(s);
if (str.endsWith("Information")) return str;
if (str.endsWith("Index")) return str.replace("Index", "Indices");
if (str.endsWith("s")) return str;
if (str.endsWith("x")) return str + "es";
return str + "s";
}
QString
Util::lowerFirst(const QString& s)
{
QString str(s);
return str.left(1).toLower() + str.right(str.length()-1);
}
QString Util::upperFirst(const QString &s)
{
QString str(s);
return str.left(1).toUpper() + str.right(str.length()-1);
}
QList<QString>
Util::getCoreClasses()
{
return getCoreClassesMap().keys();
}
QMap<QString, QString>
Util::getCoreClassesMap()
{
QMap<QString, QString> result;
QFile data(":/CoreClasses.txt");
data.open(QIODevice::ReadOnly);
QTextStream in(&data);
while(!in.atEnd()) {
QString line = in.readLine();
line = line.replace(" ", "");
QStringList fields = line.split(",");
result[fields[0].trimmed()] = fields[1].trimmed();
}
data.close();
return result;
}
QStringList Util::getKnownTypes()
{
return QStringList()
<< "enum"
<< "array"
<< "element"
<< "lo_element"
<< "inline_lo_element"
<< "string"
<< "double"
<< "int"
<< "bool"
<< "boolean"
<< "uint"
<< "unsigned int"
<< "positive int"
<< "non-negative int"
<< "integer"
<< "unsigned integer"
<< "positive integer"
<< "non-negative integer"
<< "ID"
<< "IDREF"
<< "SId"
<< "SIdRef"
<< "UnitSId"
<< "UnitSIdRef"
<< "vector";
}
QStringList& Util::getUserDefinedTypes()
{
DeviserSettings* instance = DeviserSettings::getInstance();
return instance->getUserDefinedTypes();
}
QString Util::getErrorStyleSheet()
{
QColor color = DeviserSettings::getInstance()->getValidationColor();
return QString("QLineEdit, QLineEdit:hover {"
" background-color: rgba(%1,%2,%3,%4);"
" selection-background-color:rgba(%1,%2,%3,%4);"
" border-width: 1px;"
" border-color: black;"
" border: 1px solid gray;"
"}")
.arg(color.red())
.arg(color.green())
.arg(color.blue())
.arg(color.alpha());
}
QString
Util::getClassColor()
{
return "linen";
}
QString
Util::getEnumColor()
{
return "gold";
}
QString
Util::getExtensionColor()
{
return "palegreen";
}
QString
Util::getEnumPrefix()
{
return QString::fromUtf8("\xC2\xAB%1\xC2\xBB;").arg("Enumeration");
}
QString
Util::getExecutableFilter()
{
#ifdef Q_OS_WIN
return "Executable files (*.exe; *.bat; *.cmd);;All files (*.*)";
#else
return "Executable files (*.*; *.sh);;All files (*.*)";
#endif
}
QString
Util::getLibFilter()
{
#ifdef Q_OS_WIN
return "Library files (*.lib);;All files (*.*)";
#else
return "Library files (*.a*; *.so; *.dylib);;All files (*.*)";
#endif
}
QString Util::getHeaderFilter()
{
return "Header files (*.h; *.h++; *.hpp; *.hh; *.hxx);;All files (*.*)";
}
QString Util::getImplementationFilter()
{
return "C++ files (*.c; *.c++; *.cpp; *.cc; *.cxx);;All files (*.*)";
}
bool Util::isWindows()
{
#ifdef Q_OS_WIN
return true;
#else
return false;
#endif
}
bool Util::removeDir(const QString &dirPath, const QString &filter)
{
QFileInfo dirInfo(dirPath);
if (dirInfo.isFile())
{
return QFile (dirPath).remove();
}
QDir dir(dirPath);
if (!dir.exists())
return true;
foreach(const QFileInfo &info, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot))
{
if (info.isDir())
{
if (!removeDir(info.filePath(), filter))
return false;
}
else
{
if (!filter.isEmpty() && !info.fileName().contains(filter))
continue;
if (!dir.remove(info.fileName()))
return false;
}
}
if (filter.isEmpty())
{
QDir parentDir(QFileInfo(dirPath).path());
return parentDir.rmdir(QFileInfo(dirPath).fileName());
}
return true;
}
bool
Util::copyDir(const QString &srcPath, const QString &dstPath, bool overwrite)
{
QDir parentDstDir(QFileInfo(dstPath).path());
parentDstDir.mkdir(QFileInfo(dstPath).fileName());
QFileInfo srcInfo(srcPath);
if (srcInfo.isFile())
{
QString dstItemPath = dstPath + "/" + srcInfo.fileName();
if (QFile::exists(dstItemPath) && overwrite)
QFile::remove(dstItemPath);
return QFile::copy(srcPath, dstItemPath);
}
QDir srcDir(srcPath);
foreach(const QFileInfo &info, srcDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot))
{
QString srcItemPath = srcPath + "/" + info.fileName();
QString dstItemPath = dstPath + "/" + info.fileName();
if (info.isDir())
{
if (!copyDir(srcItemPath, dstItemPath, overwrite))
{
return false;
}
}
else if (info.isFile())
{
if (QFile::exists(dstItemPath) && overwrite)
QFile::remove(dstItemPath);
if (!QFile::copy(srcItemPath, dstItemPath))
{
return false;
}
}
}
return true;
}
QString
Util::toARgbString(const QColor& color)
{
return
byteToHex(color.alpha()) +
byteToHex(color.red()) +
byteToHex(color.green()) +
byteToHex(color.blue())
;
}
QString
Util::byteToHex(int oByte)
{
if (oByte == 0)
return QString("00");
QString sResult = QString::number(oByte, 16);
if (sResult.length() == 1)
sResult = "0" + sResult;
return sResult;
}
QColor
Util::parseColor(const QString& sColor)
{
QColor oResult;
if (sColor.isEmpty()) return oResult;
if (sColor.length() == 8)
{
int nAlpha = sColor.mid(0,2).toInt(NULL, 16);
int nRed = sColor.mid(2,2).toInt(NULL, 16);
int nGreen = sColor.mid(4,2).toInt(NULL, 16);
int nBlue = sColor.mid(6,2).toInt(NULL, 16);
oResult = QColor(nRed, nGreen, nBlue, nAlpha);
}
else if (sColor.length() == 6)
{
int nRed = sColor.mid(0,2).toInt(NULL, 16);
int nGreen = sColor.mid(2,2).toInt(NULL, 16);
int nBlue = sColor.mid(4,2).toInt(NULL, 16);
oResult = QColor(nRed, nGreen, nBlue, 255);
}
return oResult;
}