Sat, 14 Dec 2019 23:00:01 +0200
fixed build
3 | 1 | #pragma once |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
2 | #include <QAbstractListModel> |
3 | 3 | #include <memory> |
4 | #include "main.h" | |
5 | #include "header.h" | |
14 | 6 | #include "linetypes/object.h" |
3 | 7 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
8 | enum class HeaderProperty |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
9 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
10 | Name |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
11 | }; |
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 | class Model : public QAbstractListModel |
3 | 14 | { |
15 | Q_OBJECT | |
16 | public: | |
17 | class EditContext; | |
18 | Model(); | |
5 | 19 | Model(const Model&) = delete; |
3 | 20 | int size() const; |
21 | EditContext edit(); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
22 | int rowCount(const QModelIndex&) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
23 | QVariant data(const QModelIndex& index, int role) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
24 | QVariant getHeaderProperty(const HeaderProperty property); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
25 | const QString& getName() const; |
13 | 26 | QVariant getObjectProperty(const int index, const linetypes::Property property) const; |
6 | 27 | signals: |
13 | 28 | void objectAdded(linetypes::Id id, int position); |
3 | 29 | private: |
13 | 30 | using ModelObjectPointer = std::unique_ptr<linetypes::Object>; |
3 | 31 | template<typename T, typename... Args> |
32 | T* append(Args&&... args); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
33 | void append(ModelObjectPointer&& object); |
3 | 34 | template<typename T, typename... Args> |
35 | T* insert(int position, Args&&... args); | |
36 | bool modified = false; | |
37 | QString path; | |
38 | LDHeader header; | |
39 | std::vector<ModelObjectPointer> body; | |
13 | 40 | std::map<linetypes::Id, linetypes::Object*> objectsById; |
3 | 41 | }; |
42 | ||
43 | template<typename T, typename... Args> | |
44 | T* Model::append(Args&&... args) | |
45 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
46 | emit layoutAboutToBeChanged(); |
3 | 47 | this->body.push_back(std::make_unique<T>(args...)); |
6 | 48 | T* pointer = static_cast<T*>(this->body.back().get()); |
49 | this->objectsById[pointer->id] = pointer; | |
50 | emit objectAdded(pointer->id, this->body.size() - 1); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
51 | emit layoutChanged(); |
6 | 52 | return pointer; |
3 | 53 | } |
54 | ||
55 | template<typename T, typename... Args> | |
56 | T* Model::insert(int position, Args&&... args) | |
57 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
58 | emit layoutAboutToBeChanged(); |
3 | 59 | this->body.insert(position, std::make_unique<T>(args...)); |
6 | 60 | T* pointer = static_cast<T*>(this->body[position]); |
61 | this->objectsById[pointer->id] = pointer; | |
62 | emit objectAdded(pointer->id, position); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
63 | emit layoutChanged(); |
6 | 64 | return pointer; |
3 | 65 | } |