Sat, 07 Mar 2020 01:25:37 +0200
document AbstractBasicShaderProgram
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
3 | 19 | #pragma once |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
20 | #include <QAbstractListModel> |
3 | 21 | #include <memory> |
22 | #include "main.h" | |
23 | #include "header.h" | |
14 | 24 | #include "linetypes/object.h" |
21 | 25 | #include "gl/common.h" |
3 | 26 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
27 | enum class HeaderProperty |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
28 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
29 | Name |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
30 | }; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
31 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
32 | class Model : public QAbstractListModel |
3 | 33 | { |
34 | Q_OBJECT | |
35 | public: | |
36 | class EditContext; | |
21 | 37 | Model(QObject* parent = nullptr); |
5 | 38 | Model(const Model&) = delete; |
3 | 39 | int size() const; |
40 | EditContext edit(); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
41 | int rowCount(const QModelIndex&) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
42 | QVariant data(const QModelIndex& index, int role) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
43 | QVariant getHeaderProperty(const HeaderProperty property); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
44 | const QString& getName() const; |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
45 | QVariant getObjectProperty(const int index, const ldraw::Property property) const; |
21 | 46 | std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const; |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
47 | QModelIndex lookup(ldraw::id_t id) const; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
48 | ldraw::id_t resolve(const QModelIndex& index) const; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
49 | template<typename R> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
50 | ldraw::Id<R> checkType(ldraw::id_t id) const; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
51 | template<typename R> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
52 | const R* get(ldraw::Id<R> id) const; |
6 | 53 | signals: |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
54 | void objectAdded(ldraw::id_t id, int position); |
3 | 55 | private: |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
56 | using ModelObjectPointer = std::unique_ptr<ldraw::Object>; |
3 | 57 | template<typename T, typename... Args> |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
58 | ldraw::Id<T> append(Args&&... args); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
59 | void append(ModelObjectPointer&& object); |
3 | 60 | template<typename T, typename... Args> |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
61 | ldraw::Id<T> insert(int position, Args&&... args); |
51 | 62 | ldraw::Object* objectAt(const QModelIndex& index); |
63 | const ldraw::Object* objectAt(const QModelIndex& index) const; | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
64 | template<typename T> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
65 | T* objectAt(ldraw::Id<T> id) |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
66 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
67 | return static_cast<T*>(this->objectAt(this->lookup(id))); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
68 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
69 | template<typename T> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
70 | const T* objectAt(ldraw::Id<T> id) const |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
71 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
72 | return static_cast<const T*>(this->objectAt(this->lookup(id))); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
73 | } |
51 | 74 | void getObjectPolygons( |
75 | const int index, | |
76 | std::vector<gl::Polygon>& polygons_out, | |
77 | ldraw::GetPolygonsContext* context) const; | |
3 | 78 | bool modified = false; |
79 | QString path; | |
80 | LDHeader header; | |
81 | std::vector<ModelObjectPointer> body; | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
82 | std::map<ldraw::id_t, ldraw::Object*> objectsById; |
21 | 83 | mutable std::vector<gl::Polygon> cachedPolygons; |
84 | mutable bool needRecache = true; | |
3 | 85 | }; |
86 | ||
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
87 | /** |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
88 | * \brief Checks type of object behind id |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
89 | * Checks whether the specified id refers to an object of the specified type. |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
90 | * \returns id casted to subclass if appropriate, null id otherwise |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
91 | */ |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
92 | template<typename R> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
93 | ldraw::Id<R> Model::checkType(ldraw::id_t id) const |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
94 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
95 | if (dynamic_cast<const R*>(this->objectAt(this->lookup(id))) != nullptr) |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
96 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
97 | return ldraw::Id<R>{id.value}; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
98 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
99 | else |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
100 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
101 | return ldraw::NULL_ID; |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
102 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
103 | } |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
104 | |
3 | 105 | template<typename T, typename... Args> |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
106 | ldraw::Id<T> Model::append(Args&&... args) |
3 | 107 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
108 | emit layoutAboutToBeChanged(); |
3 | 109 | this->body.push_back(std::make_unique<T>(args...)); |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
110 | ldraw::Object* pointer = this->body.back().get(); |
6 | 111 | this->objectsById[pointer->id] = pointer; |
112 | emit objectAdded(pointer->id, this->body.size() - 1); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
113 | emit layoutChanged(); |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
114 | return ldraw::Id<T>{pointer->id}; |
3 | 115 | } |
116 | ||
117 | template<typename T, typename... Args> | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
118 | ldraw::Id<T> Model::insert(int position, Args&&... args) |
3 | 119 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
120 | emit layoutAboutToBeChanged(); |
3 | 121 | this->body.insert(position, std::make_unique<T>(args...)); |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
122 | ldraw::Object* pointer = this->body[position].get(); |
6 | 123 | this->objectsById[pointer->id] = pointer; |
124 | emit objectAdded(pointer->id, position); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
125 | emit layoutChanged(); |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
126 | return ldraw::Id<T>{pointer->id}; |
3 | 127 | } |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
128 | |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
129 | template<typename R> |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
130 | const R* Model::get(ldraw::Id<R> id) const |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
131 | { |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
132 | return this->objectAt(id); |
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
133 | } |