summaryrefslogtreecommitdiffstats
path: root/src/widgets/graphicsview/qgraphicsscene.cpp
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andreas@hanssen.name>2012-11-24 21:50:05 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-12-17 08:58:04 +0100
commitd6f2d8dcb6dbee89fc55bd439ab4a837f4f9fb4c (patch)
treec30589451fb181249b1f8ea4731b3902feb0c9c8 /src/widgets/graphicsview/qgraphicsscene.cpp
parent4cf112b641f7ea9fb1ba87cefcef7ee98b6b0831 (diff)
Make sure panels always gain focus when activated or tab is pressed.
This changes behavior, but I would argue it's a good change. If you create a panel and activate it (e.g., by simply showing it or reparenting it onto an already-visible item), you expect the panel to gain focus / which is sort of the whole point of activating it. Prior to this change, you had to have explicitly called setFocus() on one of the panel's children, which would give that item subfocus, for that item to auto- gain focus when the panel was activated. This change makes it more automatic. If the panel itself or any of the widgets in its focus chain can gain focus, they will gain focus when the panel is activated. So the new logic is - if the panel already has a focus item, so if someone explicitly set subfocus on an item before the panel is shown, that item gets focus. Otherwise, if the panel itself can gain focus (e.g., someone makes a line edit / text edit panel), it gains focus when activated. Otherwise, we search the focus chain until we find the first item that can gain focus. This last case is the file dialog case, where the dialog itself can't gain focus but typically the first item in the focus chain is the primary focus widget, such as the search field or the directory list view. The change also fixes this for the first Tab. If you clear focus on a panel, the user expects to be able to press Tab to regain focus. Prior to this change that didn't happen. Now, the panel or the first in the focus chain that can get focus gets focus on first tab. Task-number: QTBUG-28194 Change-Id: Id7ec1741d0d5eb4ea845469909c8d684e14017f1 Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
Diffstat (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp')
-rw-r--r--src/widgets/graphicsview/qgraphicsscene.cpp33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp
index 769b488f39..139fad0163 100644
--- a/src/widgets/graphicsview/qgraphicsscene.cpp
+++ b/src/widgets/graphicsview/qgraphicsscene.cpp
@@ -771,9 +771,23 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin
QEvent event(QEvent::WindowActivate);
q->sendEvent(panel, &event);
- // Set focus on the panel's focus item.
- if (QGraphicsItem *focusItem = panel->focusItem())
+ // Set focus on the panel's focus item, or itself if it's
+ // focusable, or on the first focusable item in the panel's
+ // focus chain as a last resort.
+ if (QGraphicsItem *focusItem = panel->focusItem()) {
focusItem->setFocus(Qt::ActiveWindowFocusReason);
+ } else if (panel->flags() & QGraphicsItem::ItemIsFocusable) {
+ panel->setFocus(Qt::ActiveWindowFocusReason);
+ } else if (panel->isWidget()) {
+ QGraphicsWidget *fw = static_cast<QGraphicsWidget *>(panel)->d_func()->focusNext;
+ do {
+ if (fw->focusPolicy() & Qt::TabFocus) {
+ fw->setFocus(Qt::ActiveWindowFocusReason);
+ break;
+ }
+ fw = fw->d_func()->focusNext;
+ } while (fw != panel);
+ }
} else if (q->isActive()) {
// Activate the scene
QEvent event(QEvent::WindowActivate);
@@ -5328,6 +5342,21 @@ bool QGraphicsScene::focusNextPrevChild(bool next)
setFocusItem(d->lastFocusItem, next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
return true;
}
+ if (d->activePanel) {
+ if (d->activePanel->flags() & QGraphicsItem::ItemIsFocusable) {
+ setFocusItem(d->activePanel, next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
+ return true;
+ }
+ if (d->activePanel->isWidget()) {
+ QGraphicsWidget *fw = static_cast<QGraphicsWidget *>(d->activePanel)->d_func()->focusNext;
+ do {
+ if (fw->focusPolicy() & Qt::TabFocus) {
+ setFocusItem(fw, next ? Qt::TabFocusReason : Qt::BacktabFocusReason);
+ return true;
+ }
+ } while (fw != d->activePanel);
+ }
+ }
}
if (!item && !d->tabFocusFirst) {
// No widgets...