summaryrefslogtreecommitdiffstats
path: root/src/gui/graphicsview/qgraphicsscene.cpp
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com>2009-07-24 01:40:51 (GMT)
committerAndreas Aardal Hanssen <andreas.aardal.hanssen@nokia.com>2009-07-27 05:46:39 (GMT)
commitf68fed388dcdba6ab6dad3af4933bcd3aa123cf8 (patch)
treeb0e386b94a444db6bbf9debd93c9fd9b89473114 /src/gui/graphicsview/qgraphicsscene.cpp
parent092b004126f82545b4237e43507f21920d06ac58 (diff)
downloadQt-f68fed388dcdba6ab6dad3af4933bcd3aa123cf8.zip
Qt-f68fed388dcdba6ab6dad3af4933bcd3aa123cf8.tar.gz
Qt-f68fed388dcdba6ab6dad3af4933bcd3aa123cf8.tar.bz2
Add QGraphicsItem::ItemAutoDetectsFocusProxy and improve subfocus support.
If you set this flag on an item, and descendant item that gains input focus will become this item's focus proxy. This simplifies how focus proxy items are assigned from QML; instead of binding the possible focusProxy property to a named child widget, this assignment happens automatically as you set the focus property of a descendant to true. As part of this change, QGraphicsWidget::focusWidget behavior has been improved and moved into QGraphicsItem. For example, if you set focus on an item that it's part of a scene, it can gain focus once the parent has been assigned (which is how object trees are built in QML). Autotests are included. Reviewed-by: Michael Brasser
Diffstat (limited to 'src/gui/graphicsview/qgraphicsscene.cpp')
-rw-r--r--src/gui/graphicsview/qgraphicsscene.cpp29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp
index 0dce26b..f223cbe 100644
--- a/src/gui/graphicsview/qgraphicsscene.cpp
+++ b/src/gui/graphicsview/qgraphicsscene.cpp
@@ -484,6 +484,8 @@ void QGraphicsScenePrivate::removeItemHelper(QGraphicsItem *item)
index->removeItem(item);
}
+ item->d_ptr->clearSubFocus();
+
if (!item->d_ptr->inDestructor && item == tabFocusFirst) {
QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item);
widget->d_func()->fixFocusChainBeforeReparenting(0, 0);
@@ -572,17 +574,34 @@ void QGraphicsScenePrivate::setFocusItemHelper(QGraphicsItem *item,
Q_Q(QGraphicsScene);
if (item == focusItem)
return;
+
+ // Clear focus if asked to set focus on something that can't
+ // accept input focus.
if (item && (!(item->flags() & QGraphicsItem::ItemIsFocusable)
|| !item->isVisible() || !item->isEnabled())) {
item = 0;
}
+ // Set focus on the scene if an item requests focus.
if (item) {
q->setFocus(focusReason);
if (item == focusItem)
return;
}
+ // Auto-update focus proxy. The closest parent that detects
+ // focus proxies is updated as the proxy gains or loses focus.
+ if (item) {
+ QGraphicsItem *p = item->d_ptr->parent;
+ while (p) {
+ if (p->d_ptr->flags & QGraphicsItem::ItemAutoDetectsFocusProxy) {
+ p->setFocusProxy(item);
+ break;
+ }
+ p = p->d_ptr->parent;
+ }
+ }
+
if (focusItem) {
QFocusEvent event(QEvent::FocusOut, focusReason);
lastFocusItem = focusItem;
@@ -602,11 +621,6 @@ void QGraphicsScenePrivate::setFocusItemHelper(QGraphicsItem *item,
}
if (item) {
- if (item->isWidget()) {
- // Update focus child chain.
- static_cast<QGraphicsWidget *>(item)->d_func()->setFocusWidget();
- }
-
focusItem = item;
QFocusEvent event(QEvent::FocusIn, focusReason);
sendEvent(item, &event);
@@ -2342,6 +2356,11 @@ void QGraphicsScene::addItem(QGraphicsItem *item)
// Deliver post-change notification
item->itemChange(QGraphicsItem::ItemSceneHasChanged, newSceneVariant);
+ // Ensure that newly added items that have subfocus set, gain
+ // focus automatically if there isn't a focus item already.
+ if (!d->focusItem && item->focusItem())
+ item->focusItem()->setFocus();
+
d->updateInputMethodSensitivityInViews();
}