From bf2c2e9bb2dd0b13cb2cb6728de0c2421fbafbb7 Mon Sep 17 00:00:00 2001 From: Jim Albamont Date: Wed, 13 Mar 2019 13:23:21 -0500 Subject: Fix Entity parenting hierarchy When the initial Entity backend node hierarchy is created it skips over any non-entity nodes to ensure that Entities are only parented to other Entities. Calling QNode::setParent breaks this when reparenting Entities to non-entity nodes. Fix by sending a new "parentEntityUpdated" property update that backend Entity nodes listen for. They keep the id of their new parent and flag the need to rebuild the entity hierarchy. This triggers a new job to clear the children and parents of every backend Entity, then rebuilds the hierarchy using the stored parent ID in each Entity. This is much more forgiving of creation/parenting ordering issues and shouldn't be less performant because any Entity reparent was previously marking everything dirty anyway. Add a new test from QTBUG-73905 that creates 4 cylinders and manipulates the parents in different ways. Add a new test to tst_nodes to reparent a QEntity to a QNode and ensure the entity finds it's correct QEntity parent. Add a new test to tst_entity to ensure backend nodes correctly handle the new parenting events. Task-number: QTBUG-73905 Change-Id: Iab0203947d89bbed2868b3629fbde879675fe568 Reviewed-by: Paul Lemire --- tests/auto/core/nodes/tst_nodes.cpp | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'tests/auto/core/nodes/tst_nodes.cpp') diff --git a/tests/auto/core/nodes/tst_nodes.cpp b/tests/auto/core/nodes/tst_nodes.cpp index 3f7fb4a75..193d88c83 100644 --- a/tests/auto/core/nodes/tst_nodes.cpp +++ b/tests/auto/core/nodes/tst_nodes.cpp @@ -81,6 +81,7 @@ private slots: void removingChildEntitiesFromNode(); void checkConstructionSetParentMix(); // QTBUG-60612 + void checkParentingQEntityToQNode(); // QTBUG-73905 void checkConstructionWithParent(); void checkConstructionWithNonRootParent(); // QTBUG-73986 void checkConstructionAsListElement(); @@ -1079,6 +1080,72 @@ void tst_Nodes::checkConstructionSetParentMix() QCOMPARE(lastEvent->addedNodeId(), subTreeRoot->id()); } +void tst_Nodes::checkParentingQEntityToQNode() +{ + // GIVEN + ObserverSpy spy; + Qt3DCore::QScene scene; + QScopedPointer root(new MyQNode()); + + // WHEN + root->setArbiterAndScene(&spy, &scene); + root->setSimulateBackendCreated(true); + + // THEN + QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->scene() != nullptr); + + // WHEN + auto subTreeRoot = new Qt3DCore::QEntity(root.data()); + auto childEntity = new Qt3DCore::QEntity(subTreeRoot); + auto childNode = new Qt3DCore::QNode(subTreeRoot); + + // THEN + QCoreApplication::processEvents(); + + // Ensure first event is subTreeRoot creation + const Qt3DCore::QNodeCreatedChangeBasePtr firstEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!firstEvent.isNull()); + QCOMPARE(firstEvent->subjectId(), subTreeRoot->id()); + QCOMPARE(firstEvent->parentId(), root->id()); + + // Ensure 2nd event is childEntity creation + const Qt3DCore::QNodeCreatedChangeBasePtr secondEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!secondEvent.isNull()); + QCOMPARE(secondEvent->subjectId(), childEntity->id()); + QCOMPARE(secondEvent->parentId(), subTreeRoot->id()); + + // Ensure 3rd event is childNode creation + const Qt3DCore::QNodeCreatedChangeBasePtr thirdEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!thirdEvent.isNull()); + QCOMPARE(thirdEvent->subjectId(), childNode->id()); + QCOMPARE(thirdEvent->parentId(), subTreeRoot->id()); + + + // WHEN we reparent the childEntity to the childNode (QNode) + + spy.events.clear(); + childEntity->setParent(childNode); + // THEN we should get + // - one child removed change for childEntity->subTreeRoot, + // - one child added change for childEntity->childNode, + // - and one property updated event specifying the correct QEntity parent (subTreeRoot) + QCOMPARE(spy.events.size(), 3); + + const auto removedEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!removedEvent.isNull()); + QCOMPARE(removedEvent->subjectId(), subTreeRoot->id()); + + const auto addedEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!addedEvent.isNull()); + QCOMPARE(addedEvent->subjectId(), childNode->id()); + + const auto parentChangeEvent = spy.events.takeFirst().change().dynamicCast(); + QVERIFY(!parentChangeEvent.isNull()); + QCOMPARE(parentChangeEvent->subjectId(), childEntity->id()); + QCOMPARE(parentChangeEvent->propertyName(), "parentEntityUpdated"); + QCOMPARE(parentChangeEvent->value().value(), subTreeRoot->id()); +} + void tst_Nodes::checkConstructionWithParent() { // GIVEN -- cgit v1.2.3