src/modeleditcontext.cpp

Tue, 17 Mar 2020 23:13:29 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 17 Mar 2020 23:13:29 +0200
changeset 81
62373840e33a
parent 77
028798a72591
child 86
4bec0525ef1b
permissions
-rw-r--r--

object editor widgets start to form up

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
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: 13
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
18
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "modeleditcontext.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 Model::EditContext::EditContext(Model& model) :
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
22 storedModel{model}
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
26 ldraw::id_t Model::EditContext::append(std::unique_ptr<ldraw::Object>&& object)
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
27 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
28 const ldraw::id_t id = object->id;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
29 this->model().objectsById[id] = object.get();
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
30 this->model().append(std::move(object));
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
31 return id;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
32 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
33
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
34 void Model::EditContext::remove(int position)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
35 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
36 this->model().remove(position);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
37 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
38
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 void Model::EditContext::setObjectProperty(
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
40 ldraw::id_t id,
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
41 ldraw::Property property,
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 const QVariant& value)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 {
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
44 ldraw::Object* object = this->model().objectAt(id);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
45 if (object != nullptr)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
46 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
47 object->setProperty(property, value);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
48 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
50
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
51 void Model::EditContext::invertObject(ldraw::id_t id)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
52 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
53 auto it = this->model().objectsById.find(id);
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
54 if (it != this->model().objectsById.end())
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
55 {
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 ldraw::Object* object = it->second;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
57 object->invert();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
58 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
59 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
60
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
61 Model& Model::EditContext::model()
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
62 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
63 return this->storedModel;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
64 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
65
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
66 static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const glm::vec3(&points)[4])
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
67 {
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
68 std::array<geom::Triangle, 2> result;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
69 switch (diagonal)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
70 {
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
71 case ldraw::Diagonal::Diagonal_13:
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
72 result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}};
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
73 break;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
74 case ldraw::Diagonal::Diagonal_24:
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
75 result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}};
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
76 break;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
77 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
78 return result;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
79 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
80
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
81 auto ldraw::splitQuadrilateral(
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
82 Model::EditContext& editor,
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
83 ldraw::quadrilateralid_t quadrilateral_id,
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
84 ldraw::Diagonal splitType
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
85 ) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
86 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
87 std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result;
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
88 QModelIndex index;
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
89 const ldraw::Quadrilateral* quadrilateral = editor.model().get(quadrilateral_id, &index);
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
90 if (quadrilateral != nullptr)
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
91 {
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
92 const ldraw::Color color = quadrilateral->colorIndex;
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
93 const std::array<geom::Triangle, 2> split = splitTriangles(splitType, quadrilateral->points);
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
94 const int position = index.row();
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
95 editor.remove(position);
77
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
96 result = std::make_pair(
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
97 editor.insert<ldraw::Triangle>(position, split[0].points, color),
028798a72591 added some meta stuff, simplified quadrilateral splitting and tested it
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
98 editor.insert<ldraw::Triangle>(position, split[1].points, color));
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
99 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
100 return result;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
101 }

mercurial