From 4a9c3b2433d4552bdabb832f71204aca95e68897 Mon Sep 17 00:00:00 2001 From: Andreas Aardal Hanssen Date: Fri, 23 Nov 2012 21:52:41 +0100 Subject: Repair QGraphicsWidget focus chain when used with ItemIsPanel. Add handling of the focus chain to QGraphicsItem::setFlags(), so that the focus chain is repaired (panels pop out of the chain and non-panels merge back in) when the ItemIsPanel flag is toggled. Add handling focus chain to QGraphicsWidgetPrivate::fixFocusChainBeforeReparenting for panels. Before this fix, you must enable the ItemIsPanel flag before adding the item as a child to a parent panel, and you lose focus when using the tab key to focus around a panel after it has been reparented into another panel. Task-number: QTBUG-28187 Change-Id: I1d0d81a90697eaf715a8a337c8bf6c2159329e68 Reviewed-by: Olivier Goffart --- src/widgets/graphicsview/qgraphicsscene.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp') diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index ee65fb8fa6..769b488f39 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -607,7 +607,7 @@ void QGraphicsScenePrivate::removeItemHelper(QGraphicsItem *item) q->removeItem(item->d_ptr->children.at(i)); } - if (!item->d_ptr->inDestructor && item == tabFocusFirst) { + if (!item->d_ptr->inDestructor && !item->parentItem() && item->isWidget()) { QGraphicsWidget *widget = static_cast(item); widget->d_func()->fixFocusChainBeforeReparenting(0, oldScene, 0); } @@ -2528,14 +2528,13 @@ void QGraphicsScene::addItem(QGraphicsItem *item) // No first tab focus widget - make this the first tab focus // widget. d->tabFocusFirst = widget; - } else if (!widget->parentWidget()) { + } else if (!widget->parentWidget() && !widget->isPanel()) { // Adding a widget that is not part of a tab focus chain. - QGraphicsWidget *last = d->tabFocusFirst->d_func()->focusPrev; - QGraphicsWidget *lastNew = widget->d_func()->focusPrev; - last->d_func()->focusNext = widget; - widget->d_func()->focusPrev = last; - d->tabFocusFirst->d_func()->focusPrev = lastNew; - lastNew->d_func()->focusNext = d->tabFocusFirst; + QGraphicsWidget *myNewPrev = d->tabFocusFirst->d_func()->focusPrev; + myNewPrev->d_func()->focusNext = widget; + widget->d_func()->focusPrev->d_func()->focusNext = d->tabFocusFirst; + d->tabFocusFirst->d_func()->focusPrev = widget->d_func()->focusPrev; + widget->d_func()->focusPrev = myNewPrev; } } @@ -5330,7 +5329,7 @@ bool QGraphicsScene::focusNextPrevChild(bool next) return true; } } - if (!d->tabFocusFirst) { + if (!item && !d->tabFocusFirst) { // No widgets... return false; } @@ -5342,8 +5341,10 @@ bool QGraphicsScene::focusNextPrevChild(bool next) } else { QGraphicsWidget *test = static_cast(item); widget = next ? test->d_func()->focusNext : test->d_func()->focusPrev; - if ((next && widget == d->tabFocusFirst) || (!next && widget == d->tabFocusFirst->d_func()->focusPrev)) + if (!widget->panel() && ((next && widget == d->tabFocusFirst) || (!next && widget == d->tabFocusFirst->d_func()->focusPrev))) { + // Tab out of the scene. return false; + } } QGraphicsWidget *widgetThatHadFocus = widget; -- cgit v1.2.3 From d6f2d8dcb6dbee89fc55bd439ab4a837f4f9fb4c Mon Sep 17 00:00:00 2001 From: Andreas Aardal Hanssen Date: Sat, 24 Nov 2012 21:50:05 +0100 Subject: Make sure panels always gain focus when activated or tab is pressed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/widgets/graphicsview/qgraphicsscene.cpp | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp') 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(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(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... -- cgit v1.2.3 From af8a6cdd87c27b4ed18d5e6238757ba8885f630d Mon Sep 17 00:00:00 2001 From: Andreas Aardal Hanssen Date: Thu, 29 Nov 2012 22:33:43 +0100 Subject: Add new signal: QGraphicsScene::focusItemChanged(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This signal is emitted by QGraphicsScene whenever focus changes in the scene (i.e., when an item gains or loses input focus, or when focus passes from one item to another). You can connect to this signal if you need to keep track of when other items gain input focus. It is particularily useful for implementing virtual keyboards, input methods, and cursor items. Task-number: QTBUG-10570 Change-Id: I9cbbd9a2d15d6f568e1597c2c33ec049eb70f793 Reviewed-by: Jan Arve Sæther Reviewed-by: Yoann Lopes --- src/widgets/graphicsview/qgraphicsscene.cpp | 50 +++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp') diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index 139fad0163..e14a98f332 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -742,12 +742,14 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin if (panel == activePanel || (!q->isActive() && !duringActivationEvent)) return; + QGraphicsItem *oldFocusItem = focusItem; + // Deactivate the last active panel. if (activePanel) { if (QGraphicsItem *fi = activePanel->focusItem()) { // Remove focus from the current focus item. if (fi == q->focusItem()) - q->setFocusItem(0, Qt::ActiveWindowFocusReason); + setFocusItemHelper(0, Qt::ActiveWindowFocusReason, /* emitFocusChanged = */ false); } QEvent event(QEvent::WindowDeactivate); @@ -775,14 +777,14 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin // 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); + setFocusItemHelper(focusItem, Qt::ActiveWindowFocusReason, /* emitFocusChanged = */ false); } else if (panel->flags() & QGraphicsItem::ItemIsFocusable) { - panel->setFocus(Qt::ActiveWindowFocusReason); + setFocusItemHelper(panel, Qt::ActiveWindowFocusReason, /* emitFocusChanged = */ false); } else if (panel->isWidget()) { QGraphicsWidget *fw = static_cast(panel)->d_func()->focusNext; do { if (fw->focusPolicy() & Qt::TabFocus) { - fw->setFocus(Qt::ActiveWindowFocusReason); + setFocusItemHelper(fw, Qt::ActiveWindowFocusReason, /* emitFocusChanged = */ false); break; } fw = fw->d_func()->focusNext; @@ -796,13 +798,23 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin q->sendEvent(item, &event); } } + + emit q->focusItemChanged(focusItem, oldFocusItem, Qt::ActiveWindowFocusReason); } /*! \internal + + \a emitFocusChanged needs to be false when focus passes from one + item to another through setActivePanel(); i.e. when activation + passes from one panel to another, to avoid getting two focusChanged() + emissions; one focusChanged(0, lastFocus), then one + focusChanged(newFocus, 0). Instead setActivePanel() emits the signal + once itself: focusChanged(newFocus, oldFocus). */ void QGraphicsScenePrivate::setFocusItemHelper(QGraphicsItem *item, - Qt::FocusReason focusReason) + Qt::FocusReason focusReason, + bool emitFocusChanged) { Q_Q(QGraphicsScene); if (item == focusItem) @@ -818,10 +830,14 @@ void QGraphicsScenePrivate::setFocusItemHelper(QGraphicsItem *item, // Set focus on the scene if an item requests focus. if (item) { q->setFocus(focusReason); - if (item == focusItem) + if (item == focusItem) { + if (emitFocusChanged) + emit q->focusItemChanged(focusItem, (QGraphicsItem *)0, focusReason); return; + } } + QGraphicsItem *oldFocusItem = focusItem; if (focusItem) { lastFocusItem = focusItem; @@ -862,6 +878,9 @@ void QGraphicsScenePrivate::setFocusItemHelper(QGraphicsItem *item, QFocusEvent event(QEvent::FocusIn, focusReason); sendEvent(item, &event); } + + if (emitFocusChanged) + emit q->focusItemChanged(focusItem, oldFocusItem, focusReason); } /*! @@ -5436,6 +5455,25 @@ bool QGraphicsScene::focusNextPrevChild(bool next) \sa setSelectionArea(), selectedItems(), QGraphicsItem::setSelected() */ +/*! + \fn QGraphicsScene::focusChanged(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason) + + This signal is emitted by QGraphicsScene whenever focus changes in the + scene (i.e., when an item gains or loses input focus, or when focus + passes from one item to another). You can connect to this signal if you + need to keep track of when other items gain input focus. It is + particularily useful for implementing virtual keyboards, input methods, + and cursor items. + + \a oldFocusItem is a pointer to the item that previously had focus, or + 0 if no item had focus before the signal was emitted. \a newFocusItem + is a pointer to the item that gained input focus, or 0 if focus was lost. + \a reason is the reason for the focus change (e.g., if the scene was + deactivated while an input field had focus, \a oldFocusItem would point + to the input field item, \a newFocusItem would be 0, and \a reason would be + Qt::ActiveWindowFocusReason. +*/ + /*! \since 4.4 -- cgit v1.2.3 From ef9c3c753dd22e675b334e1b045f93401e1f9df3 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Sun, 17 Mar 2013 14:26:18 -0400 Subject: Fix license headers stating QtGui for QtWidgets files. Change-Id: I0ca49e3e1f9f603f0b0f7f3553e854b871efe303 Reviewed-by: Thiago Macieira --- src/widgets/graphicsview/qgraphicsscene.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp') diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index 78611a27ca..ba59b15a54 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -3,7 +3,7 @@ ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** -** This file is part of the QtGui module of the Qt Toolkit. +** This file is part of the QtWidgets module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage -- cgit v1.2.3