diff --git a/modules/juce_data_structures/values/juce_ValueTree.cpp b/modules/juce_data_structures/values/juce_ValueTree.cpp index a331924ce5..8edfb0e675 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.cpp +++ b/modules/juce_data_structures/values/juce_ValueTree.cpp @@ -665,6 +665,28 @@ ValueTree ValueTree::createCopy() const return ValueTree (createCopyIfNotNull (object.get())); } +void ValueTree::copyPropertiesFrom (const ValueTree& source, UndoManager* undoManager) +{ + jassert (object != nullptr || source.object == nullptr); // Trying to add properties to a null ValueTree will fail! + + if (source.object == nullptr) + removeAllProperties (undoManager); + else if (object != nullptr) + object->copyPropertiesFrom (*(source.object), undoManager); +} + +void ValueTree::copyPropertiesAndChildrenFrom (const ValueTree& source, UndoManager* undoManager) +{ + jassert (object != nullptr || source.object == nullptr); // Trying to copy to a null ValueTree will fail! + + copyPropertiesFrom (source, undoManager); + removeAllChildren (undoManager); + + if (object != nullptr && source.object != nullptr) + for (auto& child : source.object->children) + object->addChild (createCopyIfNotNull (child), -1, undoManager); +} + bool ValueTree::hasType (const Identifier& typeName) const noexcept { return object != nullptr && object->type == typeName; @@ -769,16 +791,6 @@ Identifier ValueTree::getPropertyName (int index) const noexcept : object->properties.getName (index); } -void ValueTree::copyPropertiesFrom (const ValueTree& source, UndoManager* undoManager) -{ - jassert (object != nullptr || source.object == nullptr); // Trying to add properties to a null ValueTree will fail! - - if (source.object == nullptr) - removeAllProperties (undoManager); - else if (object != nullptr) - object->copyPropertiesFrom (*(source.object), undoManager); -} - int ValueTree::getReferenceCount() const noexcept { return object != nullptr ? object->getReferenceCount() : 0; diff --git a/modules/juce_data_structures/values/juce_ValueTree.h b/modules/juce_data_structures/values/juce_ValueTree.h index f389eb5569..4a5e6ff90a 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.h +++ b/modules/juce_data_structures/values/juce_ValueTree.h @@ -134,12 +134,17 @@ public: /** Creates a reference to another ValueTree. */ ValueTree (const ValueTree&) noexcept; - /** Changes this object to be a reference to the given tree. */ - ValueTree& operator= (const ValueTree&); - /** Move constructor */ ValueTree (ValueTree&&) noexcept; + /** Changes this object to be a reference to the given tree. + Note that calling this just points this at the new object and invokes the + Listener::valueTreeRedirected callback, but it's not an undoable operation. If + you're trying to replace an entire tree in an undoable way, you probably want + to use copyPropertiesAndChildren() instead. + */ + ValueTree& operator= (const ValueTree&); + /** Destructor. */ ~ValueTree(); @@ -172,6 +177,19 @@ public: /** Returns a deep copy of this tree and all its sub-trees. */ ValueTree createCopy() const; + /** Overwrites all the properties in this tree with the properties of the source tree. + Any properties that already exist will be updated; and new ones will be added, and + any that are not present in the source tree will be removed. + @see copyPropertiesAndChildrenFrom + */ + void copyPropertiesFrom (const ValueTree& source, UndoManager* undoManager); + + /** Replaces all children and properties of this object with copies of those from + the source object. + @see copyPropertiesFrom + */ + void copyPropertiesAndChildrenFrom (const ValueTree& source, UndoManager* undoManager); + //============================================================================== /** Returns the type of this tree. The type is specified when the ValueTree is created. @@ -261,12 +279,6 @@ public: Value getPropertyAsValue (const Identifier& name, UndoManager* undoManager, bool shouldUpdateSynchronously = false); - /** Overwrites all the properties in this tree with the properties of the source tree. - Any properties that already exist will be updated; and new ones will be added, and - any that are not present in the source tree will be removed. - */ - void copyPropertiesFrom (const ValueTree& source, UndoManager* undoManager); - //============================================================================== /** Returns the number of child trees inside this one. @see getChild