src/model.h

Thu, 20 Jun 2019 08:54:35 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 20 Jun 2019 08:54:35 +0300
changeset 1440
265b2e95a8e8
parent 1377
0b9a946002be
permissions
-rw-r--r--

uuid things

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
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1306
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
1091
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"
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
22 #include "serializer.h"
1147
a26568aa3cce Renamed ldObject.cpp → linetypes/modelobject.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1113
diff changeset
23 #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
24
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
25 class IndexGenerator
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
26 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
27 class Iterator
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
28 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
29 public:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
30 Iterator(const QAbstractListModel* model, int row) :
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
31 model {model},
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
32 row {row} {}
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
33
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
34 Iterator& operator++()
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
35 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
36 this->row += 1;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
37 return *this;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
38 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
39
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
40 QModelIndex operator*() const
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
41 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
42 return this->model->index(this->row);
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
43 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
44
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
45 bool operator!=(const Iterator& other)
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
46 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
47 return this->row != other.row or this->model != other.model;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
48 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
49
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
50 private:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
51 const QAbstractListModel* model;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
52 int row;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
53 };
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
54
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
55 public:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
56 IndexGenerator(const QAbstractListModel* model) :
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
57 model {model} {}
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
58
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
59 Iterator begin() const
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
60 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
61 return {this->model, 0};
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
62 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
63
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
64 Iterator end() const
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
65 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
66 return {this->model, this->model->rowCount()};
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
67 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
68
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
69 private:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
70 const QAbstractListModel* model;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
71 };
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
72
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
73 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
74 * 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
75 */
1440
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
76 class Model : public QObject
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 {
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
78 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
79
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
80 public:
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
81 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
82 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
83 ~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
84
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
85 void insertCopy(int position, LDObject* object);
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
86 void insertFromArchive(int row, Serializer::Archive& archive);
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
87 bool setObjectAt(int idx, Serializer::Archive& archive);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
88 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
89 template<typename T, typename... Args> T* emplaceAt(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
90 void removeAt(int position);
1249
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents: 1247
diff changeset
91 void removeAt(const QModelIndex& index);
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
92 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
93 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
94 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
95 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
96 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
97 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
98 LDObject* getObject(int position) const;
1261
5d2c9d36da9d Removed LDObject::invert, inversion code moved to basic toolset
Santeri Piippo
parents: 1258
diff changeset
99 Q_SLOT void recountTriangles();
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
100 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
101 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
102 QVector<LDObject*>::iterator end();
1258
f5921a474d57 removed LDObject::lineNumber
Santeri Piippo
parents: 1249
diff changeset
103 QModelIndex indexOf(LDObject* object) const;
1077
952d6b3e7d11 Replaced uses of LDSpawn with the Model class in edit modes
Teemu Piippo <teemu@hecknology.net>
parents: 1074
diff changeset
104 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
105 class DocumentManager* documentManager() const;
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
106 IndexGenerator indices() const;
1440
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
107 LDObject* lookup(const Uuid& index) const;
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
108 QColor pickingColorForObject(const Uuid& id) const;
1278
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
109 QModelIndex objectByPickingColor(const QColor& color) const;
1306
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
110 Winding winding() const;
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
111 void setWinding(Winding winding);
1440
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
112 int rowOf(const Uuid& id);
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
113
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
114 signals:
1440
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
115 void objectAdded(const Uuid& object);
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
116 void aboutToRemoveObject(const Uuid& index, const LDObject* object);
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
117 void objectModified(LDObject* object);
1306
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
118 void windingChanged(Winding newWinding);
1377
0b9a946002be fixed changing a document not updating its references in other files
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
119 void modelChanged();
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
120
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
121 protected:
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
122 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
123
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
124 QVector<LDObject*> _objects;
1440
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1377
diff changeset
125 QMap<Uuid, LDObject *> _objectsById;
1278
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
126 QMap<LDObject*, QRgb> pickingColors;
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
127 QRgb pickingColorCursor = 0x000001;
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
128 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
129 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
130 mutable bool _needsTriangleRecount;
1306
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
131 Winding _winding = NoWinding;
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
132
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
133 private:
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
134 void installObject(int row, 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
135 };
1087
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
136
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
137 int countof(Model& model);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
138
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
139 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
140 * 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
141 * 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
142 * is added to the end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
143 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
144 * 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
145 *
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
146 * LDLine(Vertex v1, Vertex v2);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
147 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
148 * This constructor can be invoked as such:
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
149 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
150 * model->emplace<LDLine>(v1, v2);
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 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
153 T* Model::emplace(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
154 {
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
155 return emplaceAt<T>(size(), args...);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
156 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
157
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
158 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
159 * 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
160 * end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
161 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
162 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
163 T* Model::emplaceAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
164 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
165 T* object = constructObject<T>(args...);
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
166 installObject(position, object);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
167 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
168 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
169
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
170 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
171 * 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
172 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
173 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
174 T* Model::constructObject(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
175 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
176 static_assert (std::is_base_of<LDObject, T>::value, "Can only use this function with LDObject-derivatives");
1269
ec691d9472b3 Added LDObject serialization and refactored the internal resource managing to use it. No more tearing objects from one model into another, and this provides a stable way to keep an object's state in memory such as the edit history.
Santeri Piippo
parents: 1267
diff changeset
177 T* object = new T {args...};
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
178
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
179 // 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
180 // TODO: store -1 as the default color
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
181 if (object->isColored())
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
182 object->setColor(object->defaultColor());
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
183
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
184 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
185 }

mercurial