-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.cpp
328 lines (287 loc) · 8.05 KB
/
utilities.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
#include "utilities.h"
#include "graphicsitemtype.h"
#include <QFile>
#include <QGraphicsItem>
#include <QtMath>
#include "qmath.h"
#include <QDebug>
#include <QScreen>
#include <QGuiApplication>
Utilities::Utilities()
{
}
QString Utilities::loadFile(const QString & filename){
QFile file;
file.setFileName(filename);
file.open(QIODevice::ReadOnly);
QString text = file.readAll();
file.close();
return text;
}
QJsonObject Utilities::rectToJson(const QRectF & value){
QJsonObject json;
json["x"] = value.x();
json["y"] = value.y();
json["width"] = value.width();
json["height"] = value.height();
return json;
}
QRectF Utilities::jsonToRect(const QJsonObject &json)
{
QRectF rect;
rect.setX(json["x"].toDouble());
rect.setY(json["y"].toDouble());
rect.setWidth(json["width"].toDouble());
rect.setHeight(json["height"].toDouble());
return rect;
}
QJsonObject Utilities::transformToJson(const QTransform & value){
QJsonObject json;
json["m11"] = value.m11();
json["m12"] = value.m12();
json["m13"] = value.m13();
json["m21"] = value.m21();
json["m22"] = value.m22();
json["m23"] = value.m23();
json["m31"] = value.m31();
json["m32"] = value.m32();
json["m33"] = value.m33();
return json;
}
QTransform Utilities::jsonToTransform(const QJsonObject &json)
{
return QTransform(
json["m11"].toDouble(),json["m12"].toDouble(),json["m13"].toDouble(),
json["m21"].toDouble(),json["m22"].toDouble(),json["m23"].toDouble(),
json["m31"].toDouble(),json["m32"].toDouble(),json["m33"].toDouble()
);
}
qreal Utilities::square(const qreal num){return num * num;}
qreal Utilities::twoPointDistance(const QPointF& pt1, const QPointF& pt2)
{
return qSqrt(double(square(pt2.x() - pt1.x()) + square(pt2.y() - pt1.y())));
}
QPolygonF Utilities::rectToPoly(QRectF rect)
{
QPolygonF poly;
poly << rect.topLeft() << rect.bottomRight();
return poly;
}
QRectF Utilities::polyToRect(QPolygonF poly)
{
return QRectF(poly.at(0),poly.at(1));
}
static inline bool isDigit(ushort ch)
{
static quint16 magic = 0x3ff;
return ((ch >> 4) == 3) && (magic >> (ch & 15));
}
static qreal toDouble(const QChar *&str)
{
const int maxLen = 255;//technically doubles can go til 308+ but whatever
char temp[maxLen+1];
int pos = 0;
if (*str == QLatin1Char('-')) {
temp[pos++] = '-';
++str;
} else if (*str == QLatin1Char('+')) {
++str;
}
while (isDigit(str->unicode()) && pos < maxLen) {
temp[pos++] = str->toLatin1();
++str;
}
if (*str == QLatin1Char('.') && pos < maxLen) {
temp[pos++] = '.';
++str;
}
while (isDigit(str->unicode()) && pos < maxLen) {
temp[pos++] = str->toLatin1();
++str;
}
bool exponent = false;
if ((*str == QLatin1Char('e') || *str == QLatin1Char('E')) && pos < maxLen) {
exponent = true;
temp[pos++] = 'e';
++str;
if ((*str == QLatin1Char('-') || *str == QLatin1Char('+')) && pos < maxLen) {
temp[pos++] = str->toLatin1();
++str;
}
while (isDigit(str->unicode()) && pos < maxLen) {
temp[pos++] = str->toLatin1();
++str;
}
}
temp[pos] = '\0';
qreal val;
if (!exponent && pos < 10) {
int ival = 0;
const char *t = temp;
bool neg = false;
if(*t == '-') {
neg = true;
++t;
}
while(*t && *t != '.') {
ival *= 10;
ival += (*t) - '0';
++t;
}
if(*t == '.') {
++t;
int div = 1;
while(*t) {
ival *= 10;
ival += (*t) - '0';
div *= 10;
++t;
}
val = ((qreal)ival)/((qreal)div);
} else {
val = ival;
}
if (neg)
val = -val;
} else {
val = QByteArray::fromRawData(temp, pos).toDouble();
}
return val;
}
static qreal toDouble(const QString &str, bool *ok = NULL)
{
const QChar *c = str.constData();
qreal res = toDouble(c);
if (ok) {
*ok = ((*c) == QLatin1Char('\0'));
}
return res;
}
static qreal toDouble(const QStringRef &str, bool *ok = NULL)
{
const QChar *c = str.constData();
qreal res = toDouble(c);
if (ok) {
*ok = (c == (str.constData() + str.length()));
}
return res;
}
qreal Utilities::convertToPixels(qreal len, Utilities::LengthType type)
{
switch (type) {
case Utilities::LT_PERCENT:
break;
case Utilities::LT_PX:
break;
case Utilities::LT_PC:
break;
case Utilities::LT_PT:
return len * 1.25;
break;
case Utilities::LT_MM:
return len * 3.543307;
break;
case Utilities::LT_CM:
return len * 35.43307;
break;
case Utilities::LT_IN:
return len * 90;
break;
case Utilities::LT_OTHER:
break;
default:
break;
}
return len;
}
qreal Utilities::parseToPixels(const QStringRef &str)
{
Utilities::LengthType lengthType;
QStringRef numStr = str.trimmed();
if (numStr.endsWith(QLatin1Char('%'))) {
numStr.chop(1);
lengthType = Utilities::LT_PERCENT;
} else if (numStr.endsWith(QLatin1String("px"))){
numStr.chop(2);
lengthType = Utilities::LT_PX;
} else if (numStr.endsWith(QLatin1String("pc"))) {
numStr.chop(2);
lengthType = Utilities::LT_PC;
} else if (numStr.endsWith(QLatin1String("pt"))) {
numStr.chop(2);
lengthType = Utilities::LT_PT;
} else if (numStr.endsWith(QLatin1String("mm"))) {
numStr.chop(2);
lengthType = Utilities::LT_MM;
} else if (numStr.endsWith(QLatin1String("cm"))) {
numStr.chop(2);
lengthType = Utilities::LT_CM;
} else if (numStr.endsWith(QLatin1String("in"))) {
numStr.chop(2);
lengthType = Utilities::LT_IN;
} else {
lengthType = LT_PX;
}
bool ok = false;
qreal len = toDouble(numStr, &ok);
if (!ok) {
len = 0;
qWarning()<<"Error when convert string to qreal: "<<str;
}
//qDebug()<<"len is "<<len<<", from '"<<numStr << "'";
return convertToPixels(len,lengthType);
}
qreal Utilities::defaultDpiScale()
{
static qreal scale = []{
if (const QScreen *screen = QGuiApplication::primaryScreen())
return screen->logicalDotsPerInchX() / 96.0;
return 1.0;
}();
return scale;
}
qreal Utilities::dpiScaled(qreal value)
{
#ifdef Q_OS_MAC
// On mac the DPI is always 72 so we should not scale it
return value;
#else
static const qreal scale = defaultDpiScale();
return value * scale;
#endif
}
QSize Utilities::dpiScaled(QSize value)
{
return QSize(qRound(dpiScaled(value.width())),
qRound(dpiScaled(value.height())));
}
QPoint Utilities::dpiScaled(QPoint value)
{
return QPoint(qRound(dpiScaled(value.x())),
qRound(dpiScaled(value.y())));
}
QRectF Utilities::dpiScaled(QRectF value)
{
return QRectF(dpiScaled(value.x()),
dpiScaled(value.y()),
dpiScaled(value.width()),
dpiScaled(value.height()));
}
QColor Utilities::parseColor(const QString &str)
{
if (QColor::isValidColor(str))
return QColor(str);
QStringList sList = str.split(' ');
if (sList.count()<3){
qDebug()<<"invalid color settings";
}
int r = 0, g = 0 , b = 0;
qreal alpha = 1;
r = sList[0].toInt();
g = sList[1].toInt();
b = sList[2].toInt();
if (sList.count()>3){
alpha = sList[3].toFloat();
}
return QColor(r,g,b,alpha);
}