Sun, 26 Jan 2020 14:29:30 +0200
replaced matrix and vertex classes with glm
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; |
13 | 45 | QVariant getObjectProperty(const int index, const linetypes::Property property) const; |
21 | 46 | std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const; |
47 | void getObjectPolygons(const int index, std::vector<gl::Polygon>& polygons_out, linetypes::GetPolygonsContext* context) const; | |
6 | 48 | signals: |
13 | 49 | void objectAdded(linetypes::Id id, int position); |
3 | 50 | private: |
13 | 51 | using ModelObjectPointer = std::unique_ptr<linetypes::Object>; |
3 | 52 | template<typename T, typename... Args> |
26 | 53 | linetypes::Id append(Args&&... args); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
54 | void append(ModelObjectPointer&& object); |
3 | 55 | template<typename T, typename... Args> |
26 | 56 | linetypes::Id insert(int position, Args&&... args); |
3 | 57 | bool modified = false; |
58 | QString path; | |
59 | LDHeader header; | |
60 | std::vector<ModelObjectPointer> body; | |
13 | 61 | std::map<linetypes::Id, linetypes::Object*> objectsById; |
21 | 62 | mutable std::vector<gl::Polygon> cachedPolygons; |
63 | mutable bool needRecache = true; | |
3 | 64 | }; |
65 | ||
66 | template<typename T, typename... Args> | |
26 | 67 | linetypes::Id Model::append(Args&&... args) |
3 | 68 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
69 | emit layoutAboutToBeChanged(); |
3 | 70 | this->body.push_back(std::make_unique<T>(args...)); |
26 | 71 | linetypes::Object* pointer = this->body.back().get(); |
6 | 72 | this->objectsById[pointer->id] = pointer; |
73 | emit objectAdded(pointer->id, this->body.size() - 1); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
74 | emit layoutChanged(); |
26 | 75 | return pointer->id; |
3 | 76 | } |
77 | ||
78 | template<typename T, typename... Args> | |
26 | 79 | linetypes::Id Model::insert(int position, Args&&... args) |
3 | 80 | { |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
81 | emit layoutAboutToBeChanged(); |
3 | 82 | this->body.insert(position, std::make_unique<T>(args...)); |
26 | 83 | linetypes::Object* pointer = this->body[position].get(); |
6 | 84 | this->objectsById[pointer->id] = pointer; |
85 | emit objectAdded(pointer->id, position); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
86 | emit layoutChanged(); |
26 | 87 | return pointer->id; |
3 | 88 | } |