src/model.cpp

Wed, 01 Jan 2020 17:45:56 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 01 Jan 2020 17:45:56 +0200
changeset 21
0133e565e072
parent 13
6e838748867b
child 24
1a0faaaceb84
permissions
-rw-r--r--

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
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "model.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #include "modeleditcontext.h"
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
6 Model::Model(QObject* parent) :
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
7 QAbstractListModel{parent} {}
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 int Model::size() const
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 return this->body.size();
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 Model::EditContext Model::edit()
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 return {*this};
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
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
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
54 QVariant Model::getObjectProperty(const int index, const linetypes::Property property) const
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
55 {
13
6e838748867b renamings
Teemu Piippo <teemu@hecknology.net>
parents: 12
diff changeset
56 const linetypes::Object* object = this->body[index].get();
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
57 return object->getProperty(property);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
58 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
59
21
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
60 std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
61 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
62 if (this->needRecache)
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
63 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
64 this->cachedPolygons.clear();
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
65 linetypes::GetPolygonsContext context{documents};
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
66 for (int i = 0; i < this->size(); i += 1)
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
67 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
68 this->getObjectPolygons(i, this->cachedPolygons, &context);
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
69 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
70 this->needRecache = false;
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
71 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
72 return this->cachedPolygons;
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
73 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
74
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
75 void Model::getObjectPolygons(
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
76 const int index,
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
77 std::vector<gl::Polygon>& polygons_out,
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
78 linetypes::GetPolygonsContext* context) const
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
79 {
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
80 const linetypes::Object* object = this->body[index].get();
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
81 object->getPolygons(polygons_out, context);
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
82 }
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
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 }

mercurial