1/* 2 * Copyright (C) 2011 Benjamin Poulain <benjamin@webkit.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "QtPinchGestureRecognizer.h" 28 29 30#include "PageViewportControllerClientQt.h" 31#include "QtWebPageEventHandler.h" 32#include <QtCore/QLineF> 33 34namespace WebKit { 35 36const qreal pinchInitialTriggerDistanceThreshold = 5.; 37 38static inline QPointF computePinchCenter(const QTouchEvent::TouchPoint& point1, const QTouchEvent::TouchPoint& point2) 39{ 40 return (point1.pos() + point2.pos()) / 2.0f; 41} 42 43QtPinchGestureRecognizer::QtPinchGestureRecognizer(QtWebPageEventHandler* eventHandler) 44 : QtGestureRecognizer(eventHandler) 45{ 46 reset(); 47} 48 49bool QtPinchGestureRecognizer::update(const QTouchEvent::TouchPoint& point1, const QTouchEvent::TouchPoint& point2) 50{ 51 const qreal currentFingerDistance = QLineF(point1.screenPos(), point2.screenPos()).length(); 52 switch (m_state) { 53 case NoGesture: 54 m_initialFingerDistance = currentFingerDistance; 55 m_state = GestureRecognitionStarted; 56 return false; 57 case GestureRecognitionStarted: { 58 const qreal pinchDistance = qAbs(currentFingerDistance - m_initialFingerDistance); 59 if (pinchDistance < pinchInitialTriggerDistanceThreshold) 60 return false; 61 m_state = GestureRecognized; 62 if (viewportController()) 63 viewportController()->pinchGestureStarted(computePinchCenter(point1, point2)); 64 65 // We reset the initial span distance to the current distance of the 66 // touch points in order to avoid the jump caused by the events which 67 // were skipped between the recognition start and the actual recognition. 68 m_initialFingerDistance = currentFingerDistance; 69 70 // fall through 71 } 72 case GestureRecognized: 73 const qreal totalScaleFactor = currentFingerDistance / m_initialFingerDistance; 74 const QPointF touchCenterInViewCoordinates = computePinchCenter(point1, point2); 75 if (viewportController()) 76 viewportController()->pinchGestureRequestUpdate(touchCenterInViewCoordinates, totalScaleFactor); 77 return true; 78 break; 79 } 80 81 ASSERT_NOT_REACHED(); 82 return false; 83} 84 85void QtPinchGestureRecognizer::finish() 86{ 87 if (m_state == NoGesture) 88 return; 89 90 if (viewportController()) 91 viewportController()->pinchGestureEnded(); 92 reset(); 93} 94 95void QtPinchGestureRecognizer::cancel() 96{ 97 if (m_state == NoGesture) 98 return; 99 100 if (viewportController()) 101 viewportController()->pinchGestureCancelled(); 102 reset(); 103} 104 105} // namespace WebKit 106 107