forked from qt-creator/qt-creator
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:
@@ -198,10 +198,11 @@ ModelNode lowestCommonAncestor(const ModelNode &node1,
|
|||||||
auto depthOfNode = [](const ModelNode &node) -> int {
|
auto depthOfNode = [](const ModelNode &node) -> int {
|
||||||
int depth = 0;
|
int depth = 0;
|
||||||
ModelNode parentNode = node;
|
ModelNode parentNode = node;
|
||||||
while (!parentNode.isRootNode()) {
|
while (parentNode && !parentNode.isRootNode()) {
|
||||||
depth++;
|
depth++;
|
||||||
parentNode = parentNode.parentProperty().parentModelNode();
|
parentNode = parentNode.parentProperty().parentModelNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
return depth;
|
return depth;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -211,6 +212,11 @@ ModelNode lowestCommonAncestor(const ModelNode &node1,
|
|||||||
return node1;
|
return node1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (node1.model() != node2.model()) {
|
||||||
|
depthOfLCA = -1;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
if (node1.isRootNode()) {
|
if (node1.isRootNode()) {
|
||||||
depthOfLCA = 0;
|
depthOfLCA = 0;
|
||||||
return node1;
|
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
|
* \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.
|
* 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 {};
|
return {};
|
||||||
|
|
||||||
ModelNode accumulatedNode = nodes.first();
|
ModelNode accumulatedNode = nodes.front();
|
||||||
int accumulatedNodeDepth = -1;
|
int accumulatedNodeDepth = -1;
|
||||||
for (const ModelNode &node : Utils::span<const ModelNode>(nodes).subspan(1)) {
|
for (const ModelNode &node : nodes.subspan(1)) {
|
||||||
accumulatedNode = lowestCommonAncestor(accumulatedNode,
|
accumulatedNode = lowestCommonAncestor(accumulatedNode,
|
||||||
node,
|
node,
|
||||||
accumulatedNodeDepth,
|
accumulatedNodeDepth,
|
||||||
accumulatedNodeDepth);
|
accumulatedNodeDepth);
|
||||||
|
if (!accumulatedNode)
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return accumulatedNode;
|
return accumulatedNode;
|
||||||
|
@@ -39,6 +39,6 @@ QMLDESIGNERCORE_EXPORT QList<ModelNode> pruneChildren(const QList<ModelNode> &no
|
|||||||
QMLDESIGNERCORE_EXPORT QList<ModelNode> allModelNodesWithId(AbstractView *view);
|
QMLDESIGNERCORE_EXPORT QList<ModelNode> allModelNodesWithId(AbstractView *view);
|
||||||
|
|
||||||
QMLDESIGNERCORE_EXPORT bool isThisOrAncestorLocked(const ModelNode &node);
|
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
|
} // namespace QmlDesigner::ModelUtils
|
||||||
|
Reference in New Issue
Block a user