diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativeitem.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativeitem.cpp | 95 |
1 files changed, 79 insertions, 16 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index a229d4c..23735f2 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -1430,7 +1430,7 @@ QDeclarativeKeysAttached *QDeclarativeKeysAttached::qmlAttachedProperties(QObjec */ /*! - \fn void QDeclarativeItem::wantsFocusChanged(bool) + \fn void QDeclarativeItem::activeFocusChanged(bool) \internal */ @@ -2356,12 +2356,12 @@ QScriptValue QDeclarativeItem::mapToItem(const QScriptValue &item, qreal x, qrea } /*! - \qmlmethod Item::forceFocus() + \qmlmethod Item::forceActiveFocus() - Force the focus on the item. - This method sets the focus on the item and makes sure that all the focus scopes higher in the object hierarchy are given focus. + Force active focus on the item. + This method sets focus on the item and makes sure that all the focus scopes higher in the object hierarchy are also given focus. */ -void QDeclarativeItem::forceFocus() +void QDeclarativeItem::forceActiveFocus() { setFocus(true); QGraphicsItem *parent = parentItem(); @@ -2398,8 +2398,19 @@ void QDeclarativeItemPrivate::focusChanged(bool flag) { Q_Q(QDeclarativeItem); if (!(flags & QGraphicsItem::ItemIsFocusScope) && parent) - emit q->wantsFocusChanged(flag); //see also QDeclarativeItemPrivate::subFocusItemChange() - emit q->focusChanged(flag); + emit q->activeFocusChanged(flag); //see also QDeclarativeItemPrivate::subFocusItemChange() + + bool inScope = false; + QGraphicsItem *p = parent; + while (p) { + if (p->flags() & QGraphicsItem::ItemIsFocusScope) { + inScope = true; + break; + } + p = p->parentItem(); + } + if (!inScope) + emit q->focusChanged(flag); } /*! \internal */ @@ -2676,7 +2687,7 @@ bool QDeclarativeItem::sceneEvent(QEvent *event) if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) { - d->focusChanged(hasFocus()); + d->focusChanged(hasActiveFocus()); } return rv; } @@ -3090,15 +3101,32 @@ void QDeclarativeItem::setSize(const QSizeF &size) } /*! - \qmlproperty bool Item::wantsFocus + \qmlproperty bool Item::activeFocus + + This property indicates whether the item has active focus. + + An item with active focus will receive keyboard input, + or is a FocusScope ancestor of the item that will receive keyboard input. - This property indicates whether the item has has an active focus request. + Usually, activeFocus is gained by setting focus on an item and its enclosing + FocusScopes. In the following example \c input will have activeFocus. + \qml + Rectangle { + FocusScope { + focus: true + TextInput { + id: input + focus: true + } + } + } + \endqml - \sa {qmlfocus}{Keyboard Focus} + \sa focus, {qmlfocus}{Keyboard Focus} */ /*! \internal */ -bool QDeclarativeItem::wantsFocus() const +bool QDeclarativeItem::hasActiveFocus() const { Q_D(const QDeclarativeItem); return focusItem() == this || @@ -3108,16 +3136,51 @@ bool QDeclarativeItem::wantsFocus() const /*! \qmlproperty bool Item::focus - This property indicates whether the item has keyboard input focus. Set this - property to true to request focus. + This property indicates whether the item has focus within the enclosing focus scope. If true, this item + will gain active focus when the enclosing focus scope gains active focus. + In the following example, \c input will be given active focus when \c scope gains active focus. + \qml + Rectangle { + FocusScope { + id: scope + TextInput { + id: input + focus: true + } + } + } + \endqml - \sa {qmlfocus}{Keyboard Focus} + For the purposes of this property, the top level item in the scene + is assumed to act like a focus scope, and to always have active focus + when the scene has focus. On a practical level, that means the following + QML will give active focus to \c input on startup. + + \qml + Rectangle { + TextInput { + id: input + focus: true + } + } + \endqml + + \sa activeFocus, {qmlfocus}{Keyboard Focus} */ /*! \internal */ bool QDeclarativeItem::hasFocus() const { - return QGraphicsItem::hasFocus(); + Q_D(const QDeclarativeItem); + QGraphicsItem *p = d->parent; + while (p) { + if (p->flags() & QGraphicsItem::ItemIsFocusScope) { + return p->focusScopeItem() == this; + } + p = p->parentItem(); + } + + return hasActiveFocus() ? true : (!QGraphicsItem::parentItem() ? true : false); } /*! \internal */ |