Tue, 28 Sep 2021 23:07:23 +0300
Use QSaveFile to save the file more safely
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | #pragma once |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | #include "basics.h" |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | namespace utility |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | template<typename T, std::size_t N> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | constexpr std::size_t countof(T(&)[N]) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | return N; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | // http://stackoverflow.com/a/18204188/3629665 |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | template<typename T> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | inline T rotl10(T x) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | return (x << 10) | ((x >> 22) & 0x000000ff); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | template<typename T> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | inline T rotl20(T x) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | return (x << 20) | ((x >> 12) & 0x000000ff); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | inline QString format(const QString& format_string) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | return format_string; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | template<typename T, typename... Rest> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | QString format(const QString& format_string, T&& arg, Rest&&... rest) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | return format(format_string.arg(arg), std::forward<Rest>(rest)...); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | inline QString quoted(QString string) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
55 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
56 | if (string.contains("'")) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
57 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
58 | string.replace("\"", "\\\""); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
59 | string = "\"" + string + "\""; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
61 | else |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | string = "'" + string + "'"; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
65 | return string; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
66 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
67 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
68 | template<typename T, typename R> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
69 | bool contains(T&& container, R&& value) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
70 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
71 | return std::find(std::begin(container), std::end(container), value) != std::end(container); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
72 | } |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
73 | |
141 | 74 | /** |
75 | * @brief Converts the specified vertex to a simple string | |
76 | * @param vertex vertex to convert | |
77 | * @return "x y z"-formatted string | |
78 | */ | |
79 | inline QString vertexToString(const glm::vec3& vertex) | |
80 | { | |
81 | return utility::format("%1 %2 %3", vertex.x, vertex.y, vertex.z); | |
82 | } | |
83 | ||
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
84 | inline QString vertexToStringParens(const glm::vec3& vertex) |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
85 | { |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
86 | return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
87 | } |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | } |