1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QDECLARATIVEPINCHAREA_H
43 #define QDECLARATIVEPINCHAREA_H
44 
45 #include <qdeclarativeitem.h>
46 
47 QT_BEGIN_HEADER
48 
49 QT_BEGIN_NAMESPACE
50 
QT_MODULE(Declarative)51 QT_MODULE(Declarative)
52 
53 class Q_AUTOTEST_EXPORT QDeclarativePinch : public QObject
54 {
55     Q_OBJECT
56 
57     Q_ENUMS(Axis)
58     Q_PROPERTY(QGraphicsObject *target READ target WRITE setTarget RESET resetTarget)
59     Q_PROPERTY(qreal minimumScale READ minimumScale WRITE setMinimumScale NOTIFY minimumScaleChanged)
60     Q_PROPERTY(qreal maximumScale READ maximumScale WRITE setMaximumScale NOTIFY maximumScaleChanged)
61     Q_PROPERTY(qreal minimumRotation READ minimumRotation WRITE setMinimumRotation NOTIFY minimumRotationChanged)
62     Q_PROPERTY(qreal maximumRotation READ maximumRotation WRITE setMaximumRotation NOTIFY maximumRotationChanged)
63     Q_PROPERTY(Axis dragAxis READ axis WRITE setAxis NOTIFY dragAxisChanged)
64     Q_PROPERTY(qreal minimumX READ xmin WRITE setXmin NOTIFY minimumXChanged)
65     Q_PROPERTY(qreal maximumX READ xmax WRITE setXmax NOTIFY maximumXChanged)
66     Q_PROPERTY(qreal minimumY READ ymin WRITE setYmin NOTIFY minimumYChanged)
67     Q_PROPERTY(qreal maximumY READ ymax WRITE setYmax NOTIFY maximumYChanged)
68     Q_PROPERTY(bool active READ active NOTIFY activeChanged)
69 
70 public:
71     QDeclarativePinch();
72 
73     QGraphicsObject *target() const { return m_target; }
74     void setTarget(QGraphicsObject *target) {
75         if (target == m_target)
76             return;
77         m_target = target;
78         emit targetChanged();
79     }
80     void resetTarget() {
81         if (!m_target)
82             return;
83         m_target = 0;
84         emit targetChanged();
85     }
86 
87     qreal minimumScale() const { return m_minScale; }
88     void setMinimumScale(qreal s) {
89         if (s == m_minScale)
90             return;
91         m_minScale = s;
92         emit minimumScaleChanged();
93     }
94     qreal maximumScale() const { return m_maxScale; }
95     void setMaximumScale(qreal s) {
96         if (s == m_maxScale)
97             return;
98         m_maxScale = s;
99         emit maximumScaleChanged();
100     }
101 
102     qreal minimumRotation() const { return m_minRotation; }
103     void setMinimumRotation(qreal r) {
104         if (r == m_minRotation)
105             return;
106         m_minRotation = r;
107         emit minimumRotationChanged();
108     }
109     qreal maximumRotation() const { return m_maxRotation; }
110     void setMaximumRotation(qreal r) {
111         if (r == m_maxRotation)
112             return;
113         m_maxRotation = r;
114         emit maximumRotationChanged();
115     }
116 
117     enum Axis { NoDrag=0x00, XAxis=0x01, YAxis=0x02, XandYAxis=0x03 };
118     Axis axis() const { return m_axis; }
119     void setAxis(Axis a) {
120         if (a == m_axis)
121             return;
122         m_axis = a;
123         emit dragAxisChanged();
124     }
125 
126     qreal xmin() const { return m_xmin; }
127     void setXmin(qreal x) {
128         if (x == m_xmin)
129             return;
130         m_xmin = x;
131         emit minimumXChanged();
132     }
133     qreal xmax() const { return m_xmax; }
134     void setXmax(qreal x) {
135         if (x == m_xmax)
136             return;
137         m_xmax = x;
138         emit maximumXChanged();
139     }
140     qreal ymin() const { return m_ymin; }
141     void setYmin(qreal y) {
142         if (y == m_ymin)
143             return;
144         m_ymin = y;
145         emit minimumYChanged();
146     }
147     qreal ymax() const { return m_ymax; }
148     void setYmax(qreal y) {
149         if (y == m_ymax)
150             return;
151         m_ymax = y;
152         emit maximumYChanged();
153     }
154 
155     bool active() const { return m_active; }
156     void setActive(bool a) {
157         if (a == m_active)
158             return;
159         m_active = a;
160         emit activeChanged();
161     }
162 
163 signals:
164     void targetChanged();
165     void minimumScaleChanged();
166     void maximumScaleChanged();
167     void minimumRotationChanged();
168     void maximumRotationChanged();
169     void dragAxisChanged();
170     void minimumXChanged();
171     void maximumXChanged();
172     void minimumYChanged();
173     void maximumYChanged();
174     void activeChanged();
175 
176 private:
177     QGraphicsObject *m_target;
178     qreal m_minScale;
179     qreal m_maxScale;
180     qreal m_minRotation;
181     qreal m_maxRotation;
182     Axis m_axis;
183     qreal m_xmin;
184     qreal m_xmax;
185     qreal m_ymin;
186     qreal m_ymax;
187     bool m_active;
188 };
189 
190 class Q_AUTOTEST_EXPORT QDeclarativePinchEvent : public QObject
191 {
192     Q_OBJECT
193 
Q_PROPERTY(QPointF center READ center)194     Q_PROPERTY(QPointF center READ center)
195     Q_PROPERTY(QPointF startCenter READ startCenter)
196     Q_PROPERTY(QPointF previousCenter READ previousCenter)
197     Q_PROPERTY(qreal scale READ scale)
198     Q_PROPERTY(qreal previousScale READ previousScale)
199     Q_PROPERTY(qreal angle READ angle)
200     Q_PROPERTY(qreal previousAngle READ previousAngle)
201     Q_PROPERTY(qreal rotation READ rotation)
202     Q_PROPERTY(QPointF point1 READ point1)
203     Q_PROPERTY(QPointF startPoint1 READ startPoint1)
204     Q_PROPERTY(QPointF point2 READ point2)
205     Q_PROPERTY(QPointF startPoint2 READ startPoint2)
206     Q_PROPERTY(int pointCount READ pointCount)
207     Q_PROPERTY(bool accepted READ accepted WRITE setAccepted)
208 
209 public:
210     QDeclarativePinchEvent(QPointF c, qreal s, qreal a, qreal r)
211         : QObject(), m_center(c), m_scale(s), m_angle(a), m_rotation(r)
212         , m_pointCount(0), m_accepted(true) {}
213 
center()214     QPointF center() const { return m_center; }
startCenter()215     QPointF startCenter() const { return m_startCenter; }
setStartCenter(QPointF c)216     void setStartCenter(QPointF c) { m_startCenter = c; }
previousCenter()217     QPointF previousCenter() const { return m_lastCenter; }
setPreviousCenter(QPointF c)218     void setPreviousCenter(QPointF c) { m_lastCenter = c; }
scale()219     qreal scale() const { return m_scale; }
previousScale()220     qreal previousScale() const { return m_lastScale; }
setPreviousScale(qreal s)221     void setPreviousScale(qreal s) { m_lastScale = s; }
angle()222     qreal angle() const { return m_angle; }
previousAngle()223     qreal previousAngle() const { return m_lastAngle; }
setPreviousAngle(qreal a)224     void setPreviousAngle(qreal a) { m_lastAngle = a; }
rotation()225     qreal rotation() const { return m_rotation; }
point1()226     QPointF point1() const { return m_point1; }
setPoint1(QPointF p)227     void setPoint1(QPointF p) { m_point1 = p; }
startPoint1()228     QPointF startPoint1() const { return m_startPoint1; }
setStartPoint1(QPointF p)229     void setStartPoint1(QPointF p) { m_startPoint1 = p; }
point2()230     QPointF point2() const { return m_point2; }
setPoint2(QPointF p)231     void setPoint2(QPointF p) { m_point2 = p; }
startPoint2()232     QPointF startPoint2() const { return m_startPoint2; }
setStartPoint2(QPointF p)233     void setStartPoint2(QPointF p) { m_startPoint2 = p; }
pointCount()234     int pointCount() const { return m_pointCount; }
setPointCount(int count)235     void setPointCount(int count) { m_pointCount = count; }
236 
accepted()237     bool accepted() const { return m_accepted; }
setAccepted(bool a)238     void setAccepted(bool a) { m_accepted = a; }
239 
240 private:
241     QPointF m_center;
242     QPointF m_startCenter;
243     QPointF m_lastCenter;
244     qreal m_scale;
245     qreal m_lastScale;
246     qreal m_angle;
247     qreal m_lastAngle;
248     qreal m_rotation;
249     QPointF m_point1;
250     QPointF m_point2;
251     QPointF m_startPoint1;
252     QPointF m_startPoint2;
253     int m_pointCount;
254     bool m_accepted;
255 };
256 
257 
258 class QDeclarativeMouseEvent;
259 class QDeclarativePinchAreaPrivate;
260 class Q_AUTOTEST_EXPORT QDeclarativePinchArea : public QDeclarativeItem
261 {
262     Q_OBJECT
263 
264     Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
265     Q_PROPERTY(QDeclarativePinch *pinch READ pinch CONSTANT)
266 
267 public:
268     QDeclarativePinchArea(QDeclarativeItem *parent=0);
269     ~QDeclarativePinchArea();
270 
271     bool isEnabled() const;
272     void setEnabled(bool);
273 
274     QDeclarativePinch *pinch();
275 
276 Q_SIGNALS:
277     void enabledChanged();
278     void pinchStarted(QDeclarativePinchEvent *pinch);
279     void pinchUpdated(QDeclarativePinchEvent *pinch);
280     void pinchFinished(QDeclarativePinchEvent *pinch);
281 
282 protected:
283     void mousePressEvent(QGraphicsSceneMouseEvent *event);
284     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
285     void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
286     bool sceneEvent(QEvent *);
287     bool sendMouseEvent(QGraphicsSceneMouseEvent *event);
288     bool sceneEventFilter(QGraphicsItem *i, QEvent *e);
289     bool event(QEvent *);
290 
291     virtual void geometryChanged(const QRectF &newGeometry,
292                                  const QRectF &oldGeometry);
293     virtual QVariant itemChange(GraphicsItemChange change, const QVariant& value);
294 
295 private:
296     void updatePinch();
297     void handlePress();
298     void handleRelease();
299 
300 private:
301     Q_DISABLE_COPY(QDeclarativePinchArea)
302     Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativePinchArea)
303 };
304 
305 QT_END_NAMESPACE
306 
307 QML_DECLARE_TYPE(QDeclarativePinch)
308 QML_DECLARE_TYPE(QDeclarativePinchEvent)
309 QML_DECLARE_TYPE(QDeclarativePinchArea)
310 
311 QT_END_HEADER
312 
313 #endif // QDECLARATIVEPINCHAREA_H
314