src/model.h

Sat, 05 Mar 2022 13:59:54 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:59:54 +0200
changeset 166
8857351912d0
parent 152
03f8e6d42e13
child 173
8a3047468994
permissions
-rw-r--r--

Fix rendering of cursor coordinates on bright background

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
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
20 #include <QAbstractListModel>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include <memory>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "main.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include "header.h"
14
20d2ed3af73d renamings
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
24 #include "linetypes/object.h"
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
25 #include "linetypes/metacommand.h"
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
26 #include "gl/common.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
28 enum class HeaderProperty
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
29 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
30 Name
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
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
33 class Model : public QAbstractListModel
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 Q_OBJECT
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 public:
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
37 Model(QObject* parent = nullptr);
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
38 Model(const Model&) = delete;
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
39
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 int size() const;
133
e39326ee48dc Begin work on edit history
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
41 ldraw::id_t at(int index) const;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
42 int rowCount(const QModelIndex&) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
43 QVariant data(const QModelIndex& index, int role) const override;
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
44 ldraw::Object* findObjectById(const ldraw::id_t id);
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
45 const ldraw::Object* findObjectById(const ldraw::id_t id) const;
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
46 QModelIndex find(ldraw::id_t id) const;
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
47 ldraw::id_t idAt(const QModelIndex& index) const;
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
48 template<typename R>
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
49 const R* get(ldraw::Id<R> id) const;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
50 template<typename R>
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
51 struct Get2Result
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
52 {
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
53 QModelIndex index;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
54 const R* object;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
55 };
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
56 template<typename R>
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
57 Get2Result<R> get2(ldraw::Id<R> id) const;
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
58 ldraw::Object* operator[](int index);
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
59 const ldraw::Object* operator[](int index) 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
60 using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 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
62 ldraw::Id<T> append(Args&&... args);
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
63 ldraw::id_t append(ModelObjectPointer&& object);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 template<typename T, typename... Args>
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
65 ldraw::Id<T> insert(std::size_t position, Args&&... args);
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
66 void remove(int position);
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
67 void emitDataChangedSignal(int position);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
68 private:
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 bool modified = false;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 std::vector<ModelObjectPointer> body;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
71 std::map<ldraw::id_t, ldraw::Object*> objectsById;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
74 void save(const Model& model, QIODevice *device);
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
75
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 140
diff changeset
76 /**
138
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
77 * @brief Calls the specified function to all matching objects in the model
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
78 * @tparam R Type of LDraw line type object to filter by
5d6a4ad46cc7 Document model.h
Teemu Piippo <teemu@hecknology.net>
parents: 136
diff changeset
79 * @param fn Function to call.
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
80 */
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
81 template<typename R, typename Fn>
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
82 void applyToModel(const Model& model, Fn&& f)
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
83 {
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
84 for (int i = 0; i < model.size(); i += 1)
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
85 {
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
86 const ldraw::Object* object = model[i];
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
87 const R* subobject = dynamic_cast<const R*>(object);
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
88 if (subobject != nullptr)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
89 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
90 f(subobject);
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
91 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
92 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
93 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents: 116
diff changeset
94
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 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
96 ldraw::Id<T> Model::append(Args&&... args)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 {
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
98 const int position = static_cast<int>(this->body.size());
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
99 Q_EMIT beginInsertRows({}, position, position);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 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
101 ldraw::Object* pointer = this->body.back().get();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
102 this->objectsById[pointer->id] = pointer;
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
103 Q_EMIT endInsertRows();
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
104 return ldraw::Id<T>{pointer->id.value};
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 template<typename T, typename... Args>
111
1f42c03fafca Draw tool actually adds objects now
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
108 ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 {
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
110 Q_EMIT beginInsertRows({}, position, position);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
111 this->body.insert(std::begin(this->body) + 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
112 ldraw::Object* pointer = this->body[position].get();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
113 this->objectsById[pointer->id] = pointer;
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
114 Q_EMIT endInsertRows();
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
115 return ldraw::Id<T>{pointer->id.value};
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
117
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
118 template<typename R>
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
119 const R* Model::get(ldraw::Id<R> id) const
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
120 {
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
121 return this->get2(id).object;
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
122 }
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
123
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
124 template<typename R>
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
125 Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
126 {
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
127 Get2Result<R> result;
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
128 result.index = this->find(id);
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
129 if (result.index.isValid())
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
130 {
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
131 result.object = static_cast<const R*>((*this)[result.index.row()]);
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
132 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
133 else
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
134 {
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
135 result.object = nullptr;
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 76
diff changeset
136 }
116
aad3e897bc32 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
137 return result;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
138 }

mercurial