Mon, 20 Jun 2022 16:43:56 +0300
fix various things
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 | ||
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
19 | #include <QPixmap> |
3 | 20 | #include "model.h" |
21 | ||
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
22 | constexpr unsigned int gcd(unsigned int a, unsigned int b) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
23 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
24 | while (a != b) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
25 | if (b > a) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
26 | b -= a; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
27 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
28 | else if (a > b) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
29 | a -= b; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
30 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
31 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
32 | return a; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
33 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
34 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
35 | static_assert(gcd(16, 15) == 1); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
36 | static_assert(gcd(16, 4) == 4); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
37 | static_assert(gcd(272, 192) == 16); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
38 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
39 | static constexpr const char* circularPrimitiveTypeString(const CircularPrimitive& circ) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
40 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
41 | switch (circ.type) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
42 | case CircularPrimitive::Circle: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
43 | return "edge"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
44 | case CircularPrimitive::Disc: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
45 | return "disc"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
46 | case CircularPrimitive::Cylinder: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
47 | return "cyli"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
48 | case CircularPrimitive::CylinderOpen: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
49 | return "cylo"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
50 | case CircularPrimitive::CylinderClosed: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
51 | return "cylc"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
52 | case CircularPrimitive::DiscNegative: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
53 | return "ndis"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
54 | case CircularPrimitive::Chord: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
55 | return "chrd"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
56 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
57 | return ""; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
58 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
59 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
60 | static QString circularPrimitiveFilePath(const CircularPrimitive& circ) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
61 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
62 | QString result; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
63 | if (circ.fraction.divisions != 16) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
64 | result += QString::number(circ.fraction.divisions) + QStringLiteral("\\"); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
65 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
66 | const int factor = gcd(circ.fraction.segments, circ.fraction.divisions); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
67 | int num = circ.fraction.segments / factor; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
68 | int denom = circ.fraction.divisions / factor; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
69 | if (denom < 4) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
70 | num *= 4 / denom; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
71 | denom = 4; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
72 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
73 | result += QStringLiteral("%1-%2").arg(num).arg(denom); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
74 | result += QString::fromLatin1(circularPrimitiveTypeString(circ)); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
75 | result += QStringLiteral(".dat"); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
76 | return result; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
77 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
78 | |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
79 | static const char* iconPathForElement(const ModelElement& element) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
80 | { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
81 | return std::visit(overloaded{ |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
82 | [](const Colored<SubfileReference>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
83 | return ":/icons/linetype-subfile.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
84 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
85 | [](const Colored<LineSegment>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
86 | return ":/icons/linetype-edgeline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
87 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
88 | [](const Colored<Triangle>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
89 | return ":/icons/linetype-triangle.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
90 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
91 | [](const Colored<Quadrilateral>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
92 | return ":/icons/linetype-quadrilateral.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
93 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
94 | [](const Colored<ConditionalEdge>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
95 | return ":/icons/linetype-conditionaledge.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
96 | }, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
97 | [](const Colored<CircularPrimitive>&) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
98 | return ":/icons/linetype-circularprimitive.png"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
99 | }, |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
100 | [](const Comment&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
101 | return ":/icons/chatbubble-ellipses-outline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
102 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
103 | [](const Empty&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
104 | return ""; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
105 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
106 | [](const ParseError&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
107 | return ":/icons/linetype-errorline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
108 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
109 | }, element); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
110 | } |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
111 | |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
112 | static QPixmap iconForElement(const ModelElement& element) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
113 | { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
114 | // We avoid processing the same image over and over again by storing it |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
115 | // in a static constant. However, we need one per each possible type |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
116 | // of ModelElement, so we put the pixmap constant inside a templated lambda, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
117 | // which gets instiated once for each type of ModelElement. |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
118 | return std::visit([](auto&& element){ |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
119 | static const QPixmap pixmap = QPixmap::fromImage( |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
120 | QImage{iconPathForElement(element)} |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
121 | .scaledToHeight(24, Qt::SmoothTransformation) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
122 | ); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
123 | return pixmap; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
124 | }, element); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
125 | } |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
126 | |
200 | 127 | QString modelElementToString(const ModelElement &element) |
128 | { | |
129 | return std::visit(overloaded{ | |
130 | [](const Colored<SubfileReference>& ref) { | |
131 | return QStringLiteral("1 %1 %2 %3") | |
132 | .arg(ref.color.index) | |
133 | .arg(transformToString(ref.transformation)) | |
134 | .arg(ref.name); | |
135 | }, | |
136 | [](const Colored<LineSegment>& seg) { | |
137 | return QStringLiteral("2 %1 %2 %3") | |
138 | .arg(seg.color.index) | |
139 | .arg(vertexToString(seg.p1)) | |
140 | .arg(vertexToString(seg.p2)); | |
141 | }, | |
142 | [](const Colored<Triangle>& triangle) { | |
143 | return QStringLiteral("3 %1 %2 %3 %4") | |
144 | .arg(triangle.color.index) | |
145 | .arg(vertexToString(triangle.p1)) | |
146 | .arg(vertexToString(triangle.p2)) | |
147 | .arg(vertexToString(triangle.p3)); | |
148 | }, | |
149 | [](const Colored<Quadrilateral>& quad) { | |
150 | return QStringLiteral("4 %1 %2 %3 %4 %5") | |
151 | .arg(quad.color.index) | |
152 | .arg(vertexToString(quad.p1)) | |
153 | .arg(vertexToString(quad.p2)) | |
154 | .arg(vertexToString(quad.p3)) | |
155 | .arg(vertexToString(quad.p4)); | |
156 | }, | |
157 | [](const Colored<ConditionalEdge>& cedge) { | |
158 | return QStringLiteral("5 %1 %2 %3 %4 %5") | |
159 | .arg(cedge.color.index) | |
160 | .arg(vertexToString(cedge.p1)) | |
161 | .arg(vertexToString(cedge.p2)) | |
162 | .arg(vertexToString(cedge.c1)) | |
163 | .arg(vertexToString(cedge.c2)); | |
164 | }, | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
165 | [](const Colored<CircularPrimitive>& circ) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
166 | return QStringLiteral("1 %1 %2 %3") |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
167 | .arg(circ.color.index) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
168 | .arg(transformToString(circ.transformation)) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
169 | .arg(circularPrimitiveFilePath(circ)); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
170 | }, |
200 | 171 | [](const Comment& comment) { |
172 | return "0 " + comment.text; | |
173 | }, | |
174 | [](const Empty&) { | |
175 | return QStringLiteral(""); | |
176 | }, | |
177 | [](const ParseError& parseError) { | |
178 | return parseError.code; | |
179 | }, | |
180 | }, element); | |
181 | } | |
182 | ||
148 | 183 | Model::Model(QObject *parent) : |
184 | QAbstractListModel{parent} | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
185 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
186 | } |
3 | 187 | |
200 | 188 | Model::~Model() |
189 | { | |
190 | } | |
191 | ||
192 | ModelId Model::append(const ModelElement &value) | |
3 | 193 | { |
200 | 194 | const int position = static_cast<int>(this->body.size()); |
195 | const ModelId id = this->runningId; | |
196 | this->runningId.value += 1; | |
197 | Q_EMIT this->beginInsertRows({}, position, position); | |
198 | this->body.push_back({value, id}); | |
199 | this->positions[id] = position; | |
200 | Q_EMIT this->endInsertRows(); | |
201 | return id; | |
202 | } | |
203 | ||
204 | const ModelElement &Model::at(int position) const | |
205 | { | |
206 | return this->body[position].data; | |
3 | 207 | } |
208 | ||
200 | 209 | ModelId Model::idAt(int position) const |
210 | { | |
211 | return this->body[position].id; | |
212 | } | |
213 | ||
214 | void Model::assignAt(int position, const ModelElement &element) | |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
215 | { |
200 | 216 | this->body[position].data = element; |
217 | const QModelIndex index = this->index(position); | |
218 | Q_EMIT this->dataChanged(index, index); | |
219 | } | |
220 | ||
221 | std::optional<int> Model::find(ModelId id) const | |
222 | { | |
223 | return pointerToOptional(findInMap(this->positions, id)); | |
224 | } | |
225 | ||
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
226 | template<typename K, typename V> |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
227 | void removeFromMap(std::map<K, V>& map, const K& key) |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
228 | { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
229 | const auto it = map.find(key); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
230 | if (it != map.end()) { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
231 | map.erase(it); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
232 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
233 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
234 | |
200 | 235 | void Model::remove(int index) |
236 | { | |
237 | if (index >= 0 and index < this->size()) { | |
238 | Q_EMIT this->beginRemoveRows({}, index, index); | |
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
239 | removeFromMap(this->positions, this->body[index].id); |
200 | 240 | this->body.erase(this->body.begin() + index); |
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
241 | for (int i = index; i < this->size(); ++i) { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
242 | this->positions[this->body[i].id] = i; |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
243 | } |
200 | 244 | Q_EMIT this->endRemoveRows(); |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
245 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
246 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
247 | |
200 | 248 | int Model::rowCount(const QModelIndex &) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
249 | { |
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
250 | return this->size(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
251 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
252 | |
200 | 253 | QVariant Model::data(const QModelIndex &index, int role) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
254 | { |
200 | 255 | const int i = index.row(); |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
256 | const ModelElement& element = this->body[i].data; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
257 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
258 | { |
158
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
259 | case Qt::DecorationRole: |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
260 | return iconForElement(element); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
261 | case Qt::DisplayRole: |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
262 | return modelElementToString(element); |
200 | 263 | /* |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
264 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
265 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
266 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
267 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
268 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
269 | return object->textRepresentationFont(); |
200 | 270 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
271 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
272 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
273 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
274 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
275 | |
200 | 276 | const ModelElement &Model::operator[](int index) const |
277 | { | |
278 | return this->body[index].data; | |
279 | } | |
280 | ||
281 | int Model::size() const | |
51 | 282 | { |
200 | 283 | return this->body.size(); |
284 | } | |
285 | ||
286 | void save(const Model &model, QIODevice *device) | |
287 | { | |
288 | QTextStream out{device}; | |
289 | for (int i = 0; i < model.size(); ++i) { | |
290 | out << modelElementToString(model[i]) << "\r\n"; | |
173
8a3047468994
Fix performance issues in Model::find
Teemu Piippo <teemu@hecknology.net>
parents:
159
diff
changeset
|
291 | } |
51 | 292 | } |
293 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
294 | /** |
141 | 295 | * @brief Sets the path to the model |
296 | * @param path New path to use | |
297 | */ | |
200 | 298 | void updateHeaderNameField(Model& model, const QString &name) |
141 | 299 | { |
300 | // Update the "Name: 1234.dat" comment | |
200 | 301 | if (model.size() >= 2) { |
302 | if (const Comment* nameObject = std::get_if<Comment>(&model[1])) { | |
303 | if (nameObject->text.startsWith("Name: ")) { | |
304 | model[1] = Comment{"Name: " + name}; | |
141 | 305 | } |
306 | } | |
307 | } | |
308 | } |