src/modeleditcontext.cpp

changeset 149
008989bc7d6e
parent 136
e8444e0d7f1a
--- a/src/modeleditcontext.cpp	Tue Nov 02 15:43:57 2021 +0200
+++ b/src/modeleditcontext.cpp	Tue Mar 01 17:00:19 2022 +0200
@@ -25,12 +25,29 @@
 {
 }
 
+void Model::EditContext::markObjectAsModified(ldraw::id_t id, ldraw::Object *object)
+{
+	if (not this->modifiedObjects.contains(id))
+	{
+		QDataStream stream{&this->modifiedObjects[id], QIODevice::WriteOnly};
+		object->serialize(stream);
+	}
+}
+
 Model::EditContext::~EditContext()
 {
-	for (ldraw::id_t id : this->modifiedObjects)
+	mapapply(this->modifiedObjects, [&](MAP_CPARMS(this->modifiedObjects))
 	{
-		this->model().objectModified(id);
-	}
+		ldraw::Object* object = this->model().objectAt(key);
+		if (object != nullptr)
+		{
+			QByteArray newState;
+			QDataStream stream{&newState, QIODevice::WriteOnly};
+			object->serialize(stream);
+			const int position = this->model().lookup(key).row();
+			Q_EMIT this->model().objectStateChanged(position, value, newState);
+		}
+	});
 	this->model().editFinished();
 }
 
@@ -42,6 +59,19 @@
 	return id;
 }
 
+void Model::EditContext::insertFromState(int position, QByteArray &state)
+{
+	int typeInteger;
+	QDataStream stream{&state, QIODevice::ReadOnly};
+	stream >> typeInteger;
+	switch (static_cast<ldraw::Object::Type>(typeInteger))
+	{
+	case ldraw::Object::Type::Empty:
+		this->storedModel.insert<ldraw::Empty>(position);
+		break;
+	}
+}
+
 void Model::EditContext::remove(int position)
 {
 	this->model().remove(position);
@@ -56,8 +86,8 @@
 	ldraw::Object* const object = this->model().objectAt(id);
 	if (object != nullptr)
 	{
+		this->markObjectAsModified(id, object);
 		const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value});
-		modifiedObjects.insert(id);
 		return result;
 	}
 	else
@@ -71,8 +101,8 @@
 	ldraw::Object* object = this->model().objectAt(id);
 	if (object != nullptr)
 	{
+		this->markObjectAsModified(id, object);
 		object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)});
-		modifiedObjects.insert(id);
 	}
 }
 
@@ -82,10 +112,40 @@
 	if (it != this->model().objectsById.end())
 	{
 		ldraw::Object* object = it->second;
+		this->markObjectAsModified(id, object);
 		object->invert();
 	}
 }
 
+void Model::EditContext::deserialize(ldraw::id_t id, QDataStream &stream)
+{
+	auto it = this->model().objectsById.find(id);
+	if (it != this->model().objectsById.end())
+	{
+		ldraw::Object* object = it->second;
+		int typeInteger;
+		stream >> typeInteger;
+		const ldraw::Object::Type type = static_cast<ldraw::Object::Type>(typeInteger);
+		if (object->typeIdentifier() == type)
+		{
+			this->markObjectAsModified(id, object);
+			object->deserialize(stream);
+		}
+	}
+}
+
+ldraw::id_t Model::EditContext::resolve(int position) const
+{
+	if (position >= 0 and position < this->storedModel.rowCount({}))
+	{
+		return this->storedModel.objectAt(this->storedModel.index(position))->id;
+	}
+	else
+	{
+		return ldraw::NULL_ID;
+	}
+}
+
 Model& Model::EditContext::model()
 {
 	return this->storedModel;

mercurial