src/model.h

Wed, 14 Feb 2018 15:17:30 +0200

author
Santeri Piippo
date
Wed, 14 Feb 2018 15:17:30 +0200
changeset 1244
68e126e8c629
parent 1240
cebb7ef54f41
child 1245
338d66111168
permissions
-rw-r--r--

begin model 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
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
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
24 class IndexGenerator
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
25 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
26 class Iterator
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
27 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
28 public:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
29 Iterator(const QAbstractListModel* model, int row) :
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
30 model {model},
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
31 row {row} {}
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
32
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
33 Iterator& operator++()
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
34 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
35 this->row += 1;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
36 return *this;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
37 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
38
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
39 QModelIndex operator*() const
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
40 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
41 return this->model->index(this->row);
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
42 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
43
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
44 bool operator!=(const Iterator& other)
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
45 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
46 return this->row != other.row or this->model != other.model;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
47 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
48
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
49 private:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
50 const QAbstractListModel* model;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
51 int row;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
52 };
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
53
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
54 public:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
55 IndexGenerator(const QAbstractListModel* model) :
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
56 model {model} {}
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
57
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
58 Iterator begin() const
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
59 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
60 return {this->model, 0};
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
61 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
62
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
63 Iterator end() const
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
64 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
65 return {this->model, this->model->rowCount()};
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
66 }
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
67
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
68 private:
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
69 const QAbstractListModel* model;
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
70 };
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
71
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
72 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
73 * 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
74 */
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
75 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
76 {
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
77 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
78
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
79 public:
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
80 enum
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
81 {
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
82 ObjectRole = Qt::UserRole
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
83 };
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
84
1159
6ad8cdcd88d9 print() is no longer a global function but is tied to HierarchyElement.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
85 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
86 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
87 ~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
88
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
89 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
90 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
91 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
92 virtual bool setObjectAt(int idx, LDObject* obj);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113 LDObject* replaceWithFromString(LDObject* object, QString line);
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
114 IndexGenerator indices() 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
115
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
116 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
117 QVariant data(const QModelIndex& index, int role) const override;
1244
68e126e8c629 begin model rework
Santeri Piippo
parents: 1240
diff changeset
118 bool removeRows(int row, int count, const QModelIndex& ) override;
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
119
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 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
121 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
122 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
123 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
124
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
125 protected:
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
126 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
127 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
128 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
129
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 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
131 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
132 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
133 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
134 };
1087
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
135
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
136 int countof(Model& model);
1091
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 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
139 * 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
140 * 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
141 * is added to the end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
142 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
143 * 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
144 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
145 * LDLine(Vertex v1, Vertex v2, Model* model);
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 * This constructor can be invoked as such:
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 * model->emplace<LDLine>(v1, v2);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
150 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
151 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
152 T* Model::emplace(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
153 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
154 T* object = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
155 addObject(object);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
156 return object;
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 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
160 * 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
161 * end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
162 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
163 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
164 T* Model::emplaceAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
165 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
166 T* object = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
167 insertObject(position, object);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
168 return object;
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 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
172 * 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
173 * 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
174 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
175 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
176 T* Model::emplaceReplacement(LDObject* object, Args&& ...args)
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 if (object->model() == this)
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 int position = object->lineNumber();
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
181 T* replacement = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
182 setObjectAt(position, replacement);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
183 return replacement;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
184 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
185 else
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
186 return nullptr;
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 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
190 * 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
191 * The replaced object is deleted in the process.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
192 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
193 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
194 T* Model::emplaceReplacementAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
195 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
196 T* replacement = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
197 setObjectAt(position, replacement);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
198 return replacement;
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 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
202 * 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
203 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
204 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
205 T* Model::constructObject(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
206 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
207 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
208 T* object = new T {args..., this};
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
209
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
210 // 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
211 // TODO: store -1 as the default color
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
212 if (object->isColored())
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
213 object->setColor(object->defaultColor());
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
214
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
215 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
216 }

mercurial