1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef QDECLARATIVEPINCHGENERATOR_H
30 #define QDECLARATIVEPINCHGENERATOR_H
31 
32 #include <QtQuick/QQuickItem>
33 #include <QtQuick/QQuickWindow>
34 #include <QtGui/QMouseEvent>
35 #include <QtGui/QTouchEvent>
36 #include <QtGui/QKeyEvent>
37 #include <QtCore/QElapsedTimer>
38 #include <QtCore/QList>
39 #include <QtCore/QPoint>
40 #include <QDebug>
41 
42 #define SWIPES_REQUIRED 2
43 
44 QT_BEGIN_NAMESPACE
45 
46 typedef struct {
47     QList<QPoint> touchPoints;
48     QList<int> durations;
49 } Swipe;
50 
51 class QDeclarativePinchGenerator : public QQuickItem
52 {
53     Q_OBJECT
54 
55     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
56     Q_PROPERTY(QString state READ state NOTIFY stateChanged)
57     Q_PROPERTY(qreal replaySpeedFactor READ replaySpeedFactor WRITE setReplaySpeedFactor NOTIFY replaySpeedFactorChanged)
58     Q_PROPERTY(QQuickItem* target READ target WRITE setTarget NOTIFY targetChanged)
59     Q_INTERFACES(QQmlParserStatus)
60 
61 public:
62     QDeclarativePinchGenerator();
63     ~QDeclarativePinchGenerator();
64 
65     QString state() const;
66     QQuickItem* target() const;
67     void setTarget(QQuickItem* target);
68     qreal replaySpeedFactor() const;
69     void setReplaySpeedFactor(qreal factor);
70     bool enabled() const;
71     void setEnabled(bool enabled);
72 
73     Q_INVOKABLE void replay();
74     Q_INVOKABLE void clear();
75     Q_INVOKABLE void stop();
76     Q_INVOKABLE int startDragDistance();
77 
78     // programmatic interface, useful for autotests
79     Q_INVOKABLE void pinch(QPoint point1From,
80                            QPoint point1To,
81                            QPoint point2From,
82                            QPoint point2To,
83                            int interval1 = 20,
84                            int interval2 = 20,
85                            int samples1 = 10,
86                            int samples2 = 10);
87 
88     Q_INVOKABLE void pinchPress(QPoint point1From, QPoint point2From);
89     Q_INVOKABLE void pinchMoveTo(QPoint point1To, QPoint point2To);
90     Q_INVOKABLE void pinchRelease(QPoint point1To, QPoint point2To);
91 
92 signals:
93     void stateChanged();
94     void targetChanged();
95     void replaySpeedFactorChanged();
96     void enabledChanged();
97 
98 public:
99     enum GeneratorState {
100         Invalid    = 0,
101         Idle       = 1,
102         Recording  = 2,
103         Replaying  = 3
104     };
105 
106     // from QQmlParserStatus
107     virtual void componentComplete();
108     // from QQuickItem
109     void itemChange(ItemChange change, const ItemChangeData & data);
110 
111 protected:
112     void mousePressEvent(QMouseEvent *event);
113     void mouseReleaseEvent(QMouseEvent *event);
114     void mouseDoubleClickEvent(QMouseEvent *event);
115     void mouseMoveEvent(QMouseEvent *event);
116     void keyPressEvent(QKeyEvent *event);
117     void timerEvent(QTimerEvent *event);
118 
119 private:
120     void setState(GeneratorState state);
121 
122 private:
123     QQuickItem* target_;
124     GeneratorState state_;
125     QQuickWindow* window_;
126     QList<Swipe*> swipes_;
127     Swipe* activeSwipe_;
128     QElapsedTimer swipeTimer_;
129     int replayTimer_;
130     int replayBookmark_;
131     int masterSwipe_;
132     qreal replaySpeedFactor_;
133     bool enabled_;
134     QTouchDevice* device_;
135 };
136 
137 QT_END_NAMESPACE
138 
139 #endif
140