From 8d0c50753dbe327bc6ace6330d843f03fd8eee74 Mon Sep 17 00:00:00 2001 From: Julian de Bhal Date: Fri, 8 Jul 2011 15:49:20 +1000 Subject: QTBUG-19687 - Fix picking for dynamically created Item3Ds Change-Id: I3f46cf64ede0cf0647ca1cfc2fc95e837500a07c Reviewed-on: http://codereview.qt.nokia.com/1340 Reviewed-by: Qt Sanity Bot Reviewed-by: Patrick Burke --- src/imports/threed/viewport.cpp | 13 ++++++++++++ src/imports/threed/viewport.h | 1 + src/quick3d/qdeclarativeitem3d.cpp | 12 +++++++++++ tests/auto/qml3d/picking/tst_picking.qml | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/src/imports/threed/viewport.cpp b/src/imports/threed/viewport.cpp index f88d19fd..631d2aad 100644 --- a/src/imports/threed/viewport.cpp +++ b/src/imports/threed/viewport.cpp @@ -1203,4 +1203,17 @@ QPointF Viewport::viewDelta(qreal deltax, qreal deltay) return QPointF(deltax * scaleX / w, deltay * scaleY / h); } +QVariant Viewport::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) +{ + if (change == QGraphicsItem::ItemChildAddedChange) + { + QGraphicsItem *newItem = value.value(); + if (QDeclarativeItem3D* item3d = + qobject_cast(newItem)) + { + setItemViewport(item3d); + } + }; + return QGraphicsItem::itemChange(change, value); +} QT_END_NAMESPACE diff --git a/src/imports/threed/viewport.h b/src/imports/threed/viewport.h index 9e802412..77cb6fe7 100644 --- a/src/imports/threed/viewport.h +++ b/src/imports/threed/viewport.h @@ -121,6 +121,7 @@ protected: void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void wheelEvent(QGraphicsSceneWheelEvent *event); void keyPressEvent(QKeyEvent *event); + virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value); private: ViewportPrivate *d; diff --git a/src/quick3d/qdeclarativeitem3d.cpp b/src/quick3d/qdeclarativeitem3d.cpp index 7141b550..86171dc3 100644 --- a/src/quick3d/qdeclarativeitem3d.cpp +++ b/src/quick3d/qdeclarativeitem3d.cpp @@ -1040,6 +1040,8 @@ void QDeclarativeItem3D::draw(QGLPainter *painter) // Bail out if this item and its children have been disabled. if (!d->isEnabled) return; + if (!d->isInitialized) + initialize(painter); int prevId = painter->objectPickId(); painter->setObjectPickId(d->objectPickId); @@ -1163,6 +1165,16 @@ void QDeclarativeItem3D::initialize(QGLPainter *painter) { if (d->isInitialized) return; + if (!d->viewport) + { + if (QDeclarativeItem3D* parentItem = + qobject_cast(parent())) + { + d->viewport = parentItem->d->viewport; + Q_ASSERT(d->viewport); + } + } + d->objectPickId = d->viewport->registerPickableObject(this); for (int index = 0; index < children().size(); ++index) { diff --git a/tests/auto/qml3d/picking/tst_picking.qml b/tests/auto/qml3d/picking/tst_picking.qml index 4e89373b..911a3124 100644 --- a/tests/auto/qml3d/picking/tst_picking.qml +++ b/tests/auto/qml3d/picking/tst_picking.qml @@ -106,6 +106,34 @@ Rectangle } } + Component { + id: pickTestDelegate + Quad { + objectName: "foo" + transform: [ + Rotation3D { + axis: Qt.vector3d(1,0,0); + angle: 90 + }, + // Bring quad forwards a little bit to ensure it's drawn + Translation3D { translate: Qt.vector3d(0,0,0.1)} + ] + + // Ensure items are above smallerQuad + position: Qt.vector3d(0, 0, (index + 1) * 0.1) + property color color: model.color + } + } + + ListModel{ + id: pickTestModel + } + + Repeater { + delegate: pickTestDelegate + model: pickTestModel + } + // picking fails until the first paint occurs. There's no // straight-forward way to wait for the first paint, so we'll use a // timer to allow the paint to happen. @@ -153,6 +181,12 @@ Rectangle "Didn't find smaller quad at midpoint"); verify(viewport.objectForPoint(-midX,-midY) == null, "Incorrectly found object off viewport"); + pickTestModel.append({"color":"#00ff00"}); + verify(viewport.objectForPoint(midX,midY).color == "#00ff00", + "Didn't find modelview quad at midpoint"); + pickTestModel.append({"color":"#ff00ff"}); + verify(viewport.objectForPoint(midX,midY).color == "#ff00ff", + "Didn't find second modelview quad at midpoint"); } } } -- cgit v1.2.3