Merge lp:~dandrader/unity/8_directionEnum into lp:unity/8.0

Proposed by Daniel d'Andrada
Status: Merged
Approved by: Daniel d'Andrada
Approved revision: no longer in the source branch.
Merged at revision: 24
Proposed branch: lp:~dandrader/unity/8_directionEnum
Merge into: lp:unity/8.0
Diff against target: 429 lines (+116/-55)
13 files modified
Components/Stage.qml (+1/-1)
Launcher/Launcher.qml (+1/-1)
plugins/Ubuntu/Gestures/CMakeLists.txt (+1/-0)
plugins/Ubuntu/Gestures/Direction.cpp (+27/-0)
plugins/Ubuntu/Gestures/Direction.h (+43/-0)
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp (+16/-26)
plugins/Ubuntu/Gestures/DirectionalDragArea.h (+6/-13)
plugins/Ubuntu/Gestures/plugin.cpp (+8/-0)
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)
To merge this branch: bzr merge lp:~dandrader/unity/8_directionEnum
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Michael Zanetti (community) Approve
Review via email: mp+169472@code.launchpad.net

Commit message

Move Direction enum out of DirectionalDragArea

Besides leading to shorter, more readable, code it will also be used
by components other than DirectionalDragArea.

Description of the change

Move Direction enum out of DirectionalDragArea

Besides leading to shorter, more readable, code it will also be used by components other than DirectionalDragArea.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Michael Zanetti (mzanetti) wrote :

Looks mostly good. One thought tho:

In QML you can use Direction.Leftwards for example. However, the static functions isHorizontal() and isVertical() are not accessible from QML.

One idea would be to register the Direction class as a singleton (qmlRegisterSingleton()) and add those two functions as public Q_INVOKABLE ones.

Not sure if that is needed tho... Whats your opinion?

review: Needs Information
Revision history for this message
Daniel d'Andrada (dandrader) wrote :

> Looks mostly good. One thought tho:
>
> In QML you can use Direction.Leftwards for example. However, the static
> functions isHorizontal() and isVertical() are not accessible from QML.
>
> One idea would be to register the Direction class as a singleton
> (qmlRegisterSingleton()) and add those two functions as public Q_INVOKABLE
> ones.
>
> Not sure if that is needed tho... Whats your opinion?

Yes, making it a singleton was needed.
Fixed.

Revision history for this message
Michael Zanetti (mzanetti) wrote :

Q_INVOKABLE static bool isVertical(Direction::Type type);

This looks quite wrong... But ok, as long as it works and leads to the wanted results.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Components/Stage.qml'
2--- Components/Stage.qml 2013-06-07 18:01:50 +0000
3+++ Components/Stage.qml 2013-06-17 13:47:30 +0000
4@@ -438,7 +438,7 @@
5 anchors.bottom: parent.bottom
6 anchors.right: parent.right
7
8- direction: DirectionalDragArea.Leftwards
9+ direction: Direction.Leftwards
10 maxDeviation: units.gu(1)
11 wideningAngle: 20
12 distanceThreshold: units.gu(3)
13
14=== modified file 'Launcher/Launcher.qml'
15--- Launcher/Launcher.qml 2013-06-07 18:01:50 +0000
16+++ Launcher/Launcher.qml 2013-06-17 13:47:30 +0000
17@@ -184,7 +184,7 @@
18 DirectionalDragArea {
19 id: dragArea
20
21- direction: DirectionalDragArea.Rightwards
22+ direction: Direction.Rightwards
23
24 // values to be tweaked and later removed from here and set in stone as defaults
25 // once we are confident it's all good.
26
27=== modified file 'plugins/Ubuntu/Gestures/CMakeLists.txt'
28--- plugins/Ubuntu/Gestures/CMakeLists.txt 2013-06-07 18:01:50 +0000
29+++ plugins/Ubuntu/Gestures/CMakeLists.txt 2013-06-17 13:47:30 +0000
30@@ -7,6 +7,7 @@
31 set(UbuntuGestureQml_SOURCES
32 plugin.cpp
33 AxisVelocityCalculator.cpp
34+ Direction.cpp
35 DirectionalDragArea.cpp
36 )
37
38
39=== added file 'plugins/Ubuntu/Gestures/Direction.cpp'
40--- plugins/Ubuntu/Gestures/Direction.cpp 1970-01-01 00:00:00 +0000
41+++ plugins/Ubuntu/Gestures/Direction.cpp 2013-06-17 13:47:30 +0000
42@@ -0,0 +1,27 @@
43+/*
44+ * Copyright (C) 2013 Canonical, Ltd.
45+ *
46+ * This program is free software; you can redistribute it and/or modify
47+ * it under the terms of the GNU General Public License as published by
48+ * the Free Software Foundation; version 3.
49+ *
50+ * This program is distributed in the hope that it will be useful,
51+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
52+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53+ * GNU General Public License for more details.
54+ *
55+ * You should have received a copy of the GNU General Public License
56+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
57+ */
58+
59+#include "Direction.h"
60+
61+bool Direction::isHorizontal(Direction::Type type)
62+{
63+ return type == Direction::Leftwards || type == Direction::Rightwards;
64+}
65+
66+bool Direction::isVertical(Direction::Type type)
67+{
68+ return type == Direction::Upwards || type == Direction::Downwards;
69+}
70
71=== added file 'plugins/Ubuntu/Gestures/Direction.h'
72--- plugins/Ubuntu/Gestures/Direction.h 1970-01-01 00:00:00 +0000
73+++ plugins/Ubuntu/Gestures/Direction.h 2013-06-17 13:47:30 +0000
74@@ -0,0 +1,43 @@
75+/*
76+ * Copyright (C) 2013 Canonical, Ltd.
77+ *
78+ * This program is free software; you can redistribute it and/or modify
79+ * it under the terms of the GNU General Public License as published by
80+ * the Free Software Foundation; version 3.
81+ *
82+ * This program is distributed in the hope that it will be useful,
83+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
84+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85+ * GNU General Public License for more details.
86+ *
87+ * You should have received a copy of the GNU General Public License
88+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
89+ */
90+
91+#ifndef DIRECTION_H
92+#define DIRECTION_H
93+
94+#include "UbuntuGesturesGlobal.h"
95+#include <QObject>
96+
97+/*
98+ A Direction enum wrapper so that we can do things like "direction: Direction.Righwards"
99+ from QML.
100+ */
101+class UBUNTUGESTURES_EXPORT Direction : public QObject {
102+ Q_OBJECT
103+ Q_ENUMS(Type)
104+
105+public:
106+ enum Type {
107+ Rightwards, // Along the positive direction of the X axis
108+ Leftwards, // Along the negative direction of the X axis
109+ Downwards, // Along the positive direction of the Y axis
110+ Upwards // Along the negative direction of the Y axis
111+ };
112+
113+ Q_INVOKABLE static bool isHorizontal(Direction::Type type);
114+ Q_INVOKABLE static bool isVertical(Direction::Type type);
115+};
116+
117+#endif // DIRECTION_H
118
119=== modified file 'plugins/Ubuntu/Gestures/DirectionalDragArea.cpp'
120--- plugins/Ubuntu/Gestures/DirectionalDragArea.cpp 2013-06-11 18:20:17 +0000
121+++ plugins/Ubuntu/Gestures/DirectionalDragArea.cpp 2013-06-17 13:47:30 +0000
122@@ -41,7 +41,7 @@
123 : QQuickItem(parent)
124 , m_status(WaitingForTouch)
125 , m_touchId(-1)
126- , m_direction(DirectionalDragArea::Rightwards)
127+ , m_direction(Direction::Rightwards)
128 , m_wideningAngle(0)
129 , m_wideningFactor(0)
130 , m_distanceThreshold(0)
131@@ -58,12 +58,12 @@
132 m_velocityCalculator = new AxisVelocityCalculator(this);
133 }
134
135-DirectionalDragArea::Direction DirectionalDragArea::direction() const
136+Direction::Type DirectionalDragArea::direction() const
137 {
138 return m_direction;
139 }
140
141-void DirectionalDragArea::setDirection(DirectionalDragArea::Direction direction)
142+void DirectionalDragArea::setDirection(Direction::Type direction)
143 {
144 if (direction != m_direction) {
145 m_direction = direction;
146@@ -151,7 +151,7 @@
147
148 qreal DirectionalDragArea::distance() const
149 {
150- if (directionIsHorizontal()) {
151+ if (Direction::isHorizontal(m_direction)) {
152 return m_previousPos.x() - m_startPos.x();
153 } else {
154 return m_previousPos.y() - m_startPos.y();
155@@ -286,13 +286,13 @@
156 qreal dY = m_dampedPos.y() - m_startPos.y();
157
158 switch (m_direction) {
159- case Upwards:
160+ case Direction::Upwards:
161 return dY <= 0 && qFabs(dX) <= qFabs(dY) * m_wideningFactor;
162- case Downwards:
163+ case Direction::Downwards:
164 return dY >= 0 && qFabs(dX) <= dY * m_wideningFactor;
165- case Leftwards:
166+ case Direction::Leftwards:
167 return dX <= 0 && qFabs(dY) <= qFabs(dX) * m_wideningFactor;
168- default: // Rightwards:
169+ default: // Direction::Rightwards:
170 return dX >= 0 && qFabs(dY) <= dX * m_wideningFactor;
171 }
172 }
173@@ -300,20 +300,20 @@
174 bool DirectionalDragArea::movingInRightDirection() const
175 {
176 switch (m_direction) {
177- case Upwards:
178+ case Direction::Upwards:
179 return m_dampedPos.y() <= m_previousDampedPos.y();
180- case Downwards:
181+ case Direction::Downwards:
182 return m_dampedPos.y() >= m_previousDampedPos.y();
183- case Leftwards:
184+ case Direction::Leftwards:
185 return m_dampedPos.x() <= m_previousDampedPos.x();
186- default: // Rightwards:
187+ default: // Direction::Rightwards:
188 return m_dampedPos.x() >= m_previousDampedPos.x();
189 }
190 }
191
192 bool DirectionalDragArea::movedFarEnough(const QPointF &point) const
193 {
194- if (directionIsHorizontal())
195+ if (Direction::isHorizontal(m_direction))
196 return qFabs(point.x() - m_startPos.x()) > m_distanceThreshold;
197 else
198 return qFabs(point.y() - m_startPos.y()) > m_distanceThreshold;
199@@ -386,35 +386,25 @@
200
201 if (xChanged) {
202 Q_EMIT touchXChanged(point.x());
203- if (directionIsHorizontal())
204+ if (Direction::isHorizontal(m_direction))
205 Q_EMIT distanceChanged(distance());
206 }
207
208 if (yChanged) {
209 Q_EMIT touchYChanged(point.y());
210- if (directionIsVertical())
211+ if (Direction::isVertical(m_direction))
212 Q_EMIT distanceChanged(distance());
213 }
214 }
215
216 void DirectionalDragArea::updateVelocityCalculator(QPointF point)
217 {
218- if (directionIsHorizontal()) {
219+ if (Direction::isHorizontal(m_direction)) {
220 m_velocityCalculator->setTrackedPosition(point.x());
221 } else {
222 m_velocityCalculator->setTrackedPosition(point.y());
223 }
224 }
225
226-bool DirectionalDragArea::directionIsHorizontal() const
227-{
228- return m_direction == Leftwards || m_direction == Rightwards;
229-}
230-
231-bool DirectionalDragArea::directionIsVertical() const
232-{
233- return m_direction == Upwards || m_direction == Downwards;
234-}
235-
236 // Because we are defining a new QObject-based class (RecognitionTimer) here.
237 #include "DirectionalDragArea.moc"
238
239=== modified file 'plugins/Ubuntu/Gestures/DirectionalDragArea.h'
240--- plugins/Ubuntu/Gestures/DirectionalDragArea.h 2013-06-11 18:20:17 +0000
241+++ plugins/Ubuntu/Gestures/DirectionalDragArea.h 2013-06-17 13:47:30 +0000
242@@ -21,6 +21,7 @@
243 #include "AxisVelocityCalculator.h"
244 #include "UbuntuGesturesGlobal.h"
245 #include "Damper.h"
246+#include "Direction.h"
247
248 namespace UbuntuGestures {
249 /* Defines an interface for a Timer. */
250@@ -53,7 +54,7 @@
251 Q_OBJECT
252
253 // The direction in which the gesture should move in order to be recognized.
254- Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
255+ Q_PROPERTY(Direction::Type direction READ direction WRITE setDirection NOTIFY directionChanged)
256
257 // The distance travelled by the finger along the axis specified by
258 // DirectionalDragArea's direction.
259@@ -116,12 +117,8 @@
260 public:
261 DirectionalDragArea(QQuickItem *parent = 0);
262
263- enum Direction { Rightwards, // Along the positive direction of the X axis
264- Leftwards, // Along the negative direction of the X axis
265- Downwards, // Along the positive direction of the Y axis
266- Upwards }; // Along the negative direction of the Y axis
267- Direction direction() const;
268- void setDirection(Direction);
269+ Direction::Type direction() const;
270+ void setDirection(Direction::Type);
271
272 // Describes the state of the directional drag gesture.
273 enum Status {
274@@ -179,7 +176,7 @@
275 void setAxisVelocityCalculator(AxisVelocityCalculator *velCalc);
276
277 Q_SIGNALS:
278- void directionChanged(Direction direction);
279+ void directionChanged(Direction::Type direction);
280 void statusChanged(Status value);
281 void draggingChanged(bool value);
282 void distanceChanged(qreal value);
283@@ -210,10 +207,6 @@
284 void setPreviousPos(QPointF point);
285 void updateVelocityCalculator(QPointF point);
286
287- // convenience functions
288- bool directionIsHorizontal() const;
289- bool directionIsVertical() const;
290-
291 Status m_status;
292
293 QPointF m_startPos;
294@@ -225,7 +218,7 @@
295 DampedPointF m_dampedPos;
296 QPointF m_previousDampedPos;
297
298- Direction m_direction;
299+ Direction::Type m_direction;
300 qreal m_wideningAngle; // in degrees
301 qreal m_wideningFactor; // it's tan(degreesToRadian(m_wideningAngle))
302 qreal m_distanceThreshold;
303
304=== modified file 'plugins/Ubuntu/Gestures/plugin.cpp'
305--- plugins/Ubuntu/Gestures/plugin.cpp 2013-06-05 22:03:08 +0000
306+++ plugins/Ubuntu/Gestures/plugin.cpp 2013-06-17 13:47:30 +0000
307@@ -16,12 +16,20 @@
308
309 #include "plugin.h"
310 #include "AxisVelocityCalculator.h"
311+#include "Direction.h"
312 #include "DirectionalDragArea.h"
313
314 #include <qqml.h>
315
316+static QObject* directionSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
317+ Q_UNUSED(engine);
318+ Q_UNUSED(scriptEngine);
319+ return new Direction;
320+}
321+
322 void UbuntuGestureQmlPlugin::registerTypes(const char *uri)
323 {
324+ qmlRegisterSingletonType<Direction>(uri, 0, 1, "Direction", directionSingleton);
325 qmlRegisterType<DirectionalDragArea>(uri, 0, 1, "DirectionalDragArea");
326 qmlRegisterType<AxisVelocityCalculator>(uri, 0, 1, "AxisVelocityCalculator");
327 }
328
329=== modified file 'tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml'
330--- tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml 2013-06-05 22:03:08 +0000
331+++ tests/plugins/Ubuntu/Gestures/DownwardsLauncher.qml 2013-06-17 13:47:30 +0000
332@@ -51,7 +51,7 @@
333
334 height: units.gu(5)
335
336- direction: DirectionalDragArea.Downwards
337+ direction: Direction.Downwards
338 maxDeviation: units.gu(2)
339 wideningAngle: 10
340 distanceThreshold: units.gu(4)
341
342=== modified file 'tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml'
343--- tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml 2013-06-05 22:03:08 +0000
344+++ tests/plugins/Ubuntu/Gestures/LeftwardsLauncher.qml 2013-06-17 13:47:30 +0000
345@@ -54,7 +54,7 @@
346
347 width: units.gu(5)
348
349- direction: DirectionalDragArea.Leftwards
350+ direction: Direction.Leftwards
351 maxDeviation: units.gu(2)
352 wideningAngle: 10
353 distanceThreshold: units.gu(4)
354
355=== modified file 'tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml'
356--- tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml 2013-06-07 18:01:50 +0000
357+++ tests/plugins/Ubuntu/Gestures/RightwardsLauncher.qml 2013-06-17 13:47:30 +0000
358@@ -50,7 +50,7 @@
359
360 width: units.gu(5)
361
362- direction: DirectionalDragArea.Rightwards
363+ direction: Direction.Rightwards
364 maxDeviation: units.gu(2)
365 wideningAngle: 10
366 distanceThreshold: units.gu(4)
367
368=== modified file 'tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml'
369--- tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml 2013-06-05 22:03:08 +0000
370+++ tests/plugins/Ubuntu/Gestures/UpwardsLauncher.qml 2013-06-17 13:47:30 +0000
371@@ -54,7 +54,7 @@
372
373 height: units.gu(5)
374
375- direction: DirectionalDragArea.Upwards
376+ direction: Direction.Upwards
377 maxDeviation: units.gu(2)
378 wideningAngle: 10
379 distanceThreshold: units.gu(4)
380
381=== modified file 'tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp'
382--- tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp 2013-06-11 18:20:17 +0000
383+++ tests/plugins/Ubuntu/Gestures/tst_DirectionalDragArea.cpp 2013-06-17 13:47:30 +0000
384@@ -132,13 +132,13 @@
385 QPointF calculateInitialTouchPos(DirectionalDragArea *edgeDragArea, QQuickView *view)
386 {
387 switch (edgeDragArea->direction()) {
388- case DirectionalDragArea::Upwards:
389+ case Direction::Upwards:
390 return QPointF(view->width()/2.0f, view->height() - (edgeDragArea->height()/2.0f));
391- case DirectionalDragArea::Downwards:
392+ case Direction::Downwards:
393 return QPointF(view->width()/2.0f, edgeDragArea->height()/2.0f);
394- case DirectionalDragArea::Leftwards:
395+ case Direction::Leftwards:
396 return QPointF(view->width() - (edgeDragArea->width()/2.0f), view->height()/2.0f);
397- default: // DirectionalDragArea::Rightwards:
398+ default: // Direction::Rightwards:
399 return QPointF(edgeDragArea->width()/2.0f, view->height()/2.0f);
400 }
401 }
402@@ -153,13 +153,13 @@
403 qreal angleSin = qSin(angleRadians);
404
405 switch (edgeDragArea->direction()) {
406- case DirectionalDragArea::Upwards:
407+ case Direction::Upwards:
408 return QPointF(angleSin, -angleCos);
409- case DirectionalDragArea::Downwards:
410+ case Direction::Downwards:
411 return QPointF(angleSin, angleCos);
412- case DirectionalDragArea::Leftwards:
413+ case Direction::Leftwards:
414 return QPointF(-angleCos, angleSin);
415- default: // DirectionalDragArea::Rightwards:
416+ default: // Direction::Rightwards:
417 return QPointF(angleCos, angleSin);
418 }
419 }
420@@ -168,8 +168,7 @@
421 {
422 qreal deviation = edgeDragArea->maxDeviation() * 0.8;
423
424- if (edgeDragArea->direction() == DirectionalDragArea::Leftwards
425- || edgeDragArea->direction() == DirectionalDragArea::Rightwards) {
426+ if (Direction::isHorizontal(edgeDragArea->direction())) {
427 return QPointF(0, deviation);
428 } else {
429 return QPointF(deviation, 0);

Subscribers

People subscribed via source and target branches

to all changes: