src/model.h

Tue, 28 Sep 2021 22:14:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 22:14:00 +0300
changeset 143
7b62c52835a1
parent 141
185eb297dc1e
child 147
37f936073cac
permissions
-rw-r--r--

Fix memory corruption involving document tools.
I don't think that the metaobject-initialization had anything to do with this
but it is a lot simpler without it anyway.

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 #pragma once
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
20 #include <QAbstractListModel>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include <memory>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "main.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include "header.h"
14
20d2ed3af73d renamings
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
24 #include "linetypes/object.h"
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
25 #include "linetypes/metacommand.h"
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
26 #include "gl/common.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
28 enum class HeaderProperty
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
29 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
30 Name
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
31 };
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
32
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
33 class Model : public QAbstractListModel
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 Q_OBJECT
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 public:
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 class EditContext;
140
2f383e88acf4 work on saving
Teemu Piippo <teemu@hecknology.net>
parents: 138
diff changeset
38 Model(const QString& path, QObject* parent = nullptr);
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
39 Model(QObject* parent = nullptr);
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
40 Model(const Model&) = delete;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 int size() const;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
42 ldraw::id_t at(int index) const;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 EditContext edit();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
44 int rowCount(const QModelIndex&) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
45 QVariant data(const QModelIndex& index, int role) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
46 QVariant getHeaderProperty(const HeaderProperty property);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
47 const QString& getName() const;
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
48 QVariant getObjectProperty(const int index, const ldraw::Property property) const;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
49 std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
50 QModelIndex lookup(ldraw::id_t id) const;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
51 ldraw::id_t resolve(const QModelIndex& index) const;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
52 template<typename R>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
53 ldraw::Id<R> checkType(ldraw::id_t id) const;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
54 template<typename R>
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
55 bool isA(ldraw::id_t id) const;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
56 template<typename R>
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
57 const R* get(ldraw::Id<R> id) const;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
58 template<typename R>
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
59 struct Get2Result
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
60 {
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
61 QModelIndex index;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
62 const R* object;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
63 };
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
64 template<typename R>
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
65 Get2Result<R> get2(ldraw::Id<R> id) const;
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
66 template<typename R, typename Fn>
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
67 void apply(Fn f) const;
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
68 const QString& path() const;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
69 void setPath(const QString& path);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
70 bool save(QTextStream& errors) const;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
71 void makeUnofficial();
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
72 Q_SIGNALS:
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
73 void objectAdded(ldraw::id_t id, int position);
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
74 void objectModified(ldraw::id_t id, int position);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 private:
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
76 using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 template<typename T, typename... Args>
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
78 ldraw::Id<T> append(Args&&... args);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
79 void append(ModelObjectPointer&& object);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 template<typename T, typename... Args>
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
81 ldraw::Id<T> insert(std::size_t position, Args&&... args);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
82 void remove(int position);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
83 ldraw::Object* objectAt(const QModelIndex& index);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
84 const ldraw::Object* objectAt(const QModelIndex& index) const;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
85 template<typename T>
138
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
86 T* objectAt(ldraw::Id<T> id);
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
87 template<typename T>
138
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
88 const T* objectAt(ldraw::Id<T> id) const;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
89 void getObjectPolygons(
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
90 const int index,
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
91 std::vector<gl::Polygon>& polygons_out,
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
92 ldraw::GetPolygonsContext* context) const;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
93 void editFinished();
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
94 void objectModified(ldraw::id_t id);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 bool modified = false;
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
96 QString storedPath;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 LDHeader header;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 std::vector<ModelObjectPointer> body;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
99 std::map<ldraw::id_t, ldraw::Object*> objectsById;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
100 mutable std::vector<gl::Polygon> cachedPolygons;
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
101 mutable bool needRecache = true;
136
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
102 /**
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
103 * @brief Amount of model edit contexts active
e8444e0d7f1a Work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 133
diff changeset
104 */
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
105 int editCounter = 0;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
108 /**
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
109 * @brief Checks whether the id is exactly of the specified type
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
110 * @tparam R Type of LDraw line type object to test for
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
111 * @param id Id of object to test
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
112 * @returns whether the type of the object specified by @c id is the same type as R. Returns false if it is a subclass.
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
113 */
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
114 template<typename R>
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
115 bool Model::isA(ldraw::id_t id) const
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
116 {
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
117 const ldraw::Object* object = this->objectAt(this->lookup(id));
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
118 const std::type_info& a = typeid(*object);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
119 const std::type_info& b = typeid(R);
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
120 return a == b;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
121 }
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
122
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
123 /**
138
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
124 * @brief Calls the specified function to all matching objects in the model
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
125 * @tparam R Type of LDraw line type object to filter by
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
126 * @param fn Function to call.
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
127 */
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
128 template<typename R, typename Fn>
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
129 void Model::apply(Fn f) const
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
130 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
131 for (const ModelObjectPointer& object : this->body)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
132 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
133 const R* subobject = dynamic_cast<const R*>(object.get());
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
134 if (subobject != nullptr)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
135 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
136 f(subobject);
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
137 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
138 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
139 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
140
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
141 /**
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
142 * \brief Checks type of object behind id
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
143 * Checks whether the specified id refers to an object of the specified type.
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
144 * \returns id casted to subclass if appropriate, null id otherwise
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
145 */
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
146 template<typename R>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
147 ldraw::Id<R> Model::checkType(ldraw::id_t id) const
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
148 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
149 if (dynamic_cast<const R*>(this->objectAt(this->lookup(id))) != nullptr)
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
150 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
151 return ldraw::Id<R>{id.value};
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
152 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
153 else
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
154 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
155 return ldraw::NULL_ID;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
156 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
157 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
158
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 template<typename T, typename... Args>
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
160 ldraw::Id<T> Model::append(Args&&... args)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 {
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
162 const int position = static_cast<int>(this->body.size());
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
163 Q_EMIT beginInsertRows({}, position, position);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 this->body.push_back(std::make_unique<T>(args...));
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
165 ldraw::Object* pointer = this->body.back().get();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
166 this->objectsById[pointer->id] = pointer;
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
167 Q_EMIT objectAdded(pointer->id, static_cast<int>(this->body.size() - 1));
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
168 Q_EMIT endInsertRows();
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
169 this->needRecache = true;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
170 return ldraw::Id<T>{pointer->id.value};
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
172
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 template<typename T, typename... Args>
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
174 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 {
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
176 Q_EMIT beginInsertRows({}, position, position);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
177 this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...));
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
178 ldraw::Object* pointer = this->body[position].get();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
179 this->objectsById[pointer->id] = pointer;
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
180 Q_EMIT objectAdded(pointer->id, static_cast<int>(position));
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
181 Q_EMIT endInsertRows();
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
182 this->needRecache = true;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
183 return ldraw::Id<T>{pointer->id.value};
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
184 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
185
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
186 template<typename R>
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
187 const R* Model::get(ldraw::Id<R> id) const
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
188 {
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
189 return this->get2(id).object;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
190 }
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
191
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
192 template<typename R>
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
193 Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
194 {
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
195 Get2Result<R> result;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
196 result.index = this->lookup(id);
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
197 if (result.index.isValid())
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
198 {
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
199 result.object = static_cast<const R*>(this->objectAt(result.index));
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
200 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
201 else
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
202 {
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
203 result.object = nullptr;
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
204 }
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
205 return result;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
206 }
138
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
207
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
208 /**
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
209 * @brief Gets an object pointer by id. Used by the editing context to actually modify objects.
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
210 * @param id
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
211 * @return object pointer
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
212 */
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
213 template<typename T>
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
214 T* Model::objectAt(ldraw::Id<T> id)
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
215 {
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
216 return static_cast<T*>(this->objectAt(this->lookup(id)));
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
217 }
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
218
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
219 template<typename T>
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
220 const T* Model::objectAt(ldraw::Id<T> id) const
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
221 {
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
222 return static_cast<const T*>(this->objectAt(this->lookup(id)));
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
223 }

mercurial