Fri, 06 Mar 2020 16:08:45 +0200
default to XZ
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 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
19 | #include <QBrush> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
20 | #include <QFont> |
3 | 21 | #include "model.h" |
22 | #include "modeleditcontext.h" | |
23 | ||
21 | 24 | Model::Model(QObject* parent) : |
25 | QAbstractListModel{parent} {} | |
3 | 26 | |
27 | int Model::size() const | |
28 | { | |
51 | 29 | return static_cast<int>(this->body.size()); |
3 | 30 | } |
31 | ||
32 | Model::EditContext Model::edit() | |
33 | { | |
34 | return {*this}; | |
35 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
36 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
37 | int Model::rowCount(const QModelIndex&) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
38 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
39 | return size(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
40 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
41 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
42 | QVariant Model::data(const QModelIndex& index, int role) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
43 | { |
51 | 44 | const ldraw::Object* object = this->objectAt(index); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
45 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
46 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
47 | case Qt::DisplayRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
48 | return object->textRepresentation(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
49 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
50 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
51 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
52 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
53 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
54 | return object->textRepresentationFont(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
55 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
56 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
57 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
58 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
59 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
60 | QVariant Model::getHeaderProperty(const HeaderProperty property) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
61 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
62 | switch (property) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
63 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
64 | case HeaderProperty::Name: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
65 | return header.name; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
66 | } |
51 | 67 | return {}; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
68 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
69 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
70 | QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const |
12 | 71 | { |
51 | 72 | const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
12 | 73 | return object->getProperty(property); |
74 | } | |
75 | ||
21 | 76 | std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const |
77 | { | |
78 | if (this->needRecache) | |
79 | { | |
80 | this->cachedPolygons.clear(); | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
81 | ldraw::GetPolygonsContext context{documents}; |
21 | 82 | for (int i = 0; i < this->size(); i += 1) |
83 | { | |
84 | this->getObjectPolygons(i, this->cachedPolygons, &context); | |
85 | } | |
86 | this->needRecache = false; | |
87 | } | |
88 | return this->cachedPolygons; | |
89 | } | |
90 | ||
51 | 91 | QModelIndex Model::lookup(ldraw::Id id) const |
92 | { | |
93 | // FIXME: This linear search will probably cause performance issues | |
94 | for (std::size_t i = 0; i < this->body.size(); i += 1) | |
95 | { | |
96 | if (this->body[i]->id == id) | |
97 | { | |
98 | return this->index(static_cast<int>(i)); | |
99 | } | |
100 | } | |
101 | return {}; | |
102 | } | |
103 | ||
104 | ldraw::Id Model::resolve(const QModelIndex& index) const | |
105 | { | |
106 | return this->objectAt(index)->id; | |
107 | } | |
108 | ||
21 | 109 | void Model::getObjectPolygons( |
110 | const int index, | |
111 | std::vector<gl::Polygon>& polygons_out, | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
112 | ldraw::GetPolygonsContext* context) const |
21 | 113 | { |
51 | 114 | const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
21 | 115 | object->getPolygons(polygons_out, context); |
116 | } | |
117 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
118 | void Model::append(ModelObjectPointer&& object) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
119 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
120 | this->body.push_back(std::move(object)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
121 | } |
51 | 122 | |
123 | ldraw::Object* Model::objectAt(const QModelIndex& index) | |
124 | { | |
125 | return this->body[unsigned_cast(index.row())].get(); | |
126 | } | |
127 | ||
128 | const ldraw::Object* Model::objectAt(const QModelIndex& index) const | |
129 | { | |
130 | return this->body[unsigned_cast(index.row())].get(); | |
131 | } |