Mon, 04 Jul 2022 15:37:22 +0300
Only enable the text editor when having a model open
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> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
251
diff
changeset
|
20 | #include "src/model.h" |
3 | 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 | { |
242
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
41 | return circularPrimitiveStems[circ.type]; |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
42 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
43 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
44 | static QString circularPrimitiveFilePath(const CircularPrimitive& circ) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
45 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
46 | QString result; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
47 | if (circ.fraction.divisions != 16) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
48 | 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
|
49 | } |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
50 | const unsigned int factor = gcd(circ.fraction.segments, circ.fraction.divisions); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
51 | unsigned int num = circ.fraction.segments / factor; |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
52 | unsigned int denom = circ.fraction.divisions / factor; |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
53 | if (denom < 4) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
54 | num *= 4 / denom; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
55 | denom = 4; |
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 | 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
|
58 | result += QString::fromLatin1(circularPrimitiveTypeString(circ)); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
59 | result += QStringLiteral(".dat"); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
60 | return result; |
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 | |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
63 | 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
|
64 | { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
65 | return std::visit(overloaded{ |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
66 | [](const Colored<SubfileReference>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
67 | return ":/icons/linetype-subfile.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
68 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
69 | [](const Colored<LineSegment>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
70 | return ":/icons/linetype-edgeline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
71 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
72 | [](const Colored<Triangle>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
73 | return ":/icons/linetype-triangle.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
74 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
75 | [](const Colored<Quadrilateral>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
76 | return ":/icons/linetype-quadrilateral.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
77 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
78 | [](const Colored<ConditionalEdge>&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
79 | return ":/icons/linetype-conditionaledge.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
80 | }, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
81 | [](const Colored<CircularPrimitive>&) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
82 | return ":/icons/linetype-circularprimitive.png"; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
83 | }, |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
84 | [](const Comment&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
85 | return ":/icons/chatbubble-ellipses-outline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
86 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
87 | [](const Empty&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
88 | return ""; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
89 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
90 | [](const ParseError&) { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
91 | return ":/icons/linetype-errorline.png"; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
92 | }, |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
93 | }, element); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
94 | } |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
95 | |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
96 | static QPixmap iconForElement(const ModelElement& element) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
97 | { |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
98 | // 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
|
99 | // 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
|
100 | // 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
|
101 | // 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
|
102 | return std::visit([](auto&& element){ |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
103 | static const QPixmap pixmap = QPixmap::fromImage( |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
104 | QImage{iconPathForElement(element)} |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
105 | .scaledToHeight(24, Qt::SmoothTransformation) |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
106 | ); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
107 | return pixmap; |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
108 | }, element); |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
109 | } |
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
110 | |
200 | 111 | QString modelElementToString(const ModelElement &element) |
112 | { | |
113 | return std::visit(overloaded{ | |
114 | [](const Colored<SubfileReference>& ref) { | |
326
65dbfcb859a7
Add missing 0 BFC INVERTNEXT when writing out subfile references
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
115 | QString result; |
65dbfcb859a7
Add missing 0 BFC INVERTNEXT when writing out subfile references
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
116 | if (ref.inverted) { |
65dbfcb859a7
Add missing 0 BFC INVERTNEXT when writing out subfile references
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
117 | result += QStringLiteral("0 BFC INVERTNEXT\r\n"); |
65dbfcb859a7
Add missing 0 BFC INVERTNEXT when writing out subfile references
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
118 | } |
65dbfcb859a7
Add missing 0 BFC INVERTNEXT when writing out subfile references
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
119 | result += QStringLiteral("1 %1 %2 %3") |
200 | 120 | .arg(ref.color.index) |
121 | .arg(transformToString(ref.transformation)) | |
122 | .arg(ref.name); | |
326
65dbfcb859a7
Add missing 0 BFC INVERTNEXT when writing out subfile references
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
123 | return result; |
200 | 124 | }, |
125 | [](const Colored<LineSegment>& seg) { | |
126 | return QStringLiteral("2 %1 %2 %3") | |
127 | .arg(seg.color.index) | |
128 | .arg(vertexToString(seg.p1)) | |
129 | .arg(vertexToString(seg.p2)); | |
130 | }, | |
131 | [](const Colored<Triangle>& triangle) { | |
132 | return QStringLiteral("3 %1 %2 %3 %4") | |
133 | .arg(triangle.color.index) | |
134 | .arg(vertexToString(triangle.p1)) | |
135 | .arg(vertexToString(triangle.p2)) | |
136 | .arg(vertexToString(triangle.p3)); | |
137 | }, | |
138 | [](const Colored<Quadrilateral>& quad) { | |
139 | return QStringLiteral("4 %1 %2 %3 %4 %5") | |
140 | .arg(quad.color.index) | |
141 | .arg(vertexToString(quad.p1)) | |
142 | .arg(vertexToString(quad.p2)) | |
143 | .arg(vertexToString(quad.p3)) | |
144 | .arg(vertexToString(quad.p4)); | |
145 | }, | |
146 | [](const Colored<ConditionalEdge>& cedge) { | |
147 | return QStringLiteral("5 %1 %2 %3 %4 %5") | |
148 | .arg(cedge.color.index) | |
149 | .arg(vertexToString(cedge.p1)) | |
150 | .arg(vertexToString(cedge.p2)) | |
151 | .arg(vertexToString(cedge.c1)) | |
152 | .arg(vertexToString(cedge.c2)); | |
153 | }, | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
154 | [](const Colored<CircularPrimitive>& circ) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
155 | return QStringLiteral("1 %1 %2 %3") |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
156 | .arg(circ.color.index) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
157 | .arg(transformToString(circ.transformation)) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
158 | .arg(circularPrimitiveFilePath(circ)); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
208
diff
changeset
|
159 | }, |
200 | 160 | [](const Comment& comment) { |
161 | return "0 " + comment.text; | |
162 | }, | |
163 | [](const Empty&) { | |
164 | return QStringLiteral(""); | |
165 | }, | |
166 | [](const ParseError& parseError) { | |
167 | return parseError.code; | |
168 | }, | |
169 | }, element); | |
170 | } | |
171 | ||
148 | 172 | Model::Model(QObject *parent) : |
173 | QAbstractListModel{parent} | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
174 | { |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
76
diff
changeset
|
175 | } |
3 | 176 | |
200 | 177 | Model::~Model() |
178 | { | |
179 | } | |
180 | ||
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
181 | ElementId Model::append(const ModelElement &value) |
3 | 182 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
183 | const std::size_t position = this->size(); |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
184 | const ElementId id = this->runningId; |
200 | 185 | this->runningId.value += 1; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
186 | const int row = narrow<int>(signed_cast(this->size())); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
187 | Q_EMIT this->beginInsertRows({}, row, row); |
200 | 188 | this->body.push_back({value, id}); |
189 | this->positions[id] = position; | |
190 | Q_EMIT this->endInsertRows(); | |
191 | return id; | |
192 | } | |
193 | ||
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
194 | const ModelElement &Model::at(std::size_t position) const |
200 | 195 | { |
196 | return this->body[position].data; | |
3 | 197 | } |
198 | ||
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
199 | ElementId Model::idAt(std::size_t position) const |
200 | 200 | { |
201 | return this->body[position].id; | |
202 | } | |
203 | ||
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
204 | void Model::assignAt(std::size_t position, const ModelElement &element) |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
205 | { |
200 | 206 | this->body[position].data = element; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
207 | const QModelIndex index = this->index(narrow<int>(signed_cast(position))); |
200 | 208 | Q_EMIT this->dataChanged(index, index); |
209 | } | |
210 | ||
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
211 | std::optional<std::size_t> Model::find(ElementId id) const |
200 | 212 | { |
213 | return pointerToOptional(findInMap(this->positions, id)); | |
214 | } | |
215 | ||
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
216 | template<typename K, typename V> |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
217 | 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
|
218 | { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
219 | const auto it = map.find(key); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
220 | if (it != map.end()) { |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
221 | map.erase(it); |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
222 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
223 | } |
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
224 | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
225 | void Model::remove(const std::size_t index) |
200 | 226 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
227 | if (index < this->body.size()) { |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
228 | const int row = narrow<int>(signed_cast(index)); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
229 | Q_EMIT this->beginRemoveRows({}, row, row); |
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
230 | removeFromMap(this->positions, this->body[index].id); |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
231 | this->body.erase(this->body.begin() + row); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
232 | for (std::size_t i = index; i < this->body.size(); ++i) { |
204
52e10e8d88cc
Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents:
200
diff
changeset
|
233 | 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
|
234 | } |
200 | 235 | Q_EMIT this->endRemoveRows(); |
133
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
236 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
237 | } |
e39326ee48dc
Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents:
112
diff
changeset
|
238 | |
200 | 239 | int Model::rowCount(const QModelIndex &) const |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
240 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
241 | return narrow<int>(signed_cast(this->size())); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
242 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
243 | |
200 | 244 | 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
|
245 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
246 | const std::size_t i = unsigned_cast(index.row()); |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
247 | const ModelElement& element = this->body[i].data; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
248 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
249 | { |
158
5bd755eaa5a8
Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents:
153
diff
changeset
|
250 | case Qt::DecorationRole: |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
251 | return iconForElement(element); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
252 | case Qt::DisplayRole: |
208
930928b760a2
Add model icons back into the list view
Teemu Piippo <teemu@hecknology.net>
parents:
204
diff
changeset
|
253 | return modelElementToString(element); |
200 | 254 | /* |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
255 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
256 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
257 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
258 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
259 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
260 | return object->textRepresentationFont(); |
200 | 261 | */ |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
262 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
263 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
264 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
265 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
266 | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
267 | const ModelElement &Model::operator[](std::size_t index) const |
200 | 268 | { |
269 | return this->body[index].data; | |
270 | } | |
271 | ||
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
272 | std::size_t Model::size() const |
51 | 273 | { |
200 | 274 | return this->body.size(); |
275 | } | |
276 | ||
328
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
277 | void Model::clear() |
200 | 278 | { |
328
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
279 | this->beginResetModel(); |
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
280 | this->body.clear(); |
329
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
281 | this->positions.clear(); |
6d75fa09cc0c
fix some issues like subfile references not showing up properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
328
diff
changeset
|
282 | this->runningId = {1}; |
328
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
283 | this->endResetModel(); |
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
284 | } |
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
285 | |
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
286 | void save(const Model &model, QTextStream* stream) |
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
287 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
288 | for (std::size_t i = 0; i < model.size(); ++i) { |
328
3ea38fd469ca
Replace item view with a text editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
326
diff
changeset
|
289 | (*stream) << modelElementToString(model[i]) << "\r\n"; |
173
8a3047468994
Fix performance issues in Model::find
Teemu Piippo <teemu@hecknology.net>
parents:
159
diff
changeset
|
290 | } |
51 | 291 | } |
292 | ||
137
fb9990772357
Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents:
133
diff
changeset
|
293 | /** |
141 | 294 | * @brief Sets the path to the model |
295 | * @param path New path to use | |
296 | */ | |
200 | 297 | void updateHeaderNameField(Model& model, const QString &name) |
141 | 298 | { |
299 | // Update the "Name: 1234.dat" comment | |
200 | 300 | if (model.size() >= 2) { |
301 | if (const Comment* nameObject = std::get_if<Comment>(&model[1])) { | |
302 | if (nameObject->text.startsWith("Name: ")) { | |
303 | model[1] = Comment{"Name: " + name}; | |
141 | 304 | } |
305 | } | |
306 | } | |
307 | } |