Thu, 29 Mar 2018 10:31:01 +0300
refactor
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 |
1326 | 3 | * Copyright (C) 2013 - 2018 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 | |
1323
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
19 | #include <QApplication> |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
20 | #include <QSettings> |
1318
568fcfc6da71
removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents:
1315
diff
changeset
|
21 | #include <QLineF> |
568fcfc6da71
removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents:
1315
diff
changeset
|
22 | #include "basics.h" |
568fcfc6da71
removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents:
1315
diff
changeset
|
23 | #include "types/vertex.h" |
1319 | 24 | #include "format.h" |
1323
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
25 | #include "version.h" |
1036
993c46d7eb75
Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents:
1031
diff
changeset
|
26 | |
1319 | 27 | int gcd(int a, int b) |
1036
993c46d7eb75
Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents:
1031
diff
changeset
|
28 | { |
1319 | 29 | while (b != 0) |
30 | { | |
31 | int temp = a; | |
32 | a = b; | |
33 | b = temp % b; | |
34 | } | |
1036
993c46d7eb75
Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents:
1031
diff
changeset
|
35 | |
1319 | 36 | return a; |
1031
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
37 | } |
1053 | 38 | |
1319 | 39 | void simplify(int& numerator, int& denominator) |
1053 | 40 | { |
1319 | 41 | int factor = gcd(numerator, denominator); |
42 | numerator /= factor; | |
43 | denominator /= factor; | |
1053 | 44 | } |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
45 | |
1319 | 46 | |
47 | QString joinStrings(const QList<StringFormatArg>& values, QString delimeter) | |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
48 | { |
1319 | 49 | QStringList list; |
50 | ||
51 | for (const StringFormatArg& arg : values) | |
52 | list << arg.text(); | |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
53 | |
1319 | 54 | return list.join(delimeter); |
55 | } | |
56 | ||
57 | ||
1324
563a9b65777b
roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents:
1323
diff
changeset
|
58 | double roundToDecimals(double value, int decimals) |
1319 | 59 | { |
60 | if (decimals == 0) | |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
61 | { |
1324
563a9b65777b
roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents:
1323
diff
changeset
|
62 | return round(value); |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
63 | } |
1319 | 64 | else if (decimals > 0) |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
65 | { |
1319 | 66 | qreal coefficient = pow(10, decimals); |
1324
563a9b65777b
roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents:
1323
diff
changeset
|
67 | return round(value * coefficient) / coefficient; |
1181
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
68 | } |
ca6d0ef9aadb
Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents:
1177
diff
changeset
|
69 | } |
1305
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1181
diff
changeset
|
70 | |
1352 | 71 | double log1000(double x) |
72 | { | |
73 | return log10(x) / 3.0; | |
74 | } | |
75 | ||
76 | /* | |
77 | * Returns a string representation of the provided file size. | |
78 | */ | |
1319 | 79 | QString formatFileSize(qint64 size) |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
80 | { |
1352 | 81 | static const QString suffixes[] = { |
82 | QObject::tr("bytes"), | |
83 | QObject::tr("kB", "kilobytes"), | |
84 | QObject::tr("MB", "megabytes"), | |
85 | QObject::tr("GB", "gigabytes"), | |
86 | QObject::tr("TB", "terabytes"), | |
87 | QObject::tr("PB", "petabytes"), | |
88 | QObject::tr("EB", "exabytes"), | |
89 | QObject::tr("ZB", "zettabytes"), | |
90 | QObject::tr("YB", "yottabytes") | |
91 | }; | |
92 | ||
93 | if (size == 1) | |
94 | { | |
95 | return QObject::tr("1 byte"); | |
96 | } | |
97 | else if (size > 0) | |
98 | { | |
99 | // Find out which suffix to use by the use of 1000-base logarithm: | |
100 | int magnitude = static_cast<int>(floor(log1000(size))); | |
101 | // ensure that magnitude is within bounds (even if we somehow get 1000s of yottabytes) | |
102 | magnitude = qBound(0, magnitude, countof(suffixes) - 1); | |
103 | // prepare the representation | |
104 | return QString::number(size / pow(1000, magnitude), 'g', 3) + " " + suffixes[magnitude]; | |
105 | } | |
106 | else | |
107 | { | |
108 | return QString::number(size) + " " + suffixes[0]; | |
109 | } | |
1308
dcc8c02530c2
Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
110 | } |
1323
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
111 | |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
112 | /* |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
113 | * Returns a settings object that interfaces the ini file. |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
114 | */ |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
115 | QSettings& settingsObject() |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
116 | { |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
117 | static QSettings settings { |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
118 | qApp->applicationDirPath() + "/" UNIXNAME ".ini", |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
119 | QSettings::IniFormat |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
120 | }; |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
121 | return settings; |
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1319
diff
changeset
|
122 | } |
1350
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
123 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
124 | QString largeNumberRep(int number) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
125 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
126 | if (number < 0) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
127 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
128 | return "-" + largeNumberRep(-number); |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
129 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
130 | else |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
131 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
132 | QString rep; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
133 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
134 | while (number >= 1000) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
135 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
136 | rep = " " + QString::number(number % 1000); |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
137 | number /= 1000; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
138 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
139 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
140 | return QString::number(number) + rep; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
141 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
142 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
143 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
144 | static const QString superscripts = "⁰¹²³⁴⁵⁶⁷⁸⁹"; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
145 | static const QString subscripts = "₀₁₂₃₄₅₆₇₈₉"; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
146 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
147 | static QString customNumberRep(int number, const QString& script, const QString& minus) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
148 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
149 | if (number < 0) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
150 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
151 | return minus + customNumberRep(-number, script, minus); |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
152 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
153 | else |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
154 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
155 | QString rep = ""; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
156 | for (QChar character : QString::number(number)) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
157 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
158 | if (character >= '0' and character <= '9') |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
159 | rep += script[character.unicode() - '0']; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
160 | else |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
161 | rep += character; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
162 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
163 | return rep; |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
164 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
165 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
166 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
167 | QString superscript(int number) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
168 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
169 | return customNumberRep(number, superscripts, "⁻"); |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
170 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
171 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
172 | QString subscript(int number) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
173 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
174 | return customNumberRep(number, subscripts, "₋"); |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
175 | } |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
176 | |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
177 | QString fractionRep(int numerator, int denominator) |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
178 | { |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
179 | return superscript(numerator) + "⁄" + subscript(denominator); |
eb2d3bc4fc73
better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents:
1326
diff
changeset
|
180 | } |