From f021680513fc95d979c4bbf36fa86b5c87f6ff44 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Wed, 4 Dec 2019 14:09:07 +0200 Subject: [PATCH] QmlDesigner: Fix selection box size calculations Selection box for zero size items ended up being infinite, which broke some functionality like fit to camera. Now boxes should be correctly zero sized for non-model items. This also fixes issues with parent box size if it has zero size children. Change-Id: I3c8fae3ee971fbb0cf9e0de2615c107ce97a76f8 Fixes: QDS-1287 Reviewed-by: Mahmoud Badri Reviewed-by: Thomas Hartmann --- .../qml2puppet/editor3d/generalhelper.cpp | 6 +++++- .../editor3d/selectionboxgeometry.cpp | 20 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/generalhelper.cpp b/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/generalhelper.cpp index 150b8e9ab19..84e28ac4ece 100644 --- a/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/generalhelper.cpp +++ b/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/generalhelper.cpp @@ -149,7 +149,8 @@ QVector4D GeneralHelper::focusObjectToCamera(QQuick3DCamera *camera, float defau QVector3D lookAt = targetObject ? targetObject->scenePosition() : QVector3D(); // Get object bounds - qreal maxExtent = 200.; + const qreal defaultExtent = 200.; + qreal maxExtent = defaultExtent; if (auto modelNode = qobject_cast(targetObject)) { auto targetPriv = QQuick3DObjectPrivate::get(targetObject); if (auto renderModel = static_cast(targetPriv->spatialNode)) { @@ -173,6 +174,9 @@ QVector4D GeneralHelper::focusObjectToCamera(QQuick3DCamera *camera, float defau maxExtent = qSqrt(qreal(e.x() * e.x() + e.y() * e.y() + e.z() * e.z())); maxExtent *= maxScale; + if (maxExtent < 0.0001) + maxExtent = defaultExtent; + // Adjust lookAt to look directly at the center of the object bounds lookAt = renderModel->globalTransform.map(center); lookAt.setZ(-lookAt.z()); // Render node transforms have inverted z diff --git a/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/selectionboxgeometry.cpp b/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/selectionboxgeometry.cpp index 1fb4f445a6e..17b101326e9 100644 --- a/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/selectionboxgeometry.cpp +++ b/share/qtcreator/qml/qmlpuppet/qml2puppet/editor3d/selectionboxgeometry.cpp @@ -43,6 +43,8 @@ namespace Internal { static const float floatMin = std::numeric_limits::lowest(); static const float floatMax = std::numeric_limits::max(); +static const QVector3D maxVec = QVector3D(floatMax, floatMax, floatMax); +static const QVector3D minVec = QVector3D(floatMin, floatMin, floatMin); SelectionBoxGeometry::SelectionBoxGeometry() : QQuick3DGeometry() @@ -136,8 +138,8 @@ QSSGRenderGraphObject *SelectionBoxGeometry::updateSpatialNode(QSSGRenderGraphOb QByteArray vertexData; QByteArray indexData; - QVector3D minBounds = QVector3D(floatMax, floatMax, floatMax); - QVector3D maxBounds = QVector3D(floatMin, floatMin, floatMin); + QVector3D minBounds = maxVec; + QVector3D maxBounds = minVec; if (m_targetNode) { auto rootPriv = QQuick3DObjectPrivate::get(m_rootNode); @@ -165,6 +167,8 @@ QSSGRenderGraphObject *SelectionBoxGeometry::updateSpatialNode(QSSGRenderGraphOb } } else { // Fill some dummy data so geometry won't get rejected + minBounds = {}; + maxBounds = {}; appendVertexData(QMatrix4x4(), vertexData, indexData, minBounds, maxBounds); } @@ -207,8 +211,8 @@ void SelectionBoxGeometry::getBounds( trackNodeChanges(node); } - QVector3D localMinBounds = QVector3D(floatMax, floatMax, floatMax); - QVector3D localMaxBounds = QVector3D(floatMin, floatMin, floatMin); + QVector3D localMinBounds = maxVec; + QVector3D localMaxBounds = minVec; // Find bounds for children QVector minBoundsVec; @@ -277,6 +281,14 @@ void SelectionBoxGeometry::getBounds( } } } + } else { + combineMinBounds(localMinBounds, {}); + combineMaxBounds(localMaxBounds, {}); + } + + if (localMaxBounds == minVec) { + localMinBounds = {}; + localMaxBounds = {}; } // Transform local space bounding box to parent space