src/miscallenous.cpp

Sun, 18 Mar 2018 12:33:29 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 18 Mar 2018 12:33:29 +0200
changeset 1298
dbc8bb2a4d84
parent 1145
02264bf0108d
child 1196
0908803938f9
permissions
-rw-r--r--

fixed infinite recursion and integrated the subfilereference editor

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"
1145
02264bf0108d Renamed ldDocument.cpp → lddocument.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1128
diff changeset
25 #include "lddocument.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
27
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
28 int gcd (int a, int b)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 {
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
30 while (b != 0)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 {
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
32 int temp = a;
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
33 a = b;
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
34 b = temp % b;
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
35 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
37 return a;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
40
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
41 void simplify (int& numerator, int& denominator)
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
42 {
1022
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
43 int factor = gcd(numerator, denominator);
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
44 numerator /= factor;
a7f8ce5aa858 Use a better gcd algorithm, some style fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
45 denominator /= factor;
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
46 }
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 QString joinStrings (QList<StringFormatArg> vals, QString delim)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 QStringList list;
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 for (const StringFormatArg& arg : vals)
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 list << arg.text();
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 return list.join (delim);
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
59
1066
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
60 void roundToDecimals(double& value, int decimals)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 {
1066
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
62 if (decimals == 0)
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
63 {
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
64 value = round(value);
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
65 }
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
66 else if (decimals > 0)
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
67 {
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
68 qreal coefficient = pow(10, decimals);
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
69 value = round(value * coefficient) / coefficient;
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
70 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 }
733
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
72
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
73
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
74 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
75 {
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
76 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
77 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
78 }
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
79
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
80 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
81 {
cc39df788660 - reimplemented the Vertex class as a derivative of QVector3D
Santeri Piippo <crimsondusk64@gmail.com>
parents: 662
diff changeset
82 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
83 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
84 }
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
1013
fa025ba493d8 Cleanup miscallenous.cpp/.h
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1012
diff changeset
87 QString formatFileSize (qint64 size)
834
3e697ba996e8 - now prints the filesize upon successful save
Santeri Piippo <crimsondusk64@gmail.com>
parents: 818
diff changeset
88 {
1066
d7ec12688716 Refactor roundToDecimals and formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
89 static const QString suffixes[] = {" bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
1128
67a30f5d159c Fixed some compilation problems
Teemu Piippo <teemu@hecknology.net>
parents: 1116
diff changeset
90 int magnitude = (size > 0) ? floor(log10(size) / 3.0 + 1e-10) : 0;
1067
819973864620 Fixed formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1066
diff changeset
91 magnitude = qBound(0, magnitude, countof(suffixes) - 1);
819973864620 Fixed formatFileSize
Teemu Piippo <teemu@hecknology.net>
parents: 1066
diff changeset
92 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
93 }

mercurial