src/model.cpp

Tue, 01 Mar 2022 17:00:19 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 01 Mar 2022 17:00:19 +0200
changeset 149
008989bc7d6e
parent 148
e1ced2523cad
child 150
b6cbba6e29a1
permissions
-rw-r--r--

work on edit history

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"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 #include "modeleditcontext.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 connect(this, &Model::dataChanged, [&](){ this->needRecache = true; });
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
36 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
38 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
39 * @returns the amount of elements in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
40 */
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 int Model::size() const
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
43 return static_cast<int>(this->body.size());
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
46 /**
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
47 * @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
48 * @param index Index of object to look up
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
49 * @return object ID
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
50 */
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
51 ldraw::id_t Model::at(int index) const
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
52 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
53 if (index >= 0 and index < this->size())
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
54 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
55 return this->body[index]->id;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
56 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
57 else
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
58 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
59 return ldraw::NULL_ID;
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 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
62
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
63 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
64 * @brief Obtains an editing context for this model.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
65 * Editing contexts are used to perform modifications to the model.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
66 * @return editing context
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
67 */
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 Model::EditContext Model::edit()
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
70 this->editCounter += 1;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 return {*this};
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
73
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
74 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
75 * @brief @overload QAbstractListModel::rowCount
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
76 * @return size
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
77 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
78 int Model::rowCount(const QModelIndex&) const
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
79 {
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
80 return this->size();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
81 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
82
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
83 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
84 * @brief @overload QAbstractListModel::data
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
85 * @param index
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
86 * @param role
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
87 * @return QVariant
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
88 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
89 QVariant Model::data(const QModelIndex& index, int role) const
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
90 {
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
91 const ldraw::Object* object = this->objectAt(index);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
92 switch(role)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
93 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
94 case Qt::DisplayRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
95 return object->textRepresentation();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
96 case Qt::ForegroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
97 return object->textRepresentationForeground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
98 case Qt::BackgroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
99 return object->textRepresentationBackground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
100 case Qt::FontRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
101 return object->textRepresentationFont();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
102 default:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
103 return {};
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
104 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
105 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
106
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
107 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
108 * @brief Gets a property of the header
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
109 * @param property
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
110 * @return QVariant
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
111 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
112 QVariant Model::getHeaderProperty(const HeaderProperty property)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
113 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
114 switch (property)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
115 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
116 case HeaderProperty::Name:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
117 return header.name;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
118 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
119 return {};
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
120 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
121
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
122 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
123 * @brief Gets the specified property from the object at the specified index in the model.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
124 * @param index Index of object in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
125 * @param property Property to look up
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
126 * @return QVariant
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
127 */
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
128 QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
129 {
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
130 const ldraw::Object* object = this->body[unsigned_cast(index)].get();
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
131 return object->getProperty(property);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
132 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
133
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
134 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
135 * @brief Gets a list of GL polygons that are used to represent this model.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
136 * @details Will build polygons if polygons are outdated.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
137 * @param documents Documents to use to resolve subfile references
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
138 * @return vector of GL polygons
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
139 */
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
140 std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
141 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
142 if (this->needRecache)
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
143 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
144 this->cachedPolygons.clear();
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
145 const std::optional<ModelId> modelId = documents->findIdForModel(this);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
146 if (modelId.has_value())
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
147 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
148 ldraw::GetPolygonsContext context{modelId.value(), documents};
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
149 for (int i = 0; i < this->size(); i += 1)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
150 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
151 this->getObjectPolygons(i, this->cachedPolygons, &context);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
152 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
153 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
154 this->needRecache = false;
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
155 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
156 return this->cachedPolygons;
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
157 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
158
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
159 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
160 * @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
161 * @param id Object id to look for
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
162 * @return model index
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
163 */
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
164 QModelIndex Model::lookup(ldraw::id_t id) const
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
165 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
166 // FIXME: This linear search will probably cause performance issues
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
167 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
168 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
169 if (this->body[i]->id == id)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
170 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
171 return this->index(static_cast<int>(i));
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
172 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
173 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
174 return {};
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
175 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
176
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
177 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
178 * @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
179 * @param index Position of the object in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
180 * @return id
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
181 */
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
182 ldraw::id_t Model::resolve(const QModelIndex& index) const
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
183 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
184 return this->objectAt(index)->id;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
185 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
186
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
187 #if 0
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 * @brief Sets the path to the model
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
190 * @param path New path to use
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
191 */
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
192 void Model::setPath(const QString &path)
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
193 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
194 this->storedPath = path;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
195 this->header.name = QFileInfo{path}.fileName();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
196 // Update the "Name: 1234.dat" comment
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
197 if (this->body.size() >= 2)
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
198 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
199 const ldraw::id_t id = this->body[1]->id;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
200 if (this->isA<ldraw::MetaCommand>(id))
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
201 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
202 const QString& textBody = this->body[1]->getProperty<ldraw::Property::Text>();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
203 if (textBody.startsWith("Name: "))
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
204 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
205 auto editor = this->edit();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
206 editor.setObjectProperty<ldraw::Property::Text>(id, "Name: " + this->header.name);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
207 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
208 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
209 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
210 }
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
211 #endif
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
212
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
213 /**
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
214 * @brief Gets the GL polygons of the object at the specified position in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
215 * @param index Index of object in the model
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
216 * @param polygons_out Vector to add polygons into
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
217 * @param context Context to use to resolve subfile references
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
218 */
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
219 void Model::getObjectPolygons(
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
220 const int index,
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
221 std::vector<gl::Polygon>& polygons_out,
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
222 ldraw::GetPolygonsContext* context) const
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
223 {
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
224 const ldraw::Object* object = this->body[unsigned_cast(index)].get();
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
225 object->getPolygons(polygons_out, context);
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
226 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
227
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
228 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
229 * @brief Called by the editing context to signal to the model that editing is done.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
230 */
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
231 void Model::editFinished()
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
232 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
233 this->editCounter -= 1;
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
234 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
235
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
236 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
237 * @brief Called by the editing context to indicate that the specified object has been modified.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
238 * @param id ID of the object that has been modified
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
239 */
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
240 void Model::objectModified(ldraw::id_t id)
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
241 {
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
242 const QModelIndex index = this->lookup(id);
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
243 Q_EMIT this->dataChanged(index, index);
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
244 }
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
245
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
246 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
247 * @brief Adds the given object into the model.
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
248 * @param object r-value reference to the object
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
249 */
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
250 void Model::append(ModelObjectPointer&& object)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
251 {
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
252 const int position = static_cast<int>(this->body.size());
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
253 Q_EMIT beginInsertRows({}, position, position);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
254 this->body.push_back(std::move(object));
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
255 Q_EMIT endInsertRows();
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
256 this->needRecache = true;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
257 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
258
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
259 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
260 * @brief Removes the object at the specified position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
261 * @param position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
262 */
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
263 void Model::remove(int position)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
264 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
265 if (position >= 0 and position < signed_cast(this->body.size()))
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
266 {
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
267 Q_EMIT beginRemoveRows({}, position, position);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
268 this->body.erase(std::begin(this->body) + position);
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
269 Q_EMIT endRemoveRows();
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 86
diff changeset
270 this->needRecache = true;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
271 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
272 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
273
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
274 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
275 * @brief Gets the object pointer at the specified position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
276 * @param index Position of the object
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
277 * @returns object pointer
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
278 */
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
279 ldraw::Object* Model::objectAt(const QModelIndex& index)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
280 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
281 return this->body[unsigned_cast(index.row())].get();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
282 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
283
137
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
284 /**
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
285 * @brief Gets the object pointer at the specified position
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
286 * @param index Position of the object
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
287 * @returns object pointer
fb9990772357 Add documentation to model.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
288 */
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
289 const ldraw::Object* Model::objectAt(const QModelIndex& index) const
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
290 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
291 return this->body[unsigned_cast(index.row())].get();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
292 }
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
293
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
294 /**
145
4dea24d3eda0 Use QSaveFile to save the file more safely
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
295 * @brief Attempts the save the model
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
296 */
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
297 void Model::save(QIODevice* device) const
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
298 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
299 QTextStream out{device};
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
300 for (const ModelObjectPointer& object : this->body)
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
301 {
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
302 out << object.get()->toLDrawCode() << "\r\n";
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
303 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
304 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
305
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
306 /**
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
307 * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
308 */
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
309 void Model::makeUnofficial()
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
310 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
311 if (this->body.size() >= 4)
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
312 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
313 const ldraw::id_t id = this->body[3]->id;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
314 if (this->isA<ldraw::MetaCommand>(id))
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
315 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
316 const QString& body = this->body[3]->getProperty<ldraw::Property::Text>();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
317 if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_"))
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
318 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
319 QStringList tokens = body.split(" ");
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
320 tokens[1] = "Unofficial_" + tokens[1];
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
321 // Remove the UPDATE tag
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
322 if (tokens.size() >= 4 && tokens[2] == "UPDATE")
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
323 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
324 tokens.removeAt(3);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
325 tokens.removeAt(2);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
326 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
327 EditContext editor = this->edit();
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
328 editor.setObjectProperty<ldraw::Property::Text>(id, tokens.join(" "));
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
329 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
330 }
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 137
diff changeset
331 }
147
Teemu Piippo <teemu@hecknology.net>
parents: 146
diff changeset
332 }

mercurial