summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brasser <michael.brasser@nokia.com>2010-10-11 00:26:34 (GMT)
committerMichael Brasser <michael.brasser@nokia.com>2010-10-11 01:29:23 (GMT)
commite11ee40cefc981fbdcfb10816039d4efb080fb17 (patch)
treed20d1e42784154aa1bc370dbc69940cb5c17ddc7
parentaa7fa8608939676ba56e130214b85f5d0c3745df (diff)
downloadQt-e11ee40cefc981fbdcfb10816039d4efb080fb17.zip
Qt-e11ee40cefc981fbdcfb10816039d4efb080fb17.tar.gz
Qt-e11ee40cefc981fbdcfb10816039d4efb080fb17.tar.bz2
Don't give focus to a FocusScope that has had focus explicitly cleared.
If focus was explicitly cleared on a non-visible FocusScope, and then it was made visible, it would incorrectly grab focus. Task-number: QTBUG-13380
-rw-r--r--src/gui/graphicsview/qgraphicsitem.cpp16
-rw-r--r--src/gui/graphicsview/qgraphicsitem_p.h2
-rw-r--r--tests/auto/declarative/qdeclarativefocusscope/data/qtBug13380.qml24
-rw-r--r--tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp26
-rw-r--r--tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp3
5 files changed, 64 insertions, 7 deletions
diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp
index 60cd020..e63acac 100644
--- a/src/gui/graphicsview/qgraphicsitem.cpp
+++ b/src/gui/graphicsview/qgraphicsitem.cpp
@@ -2365,7 +2365,7 @@ void QGraphicsItemPrivate::setVisibleHelper(bool newVisible, bool explicitly, bo
while (fsi->d_ptr->focusScopeItem && fsi->d_ptr->focusScopeItem->isVisible())
fsi = fsi->d_ptr->focusScopeItem;
fsi->d_ptr->setFocusHelper(Qt::OtherFocusReason, /* climb = */ true,
- /* focusFromShow = */ true);
+ /* focusFromHide = */ false);
}
break;
}
@@ -2375,6 +2375,10 @@ void QGraphicsItemPrivate::setVisibleHelper(bool newVisible, bool explicitly, bo
QGraphicsItem *fi = subFocusItem;
if (fi && fi != scene->focusItem()) {
scene->setFocusItem(fi);
+ } else if (flags & QGraphicsItem::ItemIsFocusScope &&
+ !scene->focusItem() &&
+ q->isAncestorOf(scene->d_func()->lastFocusItem)) {
+ q_ptr->setFocus();
}
}
} else {
@@ -2385,7 +2389,7 @@ void QGraphicsItemPrivate::setVisibleHelper(bool newVisible, bool explicitly, bo
if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
if (p->d_ptr->visible) {
p->d_ptr->setFocusHelper(Qt::OtherFocusReason, /* climb = */ true,
- /* focusFromShow = */ true);
+ /* focusFromHide = */ true);
}
break;
}
@@ -3245,13 +3249,13 @@ bool QGraphicsItem::hasFocus() const
*/
void QGraphicsItem::setFocus(Qt::FocusReason focusReason)
{
- d_ptr->setFocusHelper(focusReason, /* climb = */ true, /* focusFromShow = */ false);
+ d_ptr->setFocusHelper(focusReason, /* climb = */ true, /* focusFromHide = */ false);
}
/*!
\internal
*/
-void QGraphicsItemPrivate::setFocusHelper(Qt::FocusReason focusReason, bool climb, bool focusFromShow)
+void QGraphicsItemPrivate::setFocusHelper(Qt::FocusReason focusReason, bool climb, bool focusFromHide)
{
// Disabled / unfocusable items cannot accept focus.
if (!q_ptr->isEnabled() || !(flags & QGraphicsItem::ItemIsFocusable))
@@ -3272,7 +3276,7 @@ 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 (!p->focusItem() && !focusFromShow) {
+ if (!p->focusItem() && !focusFromHide) {
if (oldFocusScopeItem)
oldFocusScopeItem->d_ptr->focusScopeItemChange(false);
focusScopeItemChange(true);
@@ -3334,7 +3338,7 @@ void QGraphicsItemPrivate::clearFocusHelper(bool giveFocusToParent)
while (p) {
if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
p->d_ptr->setFocusHelper(Qt::OtherFocusReason, /* climb = */ false,
- /* focusFromShow = */ false);
+ /* focusFromHide = */ false);
return;
}
p = p->d_ptr->parent;
diff --git a/src/gui/graphicsview/qgraphicsitem_p.h b/src/gui/graphicsview/qgraphicsitem_p.h
index c8a7699..8480c19 100644
--- a/src/gui/graphicsview/qgraphicsitem_p.h
+++ b/src/gui/graphicsview/qgraphicsitem_p.h
@@ -477,7 +477,7 @@ public:
inline void markParentDirty(bool updateBoundingRect = false);
- void setFocusHelper(Qt::FocusReason focusReason, bool climb, bool focusFromShow);
+ void setFocusHelper(Qt::FocusReason focusReason, bool climb, bool focusFromHide);
void clearFocusHelper(bool giveFocusToParent);
void setSubFocus(QGraphicsItem *rootItem = 0);
void clearSubFocus(QGraphicsItem *rootItem = 0);
diff --git a/tests/auto/declarative/qdeclarativefocusscope/data/qtBug13380.qml b/tests/auto/declarative/qdeclarativefocusscope/data/qtBug13380.qml
new file mode 100644
index 0000000..1784202
--- /dev/null
+++ b/tests/auto/declarative/qdeclarativefocusscope/data/qtBug13380.qml
@@ -0,0 +1,24 @@
+import QtQuick 1.0
+
+Rectangle {
+ width: 400; height: 400
+
+ property bool showRect: false
+ onShowRectChanged: if (showRect) rect.visible = true
+ property bool noFocus: !fs2.activeFocus
+
+ FocusScope {
+ id: fs1
+ focus: true
+ }
+ Rectangle {
+ id: rect
+ visible: false
+ FocusScope {
+ id: fs2
+ Rectangle {
+ focus: true
+ }
+ }
+ }
+}
diff --git a/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp b/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp
index ec8f048..4cafbd9 100644
--- a/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp
+++ b/tests/auto/declarative/qdeclarativefocusscope/tst_qdeclarativefocusscope.cpp
@@ -70,6 +70,7 @@ private slots:
void forceFocus();
void noParentFocus();
void signalEmission();
+ void qtBug13380();
};
/*
@@ -400,6 +401,31 @@ void tst_qdeclarativefocusscope::signalEmission()
delete view;
}
+void tst_qdeclarativefocusscope::qtBug13380()
+{
+ QDeclarativeView *view = new QDeclarativeView;
+ view->setSource(QUrl::fromLocalFile(SRCDIR "/data/qtBug13380.qml"));
+
+ view->show();
+ QVERIFY(view->rootObject());
+ qApp->setActiveWindow(view);
+ qApp->processEvents();
+
+#ifdef Q_WS_X11
+ // to be safe and avoid failing setFocus with window managers
+ qt_x11_wait_for_window_manager(view);
+#endif
+
+ QVERIFY(view->hasFocus());
+ QVERIFY(view->scene()->hasFocus());
+ QVERIFY(view->rootObject()->property("noFocus").toBool());
+
+ view->rootObject()->setProperty("showRect", true);
+ QVERIFY(view->rootObject()->property("noFocus").toBool());
+
+ delete view;
+}
+
QTEST_MAIN(tst_qdeclarativefocusscope)
#include "tst_qdeclarativefocusscope.moc"
diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
index 2901dd5..2ddccd2 100644
--- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -9065,6 +9065,9 @@ void tst_QGraphicsItem::focusScope()
scope2->hide();
scope2->show();
QVERIFY(!scope2->hasFocus());
+ QVERIFY(scope1->hasFocus());
+ scope2->setFocus();
+ scope3->setFocus();
QVERIFY(scope3->hasFocus());
QGraphicsRectItem *rect4 = new QGraphicsRectItem;