diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2009-05-13 13:25:53 (GMT) |
---|---|---|
committer | Bradley T. Hughes <bradley.hughes@nokia.com> | 2009-05-13 13:31:18 (GMT) |
commit | 7be95673a1266b306fd76cd43fe894f144b0e72d (patch) | |
tree | aef19868cf9e62a9f2efa5d341cc50dfa9baa086 /src/gui/graphicsview/qgraphicsview.cpp | |
parent | d9c2a93e1dc1ea0248d72fdc7f1bb863f8847658 (diff) | |
download | Qt-7be95673a1266b306fd76cd43fe894f144b0e72d.zip Qt-7be95673a1266b306fd76cd43fe894f144b0e72d.tar.gz Qt-7be95673a1266b306fd76cd43fe894f144b0e72d.tar.bz2 |
Support multiple touch targets in QGraphicsView
This required a larger change to the kernel and graphicsview directories to
make this as efficient as possible:
1. QTouchEvent::TouchPoint becomes the base for
QGraphicsSceneTouchEvent::TouchPoint - this means there is one private for
every touch point, and we can store both the screen and scene coordinates
in one place. Converting a QTouchEvent to QGraphicsSceneTouchEvent becomes
nothing more than casting the QTouchEvent::TouchPoints to
QGraphicsSceneTouchEvent::TouchPoints.
2. The logic that we use in QApplication to convert WM_TOUCH* messages to
QTouchEvents is essentially duplicated (with some minor changes) to
QGraphicsScene so that it can support mulitple touch item targets. I
will have to investigate how I can perhaps merge some of the duplicated
code.
QEvent::GraphicsSceneTouchBegin propagation is not implemented yet, and will
come in a later commit
Diffstat (limited to 'src/gui/graphicsview/qgraphicsview.cpp')
-rw-r--r-- | src/gui/graphicsview/qgraphicsview.cpp | 130 |
1 files changed, 41 insertions, 89 deletions
diff --git a/src/gui/graphicsview/qgraphicsview.cpp b/src/gui/graphicsview/qgraphicsview.cpp index 6f142e3..1d801cd 100644 --- a/src/gui/graphicsview/qgraphicsview.cpp +++ b/src/gui/graphicsview/qgraphicsview.cpp @@ -306,6 +306,30 @@ inline int q_round_bound(qreal d) //### (int)(qreal) INT_MAX != INT_MAX for sing return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1); } +static void qt_convertTouchEventToGraphicsSceneTouchEvent(QGraphicsViewPrivate *d, + QTouchEvent *originalEvent, + QGraphicsSceneTouchEvent *touchEvent) +{ + QList<QTouchEvent::TouchPoint *> originalTouchPoints = originalEvent->touchPoints(); + QList<QGraphicsSceneTouchEvent::TouchPoint *> touchPoints; + for (int i = 0; i < originalTouchPoints.count(); ++i) { + QGraphicsSceneTouchEvent::TouchPoint *touchPoint = + static_cast<QGraphicsSceneTouchEvent::TouchPoint *>(originalTouchPoints.at(i)); + // the scene will set the pos before delivering to an item + touchPoint->setScenePos(d->mapToScene(touchPoint->pos())); + // the scene will set the startPos before delivering to an item + touchPoint->setStartScenePos(d->mapToScene(touchPoint->startPos())); + // the scene will set the lastPos before delivering to an item + touchPoint->setLastScenePos(d->mapToScene(touchPoint->lastScreenPos())); + // lastScreenPos is already set in the originalTouchPoint + + touchPoints.append(touchPoint); + } + + touchEvent->setTouchPoints(touchPoints); + touchEvent->setModifiers(originalEvent->modifiers()); +} + /*! \internal */ @@ -2962,20 +2986,24 @@ bool QGraphicsView::viewportEvent(QEvent *event) { if (!isEnabled()) return false; - QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event); - switch (touchEvent->type()) { - case QEvent::TouchBegin: - d->touchBeginEvent(touchEvent); - break; - case QEvent::TouchUpdate: - d->touchUpdateEvent(touchEvent); - break; - case QEvent::TouchEnd: - d->touchEndEvent(touchEvent); - break; - default: - break; + + if (d->scene && d->sceneInteractionAllowed) { + // Convert and deliver the touch event to the scene. + QEvent::Type eventType = event->type() == QEvent::TouchBegin + ? QEvent::GraphicsSceneTouchBegin + : event->type() == QEvent::TouchEnd + ? QEvent::GraphicsSceneTouchEnd + : QEvent::GraphicsSceneTouchUpdate; + QGraphicsSceneTouchEvent touchEvent(eventType); + touchEvent.setWidget(viewport()); + qt_convertTouchEventToGraphicsSceneTouchEvent(d, + static_cast<QTouchEvent *>(event), + &touchEvent); + touchEvent.setAccepted(false); + QApplication::sendEvent(d->scene, &touchEvent); + event->setAccepted(touchEvent.isAccepted()); } + return true; } default: @@ -3916,34 +3944,6 @@ void QGraphicsView::resetTransform() setTransform(QTransform()); } -static void qt_convertTouchEventToGraphicsSceneTouchEvent(QGraphicsViewPrivate *d, QTouchEvent *originalEvent, QGraphicsSceneTouchEvent *touchEvent) -{ - QList<QTouchEvent::TouchPoint *> originalTouchPoints = originalEvent->touchPoints(); - QList<QGraphicsSceneTouchEvent::TouchPoint *> touchPoints; - for (int i = 0; i < originalTouchPoints.count(); ++i) { - QTouchEvent::TouchPoint *originalTouchPoint = originalTouchPoints.at(i); - - QGraphicsSceneTouchEvent::TouchPoint *touchPoint = new QGraphicsSceneTouchEvent::TouchPoint(); - touchPoint->setId(originalTouchPoint->id()); - touchPoint->setState(originalTouchPoint->state()); - // the scene will set the pos before delivering to an item - touchPoint->setScenePos(d->mapToScene(originalTouchPoint->pos())); - touchPoint->setScreenPos(originalTouchPoint->globalPos()); - // the scene will set the startPos before delivering to an item - touchPoint->setStartScenePos(d->mapToScene(originalTouchPoint->startPos())); - touchPoint->setStartScreenPos(originalTouchPoint->startGlobalPos()); - // the scene will set the lastPos before delivering to an item - touchPoint->setLastScenePos(d->mapToScene(originalTouchPoint->lastPos())); - touchPoint->setLastScreenPos(originalTouchPoint->lastGlobalPos()); - touchPoint->setPressure(originalTouchPoint->pressure()); - - touchPoints.append(touchPoint); - } - - touchEvent->setTouchPoints(touchPoints); - touchEvent->setModifiers(originalEvent->modifiers()); -} - QPointF QGraphicsViewPrivate::mapToScene(const QPointF &point) const { QPointF p = point; @@ -3952,54 +3952,6 @@ QPointF QGraphicsViewPrivate::mapToScene(const QPointF &point) const return identityMatrix ? p : matrix.inverted().map(p); } -void QGraphicsViewPrivate::touchBeginEvent(QTouchEvent *event) -{ - Q_Q(QGraphicsView); - - if (!scene || !sceneInteractionAllowed) - return; - - // Convert and deliver the touch event to the scene. - QGraphicsSceneTouchEvent touchEvent(QEvent::GraphicsSceneTouchBegin); - touchEvent.setWidget(q->viewport()); - qt_convertTouchEventToGraphicsSceneTouchEvent(this, event, &touchEvent); - touchEvent.setAccepted(false); - QApplication::sendEvent(scene, &touchEvent); - event->setAccepted(touchEvent.isAccepted()); -} - -void QGraphicsViewPrivate::touchUpdateEvent(QTouchEvent *event) -{ - Q_Q(QGraphicsView); - - if (!scene || !sceneInteractionAllowed) - return; - - // Convert and deliver the touch event to the scene. - QGraphicsSceneTouchEvent touchEvent(QEvent::GraphicsSceneTouchUpdate); - touchEvent.setWidget(q->viewport()); - qt_convertTouchEventToGraphicsSceneTouchEvent(this, event, &touchEvent); - touchEvent.setAccepted(false); - QApplication::sendEvent(scene, &touchEvent); - event->setAccepted(touchEvent.isAccepted()); -} - -void QGraphicsViewPrivate::touchEndEvent(QTouchEvent *event) -{ - Q_Q(QGraphicsView); - - if (!scene || !sceneInteractionAllowed) - return; - - // Convert and deliver the touch event to the scene. - QGraphicsSceneTouchEvent touchEvent(QEvent::GraphicsSceneTouchEnd); - touchEvent.setWidget(q->viewport()); - qt_convertTouchEventToGraphicsSceneTouchEvent(this, event, &touchEvent); - touchEvent.setAccepted(false); - QApplication::sendEvent(scene, &touchEvent); - event->setAccepted(touchEvent.isAccepted()); -} - QT_END_NAMESPACE #include "moc_qgraphicsview.cpp" |