src/model.cpp

Sat, 05 Mar 2022 13:32:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:32:58 +0200
changeset 163
36ea1a8aee33
parent 159
1a04364d20b5
child 173
8a3047468994
permissions
-rw-r--r--

Add icons for tools

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
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
19 #include <QBrush>
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
20 #include <QFile>
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
21 #include <QFileInfo>
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
22 #include <QFont>
145
4dea24d3eda0 Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
23 #include <QSaveFile>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include "model.h"
153
2f79053c2e9a Renamed modeleditcontext.cpp -> modeleditor.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 152
diff changeset
25 #include "modeleditor.h"
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
26 #include "documentmanager.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
28 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
29 * @brief Constructs a model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
30 * @param parent QObject parent to pass forward
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
31 */
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
32 Model::Model(QObject *parent) :
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
33 QAbstractListModel{parent}
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
34 {
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
35 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
37 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
38 * @returns the amount of elements in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
39 */
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 int Model::size() const
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 {
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
42 return static_cast<int>(this->body.size());
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
45 /**
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
46 * @brief Looks up the object ID at the specified index. If out of bounds, returns NULL_ID.
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
47 * @param index Index of object to look up
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
48 * @return object ID
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
49 */
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
50 ldraw::id_t Model::at(int index) const
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
51 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
52 if (index >= 0 and index < this->size())
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
53 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
54 return this->body[index]->id;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
55 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
56 else
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
57 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
58 return ldraw::NULL_ID;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
59 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
60 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
61
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
62 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
63 * @brief @overload QAbstractListModel::rowCount
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
64 * @return size
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
65 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
66 int Model::rowCount(const QModelIndex&) const
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
67 {
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
68 return this->size();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
69 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
70
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
71 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
72 * @brief @overload QAbstractListModel::data
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
73 * @param index
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
74 * @param role
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
75 * @return QVariant
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
76 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
77 QVariant Model::data(const QModelIndex& index, int role) const
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
78 {
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
79 const ldraw::Object* object = (*this)[index.row()];
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
80 switch(role)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
81 {
158
5bd755eaa5a8 Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
82 case Qt::DecorationRole:
5bd755eaa5a8 Add icons from ionicons
Teemu Piippo <teemu@hecknology.net>
parents: 153
diff changeset
83 return QPixmap{object->iconName()}.scaledToHeight(24);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
84 case Qt::DisplayRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
85 return object->textRepresentation();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
86 case Qt::ForegroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
87 return object->textRepresentationForeground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
88 case Qt::BackgroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
89 return object->textRepresentationBackground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
90 case Qt::FontRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
91 return object->textRepresentationFont();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
92 default:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
93 return {};
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
94 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
95 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
96
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
97 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
98 * @brief Finds the position of the specified object in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
99 * @param id Object id to look for
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
100 * @return model index
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
101 */
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
102 QModelIndex Model::find(ldraw::id_t id) const
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
103 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
104 // FIXME: This linear search will probably cause performance issues
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
105 for (std::size_t i = 0; i < this->body.size(); i += 1)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
106 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
107 if (this->body[i]->id == id)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
108 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
109 return this->index(static_cast<int>(i));
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
110 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
111 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
112 return {};
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
113 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
114
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
115 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
116 * @brief Gets an object id by position in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
117 * @param index Position of the object in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
118 * @return id
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
119 */
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
120 ldraw::id_t Model::idAt(const QModelIndex& index) const
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
121 {
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
122 return (*this)[index.row()]->id;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
123 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
124
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
125 #if 0
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
126 /**
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
127 * @brief Sets the path to the model
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
128 * @param path New path to use
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
129 */
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
130 void Model::setPath(const QString &path)
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
131 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
132 this->storedPath = path;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
133 this->header.name = QFileInfo{path}.fileName();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
134 // Update the "Name: 1234.dat" comment
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
135 if (this->body.size() >= 2)
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
136 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
137 const ldraw::id_t id = this->body[1]->id;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
138 if (this->isA<ldraw::MetaCommand>(id))
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
139 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
140 const QString& textBody = this->body[1]->getProperty<ldraw::Property::Text>();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
141 if (textBody.startsWith("Name: "))
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
142 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
143 auto editor = this->edit();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
144 editor.setObjectProperty<ldraw::Property::Text>(id, "Name: " + this->header.name);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
145 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
146 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
147 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
148 }
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
149 #endif
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
150
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
151 /**
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
152 * @brief Adds the given object into the model.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
153 * @param object r-value reference to the object
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
154 */
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
155 ldraw::id_t Model::append(ModelObjectPointer&& object)
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
156 {
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
157 const int position = static_cast<int>(this->body.size());
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
158 Q_EMIT this->beginInsertRows({}, position, position);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
159 this->body.push_back(std::move(object));
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
160 Q_EMIT this->endInsertRows();
159
1a04364d20b5 fix crash when loading a file
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
161 const ldraw::id_t id = this->body.back()->id;
1a04364d20b5 fix crash when loading a file
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
162 this->objectsById[id] = this->body.back().get();
1a04364d20b5 fix crash when loading a file
Teemu Piippo <teemu@hecknology.net>
parents: 158
diff changeset
163 return id;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
164 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
165
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
166 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
167 * @brief Removes the object at the specified position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
168 * @param position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
169 */
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
170 void Model::remove(int position)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
171 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
172 if (position >= 0 and position < signed_cast(this->body.size()))
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
173 {
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
174 Q_EMIT this->beginRemoveRows({}, position, position);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
175 this->body.erase(std::begin(this->body) + position);
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
176 Q_EMIT this->endRemoveRows();
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
177 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
178 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
179
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
180 void Model::emitDataChangedSignal(int position)
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
181 {
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
182 Q_EMIT this->dataChanged(this->index(position), this->index(position));
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
183 }
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
184
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
185 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
186 * @brief Gets the object pointer at the specified position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
187 * @param index Position of the object
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
188 * @returns object pointer
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
189 */
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
190 ldraw::Object* Model::operator[](int index)
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
191 {
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
192 if (index >= 0 and index < this->size())
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
193 {
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
194 return this->body[index].get();
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
195 }
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
196 else
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
197 {
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
198 throw std::out_of_range{"index out of range"};
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
199 }
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
200 }
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
201
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
202 /**
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
203 * @brief Gets the object pointer at the specified position
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
204 * @param index Position of the object
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
205 * @returns object pointer
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
206 */
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
207 const ldraw::Object* Model::operator[](int index) const
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
208 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
209 if (index >= 0 and index < this->size())
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
210 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
211 return this->body[index].get();
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
212 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
213 else
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
214 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
215 throw std::out_of_range{"index out of range"};
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
216 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
217 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
218
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
219 /**
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
220 * @brief Gets an object pointer by id. Used by the editing context to actually modify objects.
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
221 * @param id
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
222 * @return object pointer
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
223 */
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
224 ldraw::Object* Model::findObjectById(const ldraw::id_t id)
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
225 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
226 const QModelIndex index = this->find(id);
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
227 if (index.isValid())
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
228 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
229 return (*this)[index.row()];
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
230 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
231 else
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
232 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
233 return nullptr;
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
234 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
235 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
236
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
237 const ldraw::Object* Model::findObjectById(const ldraw::id_t id) const
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
238 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
239 const QModelIndex index = this->find(id);
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
240 if (index.isValid())
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
241 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
242 return (*this)[index.row()];
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
243 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
244 else
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
245 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
246 return nullptr;
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
247 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
248 }
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
249
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
250 /**
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
251 * @brief Attempts the save the model
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
252 */
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
253 void save(const Model &model, QIODevice *device)
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
254 {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
255 QTextStream out{device};
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
256 applyToModel<ldraw::Object>(model, [&](const ldraw::Object* object) {
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
257 out << object->toLDrawCode() << "\r\n";
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
258 });
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
259 }

mercurial