From f3f65928525465e20f40e89d0855a1f32de6d8a4 Mon Sep 17 00:00:00 2001 From: Andreas Aardal Hanssen Date: Thu, 29 Sep 2011 14:54:15 +0200 Subject: Fix bug in QGraphicsItem::isVisibleTo(). Task-number: QTBUG-21612 Reviewed-by: James Perrett Reviewed-by: Magne Pettersen Zachrisen Merge-request: 1396 Reviewed-by: Jan-Arve Saether --- src/gui/graphicsview/qgraphicsitem.cpp | 20 ++--- tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp | 108 +++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 9 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 9092593..f7ae045 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -2237,7 +2237,8 @@ bool QGraphicsItem::isVisible() const returned. \a parent can be 0, in which case this function will return whether the item is visible to the scene or not. - An item may not be visible to its ancestors even if isVisible() is true. If + An item may not be visible to its ancestors even if isVisible() is true. It + may also be visible to its ancestors even if isVisible() is false. If any ancestor is hidden, the item itself will be implicitly hidden, in which case this function will return false. @@ -2245,15 +2246,16 @@ bool QGraphicsItem::isVisible() const */ bool QGraphicsItem::isVisibleTo(const QGraphicsItem *parent) const { - if (!d_ptr->visible) + const QGraphicsItem *p = this; + if (d_ptr->explicitlyHidden) return false; - if (parent == this) - return true; - if (parentItem() && parentItem()->isVisibleTo(parent)) - return true; - if (!parent && !parentItem()) - return true; - return false; + do { + if (p == parent) + return true; + if (p->d_ptr->explicitlyHidden) + return false; + } while ((p = p->d_ptr->parent)); + return parent == 0; } /*! diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index 9b834d5..b6a402e 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -306,6 +306,7 @@ private slots: void inputMethodHints(); void toolTip(); void visible(); + void isVisibleTo(); void explicitlyVisible(); void enabled(); void explicitlyEnabled(); @@ -1138,6 +1139,113 @@ void tst_QGraphicsItem::visible() QVERIFY(!item->hasFocus()); } +void tst_QGraphicsItem::isVisibleTo() +{ + QGraphicsScene scene; + QGraphicsItem *parent = scene.addRect(QRectF(0, 0, 100, 100)); + QGraphicsItem *child = scene.addRect(QRectF(25, 25, 50, 50)); + QGraphicsItem *grandChild = scene.addRect(QRectF(50, 50, 50, 50)); + QGraphicsItem *stranger = scene.addRect(100, 100, 100, 100); + + child->setParentItem(parent); + grandChild->setParentItem(child); + + QVERIFY(grandChild->isVisible()); + QVERIFY(grandChild->isVisibleTo(grandChild)); + QVERIFY(grandChild->isVisibleTo(child)); + QVERIFY(grandChild->isVisibleTo(parent)); + QVERIFY(grandChild->isVisibleTo(0)); + QVERIFY(child->isVisible()); + QVERIFY(child->isVisibleTo(child)); + QVERIFY(child->isVisibleTo(parent)); + QVERIFY(child->isVisibleTo(0)); + QVERIFY(parent->isVisible()); + QVERIFY(parent->isVisibleTo(parent)); + QVERIFY(parent->isVisibleTo(0)); + QVERIFY(!parent->isVisibleTo(child)); + QVERIFY(!child->isVisibleTo(grandChild)); + QVERIFY(!grandChild->isVisibleTo(stranger)); + QVERIFY(!child->isVisibleTo(stranger)); + QVERIFY(!parent->isVisibleTo(stranger)); + QVERIFY(!stranger->isVisibleTo(grandChild)); + QVERIFY(!stranger->isVisibleTo(child)); + QVERIFY(!stranger->isVisibleTo(parent)); + + // Case 1: only parent is explicitly hidden + parent->hide(); + + QVERIFY(!grandChild->isVisible()); + QVERIFY(grandChild->isVisibleTo(grandChild)); + QVERIFY(grandChild->isVisibleTo(child)); + QVERIFY(grandChild->isVisibleTo(parent)); + QVERIFY(!grandChild->isVisibleTo(0)); + QVERIFY(!child->isVisible()); + QVERIFY(child->isVisibleTo(child)); + QVERIFY(child->isVisibleTo(parent)); + QVERIFY(!child->isVisibleTo(0)); + QVERIFY(!parent->isVisible()); + QVERIFY(!parent->isVisibleTo(parent)); + QVERIFY(!parent->isVisibleTo(0)); + QVERIFY(!parent->isVisibleTo(child)); + QVERIFY(!child->isVisibleTo(grandChild)); + QVERIFY(!grandChild->isVisibleTo(stranger)); + QVERIFY(!child->isVisibleTo(stranger)); + QVERIFY(!parent->isVisibleTo(stranger)); + QVERIFY(!stranger->isVisibleTo(grandChild)); + QVERIFY(!stranger->isVisibleTo(child)); + QVERIFY(!stranger->isVisibleTo(parent)); + + // Case 2: only child is hidden + parent->show(); + child->hide(); + + QVERIFY(!grandChild->isVisible()); + QVERIFY(grandChild->isVisibleTo(grandChild)); + QVERIFY(grandChild->isVisibleTo(child)); + QVERIFY(!grandChild->isVisibleTo(parent)); + QVERIFY(!grandChild->isVisibleTo(0)); + QVERIFY(!child->isVisible()); + QVERIFY(!child->isVisibleTo(child)); + QVERIFY(!child->isVisibleTo(parent)); + QVERIFY(!child->isVisibleTo(0)); + QVERIFY(parent->isVisible()); + QVERIFY(parent->isVisibleTo(parent)); + QVERIFY(parent->isVisibleTo(0)); + QVERIFY(!parent->isVisibleTo(child)); + QVERIFY(!child->isVisibleTo(grandChild)); + QVERIFY(!grandChild->isVisibleTo(stranger)); + QVERIFY(!child->isVisibleTo(stranger)); + QVERIFY(!parent->isVisibleTo(stranger)); + QVERIFY(!stranger->isVisibleTo(grandChild)); + QVERIFY(!stranger->isVisibleTo(child)); + QVERIFY(!stranger->isVisibleTo(parent)); + + // Case 3: only grand child is hidden + child->show(); + grandChild->hide(); + + QVERIFY(!grandChild->isVisible()); + QVERIFY(!grandChild->isVisibleTo(grandChild)); + QVERIFY(!grandChild->isVisibleTo(child)); + QVERIFY(!grandChild->isVisibleTo(parent)); + QVERIFY(!grandChild->isVisibleTo(0)); + QVERIFY(child->isVisible()); + QVERIFY(child->isVisibleTo(child)); + QVERIFY(child->isVisibleTo(parent)); + QVERIFY(child->isVisibleTo(0)); + QVERIFY(parent->isVisible()); + QVERIFY(parent->isVisibleTo(parent)); + QVERIFY(parent->isVisibleTo(0)); + QVERIFY(!parent->isVisibleTo(child)); + QVERIFY(!child->isVisibleTo(grandChild)); + QVERIFY(!grandChild->isVisibleTo(stranger)); + QVERIFY(!child->isVisibleTo(stranger)); + QVERIFY(!parent->isVisibleTo(stranger)); + QVERIFY(!stranger->isVisibleTo(grandChild)); + QVERIFY(!stranger->isVisibleTo(child)); + QVERIFY(!stranger->isVisibleTo(parent)); +} + void tst_QGraphicsItem::explicitlyVisible() { QGraphicsScene scene; -- cgit v0.12