src/model.h

Sun, 19 Jan 2020 02:54:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 02:54:48 +0200
changeset 22
6da867fa5429
parent 21
0133e565e072
child 24
1a0faaaceb84
permissions
-rw-r--r--

commit work on GL rendering

3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #pragma once
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
2 #include <QAbstractListModel>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include <memory>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #include "main.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 #include "header.h"
14
20d2ed3af73d renamings
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
6 #include "linetypes/object.h"
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
7 #include "gl/common.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
9 enum class HeaderProperty
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
10 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
11 Name
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
12 };
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
13
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
14 class Model : public QAbstractListModel
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 Q_OBJECT
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 public:
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 class EditContext;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
19 Model(QObject* parent = nullptr);
5
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
20 Model(const Model&) = delete;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 int size() const;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 EditContext edit();
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
23 int rowCount(const QModelIndex&) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
24 QVariant data(const QModelIndex& index, int role) const override;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
25 QVariant getHeaderProperty(const HeaderProperty property);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
26 const QString& getName() const;
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
27 QVariant getObjectProperty(const int index, const linetypes::Property property) const;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
28 std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
29 void getObjectPolygons(const int index, std::vector<gl::Polygon>& polygons_out, linetypes::GetPolygonsContext* context) const;
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
30 signals:
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
31 void objectAdded(linetypes::Id id, int position);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 private:
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
33 using ModelObjectPointer = std::unique_ptr<linetypes::Object>;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 template<typename T, typename... Args>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 T* append(Args&&... args);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
36 void append(ModelObjectPointer&& object);
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 template<typename T, typename... Args>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 T* insert(int position, Args&&... args);
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 bool modified = false;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 QString path;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 LDHeader header;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 std::vector<ModelObjectPointer> body;
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
43 std::map<linetypes::Id, linetypes::Object*> objectsById;
21
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
44 mutable std::vector<gl::Polygon> cachedPolygons;
Teemu Piippo <teemu@hecknology.net>
parents: 14
diff changeset
45 mutable bool needRecache = true;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 template<typename T, typename... Args>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 T* Model::append(Args&&... args)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
51 emit layoutAboutToBeChanged();
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 this->body.push_back(std::make_unique<T>(args...));
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
53 T* pointer = static_cast<T*>(this->body.back().get());
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
54 this->objectsById[pointer->id] = pointer;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
55 emit objectAdded(pointer->id, this->body.size() - 1);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
56 emit layoutChanged();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
57 return pointer;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 template<typename T, typename... Args>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 T* Model::insert(int position, Args&&... args)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
63 emit layoutAboutToBeChanged();
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 this->body.insert(position, std::make_unique<T>(args...));
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
65 T* pointer = static_cast<T*>(this->body[position]);
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
66 this->objectsById[pointer->id] = pointer;
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
67 emit objectAdded(pointer->id, position);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
68 emit layoutChanged();
6
73e448b2943d language support
Teemu Piippo <teemu@hecknology.net>
parents: 5
diff changeset
69 return pointer;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 }

mercurial