diff options
author | Andreas Aardal Hanssen <andreas@hanssen.name> | 2013-01-28 19:31:59 (GMT) |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-01-29 21:37:10 (GMT) |
commit | a1bc10b76fd4496cf148375eb7a1ca5145e06971 (patch) | |
tree | 4fb9bdd4bb9bfb565948ed9739ec28e88f21bd6e | |
parent | edd8be883f538ab2ca3bb768bd677e4581e364bf (diff) | |
download | Qt-a1bc10b76fd4496cf148375eb7a1ca5145e06971.zip Qt-a1bc10b76fd4496cf148375eb7a1ca5145e06971.tar.gz Qt-a1bc10b76fd4496cf148375eb7a1ca5145e06971.tar.bz2 |
Make sure QGraphicsItem notifies changes to focusScopeItem.
A glitch in QGraphicsItem's logic made it update the focusScopeItem
pointer, but fail to notify the change to QDeclarativeItem through the
d_ptr->focusScopeItemChange() virtual function, hindering
QDeclarativeItem from emitting focusChanged() correctly for focus
scopes that do not have focus.
Two lines were moved, and a comment updated to reflect the reason
why the "return" is needed at this point. It's clear that the
calls to focusScopeItemChange() are unrelated to the return.
Task-number: QTBUG-29260
Change-Id: I12ba9161b16d34c3689401a92c86d2047989f7bd
(cherry picked from qtbase/6476d6728eb3cde8e4a5fd0eb607b92977932296)
Reviewed-by: Andreas Aardal Hanssen <andreas@hanssen.name>
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
Reviewed-by: Alan Alpert <aalpert@rim.com>
-rw-r--r-- | src/gui/graphicsview/qgraphicsitem.cpp | 12 | ||||
-rw-r--r-- | tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp | 57 |
2 files changed, 64 insertions, 5 deletions
diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index d408167..63a67e0 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -3291,12 +3291,14 @@ void QGraphicsItemPrivate::setFocusHelper(Qt::FocusReason focusReason, bool clim if (p->flags() & QGraphicsItem::ItemIsFocusScope) { QGraphicsItem *oldFocusScopeItem = p->d_ptr->focusScopeItem; p->d_ptr->focusScopeItem = q_ptr; + if (oldFocusScopeItem) + oldFocusScopeItem->d_ptr->focusScopeItemChange(false); + focusScopeItemChange(true); if (!p->focusItem() && !focusFromHide) { - if (oldFocusScopeItem) - oldFocusScopeItem->d_ptr->focusScopeItemChange(false); - focusScopeItemChange(true); - // If you call setFocus on a child of a focus scope that - // doesn't currently have a focus item, then stop. + // Calling setFocus() on a child of a focus scope that does + // not have focus changes only the focus scope pointer, + // so that focus is restored the next time the scope gains + // focus. return; } break; diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp index 9667eaf..15ae53a 100644 --- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp @@ -436,6 +436,7 @@ private slots: void ensureDirtySceneTransform(); void focusScope(); void focusScope2(); + void focusScopeItemChangedWhileScopeDoesntHaveFocus(); void stackBefore(); void sceneModality(); void panelModality(); @@ -9364,6 +9365,62 @@ void tst_QGraphicsItem::focusScope2() QCOMPARE(siblingFocusScope->focusItem(), (QGraphicsItem *)siblingChild2); } +class FocusScopeItemPrivate; +class FocusScopeItem : public QGraphicsItem +{ + Q_DECLARE_PRIVATE(FocusScopeItem) +public: + FocusScopeItem(QGraphicsItem *parent = 0); + QRectF boundingRect() const { return QRectF(); } + void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) { } + + int focusScopeChanged; + FocusScopeItemPrivate *d_ptr; +}; + +class FocusScopeItemPrivate : QGraphicsItemPrivate +{ + Q_DECLARE_PUBLIC(FocusScopeItem) +public: + void focusScopeItemChange(bool) + { ++q_func()->focusScopeChanged; } +}; + +FocusScopeItem::FocusScopeItem(QGraphicsItem *parent) + : QGraphicsItem(*new FocusScopeItemPrivate, parent, 0), focusScopeChanged(0) +{ + setFlag(ItemIsFocusable); +} + +void tst_QGraphicsItem::focusScopeItemChangedWhileScopeDoesntHaveFocus() +{ + QGraphicsRectItem rect; + rect.setFlags(QGraphicsItem::ItemIsFocusScope | QGraphicsItem::ItemIsFocusable); + + FocusScopeItem *child1 = new FocusScopeItem(&rect); + FocusScopeItem *child2 = new FocusScopeItem(&rect); + + QCOMPARE(rect.focusScopeItem(), (QGraphicsItem *)0); + QCOMPARE(child1->focusScopeChanged, 0); + QCOMPARE(child2->focusScopeChanged, 0); + child1->setFocus(); + QCOMPARE(rect.focusScopeItem(), (QGraphicsItem *)child1); + QCOMPARE(child1->focusScopeChanged, 1); + QCOMPARE(child2->focusScopeChanged, 0); + child2->setFocus(); + QCOMPARE(rect.focusScopeItem(), (QGraphicsItem *)child2); + QCOMPARE(child1->focusScopeChanged, 2); + QCOMPARE(child2->focusScopeChanged, 1); + child1->setFocus(); + QCOMPARE(rect.focusScopeItem(), (QGraphicsItem *)child1); + QCOMPARE(child1->focusScopeChanged, 3); + QCOMPARE(child2->focusScopeChanged, 2); + child1->clearFocus(); + QCOMPARE(rect.focusScopeItem(), (QGraphicsItem *)0); + QCOMPARE(child1->focusScopeChanged, 4); + QCOMPARE(child2->focusScopeChanged, 2); +} + void tst_QGraphicsItem::stackBefore() { QGraphicsRectItem parent; |