Sun, 03 Nov 2019 12:56:42 +0200
added saving of splitter state and recent files
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" | |
6 | #include "objecttypes/modelobject.h" | |
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; |
6 | 26 | signals: |
27 | void objectAdded(modelobjects::Id id, int position); | |
3 | 28 | private: |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
29 | using ModelObjectPointer = std::unique_ptr<modelobjects::BaseObject>; |
3 | 30 | template<typename T, typename... Args> |
31 | T* append(Args&&... args); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
32 | void append(ModelObjectPointer&& object); |
3 | 33 | template<typename T, typename... Args> |
34 | T* insert(int position, Args&&... args); | |
35 | bool modified = false; | |
36 | QString path; | |
37 | LDHeader header; | |
38 | std::vector<ModelObjectPointer> body; | |
6 | 39 | std::map<modelobjects::Id, modelobjects::BaseObject*> objectsById; |
3 | 40 | }; |
41 | ||
42 | template<typename T, typename... Args> | |
43 | T* Model::append(Args&&... args) | |
44 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
45 | emit layoutAboutToBeChanged(); |
3 | 46 | this->body.push_back(std::make_unique<T>(args...)); |
6 | 47 | T* pointer = static_cast<T*>(this->body.back().get()); |
48 | this->objectsById[pointer->id] = pointer; | |
49 | emit objectAdded(pointer->id, this->body.size() - 1); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
50 | emit layoutChanged(); |
6 | 51 | return pointer; |
3 | 52 | } |
53 | ||
54 | template<typename T, typename... Args> | |
55 | T* Model::insert(int position, Args&&... args) | |
56 | { | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
57 | emit layoutAboutToBeChanged(); |
3 | 58 | this->body.insert(position, std::make_unique<T>(args...)); |
6 | 59 | T* pointer = static_cast<T*>(this->body[position]); |
60 | this->objectsById[pointer->id] = pointer; | |
61 | emit objectAdded(pointer->id, position); | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
62 | emit layoutChanged(); |
6 | 63 | return pointer; |
3 | 64 | } |