src/model.h

Tue, 13 Feb 2018 15:43:55 +0200

author
Santeri Piippo
date
Tue, 13 Feb 2018 15:43:55 +0200
changeset 1243
0fb1d3d17b60
parent 1240
cebb7ef54f41
child 1244
68e126e8c629
permissions
-rw-r--r--

removed unneeded stuff

1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
1 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
2 * LDForge: LDraw parts authoring CAD
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
4 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
5 * This program is free software: you can redistribute it and/or modify
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
6 * it under the terms of the GNU General Public License as published by
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
8 * (at your option) any later version.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
9 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
10 * This program is distributed in the hope that it will be useful,
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
13 * GNU General Public License for more details.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
14 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
15 * You should have received a copy of the GNU General Public License
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
17 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
18
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
1240
cebb7ef54f41 changed Model into an MVC list model and replaced the objects list with a view into the model
Santeri Piippo
parents: 1159
diff changeset
20 #include <QAbstractListModel>
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "main.h"
1147
a26568aa3cce Renamed ldObject.cpp → linetypes/modelobject.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1113
diff changeset
22 #include "linetypes/modelobject.h"
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
24 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
25 * This class represents a LDraw model, consisting of a vector of objects. It manages LDObject ownership.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
26 */
1240
cebb7ef54f41 changed Model into an MVC list model and replaced the objects list with a view into the model
Santeri Piippo
parents: 1159
diff changeset
27 class Model : public QAbstractListModel
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {
1113
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
29 Q_OBJECT
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
30
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 public:
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
32 Model(class DocumentManager* manager);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 Model(const Model& other) = delete;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 ~Model();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
36 void addObject(LDObject* object);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 virtual void insertObject(int position, LDObject* object);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 virtual bool swapObjects(LDObject* one, LDObject* other);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 virtual bool setObjectAt(int idx, LDObject* obj);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
40 template<typename T, typename... Args> T* emplace(Args&& ...args);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
41 template<typename T, typename... Args> T* emplaceAt(int position, Args&& ...args);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
42 template<typename T, typename... Args> T* emplaceReplacement(LDObject* object, Args&& ...args);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
43 template<typename T, typename... Args> T* emplaceReplacementAt(int position, Args&& ...args);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 void removeAt(int position);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 void remove(LDObject* object);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 void replace(LDObject *object, Model& model);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 void clear();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 void merge(Model& other, int position = -1);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 int size() const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 const QVector<LDObject*>& objects() const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 LDObject* getObject(int position) const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 void recountTriangles();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 int triangleCount() const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 QVector<LDObject*>::iterator begin();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 QVector<LDObject*>::iterator end();
1077
952d6b3e7d11 Replaced uses of LDSpawn with the Model class in edit modes
Teemu Piippo <teemu@hecknology.net>
parents: 1074
diff changeset
56 bool isEmpty() const;
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
57 class DocumentManager* documentManager() const;
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
58 LDObject* insertFromString(int position, QString line);
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
59 LDObject* addFromString(QString line);
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
60 LDObject* replaceWithFromString(LDObject* object, QString line);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61
1240
cebb7ef54f41 changed Model into an MVC list model and replaced the objects list with a view into the model
Santeri Piippo
parents: 1159
diff changeset
62 int rowCount(const QModelIndex& parent) const override;
cebb7ef54f41 changed Model into an MVC list model and replaced the objects list with a view into the model
Santeri Piippo
parents: 1159
diff changeset
63 QVariant data(const QModelIndex& index, int role) const override;
cebb7ef54f41 changed Model into an MVC list model and replaced the objects list with a view into the model
Santeri Piippo
parents: 1159
diff changeset
64
1113
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
65 signals:
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
66 void objectAdded(LDObject* object);
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
67 void aboutToRemoveObject(LDObject* object);
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
68 void objectModified(LDObject* object);
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
69
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 protected:
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
71 template<typename T, typename... Args> T* constructObject(Args&& ...args);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 void withdraw(LDObject* object);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 virtual LDObject* withdrawAt(int position);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 QVector<LDObject*> _objects;
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
76 class DocumentManager* _manager;
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 mutable int _triangleCount = 0;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 mutable bool _needsTriangleRecount;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 };
1087
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
80
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
81 int countof(Model& model);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
82
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
83 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
84 * Given an LDObject type as the template parameter, and any number of variadic parameters, constructs an LDObject derivative
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
85 * and inserts it into this model. The variadic parameters and this model pointer are passed to the constructor. The constructed object
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
86 * is added to the end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
87 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
88 * For instance, the LDLine contains a constructor as such:
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
89 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
90 * LDLine(Vertex v1, Vertex v2, Model* model);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
91 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
92 * This constructor can be invoked as such:
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
93 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
94 * model->emplace<LDLine>(v1, v2);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
95 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
96 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
97 T* Model::emplace(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
98 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
99 T* object = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
100 addObject(object);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
101 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
102 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
103
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
104 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
105 * Like emplace<>() but also takes a position as the first argument and emplaces the object at the given position instead of the
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
106 * end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
107 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
108 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
109 T* Model::emplaceAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
110 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
111 T* object = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
112 insertObject(position, object);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
113 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
114 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
115
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
116 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
117 * Like emplace<>() but instead of inserting the constructed object, the new object replaces the object given in the first parameter.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
118 * If the old object cannot be replaced, the new object will not be constructed at all.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
119 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
120 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
121 T* Model::emplaceReplacement(LDObject* object, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
122 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
123 if (object->model() == this)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
124 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
125 int position = object->lineNumber();
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
126 T* replacement = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
127 setObjectAt(position, replacement);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
128 return replacement;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
129 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
130 else
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
131 return nullptr;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
132 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
133
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
134 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
135 * Like emplaceAt<>() but instead of inserting the constructed object, it replaces the document at the given spot instead.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
136 * The replaced object is deleted in the process.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
137 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
138 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
139 T* Model::emplaceReplacementAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
140 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
141 T* replacement = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
142 setObjectAt(position, replacement);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
143 return replacement;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
144 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
145
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
146 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
147 * Constructs an LDObject such that it gets this model as its model pointer.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
148 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
149 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
150 T* Model::constructObject(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
151 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
152 static_assert (std::is_base_of<LDObject, T>::value, "Can only use this function with LDObject-derivatives");
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
153 T* object = new T {args..., this};
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
154
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
155 // Set default color. Relying on virtual functions, this cannot be done in the c-tor.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
156 // TODO: store -1 as the default color
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
157 if (object->isColored())
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
158 object->setColor(object->defaultColor());
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
159
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
160 return object;
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
161 }

mercurial