From 46d0d4c80c164955289e7307ffabaf2a00b04eff Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Tue, 27 Nov 2018 00:02:33 +0300 Subject: [PATCH] Gui: Gesture: QPinchGesture angle - sign, degrees, make cumulative fixes pinch gesture rotation part on Qt5 (tested only on windows, Qt4 and Qt5) --- src/Gui/SoTouchEvents.cpp | 16 +++++++-- src/Gui/SoTouchEvents.h | 2 ++ src/Gui/WinNativeGestureRecognizers.cpp | 43 ++++++++++--------------- src/Gui/WinNativeGestureRecognizers.h | 11 ++++--- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/Gui/SoTouchEvents.cpp b/src/Gui/SoTouchEvents.cpp index d7bd48bb98..2c5d4f5e66 100644 --- a/src/Gui/SoTouchEvents.cpp +++ b/src/Gui/SoTouchEvents.cpp @@ -84,8 +84,8 @@ SoGesturePinchEvent::SoGesturePinchEvent(QPinchGesture* qpinch, QWidget *widget) deltaZoom = qpinch->scaleFactor(); totalZoom = qpinch->totalScaleFactor(); - deltaAngle = qpinch->rotationAngle(); - totalAngle = qpinch->totalRotationAngle(); + deltaAngle = unbranchAngle((qpinch->rotationAngle()-qpinch->lastRotationAngle()) / 180.0 * M_PI); + totalAngle = qpinch->totalRotationAngle() / 180 * M_PI; state = SbGestureState(qpinch->state()); @@ -102,6 +102,18 @@ SbBool SoGesturePinchEvent::isSoGesturePinchEvent(const SoEvent *ev) const return ev->isOfType(SoGesturePinchEvent::getClassTypeId()); } +/*! + * \brief SoGesturePinchEvent::unbranchAngle : utility function to bring an angle into -pi..pi region. + * \param ang - in radians + * \return + */ +double SoGesturePinchEvent::unbranchAngle(double ang) +{ + const double Pi = 3.14159265358979323846; + return ang - 2.0*Pi*floor((ang+Pi)/(2.0*Pi)); +} + + //----------------------------SoGestureSwipeEvent-------------------------------- SO_EVENT_SOURCE(SoGestureSwipeEvent); diff --git a/src/Gui/SoTouchEvents.h b/src/Gui/SoTouchEvents.h index 02dc18a8c3..980931709e 100644 --- a/src/Gui/SoTouchEvents.h +++ b/src/Gui/SoTouchEvents.h @@ -91,6 +91,8 @@ public: double deltaAngle; double totalAngle; + static double unbranchAngle(double ang); + }; class SoGestureSwipeEvent : public SoGestureEvent { diff --git a/src/Gui/WinNativeGestureRecognizers.cpp b/src/Gui/WinNativeGestureRecognizers.cpp index 188c108470..e425592e69 100644 --- a/src/Gui/WinNativeGestureRecognizers.cpp +++ b/src/Gui/WinNativeGestureRecognizers.cpp @@ -129,7 +129,7 @@ QGestureRecognizer::Result WinNativeGestureRecognizerPinch::recognize(QGesture * } double ang = 0.0; if (bRotate) - ang=GID_ROTATE_ANGLE_FROM_ARGUMENT(LOWORD(ev->argument)); + ang=GID_ROTATE_ANGLE_FROM_ARGUMENT(LOWORD(ev->argument)) / M_PI * 180.0; if (q->state() == Qt::NoGesture) { //start of a new gesture, prefill stuff //d->isNewSequence = true; @@ -146,11 +146,13 @@ QGestureRecognizer::Result WinNativeGestureRecognizerPinch::recognize(QGesture * q->setTotalRotationAngle(0.0); q->setLastRotationAngle(0.0); q->setRotationAngle(0.0); q->setTotalScaleFactor(1.0); q->setLastScaleFactor(1.0); q->setScaleFactor(1.0); if(bZoom) { - q->lastFingerDistance = ev->argument; - q->fingerDistance = ev->argument; + q->myLastFingerDistance = ev->argument; + q->myFingerDistance = ev->argument; + q->myStartFingerDistance = ev->argument; } else if (bRotate) { q->myLastRotationAngle = 0; q->myRotationAngle = 0; + q->myStartAngle = 0; } } else {//in the middle of gesture @@ -158,24 +160,24 @@ QGestureRecognizer::Result WinNativeGestureRecognizerPinch::recognize(QGesture * q->setLastCenterPoint(q->centerPoint()); q->setLastRotationAngle(q->rotationAngle()); q->setLastScaleFactor(q->scaleFactor()); - q->lastFingerDistance = q->fingerDistance; + q->myLastFingerDistance = q->myFingerDistance; q->myLastRotationAngle = q->myRotationAngle; //update the current values if (bZoom) - q->fingerDistance = ev->argument; + q->myFingerDistance = ev->argument; if (bRotate) q->myRotationAngle = ang; if(ev->gestureType == QNativeGestureEvent::GestureEnd){ - q->fingerDistance = q->lastFingerDistance;//the end-of-gesture event holds no finger separation data, hence we are using the last value. + q->myFingerDistance = q->myLastFingerDistance;//the end-of-gesture event holds no finger separation data, hence we are using the last value. q->myRotationAngle = q->myLastRotationAngle; } if (bZoom) q->setScaleFactor( - (qreal)(q->fingerDistance) / (qreal)(q->lastFingerDistance) + (qreal)(q->myFingerDistance) / (qreal)(q->myLastFingerDistance) ); if (bRotate) - q->setRotationAngle(qreal(unbranchAngle(q->myRotationAngle - q->myLastRotationAngle))); + q->setRotationAngle(q->myRotationAngle); q->setCenterPoint( QPointF( qreal(ev->position.x()), @@ -183,20 +185,20 @@ QGestureRecognizer::Result WinNativeGestureRecognizerPinch::recognize(QGesture * ) ); - //compute the changes + //detect changes QPinchGesture::ChangeFlags cf = 0; if ( q->scaleFactor() != 1.0 ) cf |= QPinchGesture::ScaleFactorChanged; if (q->lastCenterPoint() != q->centerPoint()) cf |= QPinchGesture::CenterPointChanged; - if (q->rotationAngle() != 0.0) + if (q->rotationAngle() != q->lastRotationAngle()) cf |= QPinchGesture::RotationAngleChanged; q->setChangeFlags(cf); - //increment totals + //update totals q->setTotalChangeFlags (q->totalChangeFlags() | q->changeFlags()); q->setTotalScaleFactor (q->totalScaleFactor() * q->scaleFactor()); - q->setTotalRotationAngle (q->totalRotationAngle() + q->rotationAngle()); + q->setTotalRotationAngle (q->rotationAngle()); } } return result; @@ -207,7 +209,7 @@ void WinNativeGestureRecognizerPinch::reset(QGesture* gesture) { QGestureRecognizer::reset(gesture);//resets the state of the gesture, which is not write-accessible otherwise QPinchGestureN *q = static_cast(gesture); - q->lastFingerDistance = 0; + q->myLastFingerDistance = 0; q->setTotalChangeFlags(0); q->setChangeFlags(0); q->setLastCenterPoint(QPointF()); @@ -220,8 +222,8 @@ void WinNativeGestureRecognizerPinch::reset(QGesture* gesture) q->setStartCenterPoint(q->centerPoint()); q->setTotalRotationAngle(0.0); q->setLastRotationAngle(0.0); q->setRotationAngle(0.0); q->setTotalScaleFactor(1.0); q->setLastScaleFactor(1.0); q->setScaleFactor(1.0); - q->lastFingerDistance = 0; - q->fingerDistance = 0; + q->myLastFingerDistance = 0; + q->myFingerDistance = 0; } //function prototype for dymanic linking @@ -269,17 +271,6 @@ void WinNativeGestureRecognizerPinch::TuneWindowsGestures(QWidget* target) #endif } -/*! - * \brief WinNativeGestureRecognizerPinch::unbranchAngle utility function to bring an angle into -pi..pi region. - * \param ang - * \return - */ -double WinNativeGestureRecognizerPinch::unbranchAngle(double ang) -{ - const double Pi = 3.14159265358979323846; - return ang - 2.0*Pi*floor((ang+Pi)/(2.0*Pi)); -} - #endif //!defined(QT_NO_NATIVE_GESTURES) #endif // GESTURE_MESS diff --git a/src/Gui/WinNativeGestureRecognizers.h b/src/Gui/WinNativeGestureRecognizers.h index b58faabbfe..b287a577bc 100644 --- a/src/Gui/WinNativeGestureRecognizers.h +++ b/src/Gui/WinNativeGestureRecognizers.h @@ -50,10 +50,12 @@ class QPinchGestureN: public QPinchGesture { public: - int lastFingerDistance;//distance between fingers, in pixels - int fingerDistance; - double myRotationAngle; - double myLastRotationAngle; + int myFingerDistance = 0; //distance between fingers, in pixels + int myLastFingerDistance = 0; + int myStartFingerDistance = 0; //finger distance at gesture start + double myRotationAngle = 0.0; + double myLastRotationAngle = 0.0; + double myStartAngle = 0.0; }; class WinNativeGestureRecognizerPinch : public QGestureRecognizer @@ -64,7 +66,6 @@ public: virtual Result recognize ( QGesture* gesture, QObject* watched, QEvent* event ); virtual void reset ( QGesture* gesture ); static void TuneWindowsGestures(QWidget* target); - static double unbranchAngle(double ang); }; #endif //GESTUREMESS