QmlDesigner: Tweak the Lowest Common Ancestor function

* LCA function accepts generic containers as the argument
* LCA returns an invalid node as the result if a node belongs to a
different model
* An additional condition is added to check the validity of the parent
nodes for the cases which a node has an invalid parent

Change-Id: I9d9c1891e75a3a6a84e797deed5210c02fd92522
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Ali Kianian
2023-09-25 12:11:33 +03:00
parent abc0139b58
commit 2df11c8bf7
2 changed files with 14 additions and 6 deletions

View File

@@ -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<ModelNode> &nodes)
ModelNode lowestCommonAncestor(Utils::span<const ModelNode> 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<const ModelNode>(nodes).subspan(1)) {
for (const ModelNode &node : nodes.subspan(1)) {
accumulatedNode = lowestCommonAncestor(accumulatedNode,
node,
accumulatedNodeDepth,
accumulatedNodeDepth);
if (!accumulatedNode)
return {};
}
return accumulatedNode;

View File

@@ -39,6 +39,6 @@ QMLDESIGNERCORE_EXPORT QList<ModelNode> pruneChildren(const QList<ModelNode> &no
QMLDESIGNERCORE_EXPORT QList<ModelNode> allModelNodesWithId(AbstractView *view);
QMLDESIGNERCORE_EXPORT bool isThisOrAncestorLocked(const ModelNode &node);
QMLDESIGNERCORE_EXPORT ModelNode lowestCommonAncestor(const QList<ModelNode> &nodes);
QMLDESIGNERCORE_EXPORT ModelNode lowestCommonAncestor(Utils::span<const ModelNode> nodes);
} // namespace QmlDesigner::ModelUtils