on xenial help sign in
Move Direction enum out of DirectionalDragArea
3b92ecaDaniel d'Andrada
Components/Stage.qml+1−1
@@ -438,7 +438,7 @@ Showable {
438 anchors.bottom: parent.bottom
438 anchors.bottom: parent.bottom
439 anchors.right: parent.right
439 anchors.right: parent.right
440
440
441− direction: DirectionalDragArea.Leftwards
441+ direction: Direction.Leftwards
442 maxDeviation: units.gu(1)
442 maxDeviation: units.gu(1)
443 wideningAngle: 20
443 wideningAngle: 20
444 distanceThreshold: units.gu(3)
444 distanceThreshold: units.gu(3)
Launcher/Launcher.qml+1−1
@@ -184,7 +184,7 @@ Item {
184 DirectionalDragArea {
184 DirectionalDragArea {
185 id: dragArea
185 id: dragArea
186
186
187− direction: DirectionalDragArea.Rightwards
187+ direction: Direction.Rightwards
188
188
189 // values to be tweaked and later removed from here and set in stone as defaults
189 // values to be tweaked and later removed from here and set in stone as defaults
190 // once we are confident it's all good.
190 // once we are confident it's all good.
plugins/Ubuntu/Gestures/CMakeLists.txt+1
@@ -7,6 +7,7 @@ find_package(Qt5Quick REQUIRED)
7set(UbuntuGestureQml_SOURCES
7set(UbuntuGestureQml_SOURCES
8 plugin.cpp
8 plugin.cpp
9 AxisVelocityCalculator.cpp
9 AxisVelocityCalculator.cpp
10+ Direction.cpp
10 DirectionalDragArea.cpp
11 DirectionalDragArea.cpp
11)
12)
12
13
plugins/Ubuntu/Gestures/Direction.cppnew file+27
@@ -0,0 +1,27 @@
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+#include "Direction.h"
18+
19+bool Direction::isHorizontal(Direction::Type type)
20+{
21+ return type == Direction::Leftwards || type == Direction::Rightwards;
22+}
23+
24+bool Direction::isVertical(Direction::Type type)
25+{
26+ return type == Direction::Upwards || type == Direction::Downwards;
27+}
plugins/Ubuntu/Gestures/Direction.hnew file+43
@@ -0,0 +1,43 @@
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+#ifndef DIRECTION_H
18+#define DIRECTION_H
19+
20+#include "UbuntuGesturesGlobal.h"
21+#include <QObject>
22+
23+/*
24+ A Direction enum wrapper so that we can do things like "direction: Direction.Righwards"
25+ from QML.
26+ */
27+class UBUNTUGESTURES_EXPORT Direction : public QObject {
28+ Q_OBJECT
29+ Q_ENUMS(Type)
30+
31+public:
32+ enum Type {
33+ Rightwards, // Along the positive direction of the X axis
34+ Leftwards, // Along the negative direction of the X axis
35+ Downwards, // Along the positive direction of the Y axis
36+ Upwards // Along the negative direction of the Y axis
37+ };
38+
39+ Q_INVOKABLE static bool isHorizontal(Direction::Type type);
40+ Q_INVOKABLE static bool isVertical(Direction::Type type);
41+};
42+
43+#endif // DIRECTION_H
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+16−26
@@ -41,7 +41,7 @@ DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
41 : QQuickItem(parent)
41 : QQuickItem(parent)
42 , m_status(WaitingForTouch)
42 , m_status(WaitingForTouch)
43 , m_touchId(-1)
43 , m_touchId(-1)
44− , m_direction(DirectionalDragArea::Rightwards)
44+ , m_direction(Direction::Rightwards)
45 , m_wideningAngle(0)
45 , m_wideningAngle(0)
46 , m_wideningFactor(0)
46 , m_wideningFactor(0)
47 , m_distanceThreshold(0)
47 , m_distanceThreshold(0)
@@ -58,12 +58,12 @@ DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
58 m_velocityCalculator = new AxisVelocityCalculator(this);
58 m_velocityCalculator = new AxisVelocityCalculator(this);
59}
59}
60
60
61−DirectionalDragArea::Direction DirectionalDragArea::direction() const
61+Direction::Type DirectionalDragArea::direction() const
62{
62{
63 return m_direction;
63 return m_direction;
64}
64}
65
65
66−void DirectionalDragArea::setDirection(DirectionalDragArea::Direction direction)
66+void DirectionalDragArea::setDirection(Direction::Type direction)
67{
67{
68 if (direction != m_direction) {
68 if (direction != m_direction) {
69 m_direction = direction;
69 m_direction = direction;
@@ -151,7 +151,7 @@ void DirectionalDragArea::setAxisVelocityCalculator(AxisVelocityCalculator *newV
151
151
152qreal DirectionalDragArea::distance() const
152qreal DirectionalDragArea::distance() const
153{
153{
154− if (directionIsHorizontal()) {
154+ if (Direction::isHorizontal(m_direction)) {
155 return m_previousPos.x() - m_startPos.x();
155 return m_previousPos.x() - m_startPos.x();
156 } else {
156 } else {
157 return m_previousPos.y() - m_startPos.y();
157 return m_previousPos.y() - m_startPos.y();
@@ -286,13 +286,13 @@ bool DirectionalDragArea::pointInsideAllowedArea() const
286 qreal dY = m_dampedPos.y() - m_startPos.y();
286 qreal dY = m_dampedPos.y() - m_startPos.y();
287
287
288 switch (m_direction) {
288 switch (m_direction) {
289− case Upwards:
289+ case Direction::Upwards:
290 return dY <= 0 && qFabs(dX) <= qFabs(dY) * m_wideningFactor;
290 return dY <= 0 && qFabs(dX) <= qFabs(dY) * m_wideningFactor;
291− case Downwards:
291+ case Direction::Downwards:
292 return dY >= 0 && qFabs(dX) <= dY * m_wideningFactor;
292 return dY >= 0 && qFabs(dX) <= dY * m_wideningFactor;
293− case Leftwards:
293+ case Direction::Leftwards:
294 return dX <= 0 && qFabs(dY) <= qFabs(dX) * m_wideningFactor;
294 return dX <= 0 && qFabs(dY) <= qFabs(dX) * m_wideningFactor;
295− default: // Rightwards:
295+ default: // Direction::Rightwards:
296 return dX >= 0 && qFabs(dY) <= dX * m_wideningFactor;
296 return dX >= 0 && qFabs(dY) <= dX * m_wideningFactor;
297 }
297 }
298}
298}
@@ -300,20 +300,20 @@ bool DirectionalDragArea::pointInsideAllowedArea() const
300bool DirectionalDragArea::movingInRightDirection() const
300bool DirectionalDragArea::movingInRightDirection() const
301{
301{
302 switch (m_direction) {
302 switch (m_direction) {
303− case Upwards:
303+ case Direction::Upwards:
304 return m_dampedPos.y() <= m_previousDampedPos.y();
304 return m_dampedPos.y() <= m_previousDampedPos.y();
305− case Downwards:
305+ case Direction::Downwards:
306 return m_dampedPos.y() >= m_previousDampedPos.y();
306 return m_dampedPos.y() >= m_previousDampedPos.y();
307− case Leftwards:
307+ case Direction::Leftwards:
308 return m_dampedPos.x() <= m_previousDampedPos.x();
308 return m_dampedPos.x() <= m_previousDampedPos.x();
309− default: // Rightwards:
309+ default: // Direction::Rightwards:
310 return m_dampedPos.x() >= m_previousDampedPos.x();
310 return m_dampedPos.x() >= m_previousDampedPos.x();
311 }
311 }
312}
312}
313
313
314bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
314bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
315{
315{
316− if (directionIsHorizontal())
316+ if (Direction::isHorizontal(m_direction))
317 return qFabs(point.x() - m_startPos.x()) > m_distanceThreshold;
317 return qFabs(point.x() - m_startPos.x()) > m_distanceThreshold;
318 else
318 else
319 return qFabs(point.y() - m_startPos.y()) > m_distanceThreshold;
319 return qFabs(point.y() - m_startPos.y()) > m_distanceThreshold;
@@ -386,35 +386,25 @@ void DirectionalDragArea::setPreviousPos(QPointF point)
386
386
387 if (xChanged) {
387 if (xChanged) {
388 Q_EMIT touchXChanged(point.x());
388 Q_EMIT touchXChanged(point.x());
389− if (directionIsHorizontal())
389+ if (Direction::isHorizontal(m_direction))
390 Q_EMIT distanceChanged(distance());
390 Q_EMIT distanceChanged(distance());
391 }
391 }
392
392
393 if (yChanged) {
393 if (yChanged) {
394 Q_EMIT touchYChanged(point.y());
394 Q_EMIT touchYChanged(point.y());
395− if (directionIsVertical())
395+ if (Direction::isVertical(m_direction))
396 Q_EMIT distanceChanged(distance());
396 Q_EMIT distanceChanged(distance());
397 }
397 }
398}
398}
399
399
400void DirectionalDragArea::updateVelocityCalculator(QPointF point)
400void DirectionalDragArea::updateVelocityCalculator(QPointF point)
401{
401{
402− if (directionIsHorizontal()) {
402+ if (Direction::isHorizontal(m_direction)) {
403 m_velocityCalculator->setTrackedPosition(point.x());
403 m_velocityCalculator->setTrackedPosition(point.x());
404 } else {
404 } else {
405 m_velocityCalculator->setTrackedPosition(point.y());
405 m_velocityCalculator->setTrackedPosition(point.y());
406 }
406 }
407}
407}
408
408
409−bool DirectionalDragArea::directionIsHorizontal() const
410−{
411− return m_direction == Leftwards || m_direction == Rightwards;
412−}
413−
414−bool DirectionalDragArea::directionIsVertical() const
415−{
416− return m_direction == Upwards || m_direction == Downwards;
417−}
418−
419// Because we are defining a new QObject-based class (RecognitionTimer) here.
409// Because we are defining a new QObject-based class (RecognitionTimer) here.
420#include "DirectionalDragArea.moc"
410#include "DirectionalDragArea.moc"
plugins/Ubuntu/Gestures/DirectionalDragArea.h+6−13
@@ -21,6 +21,7 @@
21#include "AxisVelocityCalculator.h"
21#include "AxisVelocityCalculator.h"
22#include "UbuntuGesturesGlobal.h"
22#include "UbuntuGesturesGlobal.h"
23#include "Damper.h"
23#include "Damper.h"
24+#include "Direction.h"
24
25
25namespace UbuntuGestures {
26namespace UbuntuGestures {
26/* Defines an interface for a Timer. */
27/* Defines an interface for a Timer. */
@@ -53,7 +54,7 @@ class UBUNTUGESTURES_EXPORT DirectionalDragArea : public QQuickItem {
53 Q_OBJECT
54 Q_OBJECT
54
55
55 // The direction in which the gesture should move in order to be recognized.
56 // The direction in which the gesture should move in order to be recognized.
56− Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
57+ Q_PROPERTY(Direction::Type direction READ direction WRITE setDirection NOTIFY directionChanged)
57
58
58 // The distance travelled by the finger along the axis specified by
59 // The distance travelled by the finger along the axis specified by
59 // DirectionalDragArea's direction.
60 // DirectionalDragArea's direction.
@@ -116,12 +117,8 @@ class UBUNTUGESTURES_EXPORT DirectionalDragArea : public QQuickItem {
116public:
117public:
117 DirectionalDragArea(QQuickItem *parent = 0);
118 DirectionalDragArea(QQuickItem *parent = 0);
118
119
119− enum Direction { Rightwards, // Along the positive direction of the X axis
120− Leftwards, // Along the negative direction of the X axis
121− Downwards, // Along the positive direction of the Y axis
122− Upwards }; // Along the negative direction of the Y axis
123− Direction direction() const;
120+ Direction::Type direction() const;
124− void setDirection(Direction);
121+ void setDirection(Direction::Type);
125
122
126 // Describes the state of the directional drag gesture.
123 // Describes the state of the directional drag gesture.
127 enum Status {
124 enum Status {
@@ -179,7 +176,7 @@ public:
179 void setAxisVelocityCalculator(AxisVelocityCalculator *velCalc);
176 void setAxisVelocityCalculator(AxisVelocityCalculator *velCalc);
180
177
181Q_SIGNALS:
178Q_SIGNALS:
182− void directionChanged(Direction direction);
179+ void directionChanged(Direction::Type direction);
183 void statusChanged(Status value);
180 void statusChanged(Status value);
184 void draggingChanged(bool value);
181 void draggingChanged(bool value);
185 void distanceChanged(qreal value);
182 void distanceChanged(qreal value);
@@ -210,10 +207,6 @@ private:
210 void setPreviousPos(QPointF point);
207 void setPreviousPos(QPointF point);
211 void updateVelocityCalculator(QPointF point);
208 void updateVelocityCalculator(QPointF point);
212
209
213− // convenience functions
214− bool directionIsHorizontal() const;
215− bool directionIsVertical() const;
216−
217 Status m_status;
210 Status m_status;
218
211
219 QPointF m_startPos;
212 QPointF m_startPos;
@@ -225,7 +218,7 @@ private:
225 DampedPointF m_dampedPos;
218 DampedPointF m_dampedPos;
226 QPointF m_previousDampedPos;
219 QPointF m_previousDampedPos;
227
220
228− Direction m_direction;
221+ Direction::Type m_direction;
229 qreal m_wideningAngle; // in degrees
222 qreal m_wideningAngle; // in degrees
230 qreal m_wideningFactor; // it's tan(degreesToRadian(m_wideningAngle))
223 qreal m_wideningFactor; // it's tan(degreesToRadian(m_wideningAngle))
231 qreal m_distanceThreshold;
224 qreal m_distanceThreshold;
plugins/Ubuntu/Gestures/plugin.cpp+8
@@ -16,12 +16,20 @@
16
16
17#include "plugin.h"
17#include "plugin.h"
18#include "AxisVelocityCalculator.h"
18#include "AxisVelocityCalculator.h"
19+#include "Direction.h"
19#include "DirectionalDragArea.h"
20#include "DirectionalDragArea.h"
20
21
21#include <qqml.h>
22#include <qqml.h>
22
23
24+static QObject* directionSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
25+ Q_UNUSED(engine);
26+ Q_UNUSED(scriptEngine);
27+ return new Direction;
28+}
29+
23void UbuntuGestureQmlPlugin::registerTypes(const char *uri)
30void UbuntuGestureQmlPlugin::registerTypes(const char *uri)
24{
31{
32+ qmlRegisterSingletonType<Direction>(uri, 0, 1, "Direction", directionSingleton);
25 qmlRegisterType<DirectionalDragArea>(uri, 0, 1, "DirectionalDragArea");
33 qmlRegisterType<DirectionalDragArea>(uri, 0, 1, "DirectionalDragArea");
26 qmlRegisterType<AxisVelocityCalculator>(uri, 0, 1, "AxisVelocityCalculator");
34 qmlRegisterType<AxisVelocityCalculator>(uri, 0, 1, "AxisVelocityCalculator");
27}
35}
tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml+1−1
@@ -51,7 +51,7 @@ Item {
51
51
52 height: units.gu(5)
52 height: units.gu(5)
53
53
54− direction: DirectionalDragArea.Downwards
54+ direction: Direction.Downwards
55 maxDeviation: units.gu(2)
55 maxDeviation: units.gu(2)
56 wideningAngle: 10
56 wideningAngle: 10
57 distanceThreshold: units.gu(4)
57 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml+1−1
@@ -54,7 +54,7 @@ Item {
54
54
55 width: units.gu(5)
55 width: units.gu(5)
56
56
57− direction: DirectionalDragArea.Leftwards
57+ direction: Direction.Leftwards
58 maxDeviation: units.gu(2)
58 maxDeviation: units.gu(2)
59 wideningAngle: 10
59 wideningAngle: 10
60 distanceThreshold: units.gu(4)
60 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml+1−1
@@ -50,7 +50,7 @@ Item {
50
50
51 width: units.gu(5)
51 width: units.gu(5)
52
52
53− direction: DirectionalDragArea.Rightwards
53+ direction: Direction.Rightwards
54 maxDeviation: units.gu(2)
54 maxDeviation: units.gu(2)
55 wideningAngle: 10
55 wideningAngle: 10
56 distanceThreshold: units.gu(4)
56 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml+1−1
@@ -54,7 +54,7 @@ Item {
54
54
55 height: units.gu(5)
55 height: units.gu(5)
56
56
57− direction: DirectionalDragArea.Upwards
57+ direction: Direction.Upwards
58 maxDeviation: units.gu(2)
58 maxDeviation: units.gu(2)
59 wideningAngle: 10
59 wideningAngle: 10
60 distanceThreshold: units.gu(4)
60 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp+9−10
@@ -132,13 +132,13 @@ namespace {
132QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
132QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
133{
133{
134 switch (edgeDragArea->direction()) {
134 switch (edgeDragArea->direction()) {
135− case DirectionalDragArea::Upwards:
135+ case Direction::Upwards:
136 return QPointF(view->width()/2.0f, view->height() - (edgeDragArea->height()/2.0f));
136 return QPointF(view->width()/2.0f, view->height() - (edgeDragArea->height()/2.0f));
137− case DirectionalDragArea::Downwards:
137+ case Direction::Downwards:
138 return QPointF(view->width()/2.0f, edgeDragArea->height()/2.0f);
138 return QPointF(view->width()/2.0f, edgeDragArea->height()/2.0f);
139− case DirectionalDragArea::Leftwards:
139+ case Direction::Leftwards:
140 return QPointF(view->width() - (edgeDragArea->width()/2.0f), view->height()/2.0f);
140 return QPointF(view->width() - (edgeDragArea->width()/2.0f), view->height()/2.0f);
141− default: // DirectionalDragArea::Rightwards:
141+ default: // Direction::Rightwards:
142 return QPointF(edgeDragArea->width()/2.0f, view->height()/2.0f);
142 return QPointF(edgeDragArea->width()/2.0f, view->height()/2.0f);
143 }
143 }
144}
144}
@@ -153,13 +153,13 @@ QPointF calculateDirectionVector(DirectionalDragArea *edgeDragArea,
153 qreal angleSin = qSin(angleRadians);
153 qreal angleSin = qSin(angleRadians);
154
154
155 switch (edgeDragArea->direction()) {
155 switch (edgeDragArea->direction()) {
156− case DirectionalDragArea::Upwards:
156+ case Direction::Upwards:
157 return QPointF(angleSin, -angleCos);
157 return QPointF(angleSin, -angleCos);
158− case DirectionalDragArea::Downwards:
158+ case Direction::Downwards:
159 return QPointF(angleSin, angleCos);
159 return QPointF(angleSin, angleCos);
160− case DirectionalDragArea::Leftwards:
160+ case Direction::Leftwards:
161 return QPointF(-angleCos, angleSin);
161 return QPointF(-angleCos, angleSin);
162− default: // DirectionalDragArea::Rightwards:
162+ default: // Direction::Rightwards:
163 return QPointF(angleCos, angleSin);
163 return QPointF(angleCos, angleSin);
164 }
164 }
165}
165}
@@ -168,8 +168,7 @@ QPointF createTouchDeviation(DirectionalDragArea *edgeDragArea)
168{
168{
169 qreal deviation = edgeDragArea->maxDeviation() * 0.8;
169 qreal deviation = edgeDragArea->maxDeviation() * 0.8;
170
170
171− if (edgeDragArea->direction() == DirectionalDragArea::Leftwards
171+ if (Direction::isHorizontal(edgeDragArea->direction())) {
172− || edgeDragArea->direction() == DirectionalDragArea::Rightwards) {
173 return QPointF(0, deviation);
172 return QPointF(0, deviation);
174 } else {
173 } else {
175 return QPointF(deviation, 0);
174 return QPointF(deviation, 0);