Wed, 01 Jan 2020 17:45:56 +0200
things
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
1 | #include <QBrush> |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
2 | #include <QFont> |
3 | 3 | #include "model.h" |
4 | #include "modeleditcontext.h" | |
5 | ||
21 | 6 | Model::Model(QObject* parent) : |
7 | QAbstractListModel{parent} {} | |
3 | 8 | |
9 | int Model::size() const | |
10 | { | |
11 | return this->body.size(); | |
12 | } | |
13 | ||
14 | Model::EditContext Model::edit() | |
15 | { | |
16 | return {*this}; | |
17 | } | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
18 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
19 | int Model::rowCount(const QModelIndex&) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
20 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
21 | return size(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
22 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
23 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
24 | QVariant Model::data(const QModelIndex& index, int role) const |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
25 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
26 | const int row = index.row(); |
13 | 27 | linetypes::Object* object = this->body[row].get(); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
28 | switch(role) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
29 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
30 | case Qt::DisplayRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
31 | return object->textRepresentation(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
32 | case Qt::ForegroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
33 | return object->textRepresentationForeground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
34 | case Qt::BackgroundRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
35 | return object->textRepresentationBackground(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
36 | case Qt::FontRole: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
37 | return object->textRepresentationFont(); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
38 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
39 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
40 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
41 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
42 | |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
43 | QVariant Model::getHeaderProperty(const HeaderProperty property) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
44 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
45 | switch (property) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
46 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
47 | case HeaderProperty::Name: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
48 | return header.name; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
49 | default: |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
50 | return {}; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
51 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
52 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
53 | |
13 | 54 | QVariant Model::getObjectProperty(const int index, const linetypes::Property property) const |
12 | 55 | { |
13 | 56 | const linetypes::Object* object = this->body[index].get(); |
12 | 57 | return object->getProperty(property); |
58 | } | |
59 | ||
21 | 60 | std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const |
61 | { | |
62 | if (this->needRecache) | |
63 | { | |
64 | this->cachedPolygons.clear(); | |
65 | linetypes::GetPolygonsContext context{documents}; | |
66 | for (int i = 0; i < this->size(); i += 1) | |
67 | { | |
68 | this->getObjectPolygons(i, this->cachedPolygons, &context); | |
69 | } | |
70 | this->needRecache = false; | |
71 | } | |
72 | return this->cachedPolygons; | |
73 | } | |
74 | ||
75 | void Model::getObjectPolygons( | |
76 | const int index, | |
77 | std::vector<gl::Polygon>& polygons_out, | |
78 | linetypes::GetPolygonsContext* context) const | |
79 | { | |
80 | const linetypes::Object* object = this->body[index].get(); | |
81 | object->getPolygons(polygons_out, context); | |
82 | } | |
83 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
84 | void Model::append(ModelObjectPointer&& object) |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
85 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
86 | this->body.push_back(std::move(object)); |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
3
diff
changeset
|
87 | } |