src/model.cpp

Wed, 22 Jan 2020 22:43:28 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 22:43:28 +0200
changeset 29
4cc6b582fde8
parent 24
1a0faaaceb84
child 35
98906a94732f
permissions
-rw-r--r--

added FindGLM.cmake

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
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
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "model.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "modeleditcontext.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
24 Model::Model(QObject* parent) :
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
25 QAbstractListModel{parent} {}
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 int Model::size() const
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 return this->body.size();
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 Model::EditContext Model::edit()
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 return {*this};
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
44 const int row = index.row();
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
45 linetypes::Object* object = this->body[row].get();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
46 switch(role)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
47 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
48 case Qt::DisplayRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
49 return object->textRepresentation();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
50 case Qt::ForegroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
51 return object->textRepresentationForeground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
52 case Qt::BackgroundRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
53 return object->textRepresentationBackground();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
54 case Qt::FontRole:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
55 return object->textRepresentationFont();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
56 default:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
57 return {};
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
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
61 QVariant Model::getHeaderProperty(const HeaderProperty property)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
62 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
63 switch (property)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
64 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
65 case HeaderProperty::Name:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
66 return header.name;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
67 default:
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
68 return {};
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
69 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
70 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
71
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
72 QVariant Model::getObjectProperty(const int index, const linetypes::Property property) const
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
73 {
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
74 const linetypes::Object* object = this->body[index].get();
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
75 return object->getProperty(property);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
76 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
77
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
78 std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
79 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
80 if (this->needRecache)
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
81 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
82 this->cachedPolygons.clear();
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
83 linetypes::GetPolygonsContext context{documents};
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
84 for (int i = 0; i < this->size(); i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
85 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
86 this->getObjectPolygons(i, this->cachedPolygons, &context);
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
87 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
88 this->needRecache = false;
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
89 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
90 return this->cachedPolygons;
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
91 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
92
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
93 void Model::getObjectPolygons(
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
94 const int index,
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
95 std::vector<gl::Polygon>& polygons_out,
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
96 linetypes::GetPolygonsContext* context) const
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
97 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
98 const linetypes::Object* object = this->body[index].get();
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
99 object->getPolygons(polygons_out, context);
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
100 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
101
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
102 void Model::append(ModelObjectPointer&& object)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
103 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
104 this->body.push_back(std::move(object));
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
105 }

mercurial