summaryrefslogtreecommitdiffstats
path: root/src/widgets/graphicsview/qgraphicswidget_p.cpp
diff options
context:
space:
mode:
authorAndreas Aardal Hanssen <andrhans@cisco.com>2012-11-23 21:52:41 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-12-07 09:11:59 +0100
commit4a9c3b2433d4552bdabb832f71204aca95e68897 (patch)
tree4531a83a1c5ca5e5c70d95a7ece64f269f0834ce /src/widgets/graphicsview/qgraphicswidget_p.cpp
parent9c02a285529945bf1c3b8fb6d6faf7b382724f72 (diff)
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 <ogoffart@woboq.com>
Diffstat (limited to 'src/widgets/graphicsview/qgraphicswidget_p.cpp')
-rw-r--r--src/widgets/graphicsview/qgraphicswidget_p.cpp104
1 files changed, 45 insertions, 59 deletions
diff --git a/src/widgets/graphicsview/qgraphicswidget_p.cpp b/src/widgets/graphicsview/qgraphicswidget_p.cpp
index 4ec215a720..dfebbb5036 100644
--- a/src/widgets/graphicsview/qgraphicswidget_p.cpp
+++ b/src/widgets/graphicsview/qgraphicswidget_p.cpp
@@ -764,73 +764,59 @@ bool QGraphicsWidgetPrivate::hasDecoration() const
void QGraphicsWidgetPrivate::fixFocusChainBeforeReparenting(QGraphicsWidget *newParent, QGraphicsScene *oldScene, QGraphicsScene *newScene)
{
Q_Q(QGraphicsWidget);
-
Q_ASSERT(focusNext && focusPrev);
- QGraphicsWidget *n = q; //last one in 'new' list
- QGraphicsWidget *o = 0; //last one in 'old' list
-
- QGraphicsWidget *w = focusNext;
-
- QGraphicsWidget *firstOld = 0;
- bool wasPreviousNew = true;
-
- while (w != q) {
- bool isCurrentNew = q->isAncestorOf(w);
- if (isCurrentNew) {
- if (!wasPreviousNew) {
- n->d_func()->focusNext = w;
- w->d_func()->focusPrev = n;
- }
- n = w;
- } else /*if (!isCurrentNew)*/ {
- if (wasPreviousNew) {
- if (o) {
- o->d_func()->focusNext = w;
- w->d_func()->focusPrev = o;
- } else {
- firstOld = w;
- }
- }
- o = w;
- }
- w = w->d_func()->focusNext;
- wasPreviousNew = isCurrentNew;
- }
-
- // repair the 'old' chain
- if (firstOld) {
- o->d_func()->focusNext = firstOld;
- firstOld->d_func()->focusPrev = o;
+ if (q_ptr->isPanel()) {
+ // panels are never a part of their parent's or ancestors' focus
+ // chains. so reparenting a panel is easy; there's nothing to
+ // do.
+ return;
}
- // update tabFocusFirst for oldScene if the item is going to be removed from oldScene
- if (newParent)
- newScene = newParent->scene();
-
- if (oldScene && newScene != oldScene)
- oldScene->d_func()->tabFocusFirst = (firstOld && firstOld->scene() == oldScene) ? firstOld : 0;
-
- QGraphicsItem *topLevelItem = newParent ? newParent->topLevelItem() : 0;
- QGraphicsWidget *topLevel = 0;
- if (topLevelItem && topLevelItem->isWidget())
- topLevel = static_cast<QGraphicsWidget *>(topLevelItem);
+ // we're not a panel, so find the first widget in the focus chain
+ // (this), and the last (this, or the last widget that is still
+ // a descendent of this). also find the widgets that currently /
+ // before reparenting point to this widgets' focus chain.
+ QGraphicsWidget *focusFirst = q;
+ QGraphicsWidget *focusBefore = focusPrev;
+ QGraphicsWidget *focusLast = focusFirst;
+ QGraphicsWidget *focusAfter = focusNext;
+ do {
+ if (!q->isAncestorOf(focusAfter))
+ break;
+ focusLast = focusAfter;
+ } while ((focusAfter = focusAfter->d_func()->focusNext));
- if (topLevel && newParent) {
- QGraphicsWidget *last = topLevel->d_func()->focusPrev;
- // link last with new chain
- last->d_func()->focusNext = q;
- focusPrev = last;
+ if (!parent && oldScene && oldScene != newScene && oldScene->d_func()->tabFocusFirst == q) {
+ // detach from old scene's top level focus chain.
+ oldScene->d_func()->tabFocusFirst = (focusAfter != q) ? focusAfter : 0;
+ }
- // link last in chain with
- topLevel->d_func()->focusPrev = n;
- n->d_func()->focusNext = topLevel;
+ // detach from current focus chain; skip this widget subtree.
+ focusBefore->d_func()->focusNext = focusAfter;
+ focusAfter->d_func()->focusPrev = focusBefore;
+
+ if (newParent) {
+ // attach to new parent's focus chain as the last element
+ // in its chain.
+ QGraphicsWidget *newFocusFirst = newParent;
+ QGraphicsWidget *newFocusLast = newFocusFirst;
+ QGraphicsWidget *newFocusAfter = newFocusFirst->d_func()->focusNext;
+ do {
+ if (!newParent->isAncestorOf(newFocusAfter))
+ break;
+ newFocusLast = newFocusAfter;
+ } while ((newFocusAfter = newFocusAfter->d_func()->focusNext));
+
+ newFocusLast->d_func()->focusNext = q;
+ focusLast->d_func()->focusNext = newFocusAfter;
+ newFocusAfter->d_func()->focusPrev = focusLast;
+ focusPrev = newFocusLast;
} else {
- // q is the start of the focus chain
- n->d_func()->focusNext = q;
- focusPrev = n;
+ // no new parent, so just link up our own prev->last widgets.
+ focusPrev = focusLast;
+ focusLast->d_func()->focusNext = q;
}
-
}
void QGraphicsWidgetPrivate::setLayout_helper(QGraphicsLayout *l)