~loic.molinari/ubuntu-ui-toolkit/ubuntu-ui-toolkit-ubuntushape-new-button-style-support

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/plugin/gestures/ucswipearea_p.h

  • Committer: Tarmac
  • Author(s): Zsombor Egri, Zoltán Balogh, Benjamin Zeller
  • Date: 2015-11-17 14:44:49 UTC
  • mfrom: (1693.4.43 migrate_unity8_gestures)
  • Revision ID: tarmac-20151117144449-p0s2lj04nx4fuq80
Migrate DirectionalDragArea from Unity8, named as SwipeArea. Original code (from lp:unity8) by: Daniel d'Andrada <daniel.dandrada@canonical.com>.

Approved by PS Jenkins bot, Christian Dywan, Benjamin Zeller, Zsombor Egri, Daniel d'Andrada.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 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
 
 
18
#ifndef UCSWIPEAREAPRIVATE_H
 
19
#define UCSWIPEAREAPRIVATE_H
 
20
 
 
21
#include "ucswipearea.h"
 
22
 
 
23
// Information about an active touch point
 
24
struct UBUNTUGESTURESQML_EXPORT ActiveTouchInfo {
 
25
    ActiveTouchInfo() : id(-1), startTime(-1) {}
 
26
    bool isValid() const { return id != -1; }
 
27
    void reset() { id = -1; }
 
28
    int id;
 
29
    qint64 startTime;
 
30
};
 
31
class UBUNTUGESTURESQML_EXPORT ActiveTouchesInfo {
 
32
public:
 
33
    ActiveTouchesInfo(const UbuntuGestures::SharedTimeSource &timeSource);
 
34
    void update(QTouchEvent *event);
 
35
    qint64 touchStartTime(int id);
 
36
    bool isEmpty() const { return m_touchInfoPool.isEmpty(); }
 
37
    qint64 mostRecentStartTime();
 
38
    UbuntuGestures::SharedTimeSource m_timeSource;
 
39
private:
 
40
    void addTouchPoint(int touchId);
 
41
    void removeTouchPoint(int touchId);
 
42
    QString toString();
 
43
 
 
44
    Pool<ActiveTouchInfo> m_touchInfoPool;
 
45
};
 
46
 
 
47
class UBUNTUGESTURESQML_EXPORT UCSwipeAreaPrivate : public QObject
 
48
{
 
49
    Q_OBJECT
 
50
 
 
51
    Q_ENUMS(Status)
 
52
public:
 
53
    UCSwipeAreaPrivate(UCSwipeArea *q);
 
54
 
 
55
public Q_SLOTS:
 
56
    void giveUpIfDisabledOrInvisible();
 
57
    void rejectGesture();
 
58
 
 
59
public:
 
60
    // Describes the state of the directional drag gesture.
 
61
    enum Status {
 
62
        // Waiting for a new touch point to land on this area. No gesture is being processed
 
63
        // or tracked.
 
64
        WaitingForTouch,
 
65
 
 
66
        // A touch point has landed on this area but it's not know yet whether it is
 
67
        // performing a drag in the correct direction.
 
68
        // If it's decided that the touch point is not performing a directional drag gesture,
 
69
        // it will be rejected/ignored and status will return to WaitingForTouch.
 
70
        Undecided, //Recognizing,
 
71
 
 
72
        // There's a touch point in this area and it performed a drag in the correct
 
73
        // direction.
 
74
        //
 
75
        // Once recognized, the gesture state will move back to WaitingForTouch only once
 
76
        // that touch point ends. The gesture will remain in the Recognized state even if
 
77
        // the touch point starts moving in other directions or halts.
 
78
        Recognized,
 
79
    };
 
80
 
 
81
    void touchEvent_absent(QTouchEvent *event);
 
82
    void touchEvent_undecided(QTouchEvent *event);
 
83
    void touchEvent_recognized(QTouchEvent *event);
 
84
    bool movingInRightDirection() const;
 
85
    bool movedFarEnoughAlongGestureAxis() const;
 
86
    bool isPastMaxDistance() const;
 
87
    const QTouchEvent::TouchPoint *fetchTargetTouchPoint(QTouchEvent *event);
 
88
    void setStatus(Status newStatus);
 
89
    void updatePosition(const QPointF &point);
 
90
    void setPublicScenePos(const QPointF &point);
 
91
    bool isWithinTouchCompositionWindow();
 
92
    void updateSceneDirectionVector();
 
93
    // returns the scalar projection between the given vector (in scene coordinates)
 
94
    // and m_sceneDirectionVector
 
95
    qreal projectOntoDirectionVector(const QPointF &sceneVector) const;
 
96
    void touchOwnershipEvent(TouchOwnershipEvent *event);
 
97
    void unownedTouchEvent(UnownedTouchEvent *event);
 
98
    void unownedTouchEvent_undecided(UnownedTouchEvent *unownedTouchEvent);
 
99
    void watchPressedTouchPoints(const QList<QTouchEvent::TouchPoint> &touchPoints);
 
100
    bool recognitionIsDisabled() const;
 
101
    bool sanityCheckRecognitionProperties();
 
102
    void setMaxTime(int value);
 
103
    void setDistanceThreshold(qreal value);
 
104
    void setPixelsPerMm(qreal pixelsPerMm);
 
105
    QString objectName() const { return q->objectName(); }
 
106
 
 
107
    // Replaces the existing Timer with the given one.
 
108
    //
 
109
    // Useful for providing a fake timer when testing.
 
110
    void setRecognitionTimer(UbuntuGestures::AbstractTimer *timer);
 
111
 
 
112
    // Useful for testing, where a fake time source can be supplied
 
113
    void setTimeSource(const UbuntuGestures::SharedTimeSource &timeSource);
 
114
 
 
115
    QPointF startScenePos;
 
116
    // The touch position exposed in the public API.
 
117
    // It only starts to move once the gesture gets recognized.
 
118
    QPointF publicScenePos;
 
119
    // A movement damper is used in some of the gesture recognition calculations
 
120
    // to get rid of noise or small oscillations in the touch position.
 
121
    DampedPointF dampedScenePos;
 
122
    QPointF previousDampedScenePos;
 
123
    // Unit vector in scene coordinates describing the direction of the gesture recognition
 
124
    QPointF sceneDirectionVector;
 
125
    UbuntuGestures::SharedTimeSource timeSource;
 
126
    ActiveTouchesInfo activeTouches;
 
127
 
 
128
    UCSwipeArea *q;
 
129
    UbuntuGestures::AbstractTimer *recognitionTimer;
 
130
 
 
131
    // How far a touch point has to move from its initial position along the gesture axis in order
 
132
    // for it to be recognized as a directional drag.
 
133
    qreal distanceThreshold;
 
134
    qreal distanceThresholdSquared; // it's pow(distanceThreshold, 2)
 
135
    // Maximum distance the gesture can go without crossing the axis-aligned distance threshold
 
136
    qreal maxDistance;
 
137
    qreal sceneDistance;
 
138
 
 
139
    int touchId;
 
140
    // Maximum time (in milliseconds) the gesture can take to go beyond the distance threshold
 
141
    int maxTime;
 
142
    // Maximum time (in milliseconds) after the start of a given touch point where
 
143
    // subsequent touch starts are grouped with the first one into an N-touches gesture
 
144
    // (e.g. a two-fingers tap or drag).
 
145
    int compositionTime;
 
146
 
 
147
    // The current status of the directional drag gesture area.
 
148
    Status status;
 
149
    UCSwipeArea::Direction direction;
 
150
 
 
151
    bool immediateRecognition;
 
152
 
 
153
Q_SIGNALS:
 
154
    void statusChanged(Status value);
 
155
};
 
156
 
 
157
#endif // UCSWIPEAREAPRIVATE_H