diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativeitem.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativeitem.cpp | 200 |
1 files changed, 173 insertions, 27 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index f251ba1..9547884 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -375,7 +375,7 @@ void QDeclarativeContents::childAdded(QDeclarativeItem *item) } QDeclarativeItemKeyFilter::QDeclarativeItemKeyFilter(QDeclarativeItem *item) -: m_next(0) +: m_processPost(false), m_next(0) { QDeclarativeItemPrivate *p = item?static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(item)):0; @@ -389,19 +389,19 @@ QDeclarativeItemKeyFilter::~QDeclarativeItemKeyFilter() { } -void QDeclarativeItemKeyFilter::keyPressed(QKeyEvent *event) +void QDeclarativeItemKeyFilter::keyPressed(QKeyEvent *event, bool post) { - if (m_next) m_next->keyPressed(event); + if (m_next) m_next->keyPressed(event, post); } -void QDeclarativeItemKeyFilter::keyReleased(QKeyEvent *event) +void QDeclarativeItemKeyFilter::keyReleased(QKeyEvent *event, bool post) { - if (m_next) m_next->keyReleased(event); + if (m_next) m_next->keyReleased(event, post); } -void QDeclarativeItemKeyFilter::inputMethodEvent(QInputMethodEvent *event) +void QDeclarativeItemKeyFilter::inputMethodEvent(QInputMethodEvent *event, bool post) { - if (m_next) m_next->inputMethodEvent(event); + if (m_next) m_next->inputMethodEvent(event, post); } QVariant QDeclarativeItemKeyFilter::inputMethodQuery(Qt::InputMethodQuery query) const @@ -463,9 +463,11 @@ void QDeclarativeItemKeyFilter::componentComplete() } \endcode - KeyNavigation receives key events after the item it is attached to. + By default KeyNavigation receives key events after the item it is attached to. If the item accepts an arrow key event, the KeyNavigation - attached property will not receive an event for that key. + attached property will not receive an event for that key. Setting the + \l priority property to KeyNavigation.BeforeItem allows handling + of the key events before normal item processing. If an item has been set for a direction and the KeyNavigation attached property receives the corresponding @@ -490,6 +492,7 @@ QDeclarativeKeyNavigationAttached::QDeclarativeKeyNavigationAttached(QObject *pa : QObject(*(new QDeclarativeKeyNavigationAttachedPrivate), parent), QDeclarativeItemKeyFilter(qobject_cast<QDeclarativeItem*>(parent)) { + m_processPost = true; } QDeclarativeKeyNavigationAttached * @@ -576,12 +579,45 @@ void QDeclarativeKeyNavigationAttached::setBacktab(QDeclarativeItem *i) emit changed(); } -void QDeclarativeKeyNavigationAttached::keyPressed(QKeyEvent *event) +/*! + \qmlproperty enumeration KeyNavigation::priority + + This property determines whether the keys are processed before + or after the attached item's own key handling. + + \list + \o KeyNavigation.BeforeItem - process the key events before normal + item key processing. If the event is accepted it will not + be passed on to the item. + \o KeyNavigation.AfterItem (default) - process the key events after normal item key + handling. If the item accepts the key event it will not be + handled by the KeyNavigation attached property handler. + \endlist +*/ +QDeclarativeKeyNavigationAttached::Priority QDeclarativeKeyNavigationAttached::priority() const { - Q_D(QDeclarativeKeyNavigationAttached); + return m_processPost ? AfterItem : BeforeItem; +} + +void QDeclarativeKeyNavigationAttached::setPriority(Priority order) +{ + bool processPost = order == AfterItem; + if (processPost != m_processPost) { + m_processPost = processPost; + emit priorityChanged(); + } +} +void QDeclarativeKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post) +{ + Q_D(QDeclarativeKeyNavigationAttached); event->ignore(); + if (post != m_processPost) { + QDeclarativeItemKeyFilter::keyPressed(event, post); + return; + } + switch(event->key()) { case Qt::Key_Left: if (d->left) { @@ -623,15 +659,19 @@ void QDeclarativeKeyNavigationAttached::keyPressed(QKeyEvent *event) break; } - if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyPressed(event); + if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyPressed(event, post); } -void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event) +void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post) { Q_D(QDeclarativeKeyNavigationAttached); - event->ignore(); + if (post != m_processPost) { + QDeclarativeItemKeyFilter::keyReleased(event, post); + return; + } + switch(event->key()) { case Qt::Key_Left: if (d->left) { @@ -667,7 +707,7 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event) break; } - if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event); + if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event, post); } /*! @@ -709,6 +749,28 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event) See \l {Qt::Key}{Qt.Key} for the list of keyboard codes. + If priority is Keys.BeforeItem (default) the order of key event processing is: + + \list 1 + \o Items specified in \c forwardTo + \o specific key handlers, e.g. onReturnPressed + \o onKeyPress, onKeyRelease handlers + \o Item specific key handling, e.g. TextInput key handling + \o parent item + \endlist + + If priority is Keys.AfterItem the order of key event processing is: + \list 1 + \o Item specific key handling, e.g. TextInput key handling + \o Items specified in \c forwardTo + \o specific key handlers, e.g. onReturnPressed + \o onKeyPress, onKeyRelease handlers + \o parent item + \endlist + + If the event is accepted during any of the above steps, key + propagation stops. + \sa KeyEvent, {KeyNavigation}{KeyNavigation attached property} */ @@ -720,6 +782,22 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event) */ /*! + \qmlproperty enumeration Keys::priority + + This property determines whether the keys are processed before + or after the attached item's own key handling. + + \list + \o Keys.BeforeItem (default) - process the key events before normal + item key processing. If the event is accepted it will not + be passed on to the item. + \o Keys.AfterItem - process the key events after normal item key + handling. If the item accepts the key event it will not be + handled by the Keys attached property handler. + \endlist +*/ + +/*! \qmlproperty list<Object> Keys::forwardTo This property provides a way to forward key presses, key releases, and keyboard input @@ -1039,6 +1117,7 @@ QDeclarativeKeysAttached::QDeclarativeKeysAttached(QObject *parent) QDeclarativeItemKeyFilter(qobject_cast<QDeclarativeItem*>(parent)) { Q_D(QDeclarativeKeysAttached); + m_processPost = false; d->item = qobject_cast<QDeclarativeItem*>(parent); } @@ -1046,6 +1125,20 @@ QDeclarativeKeysAttached::~QDeclarativeKeysAttached() { } +QDeclarativeKeysAttached::Priority QDeclarativeKeysAttached::priority() const +{ + return m_processPost ? AfterItem : BeforeItem; +} + +void QDeclarativeKeysAttached::setPriority(Priority order) +{ + bool processPost = order == AfterItem; + if (processPost != m_processPost) { + m_processPost = processPost; + emit priorityChanged(); + } +} + void QDeclarativeKeysAttached::componentComplete() { Q_D(QDeclarativeKeysAttached); @@ -1060,11 +1153,12 @@ void QDeclarativeKeysAttached::componentComplete() } } -void QDeclarativeKeysAttached::keyPressed(QKeyEvent *event) +void QDeclarativeKeysAttached::keyPressed(QKeyEvent *event, bool post) { Q_D(QDeclarativeKeysAttached); - if (!d->enabled || d->inPress) { + if (post != m_processPost || !d->enabled || d->inPress) { event->ignore(); + QDeclarativeItemKeyFilter::keyPressed(event, post); return; } @@ -1099,14 +1193,15 @@ void QDeclarativeKeysAttached::keyPressed(QKeyEvent *event) emit pressed(&ke); event->setAccepted(ke.isAccepted()); - if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyPressed(event); + if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyPressed(event, post); } -void QDeclarativeKeysAttached::keyReleased(QKeyEvent *event) +void QDeclarativeKeysAttached::keyReleased(QKeyEvent *event, bool post) { Q_D(QDeclarativeKeysAttached); - if (!d->enabled || d->inRelease) { + if (post != m_processPost || !d->enabled || d->inRelease) { event->ignore(); + QDeclarativeItemKeyFilter::keyReleased(event, post); return; } @@ -1129,13 +1224,13 @@ void QDeclarativeKeysAttached::keyReleased(QKeyEvent *event) emit released(&ke); event->setAccepted(ke.isAccepted()); - if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event); + if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event, post); } -void QDeclarativeKeysAttached::inputMethodEvent(QInputMethodEvent *event) +void QDeclarativeKeysAttached::inputMethodEvent(QInputMethodEvent *event, bool post) { Q_D(QDeclarativeKeysAttached); - if (d->item && !d->inIM && d->item->scene()) { + if (post == m_processPost && d->item && !d->inIM && d->item->scene()) { d->inIM = true; for (int ii = 0; ii < d->targets.count(); ++ii) { QGraphicsItem *i = d->finalFocusProxy(d->targets.at(ii)); @@ -1150,7 +1245,7 @@ void QDeclarativeKeysAttached::inputMethodEvent(QInputMethodEvent *event) } d->inIM = false; } - if (!event->isAccepted()) QDeclarativeItemKeyFilter::inputMethodEvent(event); + if (!event->isAccepted()) QDeclarativeItemKeyFilter::inputMethodEvent(event, post); } class QDeclarativeItemAccessor : public QGraphicsItem @@ -1822,8 +1917,11 @@ void QDeclarativeItemPrivate::removeItemChangeListener(QDeclarativeItemChangeLis void QDeclarativeItem::keyPressEvent(QKeyEvent *event) { Q_D(QDeclarativeItem); + keyPressPreHandler(event); + if (event->isAccepted()) + return; if (d->keyHandler) - d->keyHandler->keyPressed(event); + d->keyHandler->keyPressed(event, true); else event->ignore(); } @@ -1832,8 +1930,11 @@ void QDeclarativeItem::keyPressEvent(QKeyEvent *event) void QDeclarativeItem::keyReleaseEvent(QKeyEvent *event) { Q_D(QDeclarativeItem); + keyReleasePreHandler(event); + if (event->isAccepted()) + return; if (d->keyHandler) - d->keyHandler->keyReleased(event); + d->keyHandler->keyReleased(event, true); else event->ignore(); } @@ -1842,8 +1943,11 @@ void QDeclarativeItem::keyReleaseEvent(QKeyEvent *event) void QDeclarativeItem::inputMethodEvent(QInputMethodEvent *event) { Q_D(QDeclarativeItem); + inputMethodPreHandler(event); + if (event->isAccepted()) + return; if (d->keyHandler) - d->keyHandler->inputMethodEvent(event); + d->keyHandler->inputMethodEvent(event, true); else event->ignore(); } @@ -1862,6 +1966,37 @@ QVariant QDeclarativeItem::inputMethodQuery(Qt::InputMethodQuery query) const return v; } +void QDeclarativeItem::keyPressPreHandler(QKeyEvent *event) +{ + Q_D(QDeclarativeItem); + if (d->keyHandler && !d->doneEventPreHandler) + d->keyHandler->keyPressed(event, false); + else + event->ignore(); + d->doneEventPreHandler = true; +} + +void QDeclarativeItem::keyReleasePreHandler(QKeyEvent *event) +{ + Q_D(QDeclarativeItem); + if (d->keyHandler && !d->doneEventPreHandler) + d->keyHandler->keyReleased(event, false); + else + event->ignore(); + d->doneEventPreHandler = true; +} + +void QDeclarativeItem::inputMethodPreHandler(QInputMethodEvent *event) +{ + Q_D(QDeclarativeItem); + if (d->keyHandler && !d->doneEventPreHandler) + d->keyHandler->inputMethodEvent(event, false); + else + event->ignore(); + d->doneEventPreHandler = true; +} + + /*! \internal */ @@ -2976,6 +3111,17 @@ void QDeclarativeItem::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidg */ bool QDeclarativeItem::event(QEvent *ev) { + Q_D(QDeclarativeItem); + switch (ev->type()) { + case QEvent::KeyPress: + case QEvent::KeyRelease: + case QEvent::InputMethod: + d->doneEventPreHandler = false; + break; + default: + break; + } + return QGraphicsObject::event(ev); } |