src/miscallenous.cpp

Sun, 29 Jan 2017 21:07:15 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Jan 2017 21:07:15 +0200
changeset 1080
6dac2d52bd9a
parent 1072
9ce9496427f2
child 1116
3b1d2cc6603e
permissions
-rw-r--r--

Removed the LDSpawn function and replaced its last use cases.

655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1072
9ce9496427f2 Happy new year 2017!
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <math.h>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <locale.h>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include <QColor>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "main.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "miscallenous.h"
962
a4b463a7ee82 Rename MainWindow files
Teemu Piippo <crimsondusk64@gmail.com>
parents: 958
diff changeset
24 #include "mainwindow.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include "dialogs.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include "ldDocument.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
28
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
29 int gcd (int a, int b)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 {
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
31 while (b != 0)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 {
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
33 int temp = a;
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
34 a = b;
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
35 b = temp % b;
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
36 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
38 return a;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
41
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
42 void simplify (int& numerator, int& denominator)
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
43 {
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
44 int factor = gcd(numerator, denominator);
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
45 numerator /= factor;
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
46 denominator /= factor;
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
47 }
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
48
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
49
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
50 QString joinStrings (QList<StringFormatArg> vals, QString delim)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 QStringList list;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 for (const StringFormatArg& arg : vals)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 list << arg.text();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 return list.join (delim);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
60
1066
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
61 void roundToDecimals(double& value, int decimals)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
1066
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
63 if (decimals == 0)
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
64 {
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
65 value = round(value);
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
66 }
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
67 else if (decimals > 0)
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
68 {
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
69 qreal coefficient = pow(10, decimals);
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
70 value = round(value * coefficient) / coefficient;
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
71 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 }
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
73
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
74
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
75 void applyToMatrix (Matrix& a, ApplyToMatrixFunction func)
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
76 {
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
77 for (int i = 0; i < 9; ++i)
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1067
diff changeset
78 func(i, a.value(i));
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
79 }
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
80
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
81 void applyToMatrix (const Matrix& a, ApplyToMatrixConstFunction func)
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
82 {
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
83 for (int i = 0; i < 9; ++i)
1068
283de3bd8b0e Reworked the Matrix interface so that less index math is involved
Teemu Piippo <teemu@hecknology.net>
parents: 1067
diff changeset
84 func(i, a.value(i));
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
85 }
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
86
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
87
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
88 QString formatFileSize (qint64 size)
834
3e697ba996e8 - now prints the filesize upon successful save
Santeri Piippo <crimsondusk64@gmail.com>
parents: 818
diff changeset
89 {
1066
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
90 static const QString suffixes[] = {" bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
1067
819973864620 Fixed formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1066
diff changeset
91 int magnitude = magnitude = (size > 0) ? floor(log(size) / log(1000) + 1e-10) : 0;
819973864620 Fixed formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1066
diff changeset
92 magnitude = qBound(0, magnitude, countof(suffixes) - 1);
819973864620 Fixed formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1066
diff changeset
93 return QString::number(size / pow(1000, magnitude)) + suffixes[magnitude];
834
3e697ba996e8 - now prints the filesize upon successful save
Santeri Piippo <crimsondusk64@gmail.com>
parents: 818
diff changeset
94 }

mercurial