src/model.h

Tue, 07 Jun 2022 21:35:29 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 07 Jun 2022 21:35:29 +0300
changeset 203
1909a0123c72
parent 200
ca23936b455b
child 206
654661eab7f3
permissions
-rw-r--r--

Move editing modes tool bar, tool options widget stack and model list view into the main window

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"
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
23 #include "colors.h"
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
24
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
25 struct SubfileReference
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
26 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
27 QString name;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
28 glm::mat4 transformation;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
29 bool inverted = false;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
30 };
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
31
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
32 template<typename T>
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
33 struct Colored : T
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
34 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
35 ldraw::Color color;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
36 };
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
37
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
38 struct Comment
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
39 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
40 QString text;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
41 };
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
43 struct ParseError
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
44 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
45 QString code;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
46 };
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
47
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
48 struct Empty {};
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
49
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
50 using ModelElement = std::variant<
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
51 Colored<SubfileReference>,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
52 Colored<LineSegment>,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
53 Colored<Triangle>,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
54 Colored<Quadrilateral>,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
55 Colored<ConditionalEdge>,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
56 Comment,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
57 Empty,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
58 ParseError>;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
59
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
60 QString modelElementToString(const ModelElement& element);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
61 struct ModelId
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
62 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
63 std::int32_t value;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
64 constexpr auto operator<=>(const ModelId& other) const = default;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
65 };
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
66
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
67 constexpr int qHash(ModelId id)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
68 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
69 return qHash(id.value);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
70 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
71
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
72 class Model : public QAbstractListModel
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 Q_OBJECT
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
75 struct Entry {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
76 ModelElement data;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
77 ModelId id;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
78 };
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
79 std::vector<Entry> body;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
80 std::map<ModelId, int> positions;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
81 ModelId runningId = {1};
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 public:
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
83 Model(QObject* parent);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
84 virtual ~Model();
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
85 ModelId append(const ModelElement& value);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
86 const ModelElement& at(int position) const;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
87 ModelId idAt(int position) const;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
88 void assignAt(int position, const ModelElement& element);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
89 std::optional<int> find(ModelId id) const;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
90 void remove(int index);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
91 int rowCount(const QModelIndex&) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
92 QVariant data(const QModelIndex& index, int role) const override;
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
93 const ModelElement& operator[](int index) const;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
94 int size() const;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
95 auto operator[](int index) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
96 struct {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
97 Model& model;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
98 int index;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
99 operator const ModelElement&() {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
100 return model.at(index);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
101 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
102 auto& operator=(const ModelElement& newData) {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
103 model.assignAt(index, newData);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
104 return *this;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
105 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
106 const auto* operator&() {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
107 return &(this->operator const ModelElement&());
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
108 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
109 } result{*this, index};
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
110 return result;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
111 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113
151
e628fc2e0c72 Clean up Model
Teemu Piippo <teemu@hecknology.net>
parents: 150
diff changeset
114 void save(const Model& model, QIODevice *device);
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 173
diff changeset
115 void updateHeaderNameField(Model& model, const QString &name);

mercurial