on xenial help sign in
Move Direction enum out of DirectionalDragArea

Besides leading to shorter, more readable, code it will also be used by components other than DirectionalDragArea. Approved by PS Jenkins bot, Michael Zanetti.

commit3b92eca37f9ea343e6f59701ed8e00aefdb5e2b5 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateMon 17 Jun 2013, 16:16 (13 years ago) parent829af88856e0c3
13 files changed+116−55
Components/Stage.qml+1−1 Launcher/Launcher.qml+1−1 plugins/Ubuntu/Gestures/CMakeLists.txt+1 plugins/Ubuntu/Gestures/Direction.cpp+27 plugins/Ubuntu/Gestures/Direction.h+43 plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+16−26 plugins/Ubuntu/Gestures/DirectionalDragArea.h+6−13 plugins/Ubuntu/Gestures/plugin.cpp+8 tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml+1−1 tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml+1−1 tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml+1−1 tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml+1−1 tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp+9−10
Components/Stage.qml+1−1
@@ -438,7 +438,7 @@ Showable {
438438 anchors.bottom: parent.bottom
439439 anchors.right: parent.right
440440
441− direction: DirectionalDragArea.Leftwards
441+ direction: Direction.Leftwards
442442 maxDeviation: units.gu(1)
443443 wideningAngle: 20
444444 distanceThreshold: units.gu(3)
Launcher/Launcher.qml+1−1
@@ -184,7 +184,7 @@ Item {
184184 DirectionalDragArea {
185185 id: dragArea
186186
187− direction: DirectionalDragArea.Rightwards
187+ direction: Direction.Rightwards
188188
189189 // values to be tweaked and later removed from here and set in stone as defaults
190190 // once we are confident it's all good.
plugins/Ubuntu/Gestures/CMakeLists.txt+1
@@ -7,6 +7,7 @@ find_package(Qt5Quick REQUIRED)
77set(UbuntuGestureQml_SOURCES
88 plugin.cpp
99 AxisVelocityCalculator.cpp
10+ Direction.cpp
1011 DirectionalDragArea.cpp
1112)
1213
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)
4141 : QQuickItem(parent)
4242 , m_status(WaitingForTouch)
4343 , m_touchId(-1)
44− , m_direction(DirectionalDragArea::Rightwards)
44+ , m_direction(Direction::Rightwards)
4545 , m_wideningAngle(0)
4646 , m_wideningFactor(0)
4747 , m_distanceThreshold(0)
@@ -58,12 +58,12 @@ DirectionalDragArea::DirectionalDragArea(QQuickItem *parent)
5858 m_velocityCalculator = new AxisVelocityCalculator(this);
5959}
6060
61−DirectionalDragArea::Direction DirectionalDragArea::direction() const
61+Direction::Type DirectionalDragArea::direction() const
6262{
6363 return m_direction;
6464}
6565
66−void DirectionalDragArea::setDirection(DirectionalDragArea::Direction direction)
66+void DirectionalDragArea::setDirection(Direction::Type direction)
6767{
6868 if (direction != m_direction) {
6969 m_direction = direction;
@@ -151,7 +151,7 @@ void DirectionalDragArea::setAxisVelocityCalculator(AxisVelocityCalculator *newV
151151
152152qreal DirectionalDragArea::distance() const
153153{
154− if (directionIsHorizontal()) {
154+ if (Direction::isHorizontal(m_direction)) {
155155 return m_previousPos.x() - m_startPos.x();
156156 } else {
157157 return m_previousPos.y() - m_startPos.y();
@@ -286,13 +286,13 @@ bool DirectionalDragArea::pointInsideAllowedArea() const
286286 qreal dY = m_dampedPos.y() - m_startPos.y();
287287
288288 switch (m_direction) {
289− case Upwards:
289+ case Direction::Upwards:
290290 return dY <= 0 && qFabs(dX) <= qFabs(dY) * m_wideningFactor;
291− case Downwards:
291+ case Direction::Downwards:
292292 return dY >= 0 && qFabs(dX) <= dY * m_wideningFactor;
293− case Leftwards:
293+ case Direction::Leftwards:
294294 return dX <= 0 && qFabs(dY) <= qFabs(dX) * m_wideningFactor;
295− default: // Rightwards:
295+ default: // Direction::Rightwards:
296296 return dX >= 0 && qFabs(dY) <= dX * m_wideningFactor;
297297 }
298298}
@@ -300,20 +300,20 @@ bool DirectionalDragArea::pointInsideAllowedArea() const
300300bool DirectionalDragArea::movingInRightDirection() const
301301{
302302 switch (m_direction) {
303− case Upwards:
303+ case Direction::Upwards:
304304 return m_dampedPos.y() <= m_previousDampedPos.y();
305− case Downwards:
305+ case Direction::Downwards:
306306 return m_dampedPos.y() >= m_previousDampedPos.y();
307− case Leftwards:
307+ case Direction::Leftwards:
308308 return m_dampedPos.x() <= m_previousDampedPos.x();
309− default: // Rightwards:
309+ default: // Direction::Rightwards:
310310 return m_dampedPos.x() >= m_previousDampedPos.x();
311311 }
312312}
313313
314314bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
315315{
316− if (directionIsHorizontal())
316+ if (Direction::isHorizontal(m_direction))
317317 return qFabs(point.x() - m_startPos.x()) > m_distanceThreshold;
318318 else
319319 return qFabs(point.y() - m_startPos.y()) > m_distanceThreshold;
@@ -386,35 +386,25 @@ void DirectionalDragArea::setPreviousPos(QPointF point)
386386
387387 if (xChanged) {
388388 Q_EMIT touchXChanged(point.x());
389− if (directionIsHorizontal())
389+ if (Direction::isHorizontal(m_direction))
390390 Q_EMIT distanceChanged(distance());
391391 }
392392
393393 if (yChanged) {
394394 Q_EMIT touchYChanged(point.y());
395− if (directionIsVertical())
395+ if (Direction::isVertical(m_direction))
396396 Q_EMIT distanceChanged(distance());
397397 }
398398}
399399
400400void DirectionalDragArea::updateVelocityCalculator(QPointF point)
401401{
402− if (directionIsHorizontal()) {
402+ if (Direction::isHorizontal(m_direction)) {
403403 m_velocityCalculator->setTrackedPosition(point.x());
404404 } else {
405405 m_velocityCalculator->setTrackedPosition(point.y());
406406 }
407407}
408408
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−
419409// Because we are defining a new QObject-based class (RecognitionTimer) here.
420410#include "DirectionalDragArea.moc"
plugins/Ubuntu/Gestures/DirectionalDragArea.h+6−13
@@ -21,6 +21,7 @@
2121#include "AxisVelocityCalculator.h"
2222#include "UbuntuGesturesGlobal.h"
2323#include "Damper.h"
24+#include "Direction.h"
2425
2526namespace UbuntuGestures {
2627/* Defines an interface for a Timer. */
@@ -53,7 +54,7 @@ class UBUNTUGESTURES_EXPORT DirectionalDragArea : public QQuickItem {
5354 Q_OBJECT
5455
5556 // 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)
5758
5859 // The distance travelled by the finger along the axis specified by
5960 // DirectionalDragArea's direction.
@@ -116,12 +117,8 @@ class UBUNTUGESTURES_EXPORT DirectionalDragArea : public QQuickItem {
116117public:
117118 DirectionalDragArea(QQuickItem *parent = 0);
118119
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;
124− void setDirection(Direction);
120+ Direction::Type direction() const;
121+ void setDirection(Direction::Type);
125122
126123 // Describes the state of the directional drag gesture.
127124 enum Status {
@@ -179,7 +176,7 @@ public:
179176 void setAxisVelocityCalculator(AxisVelocityCalculator *velCalc);
180177
181178Q_SIGNALS:
182− void directionChanged(Direction direction);
179+ void directionChanged(Direction::Type direction);
183180 void statusChanged(Status value);
184181 void draggingChanged(bool value);
185182 void distanceChanged(qreal value);
@@ -210,10 +207,6 @@ private:
210207 void setPreviousPos(QPointF point);
211208 void updateVelocityCalculator(QPointF point);
212209
213− // convenience functions
214− bool directionIsHorizontal() const;
215− bool directionIsVertical() const;
216−
217210 Status m_status;
218211
219212 QPointF m_startPos;
@@ -225,7 +218,7 @@ private:
225218 DampedPointF m_dampedPos;
226219 QPointF m_previousDampedPos;
227220
228− Direction m_direction;
221+ Direction::Type m_direction;
229222 qreal m_wideningAngle; // in degrees
230223 qreal m_wideningFactor; // it's tan(degreesToRadian(m_wideningAngle))
231224 qreal m_distanceThreshold;
plugins/Ubuntu/Gestures/plugin.cpp+8
@@ -16,12 +16,20 @@
1616
1717#include "plugin.h"
1818#include "AxisVelocityCalculator.h"
19+#include "Direction.h"
1920#include "DirectionalDragArea.h"
2021
2122#include <qqml.h>
2223
24+static QObject* directionSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
25+ Q_UNUSED(engine);
26+ Q_UNUSED(scriptEngine);
27+ return new Direction;
28+}
29+
2330void UbuntuGestureQmlPlugin::registerTypes(const char *uri)
2431{
32+ qmlRegisterSingletonType<Direction>(uri, 0, 1, "Direction", directionSingleton);
2533 qmlRegisterType<DirectionalDragArea>(uri, 0, 1, "DirectionalDragArea");
2634 qmlRegisterType<AxisVelocityCalculator>(uri, 0, 1, "AxisVelocityCalculator");
2735}
tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml+1−1
@@ -51,7 +51,7 @@ Item {
5151
5252 height: units.gu(5)
5353
54− direction: DirectionalDragArea.Downwards
54+ direction: Direction.Downwards
5555 maxDeviation: units.gu(2)
5656 wideningAngle: 10
5757 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml+1−1
@@ -54,7 +54,7 @@ Item {
5454
5555 width: units.gu(5)
5656
57− direction: DirectionalDragArea.Leftwards
57+ direction: Direction.Leftwards
5858 maxDeviation: units.gu(2)
5959 wideningAngle: 10
6060 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml+1−1
@@ -50,7 +50,7 @@ Item {
5050
5151 width: units.gu(5)
5252
53− direction: DirectionalDragArea.Rightwards
53+ direction: Direction.Rightwards
5454 maxDeviation: units.gu(2)
5555 wideningAngle: 10
5656 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml+1−1
@@ -54,7 +54,7 @@ Item {
5454
5555 height: units.gu(5)
5656
57− direction: DirectionalDragArea.Upwards
57+ direction: Direction.Upwards
5858 maxDeviation: units.gu(2)
5959 wideningAngle: 10
6060 distanceThreshold: units.gu(4)
tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp+9−10
@@ -132,13 +132,13 @@ namespace {
132132QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
133133{
134134 switch (edgeDragArea->direction()) {
135− case DirectionalDragArea::Upwards:
135+ case Direction::Upwards:
136136 return QPointF(view->width()/2.0f, view->height() - (edgeDragArea->height()/2.0f));
137− case DirectionalDragArea::Downwards:
137+ case Direction::Downwards:
138138 return QPointF(view->width()/2.0f, edgeDragArea->height()/2.0f);
139− case DirectionalDragArea::Leftwards:
139+ case Direction::Leftwards:
140140 return QPointF(view->width() - (edgeDragArea->width()/2.0f), view->height()/2.0f);
141− default: // DirectionalDragArea::Rightwards:
141+ default: // Direction::Rightwards:
142142 return QPointF(edgeDragArea->width()/2.0f, view->height()/2.0f);
143143 }
144144}
@@ -153,13 +153,13 @@ QPointF calculateDirectionVector(DirectionalDragArea *edgeDragArea,
153153 qreal angleSin = qSin(angleRadians);
154154
155155 switch (edgeDragArea->direction()) {
156− case DirectionalDragArea::Upwards:
156+ case Direction::Upwards:
157157 return QPointF(angleSin, -angleCos);
158− case DirectionalDragArea::Downwards:
158+ case Direction::Downwards:
159159 return QPointF(angleSin, angleCos);
160− case DirectionalDragArea::Leftwards:
160+ case Direction::Leftwards:
161161 return QPointF(-angleCos, angleSin);
162− default: // DirectionalDragArea::Rightwards:
162+ default: // Direction::Rightwards:
163163 return QPointF(angleCos, angleSin);
164164 }
165165}
@@ -168,8 +168,7 @@ QPointF createTouchDeviation(DirectionalDragArea *edgeDragArea)
168168{
169169 qreal deviation = edgeDragArea->maxDeviation() * 0.8;
170170
171− if (edgeDragArea->direction() == DirectionalDragArea::Leftwards
172− || edgeDragArea->direction() == DirectionalDragArea::Rightwards) {
171+ if (Direction::isHorizontal(edgeDragArea->direction())) {
173172 return QPointF(0, deviation);
174173 } else {
175174 return QPointF(deviation, 0);