src/model.cpp

Wed, 08 Jun 2022 20:41:21 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Jun 2022 20:41:21 +0300
changeset 205
1a4342d80de7
parent 204
52e10e8d88cc
child 208
930928b760a2
permissions
-rw-r--r--

Refactor colors.cpp/.h

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
18
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "model.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
21 QString modelElementToString(const ModelElement &element)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
22 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
23 return std::visit(overloaded{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
24 [](const Colored<SubfileReference>& ref) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
25 return QStringLiteral("1 %1 %2 %3")
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
26 .arg(ref.color.index)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
27 .arg(transformToString(ref.transformation))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
28 .arg(ref.name);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
29 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
30 [](const Colored<LineSegment>& seg) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
31 return QStringLiteral("2 %1 %2 %3")
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
32 .arg(seg.color.index)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
33 .arg(vertexToString(seg.p1))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
34 .arg(vertexToString(seg.p2));
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
35 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
36 [](const Colored<Triangle>& triangle) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
37 return QStringLiteral("3 %1 %2 %3 %4")
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
38 .arg(triangle.color.index)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
39 .arg(vertexToString(triangle.p1))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
40 .arg(vertexToString(triangle.p2))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
41 .arg(vertexToString(triangle.p3));
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
42 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
43 [](const Colored<Quadrilateral>& quad) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
44 return QStringLiteral("4 %1 %2 %3 %4 %5")
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
45 .arg(quad.color.index)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
46 .arg(vertexToString(quad.p1))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
47 .arg(vertexToString(quad.p2))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
48 .arg(vertexToString(quad.p3))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
49 .arg(vertexToString(quad.p4));
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
50 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
51 [](const Colored<ConditionalEdge>& cedge) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
52 return QStringLiteral("5 %1 %2 %3 %4 %5")
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
53 .arg(cedge.color.index)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
54 .arg(vertexToString(cedge.p1))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
55 .arg(vertexToString(cedge.p2))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
56 .arg(vertexToString(cedge.c1))
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
57 .arg(vertexToString(cedge.c2));
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
58 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
59 [](const Comment& comment) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
60 return "0 " + comment.text;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
61 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
62 [](const Empty&) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
63 return QStringLiteral("");
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
64 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
65 [](const ParseError& parseError) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
66 return parseError.code;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
67 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
68 }, element);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
69 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
70
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
71 Model::Model(QObject *parent) :
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
72 QAbstractListModel{parent}
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
73 {
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
74 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
76 Model::~Model()
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
77 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
78 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
79
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
80 ModelId Model::append(const ModelElement &value)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
82 const int position = static_cast<int>(this->body.size());
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
83 const ModelId id = this->runningId;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
84 this->runningId.value += 1;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
85 Q_EMIT this->beginInsertRows({}, position, position);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
86 this->body.push_back({value, id});
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
87 this->positions[id] = position;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
88 Q_EMIT this->endInsertRows();
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
89 return id;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
90 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
91
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
92 const ModelElement &Model::at(int position) const
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
93 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
94 return this->body[position].data;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
97 ModelId Model::idAt(int position) const
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
98 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
99 return this->body[position].id;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
100 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
101
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
102 void Model::assignAt(int position, const ModelElement &element)
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
103 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
104 this->body[position].data = element;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
105 const QModelIndex index = this->index(position);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
106 Q_EMIT this->dataChanged(index, index);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
107 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
108
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
109 std::optional<int> Model::find(ModelId id) const
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
110 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
111 return pointerToOptional(findInMap(this->positions, id));
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
112 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
113
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
114 template<typename K, typename V>
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
115 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
116 {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
117 const auto it = map.find(key);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
118 if (it != map.end()) {
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
119 map.erase(it);
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
120 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
121 }
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
122
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
123 void Model::remove(int index)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
124 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
125 if (index >= 0 and index < this->size()) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
126 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
127 removeFromMap(this->positions, this->body[index].id);
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
128 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
129 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
130 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
131 }
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
132 Q_EMIT this->endRemoveRows();
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
133 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
134 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
135
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
136 int Model::rowCount(const QModelIndex &) const
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
137 {
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
138 return this->size();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
139 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
140
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
141 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
142 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
143 const int i = index.row();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
144 switch(role)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
145 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
146 /*
158
5bd755eaa5a8 Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
147 case Qt::DecorationRole:
5bd755eaa5a8 Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
148 return QPixmap{object->iconName()}.scaledToHeight(24);
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
149 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
150 case Qt::DisplayRole:
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
151 return modelElementToString(this->body[i].data);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
152 /*
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
153 case Qt::ForegroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
154 return object->textRepresentationForeground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
155 case Qt::BackgroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
156 return object->textRepresentationBackground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
157 case Qt::FontRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
158 return object->textRepresentationFont();
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
159 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
160 default:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
161 return {};
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
162 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
163 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
164
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
165 const ModelElement &Model::operator[](int index) const
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
166 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
167 return this->body[index].data;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
168 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
169
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
170 int Model::size() const
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
171 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
172 return this->body.size();
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
173 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
174
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
175 void save(const Model &model, QIODevice *device)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
176 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
177 QTextStream out{device};
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
178 for (int i = 0; i < model.size(); ++i) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
179 out << modelElementToString(model[i]) << "\r\n";
173
8a3047468994 Fix performance issues in Model::find
Teemu Piippo <teemu@hecknology.net>
parents: 159
diff changeset
180 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
181 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
182
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
183 /**
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
184 * @brief Sets the path to the model
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
185 * @param path New path to use
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
186 */
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
187 void updateHeaderNameField(Model& model, const QString &name)
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
188 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
189 // Update the "Name: 1234.dat" comment
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
190 if (model.size() >= 2) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
191 if (const Comment* nameObject = std::get_if<Comment>(&model[1])) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
192 if (nameObject->text.startsWith("Name: ")) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
193 model[1] = Comment{"Name: " + name};
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
194 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
195 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
196 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
197 }

mercurial