From 553d7f7416eca57b63992a453ceb3b333749c11a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 28 Oct 2009 16:15:06 +0200 Subject: Introduced QGraphicsItem::ItemSendsScenePositionChanges and QGraphicsItem::ItemScenePositionHasChanged Merge-Request: 1945 Reviewed-By: Andreas --- src/gui/graphicsview/qgraphicsitem.cpp | 63 ++++++++++++++++++++-- src/gui/graphicsview/qgraphicsitem.h | 6 ++- src/gui/graphicsview/qgraphicsitem_p.h | 7 ++- src/gui/graphicsview/qgraphicsscene.cpp | 56 +++++++++++++++++++ src/gui/graphicsview/qgraphicsscene.h | 1 + src/gui/graphicsview/qgraphicsscene_p.h | 7 +++ tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp | 74 ++++++++++++++++++++++++++ 7 files changed, 206 insertions(+), 8 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 4665916..93b9ba9 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -386,6 +386,12 @@ introduced in Qt 4.6. \omitvalue ItemIsFocusScope Internal only (for now). + + \value ItemSendsScenePositionChanges The item enables itemChange() + notifications for ItemScenePositionHasChanged. For performance reasons, + these notifications are disabled by default. You must enable this flag + to receive notifications for scene position changes. This flag was + introduced in Qt 4.6. */ /*! @@ -562,6 +568,14 @@ \value ItemOpacityHasChanged The item's opacity has changed. The value argument is the new opacity (i.e., a double). Do not call setOpacity() as this notification is delivered. The return value is ignored. + + \value ItemScenePositionHasChanged The item's scene position has changed. + This notification is sent if the ItemSendsScenePositionChanges flag is + enabled, and after the item's scene position has changed (i.e., the + position or transformation of the item itself or the position or + transformation of any ancestor has changed). The value argument is the + new scene position (the same as scenePos()), and QGraphicsItem ignores + the return value for this notification (i.e., a read-only notification). */ /*! @@ -990,6 +1004,10 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent) if (scene) { // Deliver the change to the index scene->d_func()->index->itemChange(q, QGraphicsItem::ItemParentChange, newParentVariant); + + // Disable scene pos notifications for old ancestors + if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges)) + scene->d_func()->setScenePosItemEnabled(q, false); } if (subFocusItem && parent) { @@ -1084,10 +1102,15 @@ void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent) parent->d_ptr->addChild(q); parent->itemChange(QGraphicsItem::ItemChildAddedChange, thisPointerVariant); - if (!implicitUpdate && scene) { - scene->d_func()->markDirty(q_ptr, QRect(), - /*invalidateChildren=*/false, - /*maybeDirtyClipPath=*/true); + if (scene) { + if (!implicitUpdate) + scene->d_func()->markDirty(q_ptr, QRect(), + /*invalidateChildren=*/false, + /*maybeDirtyClipPath=*/true); + + // Re-enable scene pos notifications for new ancestors + if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges)) + scene->d_func()->setScenePosItemEnabled(q, true); } // Inherit ancestor flags from the new parent. @@ -1746,6 +1769,12 @@ void QGraphicsItem::setFlags(GraphicsItemFlags flags) } if (d_ptr->scene) { + if ((flags & ItemSendsScenePositionChanges) != (oldFlags & ItemSendsScenePositionChanges)) { + if (flags & ItemSendsScenePositionChanges) + d_ptr->scene->d_func()->registerScenePosItem(this); + else + d_ptr->scene->d_func()->unregisterScenePosItem(this); + } d_ptr->scene->d_func()->markDirty(this, QRectF(), /*invalidateChildren=*/true, /*maybeDirtyClipPath*/true); @@ -3412,6 +3441,7 @@ void QGraphicsItem::setPos(const QPointF &pos) // Send post-notification. itemChange(QGraphicsItem::ItemPositionHasChanged, newPosVariant); + d_ptr->sendScenePosChange(); } /*! @@ -4022,6 +4052,7 @@ void QGraphicsItem::setTransform(const QTransform &matrix, bool combine) // Send post-notification. itemChange(ItemTransformHasChanged, newTransformVariant); + d_ptr->sendScenePosChange(); } /*! @@ -4246,6 +4277,24 @@ void QGraphicsItemPrivate::ensureSequentialSiblingIndex() } /*! + \internal +*/ +inline void QGraphicsItemPrivate::sendScenePosChange() +{ + Q_Q(QGraphicsItem); + if (scene) { + if (flags & QGraphicsItem::ItemSendsScenePositionChanges) + q->itemChange(QGraphicsItem::ItemScenePositionHasChanged, q->scenePos()); + if (scenePosDescendants) { + foreach (QGraphicsItem *item, scene->d_func()->scenePosItems) { + if (q->isAncestorOf(item)) + item->itemChange(QGraphicsItem::ItemScenePositionHasChanged, item->scenePos()); + } + } + } +} + +/*! \since 4.6 Stacks this item before \a sibling, which must be a sibling item (i.e., the @@ -10925,6 +10974,9 @@ QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemChange change) case QGraphicsItem::ItemOpacityHasChanged: str = "ItemOpacityHasChanged"; break; + case QGraphicsItem::ItemScenePositionHasChanged: + str = "ItemScenePositionHasChanged"; + break; } debug << str; return debug; @@ -10982,6 +11034,9 @@ QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlag flag) case QGraphicsItem::ItemIsFocusScope: str = "ItemIsFocusScope"; break; + case QGraphicsItem::ItemSendsScenePositionChanges: + str = "ItemSendsScenePositionChanges"; + break; } debug << str; return debug; diff --git a/src/gui/graphicsview/qgraphicsitem.h b/src/gui/graphicsview/qgraphicsitem.h index f3fe99c..6e1c632 100644 --- a/src/gui/graphicsview/qgraphicsitem.h +++ b/src/gui/graphicsview/qgraphicsitem.h @@ -105,7 +105,8 @@ public: ItemAcceptsInputMethod = 0x1000, ItemNegativeZStacksBehindParent = 0x2000, ItemIsPanel = 0x4000, - ItemIsFocusScope = 0x8000 // internal + ItemIsFocusScope = 0x8000, // internal + ItemSendsScenePositionChanges = 0x10000 // NB! Don't forget to increase the d_ptr->flags bit field by 1 when adding a new flag. }; Q_DECLARE_FLAGS(GraphicsItemFlags, GraphicsItemFlag) @@ -137,7 +138,8 @@ public: ItemZValueChange, ItemZValueHasChanged, ItemOpacityChange, - ItemOpacityHasChanged + ItemOpacityHasChanged, + ItemScenePositionHasChanged }; enum CacheMode { diff --git a/src/gui/graphicsview/qgraphicsitem_p.h b/src/gui/graphicsview/qgraphicsitem_p.h index ca56c18..92d45f6 100644 --- a/src/gui/graphicsview/qgraphicsitem_p.h +++ b/src/gui/graphicsview/qgraphicsitem_p.h @@ -179,6 +179,7 @@ public: holesInSiblingIndex(0), sequentialOrdering(1), updateDueToGraphicsEffect(0), + scenePosDescendants(0), globalStackingOrder(-1), q_ptr(0) { @@ -429,6 +430,7 @@ public: inline void ensureSortedChildren(); static inline bool insertionOrder(QGraphicsItem *a, QGraphicsItem *b); void ensureSequentialSiblingIndex(); + inline void sendScenePosChange(); QPainterPath cachedClipPath; QRectF childrenBoundingRect; @@ -483,7 +485,7 @@ public: // Packed 32 bits quint32 fullUpdatePending : 1; - quint32 flags : 16; + quint32 flags : 17; quint32 dirtyChildrenBoundingRect : 1; quint32 paintedViewBoundingRectsNeedRepaint : 1; quint32 dirtySceneTransform : 1; @@ -498,14 +500,15 @@ public: quint32 sceneTransformTranslateOnly : 1; quint32 notifyBoundingRectChanged : 1; quint32 notifyInvalidated : 1; - quint32 mouseSetsFocus : 1; // New 32 bits + quint32 mouseSetsFocus : 1; quint32 explicitActivate : 1; quint32 wantsActive : 1; quint32 holesInSiblingIndex : 1; quint32 sequentialOrdering : 1; quint32 updateDueToGraphicsEffect : 1; + quint32 scenePosDescendants : 1; // Optional stacking order int globalStackingOrder; diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index a4965e4..b992398 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -294,6 +294,7 @@ QGraphicsScenePrivate::QGraphicsScenePrivate() needSortTopLevelItems(true), holesInTopLevelSiblingIndex(false), topLevelSequentialOrdering(true), + scenePosDescendantsUpdatePending(false), stickyFocus(false), hasFocus(false), focusItem(0), @@ -488,6 +489,55 @@ void QGraphicsScenePrivate::_q_processDirtyItems() /*! \internal +*/ +void QGraphicsScenePrivate::setScenePosItemEnabled(QGraphicsItem *item, bool enabled) +{ + QGraphicsItem *p = item->d_ptr->parent; + while (p) { + p->d_ptr->scenePosDescendants = enabled; + p = p->d_ptr->parent; + } + if (!enabled && !scenePosDescendantsUpdatePending) { + scenePosDescendantsUpdatePending = true; + QMetaObject::invokeMethod(q_func(), "_q_updateScenePosDescendants", Qt::QueuedConnection); + } +} + +/*! + \internal +*/ +void QGraphicsScenePrivate::registerScenePosItem(QGraphicsItem *item) +{ + scenePosItems.insert(item); + setScenePosItemEnabled(item, true); +} + +/*! + \internal +*/ +void QGraphicsScenePrivate::unregisterScenePosItem(QGraphicsItem *item) +{ + scenePosItems.remove(item); + setScenePosItemEnabled(item, false); +} + +/*! + \internal +*/ +void QGraphicsScenePrivate::_q_updateScenePosDescendants() +{ + foreach (QGraphicsItem *item, scenePosItems) { + QGraphicsItem *p = item->d_ptr->parent; + while (p) { + p->d_ptr->scenePosDescendants = 1; + p = p->d_ptr->parent; + } + } + scenePosDescendantsUpdatePending = false; +} + +/*! + \internal Schedules an item for removal. This function leaves some stale indexes around in the BSP tree if called from the item's destructor; these will @@ -523,6 +573,9 @@ void QGraphicsScenePrivate::removeItemHelper(QGraphicsItem *item) widget->d_func()->fixFocusChainBeforeReparenting(0, 0); } + if (item->flags() & QGraphicsItem::ItemSendsScenePositionChanges) + unregisterScenePosItem(item); + item->d_func()->scene = 0; //We need to remove all children first because they might use their parent @@ -2540,6 +2593,9 @@ void QGraphicsScene::addItem(QGraphicsItem *item) } } + if (item->flags() & QGraphicsItem::ItemSendsScenePositionChanges) + d->registerScenePosItem(item); + // Ensure that newly added items that have subfocus set, gain // focus automatically if there isn't a focus item already. if (!d->focusItem && item != d->lastFocusItem && item->focusItem() == item) diff --git a/src/gui/graphicsview/qgraphicsscene.h b/src/gui/graphicsview/qgraphicsscene.h index d6d48d7..a47574e 100644 --- a/src/gui/graphicsview/qgraphicsscene.h +++ b/src/gui/graphicsview/qgraphicsscene.h @@ -299,6 +299,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_emitUpdated()) Q_PRIVATE_SLOT(d_func(), void _q_polishItems()) Q_PRIVATE_SLOT(d_func(), void _q_processDirtyItems()) + Q_PRIVATE_SLOT(d_func(), void _q_updateScenePosDescendants()) friend class QGraphicsItem; friend class QGraphicsItemPrivate; friend class QGraphicsView; diff --git a/src/gui/graphicsview/qgraphicsscene_p.h b/src/gui/graphicsview/qgraphicsscene_p.h index cd20fd0..6d46db5 100644 --- a/src/gui/graphicsview/qgraphicsscene_p.h +++ b/src/gui/graphicsview/qgraphicsscene_p.h @@ -122,6 +122,13 @@ public: void _q_processDirtyItems(); + QSet scenePosItems; + bool scenePosDescendantsUpdatePending; + void setScenePosItemEnabled(QGraphicsItem *item, bool enabled); + void registerScenePosItem(QGraphicsItem *item); + void unregisterScenePosItem(QGraphicsItem *item); + void _q_updateScenePosDescendants(); + void removeItemHelper(QGraphicsItem *item); QBrush backgroundBrush; diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index 684ad4f..1081fd5 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -401,6 +401,7 @@ private slots: void modality_clickFocus(); void modality_keyEvents(); void itemIsInFront(); + void scenePosChange(); // task specific tests below me void task141694_textItemEnsureVisible(); @@ -4229,6 +4230,8 @@ protected: break; case QGraphicsItem::ItemOpacityHasChanged: break; + case QGraphicsItem::ItemScenePositionHasChanged: + break; } return itemChangeReturnValue.isValid() ? itemChangeReturnValue : value; } @@ -9691,5 +9694,76 @@ void tst_QGraphicsItem::itemIsInFront() QCOMPARE(qt_closestItemFirst(rect1child1_2, rect2child1), false); } +class ScenePosChangeTester : public ItemChangeTester +{ +public: + ScenePosChangeTester() + { } + ScenePosChangeTester(QGraphicsItem *parent) : ItemChangeTester(parent) + { } +}; + +void tst_QGraphicsItem::scenePosChange() +{ + ScenePosChangeTester* root = new ScenePosChangeTester; + ScenePosChangeTester* child1 = new ScenePosChangeTester(root); + ScenePosChangeTester* grandChild1 = new ScenePosChangeTester(child1); + ScenePosChangeTester* child2 = new ScenePosChangeTester(root); + ScenePosChangeTester* grandChild2 = new ScenePosChangeTester(child2); + + child1->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true); + grandChild2->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true); + + QGraphicsScene scene; + scene.addItem(root); + + // ignore uninteresting changes + child1->clear(); + child2->clear(); + grandChild1->clear(); + grandChild2->clear(); + + // move whole tree + root->moveBy(1.0, 1.0); + QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1); + QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(grandChild2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1); + + // move subtree + child2->moveBy(1.0, 1.0); + QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1); + QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(grandChild2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 2); + + // reparent + grandChild2->setParentItem(child1); + child1->moveBy(1.0, 1.0); + QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 2); + QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(grandChild2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 3); + + // change flags + grandChild1->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true); + grandChild2->setFlag(QGraphicsItem::ItemSendsScenePositionChanges, false); + QCoreApplication::processEvents(); // QGraphicsScenePrivate::_q_updateScenePosDescendants() + child1->moveBy(1.0, 1.0); + QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 3); + QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1); + QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); + QCOMPARE(grandChild2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 3); + + // remove + scene.removeItem(grandChild1); + delete grandChild2; grandChild2 = 0; + QCoreApplication::processEvents(); // QGraphicsScenePrivate::_q_updateScenePosDescendants() + root->moveBy(1.0, 1.0); + QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 4); + QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1); + QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0); +} + QTEST_MAIN(tst_QGraphicsItem) #include "tst_qgraphicsitem.moc" -- cgit v0.12