From 6c7c11ab6ba3c3e42ebf894a8dbbc093f602bda7 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Fri, 6 Sep 2013 15:30:32 +0200 Subject: [PATCH] Added deepCopy() p4#: 22334 --- cafProjectDataModel/cafPdmObject.cpp | 54 ++++++++++++++++++++++++++++ cafProjectDataModel/cafPdmObject.h | 2 ++ 2 files changed, 56 insertions(+) diff --git a/cafProjectDataModel/cafPdmObject.cpp b/cafProjectDataModel/cafPdmObject.cpp index 924319c80b..cf4aaf9117 100644 --- a/cafProjectDataModel/cafPdmObject.cpp +++ b/cafProjectDataModel/cafPdmObject.cpp @@ -24,6 +24,8 @@ #include #include +#include "cafPdmObjectFactory.h" +#include "cafPdmDocument.h" namespace caf { @@ -309,6 +311,58 @@ bool PdmObject::isValidXmlElementName(const QString& name) return true; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +PdmObject* PdmObject::deepCopy() +{ + QString encodedXml; + + { + PdmObjectGroup typedObjectGroup; + typedObjectGroup.addObject(this); + + QXmlStreamWriter xmlStream(&encodedXml); + xmlStream.setAutoFormatting(true); + + typedObjectGroup.writeFields(xmlStream); + } + + PdmObject* pdmCopy = NULL; + { + // Read back XML into object group, factory methods will be called that will create new objects + PdmObjectGroup destinationObjectGroup; + QXmlStreamReader xmlStream(encodedXml); + destinationObjectGroup.readFields(xmlStream); + + if (destinationObjectGroup.objects.size() == 1) + { + pdmCopy = destinationObjectGroup.objects[0]; + } + } + + return pdmCopy; + + /* + PdmObject* cloneRoot = PdmObjectFactory::instance()->create(this->classKeyword()); + + + + QString encodedXml; + { + QXmlStreamWriter xmlStream(&encodedXml); + xmlStream.setAutoFormatting(true); + + this->writeFields(xmlStream); + } + + QXmlStreamReader xmlStream(encodedXml); + cloneRoot->readFields(xmlStream); + + return cloneRoot; + */ +} + } //End of namespace caf diff --git a/cafProjectDataModel/cafPdmObject.h b/cafProjectDataModel/cafPdmObject.h index d5407d188d..a805169cd1 100644 --- a/cafProjectDataModel/cafPdmObject.h +++ b/cafProjectDataModel/cafPdmObject.h @@ -122,6 +122,8 @@ public: void readFields (QXmlStreamReader& inputStream ); void writeFields(QXmlStreamWriter& outputStream); + PdmObject* deepCopy(); + /// The registered fields contained in this PdmObject. Registered by subclasses. void fields(std::vector& fields) const; /// The fields containing pointers to this PdmObject. Use ownerObject() on the fieldHandle to get the PdmObject parent.