src/model.h

Sat, 31 Mar 2018 18:41:24 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 31 Mar 2018 18:41:24 +0300
changeset 1366
69087b1e123b
parent 1326
69a90bd2dba2
permissions
-rw-r--r--

start rework

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 */
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
76 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
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:
1366
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
81 enum
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
82 {
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
83 LDObjectTypeRole = Qt::UserRole,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
84 Vertex0Role,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
85 Vertex1Role,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
86 Vertex2Role,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
87 Vertex3Role,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
88 ColorRole,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
89 PositionRole,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
90 TransformationMatrixRole,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
91 ReferenceNameRole,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
92 CommentTextRole,
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
93 };
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
94
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
95 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
96 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
97 ~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
98
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
99 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
100 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
101 bool setObjectAt(int idx, Serializer::Archive& archive);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
102 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
103 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
104 void removeAt(int position);
1249
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents: 1247
diff changeset
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 LDObject* getObject(int position) const;
1261
5d2c9d36da9d Removed LDObject::invert, inversion code moved to basic toolset
Santeri Piippo
parents: 1258
diff changeset
113 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
114 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
115 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
116 QVector<LDObject*>::iterator end();
1258
f5921a474d57 removed LDObject::lineNumber
Santeri Piippo
parents: 1249
diff changeset
117 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
118 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
119 class DocumentManager* documentManager() const;
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
120 IndexGenerator indices() const;
1245
338d66111168 more work on mvc
Santeri Piippo
parents: 1244
diff changeset
121 LDObject* lookup(const QModelIndex& index) const;
1278
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
122 QColor pickingColorForObject(const QModelIndex& objectIndex) const;
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
123 QModelIndex objectByPickingColor(const QColor& color) const;
1306
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
124 Winding winding() const;
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
125 void setWinding(Winding winding);
1366
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
126 const GLRotationMatrix& transformationMatrix() const;
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
127 void setTransformationMatrix(const GLRotationMatrix& matrix);
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
128 Vertex map(const Vertex& global) const;
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
129 Vertex unmap(const Vertex& local) const;
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
130
1273
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
131 bool moveRows(
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
132 const QModelIndex& sourceParent,
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
133 int sourceRow,
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
134 int count,
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
135 const QModelIndex& destinationParent,
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
136 int destinationChild
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
137 ) override;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents: 1272
diff changeset
138
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
139 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
140 QVariant data(const QModelIndex& index, int role) const override;
1366
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
141 bool setData(const QModelIndex& index, const QVariant& value, int role);
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
142
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
143 signals:
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
144 void objectAdded(const QModelIndex& 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
145 void aboutToRemoveObject(const QModelIndex& index);
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
146 void objectModified(LDObject* object);
1306
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
147 void windingChanged(Winding newWinding);
1366
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
148 void transformationMatrixChanged(const Matrix& newMatrix);
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
149
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
150 protected:
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
151 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
152
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
153 QVector<LDObject*> _objects;
1278
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
154 QMap<LDObject*, QRgb> pickingColors;
6e1ea24e5a5e moved LDObject indices from a global array into Model
Santeri Piippo
parents: 1277
diff changeset
155 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
156 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
157 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
158 mutable bool _needsTriangleRecount;
1306
be85306198a2 red/green view rework complete
Teemu Piippo <teemu@hecknology.net>
parents: 1288
diff changeset
159 Winding _winding = NoWinding;
1366
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
160 GLRotationMatrix storedTransformationmatrix;
69087b1e123b start rework
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
161 GLRotationMatrix invertedTransformationmatrix;
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
162
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
163 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
164 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
165 };
1087
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
166
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
167 int countof(Model& model);
1091
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 * 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
171 * 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
172 * is added to the end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
173 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
174 * 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
175 *
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
176 * LDLine(Vertex v1, Vertex v2);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
177 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
178 * This constructor can be invoked as such:
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
179 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
180 * model->emplace<LDLine>(v1, v2);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
181 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
182 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
183 T* Model::emplace(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
184 {
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
185 return emplaceAt<T>(size(), args...);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
186 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
187
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
188 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
189 * 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
190 * end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
191 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
192 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
193 T* Model::emplaceAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
194 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
195 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
196 installObject(position, object);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
197 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
198 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
199
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
200 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
201 * 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
202 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
203 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
204 T* Model::constructObject(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
205 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
206 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
207 T* object = new T {args...};
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
208
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
209 // 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
210 // TODO: store -1 as the default color
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
211 if (object->isColored())
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
212 object->setColor(object->defaultColor());
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
213
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
214 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
215 }

mercurial