src/model.cpp

changeset 51
1a9eac27698d
parent 35
98906a94732f
child 73
97df974b5ed5
--- a/src/model.cpp	Fri Feb 07 02:02:16 2020 +0200
+++ b/src/model.cpp	Fri Feb 07 23:59:06 2020 +0200
@@ -26,7 +26,7 @@
 
 int Model::size() const
 {
-	return this->body.size();
+	return static_cast<int>(this->body.size());
 }
 
 Model::EditContext Model::edit()
@@ -41,8 +41,7 @@
 
 QVariant Model::data(const QModelIndex& index, int role) const
 {
-	const int row = index.row();
-	ldraw::Object* object = this->body[row].get();
+	const ldraw::Object* object = this->objectAt(index);
 	switch(role)
 	{
 	case Qt::DisplayRole:
@@ -64,14 +63,13 @@
 	{
 	case HeaderProperty::Name:
 		return header.name;
-	default:
-		return {};
 	}
+	return {};
 }
 
 QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const
 {
-	const ldraw::Object* object = this->body[index].get();
+	const ldraw::Object* object = this->body[unsigned_cast(index)].get();
 	return object->getProperty(property);
 }
 
@@ -90,12 +88,30 @@
 	return this->cachedPolygons;
 }
 
+QModelIndex Model::lookup(ldraw::Id id) const
+{
+	// FIXME: This linear search will probably cause performance issues
+	for (std::size_t i = 0; i < this->body.size(); i += 1)
+	{
+		if (this->body[i]->id == id)
+		{
+			return this->index(static_cast<int>(i));
+		}
+	}
+	return {};
+}
+
+ldraw::Id Model::resolve(const QModelIndex& index) const
+{
+	return this->objectAt(index)->id;
+}
+
 void Model::getObjectPolygons(
 	const int index,
 	std::vector<gl::Polygon>& polygons_out,
 	ldraw::GetPolygonsContext* context) const
 {
-	const ldraw::Object* object = this->body[index].get();
+	const ldraw::Object* object = this->body[unsigned_cast(index)].get();
 	object->getPolygons(polygons_out, context);
 }
 
@@ -103,3 +119,13 @@
 {
 	this->body.push_back(std::move(object));
 }
+
+ldraw::Object* Model::objectAt(const QModelIndex& index)
+{
+	return this->body[unsigned_cast(index.row())].get();
+}
+
+const ldraw::Object* Model::objectAt(const QModelIndex& index) const
+{
+	return this->body[unsigned_cast(index.row())].get();
+}

mercurial