src/model.h

Thu, 06 Feb 2020 20:33:05 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 06 Feb 2020 20:33:05 +0200
changeset 46
98645c8e7704
parent 35
98906a94732f
child 51
1a9eac27698d
permissions
-rw-r--r--

added the pick scene

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"
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
25 #include "gl/common.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
27 enum class HeaderProperty
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
28 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
29 Name
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
30 };
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 class Model : public QAbstractListModel
3
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 Q_OBJECT
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 public:
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 class EditContext;
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;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 int size() const;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 EditContext edit();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
41 int rowCount(const QModelIndex&) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
42 QVariant data(const QModelIndex& index, int role) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
43 QVariant getHeaderProperty(const HeaderProperty property);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
44 const QString& getName() 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
45 QVariant getObjectProperty(const int index, const ldraw::Property property) const;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
46 std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) 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
47 void getObjectPolygons(const int index, std::vector<gl::Polygon>& polygons_out, ldraw::GetPolygonsContext* context) const;
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
48 signals:
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
49 void objectAdded(ldraw::Id id, int position);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 private:
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
51 using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 template<typename T, typename... 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
53 ldraw::Id append(Args&&... args);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
54 void append(ModelObjectPointer&& object);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 template<typename T, typename... 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
56 ldraw::Id insert(int position, Args&&... args);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 bool modified = false;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 QString path;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 LDHeader header;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 std::vector<ModelObjectPointer> body;
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
61 std::map<ldraw::Id, ldraw::Object*> objectsById;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
62 mutable std::vector<gl::Polygon> cachedPolygons;
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
63 mutable bool needRecache = true;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 template<typename T, typename... 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
67 ldraw::Id Model::append(Args&&... args)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
69 emit layoutAboutToBeChanged();
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 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
71 ldraw::Object* pointer = this->body.back().get();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
72 this->objectsById[pointer->id] = pointer;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
73 emit objectAdded(pointer->id, this->body.size() - 1);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
74 emit layoutChanged();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
75 return pointer->id;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 template<typename T, typename... 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
79 ldraw::Id Model::insert(int position, Args&&... args)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 {
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
81 emit layoutAboutToBeChanged();
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 this->body.insert(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
83 ldraw::Object* pointer = this->body[position].get();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
84 this->objectsById[pointer->id] = pointer;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
85 emit objectAdded(pointer->id, position);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
86 emit layoutChanged();
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
87 return pointer->id;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 }

mercurial