diff --git a/src/plugins/qmldesigner/designercore/model/modelutils.cpp b/src/plugins/qmldesigner/designercore/model/modelutils.cpp index 22f472c3e5f..9ef98a39944 100644 --- a/src/plugins/qmldesigner/designercore/model/modelutils.cpp +++ b/src/plugins/qmldesigner/designercore/model/modelutils.cpp @@ -198,10 +198,11 @@ ModelNode lowestCommonAncestor(const ModelNode &node1, auto depthOfNode = [](const ModelNode &node) -> int { int depth = 0; ModelNode parentNode = node; - while (!parentNode.isRootNode()) { + while (parentNode && !parentNode.isRootNode()) { depth++; parentNode = parentNode.parentProperty().parentModelNode(); } + return depth; }; @@ -211,6 +212,11 @@ ModelNode lowestCommonAncestor(const ModelNode &node1, return node1; } + if (node1.model() != node2.model()) { + depthOfLCA = -1; + return {}; + } + if (node1.isRootNode()) { depthOfLCA = 0; return node1; @@ -250,18 +256,20 @@ ModelNode lowestCommonAncestor(const ModelNode &node1, * \brief The lowest common node containing all nodes. If one of the nodes (Node A) is * the ancestor of the other nodes, the return value is Node A and not the parent of Node A. */ -ModelNode lowestCommonAncestor(const QList &nodes) +ModelNode lowestCommonAncestor(Utils::span nodes) { - if (nodes.isEmpty()) + if (nodes.empty()) return {}; - ModelNode accumulatedNode = nodes.first(); + ModelNode accumulatedNode = nodes.front(); int accumulatedNodeDepth = -1; - for (const ModelNode &node : Utils::span(nodes).subspan(1)) { + for (const ModelNode &node : nodes.subspan(1)) { accumulatedNode = lowestCommonAncestor(accumulatedNode, node, accumulatedNodeDepth, accumulatedNodeDepth); + if (!accumulatedNode) + return {}; } return accumulatedNode; diff --git a/src/plugins/qmldesigner/designercore/model/modelutils.h b/src/plugins/qmldesigner/designercore/model/modelutils.h index 26cf29aac00..3f8ddeab56b 100644 --- a/src/plugins/qmldesigner/designercore/model/modelutils.h +++ b/src/plugins/qmldesigner/designercore/model/modelutils.h @@ -39,6 +39,6 @@ QMLDESIGNERCORE_EXPORT QList pruneChildren(const QList &no QMLDESIGNERCORE_EXPORT QList allModelNodesWithId(AbstractView *view); QMLDESIGNERCORE_EXPORT bool isThisOrAncestorLocked(const ModelNode &node); -QMLDESIGNERCORE_EXPORT ModelNode lowestCommonAncestor(const QList &nodes); +QMLDESIGNERCORE_EXPORT ModelNode lowestCommonAncestor(Utils::span nodes); } // namespace QmlDesigner::ModelUtils