24 Model::Model(QObject* parent) : |
24 Model::Model(QObject* parent) : |
25 QAbstractListModel{parent} {} |
25 QAbstractListModel{parent} {} |
26 |
26 |
27 int Model::size() const |
27 int Model::size() const |
28 { |
28 { |
29 return this->body.size(); |
29 return static_cast<int>(this->body.size()); |
30 } |
30 } |
31 |
31 |
32 Model::EditContext Model::edit() |
32 Model::EditContext Model::edit() |
33 { |
33 { |
34 return {*this}; |
34 return {*this}; |
39 return size(); |
39 return size(); |
40 } |
40 } |
41 |
41 |
42 QVariant Model::data(const QModelIndex& index, int role) const |
42 QVariant Model::data(const QModelIndex& index, int role) const |
43 { |
43 { |
44 const int row = index.row(); |
44 const ldraw::Object* object = this->objectAt(index); |
45 ldraw::Object* object = this->body[row].get(); |
|
46 switch(role) |
45 switch(role) |
47 { |
46 { |
48 case Qt::DisplayRole: |
47 case Qt::DisplayRole: |
49 return object->textRepresentation(); |
48 return object->textRepresentation(); |
50 case Qt::ForegroundRole: |
49 case Qt::ForegroundRole: |
62 { |
61 { |
63 switch (property) |
62 switch (property) |
64 { |
63 { |
65 case HeaderProperty::Name: |
64 case HeaderProperty::Name: |
66 return header.name; |
65 return header.name; |
67 default: |
|
68 return {}; |
|
69 } |
66 } |
|
67 return {}; |
70 } |
68 } |
71 |
69 |
72 QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const |
70 QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const |
73 { |
71 { |
74 const ldraw::Object* object = this->body[index].get(); |
72 const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
75 return object->getProperty(property); |
73 return object->getProperty(property); |
76 } |
74 } |
77 |
75 |
78 std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const |
76 std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const |
79 { |
77 { |
88 this->needRecache = false; |
86 this->needRecache = false; |
89 } |
87 } |
90 return this->cachedPolygons; |
88 return this->cachedPolygons; |
91 } |
89 } |
92 |
90 |
|
91 QModelIndex Model::lookup(ldraw::Id id) const |
|
92 { |
|
93 // FIXME: This linear search will probably cause performance issues |
|
94 for (std::size_t i = 0; i < this->body.size(); i += 1) |
|
95 { |
|
96 if (this->body[i]->id == id) |
|
97 { |
|
98 return this->index(static_cast<int>(i)); |
|
99 } |
|
100 } |
|
101 return {}; |
|
102 } |
|
103 |
|
104 ldraw::Id Model::resolve(const QModelIndex& index) const |
|
105 { |
|
106 return this->objectAt(index)->id; |
|
107 } |
|
108 |
93 void Model::getObjectPolygons( |
109 void Model::getObjectPolygons( |
94 const int index, |
110 const int index, |
95 std::vector<gl::Polygon>& polygons_out, |
111 std::vector<gl::Polygon>& polygons_out, |
96 ldraw::GetPolygonsContext* context) const |
112 ldraw::GetPolygonsContext* context) const |
97 { |
113 { |
98 const ldraw::Object* object = this->body[index].get(); |
114 const ldraw::Object* object = this->body[unsigned_cast(index)].get(); |
99 object->getPolygons(polygons_out, context); |
115 object->getPolygons(polygons_out, context); |
100 } |
116 } |
101 |
117 |
102 void Model::append(ModelObjectPointer&& object) |
118 void Model::append(ModelObjectPointer&& object) |
103 { |
119 { |
104 this->body.push_back(std::move(object)); |
120 this->body.push_back(std::move(object)); |
105 } |
121 } |
|
122 |
|
123 ldraw::Object* Model::objectAt(const QModelIndex& index) |
|
124 { |
|
125 return this->body[unsigned_cast(index.row())].get(); |
|
126 } |
|
127 |
|
128 const ldraw::Object* Model::objectAt(const QModelIndex& index) const |
|
129 { |
|
130 return this->body[unsigned_cast(index.row())].get(); |
|
131 } |