on xenial help sign in
Make DirectionalDragArea work when rotated

The drag gesture direction is in local coordinates, not in scene coordinates

commit37a0c6879f1349ec1632c327c6e88b7ef5e7c1d9 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateWed 12 Feb 2014, 13:37 (13 years ago) parent084e2eb6872f77
8 files changed+209−62
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+88−35 plugins/Ubuntu/Gestures/DirectionalDragArea.h+9−1 tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml+8−1 tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml+7 tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml+7 tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml+7 tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp+53−17 tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.qml+30−8
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+88−35
@@ -114,6 +114,7 @@ DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
114114 , m_wideningAngle(0)
115115 , m_wideningFactor(0)
116116 , m_distanceThreshold(0)
117+ , m_distanceThresholdSquared(0.)
117118 , m_minSpeed(0)
118119 , m_maxSilenceTime(200)
119120 , m_silenceTime(0)
@@ -162,7 +163,14 @@ void DirectionalDragArea::setWideningAngle(qreal angle)
162163 return;
163164
164165 m_wideningAngle = angle;
165− m_wideningFactor = qTan(angle * M_PI / 180.0);
166+
167+ // wideningFactor = pow(cosine(angle), 2)
168+ {
169+ qreal angleRadians = angle * M_PI / 180.0;
170+ m_wideningFactor = qCos(angleRadians);
171+ m_wideningFactor = m_wideningFactor * m_wideningFactor;
172+ }
173+
166174 Q_EMIT wideningAngleChanged(angle);
167175}
168176
@@ -170,6 +178,7 @@ void DirectionalDragArea::setDistanceThreshold(qreal value)
170178{
171179 if (m_distanceThreshold != value) {
172180 m_distanceThreshold = value;
181+ m_distanceThresholdSquared = m_distanceThreshold * m_distanceThreshold;
173182 Q_EMIT distanceThresholdChanged(value);
174183 }
175184}
@@ -335,12 +344,13 @@ void DirectionalDragArea::touchEvent_absent(QTouchEvent *event)
335344 m_startScenePos = newTouchPoint->scenePos();
336345 m_touchId = newTouchPoint->id();
337346 m_dampedScenePos.reset(m_startScenePos);
338− updateVelocityCalculator(m_startScenePos);
347+ m_velocityCalculator->setTrackedPosition(0.);
339348 m_velocityCalculator->reset();
340349 m_numSamplesOnLastSpeedCheck = 0;
341350 m_silenceTime = 0;
342351 setPreviousPos(m_startPos);
343352 setPreviousScenePos(m_startScenePos);
353+ updateSceneDirectionVector();
344354
345355 setStatus(Undecided);
346356}
@@ -441,44 +451,53 @@ const QTouchEvent::TouchPoint *DirectionalDragArea::fetchTargetTouchPoint(QTouch
441451
442452bool DirectionalDragArea::pointInsideAllowedArea() const
443453{
444− qreal dX = m_dampedScenePos.x() - m_startScenePos.x();
445− qreal dY = m_dampedScenePos.y() - m_startScenePos.y();
454+ // NB: Using squared values to avoid computing the square root to find
455+ // the length totalMovement
446456
447− switch (m_direction) {
448− case Direction::Upwards:
449− return dY <= 0 && qFabs(dX) <= qFabs(dY) * m_wideningFactor;
450− case Direction::Downwards:
451− return dY >= 0 && qFabs(dX) <= dY * m_wideningFactor;
452− case Direction::Leftwards:
453− return dX <= 0 && qFabs(dY) <= qFabs(dX) * m_wideningFactor;
454− default: // Direction::Rightwards:
455− return dX >= 0 && qFabs(dY) <= dX * m_wideningFactor;
457+ QPointF totalMovement(m_dampedScenePos.x() - m_startScenePos.x(),
458+ m_dampedScenePos.y() - m_startScenePos.y());
459+
460+ qreal squaredTotalMovSize = totalMovement.x() * totalMovement.x() +
461+ totalMovement.y() * totalMovement.y();
462+
463+ if (squaredTotalMovSize == 0.) {
464+ // didn't move
465+ return true;
456466 }
467+
468+ qreal projectedMovement = projectOntoDirectionVector(totalMovement);
469+
470+
471+ qreal cosineAngleSquared = (projectedMovement * projectedMovement) / squaredTotalMovSize;
472+
473+ // Same as:
474+ // angle_between_movement_vector_and_gesture_direction_vector <= widening_angle
475+ return cosineAngleSquared >= m_wideningFactor;
457476}
458477
459478bool DirectionalDragArea::movingInRightDirection() const
460479{
461− switch (m_direction) {
462− case Direction::Upwards:
463− return m_dampedScenePos.y() <= m_previousDampedScenePos.y();
464− case Direction::Downwards:
465− return m_dampedScenePos.y() >= m_previousDampedScenePos.y();
466− case Direction::Leftwards:
467− return m_dampedScenePos.x() <= m_previousDampedScenePos.x();
468− default: // Direction::Rightwards:
469− return m_dampedScenePos.x() >= m_previousDampedScenePos.x();
470− }
480+ QPointF movementVector(m_dampedScenePos.x() - m_previousDampedScenePos.x(),
481+ m_dampedScenePos.y() - m_previousDampedScenePos.y());
482+
483+ qreal scalarProjection = projectOntoDirectionVector(movementVector);
484+
485+ return scalarProjection >= 0.;
471486}
472487
473488bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
474489{
475− if (m_distanceThreshold > 0) {
476− if (Direction::isHorizontal(m_direction))
477− return qFabs(point.x() - m_startScenePos.x()) > m_distanceThreshold;
478− else
479− return qFabs(point.y() - m_startScenePos.y()) > m_distanceThreshold;
480− } else {
490+ if (m_distanceThreshold <= 0.) {
491+ // distance threshold check is disabled
481492 return true;
493+ } else {
494+ QPointF totalMovement(point.x() - m_startScenePos.x(),
495+ point.y() - m_startScenePos.y());
496+
497+ qreal squaredTotalMovSize = totalMovement.x() * totalMovement.x() +
498+ totalMovement.y() * totalMovement.y();
499+
500+ return squaredTotalMovSize > m_distanceThresholdSquared;
482501 }
483502}
484503
@@ -583,13 +602,13 @@ void DirectionalDragArea::setPreviousScenePos(const QPointF &point)
583602 }
584603}
585604
586−void DirectionalDragArea::updateVelocityCalculator(const QPointF &point)
605+void DirectionalDragArea::updateVelocityCalculator(const QPointF &scenePos)
587606{
588− if (Direction::isHorizontal(m_direction)) {
589− m_velocityCalculator->setTrackedPosition(point.x());
590− } else {
591− m_velocityCalculator->setTrackedPosition(point.y());
592− }
607+ QPointF totalSceneMovement = scenePos - m_startScenePos;
608+
609+ qreal scalarProjection = projectOntoDirectionVector(totalSceneMovement);
610+
611+ m_velocityCalculator->setTrackedPosition(scalarProjection);
593612}
594613
595614bool DirectionalDragArea::isWithinTouchCompositionWindow()
@@ -693,5 +712,39 @@ qint64 DirectionalDragArea::ActiveTouchesInfo::mostRecentStartTime()
693712 return highestStartTime;
694713}
695714
715+void DirectionalDragArea::updateSceneDirectionVector()
716+{
717+ QPointF localOrigin(0., 0.);
718+ QPointF localDirection;
719+ switch (m_direction) {
720+ case Direction::Upwards:
721+ localDirection.rx() = 0.;
722+ localDirection.ry() = -1.;
723+ break;
724+ case Direction::Downwards:
725+ localDirection.rx() = 0.;
726+ localDirection.ry() = 1;
727+ break;
728+ case Direction::Leftwards:
729+ localDirection.rx() = -1.;
730+ localDirection.ry() = 0.;
731+ break;
732+ default: // Direction::Rightwards:
733+ localDirection.rx() = 1.;
734+ localDirection.ry() = 0.;
735+ break;
736+ }
737+ QPointF sceneOrigin = mapToScene(localOrigin);
738+ QPointF sceneDirection = mapToScene(localDirection);
739+ m_sceneDirectionVector = sceneDirection - sceneOrigin;
740+}
741+
742+qreal DirectionalDragArea::projectOntoDirectionVector(const QPointF &sceneVector) const
743+{
744+ // same as dot product as m_sceneDirectionVector is a unit vector
745+ return sceneVector.x() * m_sceneDirectionVector.x() +
746+ sceneVector.y() * m_sceneDirectionVector.y();
747+}
748+
696749// Because we are defining a new QObject-based class (RecognitionTimer) here.
697750#include "DirectionalDragArea.moc"
plugins/Ubuntu/Gestures/DirectionalDragArea.h+9−1
@@ -231,6 +231,10 @@ private:
231231 void setPreviousScenePos(const QPointF &point);
232232 void updateVelocityCalculator(const QPointF &point);
233233 bool isWithinTouchCompositionWindow();
234+ void updateSceneDirectionVector();
235+ // returns the scalar projection between the given vector (in scene coordinates)
236+ // and m_sceneDirectionVector
237+ qreal projectOntoDirectionVector(const QPointF &sceneVector) const;
234238
235239 Status m_status;
236240
@@ -245,10 +249,14 @@ private:
245249 DampedPointF m_dampedScenePos;
246250 QPointF m_previousDampedScenePos;
247251
252+ // Unit vector in scene coordinates describing the direction of the gesture recognition
253+ QPointF m_sceneDirectionVector;
254+
248255 Direction::Type m_direction;
249256 qreal m_wideningAngle; // in degrees
250− qreal m_wideningFactor; // it's tan(degreesToRadian(m_wideningAngle))
257+ qreal m_wideningFactor; // it's pow(cosine(m_wideningAngle), 2)
251258 qreal m_distanceThreshold;
259+ qreal m_distanceThresholdSquared; // it's pow(m_distanceThreshold, 2)
252260 qreal m_minSpeed;
253261 int m_maxSilenceTime; // in milliseconds
254262 int m_silenceTime; // in milliseconds
tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml+8−1
@@ -63,7 +63,7 @@ Item {
6363 dragAreaRect.opacity = 0.3
6464 launcher.y = Qt.binding(launcher.followDragArea)
6565 break;
66− defaut: // DirectionalDragArea.Recognized:
66+ default: // DirectionalDragArea.Recognized:
6767 dragAreaRect.color = "green"
6868 dragAreaRect.opacity = 0.5
6969 break;
@@ -76,4 +76,11 @@ Item {
7676 top: parent.top
7777 }
7878 }
79+
80+ Label {
81+ text: "Downwards"
82+ anchors.top: parent.top
83+ anchors.horizontalCenter: parent.horizontalCenter
84+ anchors.topMargin: units.gu(1)
85+ }
7986}
tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml+7
@@ -79,4 +79,11 @@ Item {
7979 bottom: parent.bottom
8080 }
8181 }
82+
83+ Label {
84+ text: "Leftwards"
85+ anchors.right: parent.right
86+ anchors.verticalCenter: parent.verticalCenter
87+ anchors.rightMargin: units.gu(1)
88+ }
8289}
tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml+7
@@ -77,4 +77,11 @@ Item {
7777 bottom: parent.bottom
7878 }
7979 }
80+
81+ Label {
82+ text: "Rightwards"
83+ anchors.left: parent.left
84+ anchors.verticalCenter: parent.verticalCenter
85+ anchors.leftMargin: units.gu(1)
86+ }
8087}
tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml+7
@@ -79,4 +79,11 @@ Item {
7979 bottom: parent.bottom
8080 }
8181 }
82+
83+ Label {
84+ text: "Upwards"
85+ anchors.bottom: parent.bottom
86+ anchors.horizontalCenter: parent.horizontalCenter
87+ anchors.bottomMargin: units.gu(1)
88+ }
8289}
tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp+53−17
@@ -83,6 +83,7 @@ private Q_SLOTS:
8383 void twoFingerTap();
8484 void movingDDA();
8585 void ignoreOldFinger();
86+ void rotated();
8687
8788private:
8889 void passTime(qint64 timeSpan);
@@ -131,18 +132,10 @@ void tst_DirectionalDragArea::passTime(qint64 timeSpan)
131132}
132133
133134namespace {
134−QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
135+QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea)
135136{
136− switch (edgeDragArea->direction()) {
137− case Direction::Upwards:
138− return QPointF(view->width()/2.0f, view->height() - (edgeDragArea->height()/2.0f));
139− case Direction::Downwards:
140− return QPointF(view->width()/2.0f, edgeDragArea->height()/2.0f);
141− case Direction::Leftwards:
142− return QPointF(view->width() - (edgeDragArea->width()/2.0f), view->height()/2.0f);
143− default: // Direction::Rightwards:
144− return QPointF(edgeDragArea->width()/2.0f, view->height()/2.0f);
145− }
137+ QPointF localCenter(edgeDragArea->width() / 2., edgeDragArea->height() / 2.);
138+ return edgeDragArea->mapToScene(localCenter);
146139}
147140
148141QPointF calculateDirectionVector(DirectionalDragArea *edgeDragArea,
@@ -193,7 +186,7 @@ void tst_DirectionalDragArea::edgeDrag()
193186
194187 QSignalSpy draggingSpy(edgeDragArea, SIGNAL(draggingChanged(bool)));
195188
196− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
189+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
197190 QPointF touchPoint = initialTouchPos;
198191
199192 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*dragDistanceFactor;
@@ -300,7 +293,7 @@ void tst_DirectionalDragArea::dragWithShortDirectionChange()
300293 edgeDragArea->setRecognitionTimer(fakeTimer);
301294 edgeDragArea->setTimeSource(fakeTimeSource);
302295
303− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
296+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
304297 QPointF touchPoint = initialTouchPos;
305298
306299 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0;
@@ -360,7 +353,7 @@ void tst_DirectionalDragArea::minSpeed()
360353
361354 edgeDragArea->setMinSpeed(minSpeed);
362355
363− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
356+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
364357 QPointF touchPoint = initialTouchPos;
365358
366359 QPointF dragDirectionVector(1.0, 0.0);
@@ -408,7 +401,7 @@ void tst_DirectionalDragArea::recognitionTimerUsage()
408401
409402 int timeStepMs = 5; // some arbitrary small value.
410403
411− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
404+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
412405 QPointF touchPoint = initialTouchPos;
413406
414407 QPointF dragDirectionVector(1.0, 0.0);
@@ -452,7 +445,7 @@ void tst_DirectionalDragArea::maxSilenceTime()
452445 // Make sure this property is not disabled
453446 edgeDragArea->setMaxSilenceTime(100);
454447
455− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
448+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
456449 QPointF touchPoint = initialTouchPos;
457450
458451 QTest::touchEvent(m_view, m_device).press(0, touchPoint.toPoint());
@@ -620,7 +613,7 @@ void tst_DirectionalDragArea::movingDDA()
620613 edgeDragArea->setRecognitionTimer(fakeTimer);
621614 edgeDragArea->setTimeSource(fakeTimeSource);
622615
623− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
616+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
624617 QPointF touchPoint = initialTouchPos;
625618
626619 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0f;
@@ -704,6 +697,49 @@ void tst_DirectionalDragArea::ignoreOldFinger()
704697 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::WaitingForTouch);
705698}
706699
700+/*
701+ A Rightwards DDA that is rotated 90 degrees clockwise should recognize gestures
702+ that are done downwards in scene coordinates. I.e. the gesture recognition direction
703+ should be in local coordinates, not scene coordinates.
704+ */
705+void tst_DirectionalDragArea::rotated()
706+{
707+ QQuickItem *baseItem = m_view->rootObject()->findChild<QQuickItem*>("baseItem");
708+ baseItem->setRotation(90.);
709+
710+ QQuickItem *rightwardsLauncher = m_view->rootObject()->findChild<QQuickItem*>("rightwardsLauncher");
711+ Q_ASSERT(rightwardsLauncher != 0);
712+
713+ DirectionalDragArea *edgeDragArea =
714+ rightwardsLauncher->findChild<DirectionalDragArea*>("hpDragArea");
715+ Q_ASSERT(edgeDragArea != 0);
716+ edgeDragArea->setRecognitionTimer(fakeTimer);
717+ edgeDragArea->setTimeSource(fakeTimeSource);
718+
719+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
720+ QPointF touchPoint = initialTouchPos;
721+
722+ qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0f;
723+ QPointF dragDirectionVector(0.0f, 1.0f);
724+
725+ qreal movementStepDistance = edgeDragArea->distanceThreshold() * 0.1f;
726+ QPointF touchMovement = dragDirectionVector * movementStepDistance;
727+ int totalMovementSteps = qCeil(desiredDragDistance / movementStepDistance);
728+ int movementTimeStepMs = (edgeDragArea->compositionTime() * 1.5f) / totalMovementSteps;
729+
730+ QTest::touchEvent(m_view, m_device).press(0, touchPoint.toPoint());
731+
732+ for (int i = 0; i < totalMovementSteps; ++i) {
733+ touchPoint += touchMovement;
734+ passTime(movementTimeStepMs);
735+ QTest::touchEvent(m_view, m_device).move(0, touchPoint.toPoint());
736+ }
737+
738+ QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Recognized);
739+
740+ QTest::touchEvent(m_view, m_device).release(0, touchPoint.toPoint());
741+}
742+
707743QTEST_MAIN(tst_DirectionalDragArea)
708744
709745#include "tst_DirectionalDragArea.moc"
tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.qml+30−8
@@ -18,8 +18,8 @@ import QtQuick 2.0
1818import Ubuntu.Components 0.1
1919
2020Rectangle {
21− width: units.gu(40)
22− height: units.gu(71)
21+ width: units.gu(60)
22+ height: units.gu(60)
2323 color: "white"
2424
2525 MouseArea {
@@ -33,14 +33,36 @@ Rectangle {
3333 }
3434 }
3535
36− // NB: Do not anchor it as we will move it programmatically from the test
37− RightwardsLauncher {
38− id: hpLauncher;
36+ Item {
37+ id: baseItem
38+ objectName: "baseItem"
3939 width: parent.width
4040 height: parent.height
41+
42+ // NB: Do not anchor it as we will move it programmatically from the test
43+ RightwardsLauncher {
44+ id: hpLauncher;
45+ width: parent.width
46+ height: parent.height
47+ }
48+
49+ LeftwardsLauncher { id: hnLauncher; anchors.fill: parent }
50+ DownwardsLauncher { id: vpLauncher; anchors.fill: parent }
51+ UpwardsLauncher { id: vnLauncher; anchors.fill: parent }
4152 }
4253
43− LeftwardsLauncher { id: hnLauncher; anchors.fill: parent }
44− DownwardsLauncher { id: vpLauncher; anchors.fill: parent }
45− UpwardsLauncher { id: vnLauncher; anchors.fill: parent }
54+ Button {
55+ anchors.bottom: parent.bottom
56+ anchors.left: parent.left
57+ anchors.margins: units.gu(1)
58+
59+ text: "rotation: " + baseItem.rotation
60+ onClicked: {
61+ if (baseItem.rotation === 0.0) {
62+ baseItem.rotation = 90.0
63+ } else {
64+ baseItem.rotation = 0.0
65+ }
66+ }
67+ }
4668}