on xenial help sign in
Make DirectionalDragArea work when rotated

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

commit8142dd43a3486044c4dabb3ba9b5a9c2e34399de authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateTue 18 Feb 2014, 11:39 (13 years ago) parenta57f52bc8eb9eb
12 files changed+401−174
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+104−44 plugins/Ubuntu/Gestures/DirectionalDragArea.h+11−1 qml/Components/DragHandle.qml+2−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+108−17 tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.qml+30−8 tests/qmltests/Components/tst_DragHandle.cpp+45−5 tests/qmltests/Components/tst_DragHandle.qml+72−56 tests/qmltests/Components/tst_DragHandle/TestButton.qml−41
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+104−44
@@ -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}
@@ -237,13 +246,15 @@ qreal DirectionalDragArea::distance() const
237246 }
238247}
239248
249+void DirectionalDragArea::updateSceneDistance()
250+{
251+ QPointF totalMovement = m_previousScenePos - m_startScenePos;
252+ m_sceneDistance = projectOntoDirectionVector(totalMovement);
253+}
254+
240255qreal DirectionalDragArea::sceneDistance() const
241256{
242− if (Direction::isHorizontal(m_direction)) {
243− return m_previousScenePos.x() - m_startScenePos.x();
244− } else {
245− return m_previousScenePos.y() - m_startScenePos.y();
246− }
257+ return m_sceneDistance;
247258}
248259
249260qreal DirectionalDragArea::touchX() const
@@ -335,12 +346,13 @@ void DirectionalDragArea::touchEvent_absent(QTouchEvent *event)
335346 m_startScenePos = newTouchPoint->scenePos();
336347 m_touchId = newTouchPoint->id();
337348 m_dampedScenePos.reset(m_startScenePos);
338− updateVelocityCalculator(m_startScenePos);
349+ m_velocityCalculator->setTrackedPosition(0.);
339350 m_velocityCalculator->reset();
340351 m_numSamplesOnLastSpeedCheck = 0;
341352 m_silenceTime = 0;
342353 setPreviousPos(m_startPos);
343354 setPreviousScenePos(m_startScenePos);
355+ updateSceneDirectionVector();
344356
345357 setStatus(Undecided);
346358}
@@ -441,44 +453,53 @@ const QTouchEvent::TouchPoint *DirectionalDragArea::fetchTargetTouchPoint(QTouch
441453
442454bool DirectionalDragArea::pointInsideAllowedArea() const
443455{
444− qreal dX = m_dampedScenePos.x() - m_startScenePos.x();
445− qreal dY = m_dampedScenePos.y() - m_startScenePos.y();
456+ // NB: Using squared values to avoid computing the square root to find
457+ // the length totalMovement
446458
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;
459+ QPointF totalMovement(m_dampedScenePos.x() - m_startScenePos.x(),
460+ m_dampedScenePos.y() - m_startScenePos.y());
461+
462+ qreal squaredTotalMovSize = totalMovement.x() * totalMovement.x() +
463+ totalMovement.y() * totalMovement.y();
464+
465+ if (squaredTotalMovSize == 0.) {
466+ // didn't move
467+ return true;
456468 }
469+
470+ qreal projectedMovement = projectOntoDirectionVector(totalMovement);
471+
472+
473+ qreal cosineAngleSquared = (projectedMovement * projectedMovement) / squaredTotalMovSize;
474+
475+ // Same as:
476+ // angle_between_movement_vector_and_gesture_direction_vector <= widening_angle
477+ return cosineAngleSquared >= m_wideningFactor;
457478}
458479
459480bool DirectionalDragArea::movingInRightDirection() const
460481{
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− }
482+ QPointF movementVector(m_dampedScenePos.x() - m_previousDampedScenePos.x(),
483+ m_dampedScenePos.y() - m_previousDampedScenePos.y());
484+
485+ qreal scalarProjection = projectOntoDirectionVector(movementVector);
486+
487+ return scalarProjection >= 0.;
471488}
472489
473490bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
474491{
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 {
492+ if (m_distanceThreshold <= 0.) {
493+ // distance threshold check is disabled
481494 return true;
495+ } else {
496+ QPointF totalMovement(point.x() - m_startScenePos.x(),
497+ point.y() - m_startScenePos.y());
498+
499+ qreal squaredTotalMovSize = totalMovement.x() * totalMovement.x() +
500+ totalMovement.y() * totalMovement.y();
501+
502+ return squaredTotalMovSize > m_distanceThresholdSquared;
482503 }
483504}
484505
@@ -568,28 +589,33 @@ void DirectionalDragArea::setPreviousScenePos(const QPointF &point)
568589 bool xChanged = m_previousScenePos.x() != point.x();
569590 bool yChanged = m_previousScenePos.y() != point.y();
570591
592+ if (!xChanged && !yChanged)
593+ return;
594+
595+ qreal oldSceneDistance = sceneDistance();
571596 m_previousScenePos = point;
597+ updateSceneDistance();
598+
599+ if (oldSceneDistance != sceneDistance()) {
600+ Q_EMIT sceneDistanceChanged(sceneDistance());
601+ }
572602
573603 if (xChanged) {
574604 Q_EMIT touchSceneXChanged(point.x());
575− if (Direction::isHorizontal(m_direction))
576− Q_EMIT sceneDistanceChanged(sceneDistance());
577605 }
578606
579607 if (yChanged) {
580608 Q_EMIT touchSceneYChanged(point.y());
581− if (Direction::isVertical(m_direction))
582− Q_EMIT sceneDistanceChanged(sceneDistance());
583609 }
584610}
585611
586−void DirectionalDragArea::updateVelocityCalculator(const QPointF &point)
612+void DirectionalDragArea::updateVelocityCalculator(const QPointF &scenePos)
587613{
588− if (Direction::isHorizontal(m_direction)) {
589− m_velocityCalculator->setTrackedPosition(point.x());
590− } else {
591− m_velocityCalculator->setTrackedPosition(point.y());
592− }
614+ QPointF totalSceneMovement = scenePos - m_startScenePos;
615+
616+ qreal scalarProjection = projectOntoDirectionVector(totalSceneMovement);
617+
618+ m_velocityCalculator->setTrackedPosition(scalarProjection);
593619}
594620
595621bool DirectionalDragArea::isWithinTouchCompositionWindow()
@@ -693,5 +719,39 @@ qint64 DirectionalDragArea::ActiveTouchesInfo::mostRecentStartTime()
693719 return highestStartTime;
694720}
695721
722+void DirectionalDragArea::updateSceneDirectionVector()
723+{
724+ QPointF localOrigin(0., 0.);
725+ QPointF localDirection;
726+ switch (m_direction) {
727+ case Direction::Upwards:
728+ localDirection.rx() = 0.;
729+ localDirection.ry() = -1.;
730+ break;
731+ case Direction::Downwards:
732+ localDirection.rx() = 0.;
733+ localDirection.ry() = 1;
734+ break;
735+ case Direction::Leftwards:
736+ localDirection.rx() = -1.;
737+ localDirection.ry() = 0.;
738+ break;
739+ default: // Direction::Rightwards:
740+ localDirection.rx() = 1.;
741+ localDirection.ry() = 0.;
742+ break;
743+ }
744+ QPointF sceneOrigin = mapToScene(localOrigin);
745+ QPointF sceneDirection = mapToScene(localDirection);
746+ m_sceneDirectionVector = sceneDirection - sceneOrigin;
747+}
748+
749+qreal DirectionalDragArea::projectOntoDirectionVector(const QPointF &sceneVector) const
750+{
751+ // same as dot product as m_sceneDirectionVector is a unit vector
752+ return sceneVector.x() * m_sceneDirectionVector.x() +
753+ sceneVector.y() * m_sceneDirectionVector.y();
754+}
755+
696756// Because we are defining a new QObject-based class (RecognitionTimer) here.
697757#include "DirectionalDragArea.moc"
plugins/Ubuntu/Gestures/DirectionalDragArea.h+11−1
@@ -160,6 +160,7 @@ public:
160160
161161 qreal distance() const;
162162 qreal sceneDistance() const;
163+ void updateSceneDistance();
163164
164165 qreal touchX() const;
165166 qreal touchY() const;
@@ -231,6 +232,10 @@ private:
231232 void setPreviousScenePos(const QPointF &point);
232233 void updateVelocityCalculator(const QPointF &point);
233234 bool isWithinTouchCompositionWindow();
235+ void updateSceneDirectionVector();
236+ // returns the scalar projection between the given vector (in scene coordinates)
237+ // and m_sceneDirectionVector
238+ qreal projectOntoDirectionVector(const QPointF &sceneVector) const;
234239
235240 Status m_status;
236241
@@ -238,6 +243,7 @@ private:
238243 QPointF m_startScenePos;
239244 QPointF m_previousPos;
240245 QPointF m_previousScenePos;
246+ qreal m_sceneDistance;
241247 int m_touchId;
242248
243249 // A movement damper is used in some of the gesture recognition calculations
@@ -245,10 +251,14 @@ private:
245251 DampedPointF m_dampedScenePos;
246252 QPointF m_previousDampedScenePos;
247253
254+ // Unit vector in scene coordinates describing the direction of the gesture recognition
255+ QPointF m_sceneDirectionVector;
256+
248257 Direction::Type m_direction;
249258 qreal m_wideningAngle; // in degrees
250− qreal m_wideningFactor; // it's tan(degreesToRadian(m_wideningAngle))
259+ qreal m_wideningFactor; // it's pow(cosine(m_wideningAngle), 2)
251260 qreal m_distanceThreshold;
261+ qreal m_distanceThresholdSquared; // it's pow(m_distanceThreshold, 2)
252262 qreal m_minSpeed;
253263 int m_maxSilenceTime; // in milliseconds
254264 int m_silenceTime; // in milliseconds
qml/Components/DragHandle.qml+2−1
@@ -167,7 +167,8 @@ EdgeDragArea {
167167 EdgeDragEvaluator {
168168 objectName: "edgeDragEvaluator"
169169 id: dragEvaluator
170− trackedPosition: sceneDistance
170+ // Effectively convert distance into the drag position projected onto the gesture direction axis
171+ trackedPosition: Direction.isPositive(dragArea.direction) ? sceneDistance : -sceneDistance
171172 maxDragDistance: maxTotalDragDistance
172173 direction: dragArea.direction
173174 }
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+108−17
@@ -83,6 +83,9 @@ private Q_SLOTS:
8383 void twoFingerTap();
8484 void movingDDA();
8585 void ignoreOldFinger();
86+ void rotated();
87+ void sceneDistance();
88+ void sceneDistance_data();
8689
8790private:
8891 void passTime(qint64 timeSpan);
@@ -131,18 +134,10 @@ void tst_DirectionalDragArea::passTime(qint64 timeSpan)
131134}
132135
133136namespace {
134−QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
137+QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea)
135138{
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− }
139+ QPointF localCenter(edgeDragArea->width() / 2., edgeDragArea->height() / 2.);
140+ return edgeDragArea->mapToScene(localCenter);
146141}
147142
148143QPointF calculateDirectionVector(DirectionalDragArea *edgeDragArea,
@@ -193,7 +188,7 @@ void tst_DirectionalDragArea::edgeDrag()
193188
194189 QSignalSpy draggingSpy(edgeDragArea, SIGNAL(draggingChanged(bool)));
195190
196− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
191+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
197192 QPointF touchPoint = initialTouchPos;
198193
199194 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*dragDistanceFactor;
@@ -300,7 +295,7 @@ void tst_DirectionalDragArea::dragWithShortDirectionChange()
300295 edgeDragArea->setRecognitionTimer(fakeTimer);
301296 edgeDragArea->setTimeSource(fakeTimeSource);
302297
303− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
298+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
304299 QPointF touchPoint = initialTouchPos;
305300
306301 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0;
@@ -360,7 +355,7 @@ void tst_DirectionalDragArea::minSpeed()
360355
361356 edgeDragArea->setMinSpeed(minSpeed);
362357
363− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
358+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
364359 QPointF touchPoint = initialTouchPos;
365360
366361 QPointF dragDirectionVector(1.0, 0.0);
@@ -408,7 +403,7 @@ void tst_DirectionalDragArea::recognitionTimerUsage()
408403
409404 int timeStepMs = 5; // some arbitrary small value.
410405
411− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
406+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
412407 QPointF touchPoint = initialTouchPos;
413408
414409 QPointF dragDirectionVector(1.0, 0.0);
@@ -452,7 +447,7 @@ void tst_DirectionalDragArea::maxSilenceTime()
452447 // Make sure this property is not disabled
453448 edgeDragArea->setMaxSilenceTime(100);
454449
455− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
450+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
456451 QPointF touchPoint = initialTouchPos;
457452
458453 QTest::touchEvent(m_view, m_device).press(0, touchPoint.toPoint());
@@ -620,7 +615,7 @@ void tst_DirectionalDragArea::movingDDA()
620615 edgeDragArea->setRecognitionTimer(fakeTimer);
621616 edgeDragArea->setTimeSource(fakeTimeSource);
622617
623− QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea, m_view);
618+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
624619 QPointF touchPoint = initialTouchPos;
625620
626621 qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0f;
@@ -704,6 +699,102 @@ void tst_DirectionalDragArea::ignoreOldFinger()
704699 QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::WaitingForTouch);
705700}
706701
702+/*
703+ A Rightwards DDA that is rotated 90 degrees clockwise should recognize gestures
704+ that are done downwards in scene coordinates. I.e. the gesture recognition direction
705+ should be in local coordinates, not scene coordinates.
706+ */
707+void tst_DirectionalDragArea::rotated()
708+{
709+ QQuickItem *baseItem = m_view->rootObject()->findChild<QQuickItem*>("baseItem");
710+ baseItem->setRotation(90.);
711+
712+ QQuickItem *rightwardsLauncher = m_view->rootObject()->findChild<QQuickItem*>("rightwardsLauncher");
713+ Q_ASSERT(rightwardsLauncher != 0);
714+
715+ DirectionalDragArea *edgeDragArea =
716+ rightwardsLauncher->findChild<DirectionalDragArea*>("hpDragArea");
717+ Q_ASSERT(edgeDragArea != 0);
718+ edgeDragArea->setRecognitionTimer(fakeTimer);
719+ edgeDragArea->setTimeSource(fakeTimeSource);
720+
721+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
722+ QPointF touchPoint = initialTouchPos;
723+
724+ qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0f;
725+ QPointF dragDirectionVector(0.0f, 1.0f);
726+
727+ qreal movementStepDistance = edgeDragArea->distanceThreshold() * 0.1f;
728+ QPointF touchMovement = dragDirectionVector * movementStepDistance;
729+ int totalMovementSteps = qCeil(desiredDragDistance / movementStepDistance);
730+ int movementTimeStepMs = (edgeDragArea->compositionTime() * 1.5f) / totalMovementSteps;
731+
732+ QTest::touchEvent(m_view, m_device).press(0, touchPoint.toPoint());
733+
734+ for (int i = 0; i < totalMovementSteps; ++i) {
735+ touchPoint += touchMovement;
736+ passTime(movementTimeStepMs);
737+ QTest::touchEvent(m_view, m_device).move(0, touchPoint.toPoint());
738+ }
739+
740+ QCOMPARE((int)edgeDragArea->status(), (int)DirectionalDragArea::Recognized);
741+
742+ QTest::touchEvent(m_view, m_device).release(0, touchPoint.toPoint());
743+}
744+
745+void tst_DirectionalDragArea::sceneDistance()
746+{
747+ QQuickItem *baseItem = m_view->rootObject()->findChild<QQuickItem*>("baseItem");
748+ QFETCH(qreal, rotation);
749+ QFETCH(QPointF, dragDirectionVector);
750+ baseItem->setRotation(rotation);
751+
752+ QQuickItem *rightwardsLauncher = m_view->rootObject()->findChild<QQuickItem*>("rightwardsLauncher");
753+ Q_ASSERT(rightwardsLauncher != 0);
754+
755+ DirectionalDragArea *edgeDragArea =
756+ rightwardsLauncher->findChild<DirectionalDragArea*>("hpDragArea");
757+ Q_ASSERT(edgeDragArea != 0);
758+ edgeDragArea->setRecognitionTimer(fakeTimer);
759+ edgeDragArea->setTimeSource(fakeTimeSource);
760+
761+ QPointF initialTouchPos = calculateInitialTouchPos(edgeDragArea);
762+ QPointF touchPoint = initialTouchPos;
763+
764+ qreal desiredDragDistance = edgeDragArea->distanceThreshold()*2.0f;
765+
766+ qreal movementStepDistance = edgeDragArea->distanceThreshold() * 0.1f;
767+ QPointF touchMovement = dragDirectionVector * movementStepDistance;
768+ int totalMovementSteps = qCeil(desiredDragDistance / movementStepDistance);
769+ int movementTimeStepMs = (edgeDragArea->compositionTime() * 1.5f) / totalMovementSteps;
770+
771+ QTest::touchEvent(m_view, m_device).press(0, touchPoint.toPoint());
772+
773+ for (int i = 0; i < totalMovementSteps; ++i) {
774+ touchPoint += touchMovement;
775+ passTime(movementTimeStepMs);
776+ QTest::touchEvent(m_view, m_device).move(0, touchPoint.toPoint());
777+ }
778+
779+ qreal actualDragDistance = ((qreal)totalMovementSteps) * movementStepDistance;
780+
781+ // DirectionalDragArea::sceneDistance() must match the actual drag distance as the
782+ // drag was aligned with the gesture direction
783+ // NB: qFuzzyCompare(), used internally by QCOMPARE(), is broken.
784+ QVERIFY(qAbs(edgeDragArea->sceneDistance() - actualDragDistance) < 0.001);
785+
786+ QTest::touchEvent(m_view, m_device).release(0, touchPoint.toPoint());
787+}
788+
789+void tst_DirectionalDragArea::sceneDistance_data()
790+{
791+ QTest::addColumn<qreal>("rotation");
792+ QTest::addColumn<QPointF>("dragDirectionVector");
793+
794+ QTest::newRow("not rotated") << 0. << QPointF(1., 0.);
795+ QTest::newRow("rotated by 90 degrees") << 90. << QPointF(0., 1.);
796+}
797+
707798QTEST_MAIN(tst_DirectionalDragArea)
708799
709800#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}
tests/qmltests/Components/tst_DragHandle.cpp+45−5
@@ -58,7 +58,9 @@ private Q_SLOTS:
5858 void cleanup(); // called right after each and every test function is executed
5959
6060 void dragThreshold_horizontal();
61+ void dragThreshold_horizontal_data();
6162 void dragThreshold_vertical();
63+ void dragThreshold_vertical_data();
6264 void stretch_horizontal();
6365 void stretch_vertical();
6466 void hintingAnimation();
@@ -145,17 +147,29 @@ void tst_DragHandle::tryCompare(std::function<qreal ()> actualFunc,
145147namespace {
146148QPointF calculateDirectionVector(DirectionalDragArea *edgeDragArea)
147149{
148−
150+ QPointF localOrigin(0., 0.);
151+ QPointF localDirection;
149152 switch (edgeDragArea->direction()) {
150153 case Direction::Upwards:
151− return QPointF(0, -1);
154+ localDirection.rx() = 0.;
155+ localDirection.ry() = -1.;
156+ break;
152157 case Direction::Downwards:
153− return QPointF(0, 1);
158+ localDirection.rx() = 0.;
159+ localDirection.ry() = 1;
160+ break;
154161 case Direction::Leftwards:
155− return QPointF(-1, 0);
162+ localDirection.rx() = -1.;
163+ localDirection.ry() = 0.;
164+ break;
156165 default: // Direction::Rightwards:
157− return QPointF(1, 0);
166+ localDirection.rx() = 1.;
167+ localDirection.ry() = 0.;
168+ break;
158169 }
170+ QPointF sceneOrigin = edgeDragArea->mapToScene(localOrigin);
171+ QPointF sceneDirection = edgeDragArea->mapToScene(localDirection);
172+ return sceneDirection - sceneOrigin;
159173}
160174}
161175
@@ -222,6 +236,11 @@ qreal tst_DragHandle::fetchDragThreshold(DirectionalDragArea *dragHandle)
222236 */
223237void tst_DragHandle::dragThreshold_horizontal()
224238{
239+ QFETCH(qreal, rotation);
240+
241+ QQuickItem *baseItem = m_view->rootObject()->findChild<QQuickItem*>("baseItem");
242+ baseItem->setRotation(rotation);
243+
225244 DirectionalDragArea *dragHandle = fetchAndSetupDragHandle("rightwardsDragHandle");
226245
227246 qreal dragThreshold = fetchDragThreshold(dragHandle);
@@ -260,8 +279,21 @@ void tst_DragHandle::dragThreshold_horizontal()
260279 QCOMPARE(parentItem->property("shown").toBool(), false);
261280}
262281
282+void tst_DragHandle::dragThreshold_horizontal_data()
283+{
284+ QTest::addColumn<qreal>("rotation");
285+
286+ QTest::newRow("not rotated") << 0.;
287+ QTest::newRow("rotated 90") << 90.;
288+}
289+
263290void tst_DragHandle::dragThreshold_vertical()
264291{
292+ QFETCH(qreal, rotation);
293+
294+ QQuickItem *baseItem = m_view->rootObject()->findChild<QQuickItem*>("baseItem");
295+ baseItem->setRotation(rotation);
296+
265297 DirectionalDragArea *dragHandle = fetchAndSetupDragHandle("downwardsDragHandle");
266298
267299 qreal dragThreshold = fetchDragThreshold(dragHandle);
@@ -300,6 +332,14 @@ void tst_DragHandle::dragThreshold_vertical()
300332 QCOMPARE(parentItem->property("shown").toBool(), false);
301333}
302334
335+void tst_DragHandle::dragThreshold_vertical_data()
336+{
337+ QTest::addColumn<qreal>("rotation");
338+
339+ QTest::newRow("not rotated") << 0.;
340+ QTest::newRow("rotated 90") << 90.;
341+}
342+
303343/*
304344 Checks that when the stretch property is true, dragging the DragHandle increases
305345 the width or height (depending on its direction) of its parent Showable
tests/qmltests/Components/tst_DragHandle.qml+72−56
@@ -29,62 +29,68 @@ import Ubuntu.Gestures 0.1
2929 */
3030Item {
3131 id: root
32− width: 700
33− height: 500
32+ width: units.gu(70)
33+ height: units.gu(70)
3434
3535 property var dragHandle
3636
3737 property bool stretch: false
3838 property real hintDisplacement: 0
3939
40− VerticalShowable {
41− onDragHandleRecognizedGesture: { root.dragHandle = dragHandle }
42− stretch: root.stretch
43− hintDisplacement: root.hintDisplacement
44− }
45−
46− HorizontalShowable {
47− onDragHandleRecognizedGesture: { root.dragHandle = dragHandle }
48− stretch: root.stretch
49− hintDisplacement: root.hintDisplacement
50− }
40+ Item {
41+ id: baseItem
42+ objectName: "baseItem"
43+ anchors.fill: parent
5144
52− // Visually mark drag threshold
53− Rectangle {
54− color: "black"
55− width: 2
56− height: parent.height
45+ VerticalShowable {
46+ onDragHandleRecognizedGesture: { root.dragHandle = dragHandle }
47+ stretch: root.stretch
48+ hintDisplacement: root.hintDisplacement
49+ }
5750
58− visible: dragHandle !== undefined && Direction.isHorizontal(dragHandle.direction)
51+ HorizontalShowable {
52+ onDragHandleRecognizedGesture: { root.dragHandle = dragHandle }
53+ stretch: root.stretch
54+ hintDisplacement: root.hintDisplacement
55+ }
5956
60− x: {
61− if (dragHandle) {
62− if (dragHandle.direction === Direction.Rightwards) {
63− dragHandle.edgeDragEvaluator.dragThreshold;
57+ // Visually mark drag threshold
58+ Rectangle {
59+ color: "black"
60+ width: 2
61+ height: parent.height
62+
63+ visible: dragHandle !== undefined && Direction.isHorizontal(dragHandle.direction)
64+
65+ x: {
66+ if (dragHandle) {
67+ if (dragHandle.direction === Direction.Rightwards) {
68+ dragHandle.edgeDragEvaluator.dragThreshold;
69+ } else {
70+ parent.width - dragHandle.edgeDragEvaluator.dragThreshold;
71+ }
6472 } else {
65− parent.width - dragHandle.edgeDragEvaluator.dragThreshold;
73+ 0
6674 }
67− } else {
68− 0
6975 }
7076 }
71− }
72− Rectangle {
73− color: "black";
74− height: 2
75− width: parent.width
76−
77− visible: dragHandle !== undefined && Direction.isVertical(dragHandle.direction)
78−
79− y: {
80− if (dragHandle) {
81− if (dragHandle.direction === Direction.Downwards) {
82− dragHandle.edgeDragEvaluator.dragThreshold;
77+ Rectangle {
78+ color: "black";
79+ height: 2
80+ width: parent.width
81+
82+ visible: dragHandle !== undefined && Direction.isVertical(dragHandle.direction)
83+
84+ y: {
85+ if (dragHandle) {
86+ if (dragHandle.direction === Direction.Downwards) {
87+ dragHandle.edgeDragEvaluator.dragThreshold;
88+ } else {
89+ parent.height - dragHandle.edgeDragEvaluator.dragThreshold;
90+ }
8391 } else {
84− parent.height - dragHandle.edgeDragEvaluator.dragThreshold;
92+ 0
8593 }
86− } else {
87− 0
8894 }
8995 }
9096 }
@@ -119,27 +125,37 @@ Item {
119125 }
120126 }
121127
122− TestButton {
123− id: stretchButton
128+ Row {
124129 anchors.bottom: parent.bottom
125130 anchors.left: parent.left
126131 anchors.margins: units.gu(1)
132+ spacing: units.gu(1)
127133
128− text: root.stretch ? "stretch" : "move"
129− onClicked: { root.stretch = !root.stretch; }
130− }
134+ Button {
135+ id: stretchButton
136+ text: root.stretch ? "stretch" : "move"
137+ onClicked: { root.stretch = !root.stretch; }
138+ }
131139
132− TestButton {
133− anchors.bottom: parent.bottom
134− anchors.left: stretchButton.right
135− anchors.margins: units.gu(1)
140+ Button {
141+ text: root.hintDisplacement > 0 ? "hint" : "no hint"
142+ onClicked: {
143+ if (root.hintDisplacement > 0) {
144+ root.hintDisplacement = 0;
145+ } else {
146+ root.hintDisplacement = units.gu(6);
147+ }
148+ }
149+ }
136150
137− text: root.hintDisplacement > 0 ? "hint" : "no hint"
138− onClicked: {
139− if (root.hintDisplacement > 0) {
140− root.hintDisplacement = 0;
141− } else {
142− root.hintDisplacement = units.gu(6);
151+ Button {
152+ text: "rotation: " + baseItem.rotation
153+ onClicked: {
154+ if (baseItem.rotation === 0.0) {
155+ baseItem.rotation = 90.0
156+ } else {
157+ baseItem.rotation = 0.0
158+ }
143159 }
144160 }
145161 }
tests/qmltests/Components/tst_DragHandle/TestButton.qmldeleted−41
@@ -1,41 +0,0 @@
1−/*
2− * Copyright (C) 2013 Canonical, Ltd.
3− *
4− * This program is free software; you can redistribute it and/or modify
5− * it under the terms of the GNU General Public License as published by
6− * the Free Software Foundation; version 3.
7− *
8− * This program is distributed in the hope that it will be useful,
9− * but WITHOUT ANY WARRANTY; without even the implied warranty of
10− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11− * GNU General Public License for more details.
12− *
13− * You should have received a copy of the GNU General Public License
14− * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− */
16−
17−import QtQuick 2.0
18−import Ubuntu.Components 0.1
19−
20−Rectangle {
21− id: rect
22− width: childrenRect.width
23− height: childrenRect.height
24−
25− property alias text: textComponent.text
26−
27− signal clicked
28−
29− color: "yellow"
30− Text {
31− id: textComponent
32− font.pixelSize: units.gu(2)
33− text: root.stretch ? "stretch" : "move"
34− MouseArea {
35− anchors.fill: parent
36− onClicked: {
37− rect.clicked()
38− }
39− }
40− }
41−}