Skip to content

Commit

Permalink
add wrapper group for animateMotion
Browse files Browse the repository at this point in the history
  • Loading branch information
pgilfernandez committed Nov 27, 2024
1 parent ae6b430 commit e82a04c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
53 changes: 52 additions & 1 deletion src/core/TransformEffects/followpatheffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ void FollowPathEffect::applyEffect(const qreal relFrame,
posY += posYChange; //p1.y()*infl;
}

QDomNode findNodeRecursive(const QDomNode &node, const std::function<bool(const QDomNode&)> &predicate) {
// Verificar si el nodo actual cumple con el criterio
if (predicate(node)) {
return node;
}

// Recorrer los hijos del nodo actual
QDomNodeList children = node.childNodes();
for (int i = 0; i < children.size(); ++i) {
QDomNode found = findNodeRecursive(children.at(i), predicate);
if (!found.isNull()) {
return found; // Retornar el nodo si se encuentra
}
}

// Retornar un nodo nulo si no se encuentra
return QDomNode();
}

QDomElement FollowPathEffect::saveFollowPathSVG(SvgExporter &exp,
const FrameRange &visRange,
QDomElement &childElement,
Expand All @@ -207,5 +226,37 @@ QDomElement FollowPathEffect::saveFollowPathSVG(SvgExporter &exp,
const auto transformed = transform->saveSVG(exp,
visRange,
parentElement);
return transformed;

// Make a mutable copy of `transformed`
QDomElement mutableTransformed = transformed;

// Recursive search of <animateMotion> node
QDomNode animateMotionNode = findNodeRecursive(mutableTransformed, [](const QDomNode &node) {
return node.isElement() && node.toElement().tagName() == "animateMotion";
});

// Recursive search of comment `<!--wrapper-end-->` (added in transformeffectcollection.cpp)
QDomNode wrapperEndComment = findNodeRecursive(mutableTransformed, [](const QDomNode &node) {
return node.isComment() && node.nodeValue().trimmed() == "wrapper-end";
});

if (!animateMotionNode.isNull() && !wrapperEndComment.isNull()) {
// Get nodes parents
QDomNode animateMotionParent = animateMotionNode.parentNode();
QDomNode commentParent = wrapperEndComment.parentNode();

// Check that parents aren't null
if (!animateMotionParent.isNull() && !commentParent.isNull()) {
// Detete animateMotionNode from his parent
animateMotionParent.removeChild(animateMotionNode);

// Insert animateMotionNode right before wrapperEndComment
commentParent.insertBefore(animateMotionNode, wrapperEndComment);

// Delete `<!--wrapper-end-->` comment
commentParent.removeChild(wrapperEndComment);
}
}

return mutableTransformed;
}
26 changes: 25 additions & 1 deletion src/core/TransformEffects/transformeffectcollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,31 @@ QDomElement TransformEffectCollection::saveEffectsSVG(SvgExporter &exp,
QDomElement &childElement,
const QDomElement &parentElement) const
{
QDomElement result = parentElement;

// Create a copy no constant of parentElement
QDomElement mutableParent = parentElement;

// Get the main doc
QDomDocument doc = mutableParent.ownerDocument();

// Create a new "wrapper" group
QDomElement wrapperGroup = doc.createElement("g");
wrapperGroup.setAttribute("class", "wrapper");

// Insert the wrapper before parentElement
QDomNode parentNode = mutableParent.parentNode();
if (!parentNode.isNull()) {
parentNode.insertBefore(wrapperGroup, mutableParent);
}

// Move parentElement inside the wrapper
wrapperGroup.appendChild(mutableParent);

// Add a comment to parentElement to mark the point where the 'animationMotion' must be placed (check followpatheffect.cpp file to see the replacement method)
auto comment = wrapperGroup.ownerDocument().createComment("wrapper-end");
wrapperGroup.appendChild(comment);

QDomElement result = wrapperGroup;
const auto& children = ca_getChildren();
for (const auto& effect : children) {
if (const auto path = enve_cast<FollowPathEffect*>(effect.get())) {
Expand Down

0 comments on commit e82a04c

Please sign in to comment.