src/model.cpp

changeset 151
e628fc2e0c72
parent 150
b6cbba6e29a1
child 152
03f8e6d42e13
--- a/src/model.cpp	Thu Mar 03 11:42:52 2022 +0200
+++ b/src/model.cpp	Thu Mar 03 21:13:16 2022 +0200
@@ -87,7 +87,7 @@
  */
 QVariant Model::data(const QModelIndex& index, int role) const
 {
-	const ldraw::Object* object = this->objectAt(index);
+	const ldraw::Object* object = (*this)[index.row()];
 	switch(role)
 	{
 	case Qt::DisplayRole:
@@ -104,33 +104,6 @@
 }
 
 /**
- * @brief Gets a property of the header
- * @param property
- * @return QVariant
- */
-QVariant Model::getHeaderProperty(const HeaderProperty property)
-{
-	switch (property)
-	{
-	case HeaderProperty::Name:
-		return header.name;
-	}
-	return {};
-}
-
-/**
- * @brief Gets the specified property from the object at the specified index in the model.
- * @param index Index of object in the model
- * @param property Property to look up
- * @return QVariant
- */
-QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const
-{
-	const ldraw::Object* object = this->body[unsigned_cast(index)].get();
-	return object->getProperty(property);
-}
-
-/**
  * @brief Finds the position of the specified object in the model
  * @param id Object id to look for
  * @return model index
@@ -155,7 +128,7 @@
  */
 ldraw::id_t Model::idAt(const QModelIndex& index) const
 {
-	return this->objectAt(index)->id;
+	return (*this)[index.row()]->id;
 }
 
 #if 0
@@ -236,33 +209,6 @@
  * @param index Position of the object
  * @returns object pointer
  */
-ldraw::Object* Model::objectAt(const QModelIndex& index)
-{
-	return this->body[unsigned_cast(index.row())].get();
-}
-
-/**
- * @brief Gets the object pointer at the specified position
- * @param index Position of the object
- * @returns object pointer
- */
-const ldraw::Object* Model::objectAt(const QModelIndex& index) const
-{
-	return this->body[unsigned_cast(index.row())].get();
-}
-
-/**
- * @brief Attempts the save the model
- */
-void Model::save(QIODevice* device) const
-{
-	QTextStream out{device};
-	for (const ModelObjectPointer& object : this->body)
-	{
-		out << object.get()->toLDrawCode() << "\r\n";
-	}
-}
-
 ldraw::Object* Model::operator[](int index)
 {
 	if (index >= 0 and index < this->size())
@@ -276,6 +222,65 @@
 }
 
 /**
+ * @brief Gets the object pointer at the specified position
+ * @param index Position of the object
+ * @returns object pointer
+ */
+const ldraw::Object* Model::operator[](int index) const
+{
+	if (index >= 0 and index < this->size())
+	{
+		return this->body[index].get();
+	}
+	else
+	{
+		throw std::out_of_range{"index out of range"};
+	}
+}
+
+/**
+ * @brief Gets an object pointer by id. Used by the editing context to actually modify objects.
+ * @param id
+ * @return object pointer
+ */
+ldraw::Object* Model::findObjectById(const ldraw::id_t id)
+{
+	const QModelIndex index = this->find(id);
+	if (index.isValid())
+	{
+		return (*this)[index.row()];
+	}
+	else
+	{
+		return nullptr;
+	}
+}
+
+const ldraw::Object* Model::findObjectById(const ldraw::id_t id) const
+{
+	const QModelIndex index = this->find(id);
+	if (index.isValid())
+	{
+		return (*this)[index.row()];
+	}
+	else
+	{
+		return nullptr;
+	}
+}
+
+/**
+ * @brief Attempts the save the model
+ */
+void save(const Model &model, QIODevice *device)
+{
+	QTextStream out{device};
+	applyToModel<ldraw::Object>(model, [&](const ldraw::Object* object) {
+		out << object->toLDrawCode() << "\r\n";
+	});
+}
+
+/**
  * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial
  */
 void makeUnofficial(Model& model)

mercurial