sync
[mardrone] / mardrone / qdeclarativetoucharea.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QML Touch Area plugin of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #ifndef QDECLARATIVETOUCHAREA_H
44 #define QDECLARATIVETOUCHAREA_H
45
46 #include "qdeclarativeitem.h"
47 #include "qevent.h"
48
49 #include <QMap>
50 #include <QList>
51 #include <QDeclarativeScriptString>
52
53 class QDeclarativeTouchPoint : public QObject {
54     Q_OBJECT
55     Q_PROPERTY(int id READ id)
56     Q_PROPERTY(qreal x READ x NOTIFY xChanged)
57     Q_PROPERTY(qreal y READ y NOTIFY yChanged)
58     Q_PROPERTY(qreal sceneX READ sceneX NOTIFY sceneXChanged)
59     Q_PROPERTY(qreal sceneY READ sceneY NOTIFY sceneYChanged)
60     Q_PROPERTY(bool valid READ isValid NOTIFY validityChanged)
61 public:
62     QDeclarativeTouchPoint(bool qmlReference = true)
63         : _id(0),
64           _x(0.0),
65           _y(0.0),
66           _sceneX(0.0),
67           _sceneY(0.0),
68           _qmlReferenced(qmlReference),
69           _valid(!qmlReference)
70     {}
71
72     int id() const { return _id; }
73     void setId(int id) { _id = id; }
74
75     qreal x() const { return _x; }
76     void setX(qreal x) {
77         _x = x;
78         emit(xChanged());
79     }
80
81     qreal y() const { return _y; }
82     void setY(qreal y) {
83         _y = y;
84         emit(yChanged());
85     }
86
87     qreal sceneX() const { return _sceneX; }
88     void setSceneX(qreal sceneX) {
89         _sceneX = sceneX;
90         emit(sceneXChanged());
91     }
92
93     qreal sceneY() const { return _sceneY; }
94     void setSceneY(qreal sceneY) {
95         _sceneY = sceneY;
96         emit(sceneYChanged());
97     }
98
99     bool isQmlReferenced() { return _qmlReferenced; }
100
101     bool isValid() { return _valid; }
102     void setValid(bool valid) {
103         _valid = valid;
104         emit(validityChanged());
105     }
106
107 Q_SIGNALS:
108     void xChanged();
109     void yChanged();
110     void sceneXChanged();
111     void sceneYChanged();
112     void validityChanged();
113
114 private:
115     int _id;
116     qreal _x;
117     qreal _y;
118     qreal _sceneX;
119     qreal _sceneY;
120     bool _qmlReferenced;
121     bool _valid;
122 };
123
124
125
126 class QDeclarativeTouchArea : public QDeclarativeItem
127 {
128     Q_OBJECT
129
130     Q_PROPERTY(QList<QObject*> touches READ touches)
131     Q_PROPERTY(QList<QObject*> changedTouches READ changedTouches)
132     Q_PROPERTY(QList<QObject*> releasedTouches READ releasedTouches)
133
134     Q_PROPERTY(QDeclarativeListProperty<QDeclarativeTouchPoint> touchPoints READ touchPoints NOTIFY touchPointsChanged)
135     Q_PROPERTY(int minimumTouches READ minimumTouches WRITE setMinimumTouches)
136     Q_PROPERTY(int maximumTouches READ maximumTouches WRITE setMaximumTouches)
137     Q_PROPERTY(qreal scaleFactor READ scaleFactor NOTIFY scaleFactorChanged)
138     Q_PROPERTY(qreal rotationAngle READ rotationAngle NOTIFY rotationAngleChanged)
139     Q_PROPERTY(QDeclarativeScriptString keepMouseFocus READ keepMouseFocus WRITE setKeepMouseFocus)
140
141 public:
142     static void registerQML();
143
144     QDeclarativeTouchArea(QDeclarativeItem *parent=0);
145     ~QDeclarativeTouchArea();
146
147     int minimumTouches() const { return _minimumTouches; }
148     void setMinimumTouches(int num) { _minimumTouches = num; }
149     int maximumTouches() const { return _maximumTouches; }
150     void setMaximumTouches(int num) { _maximumTouches = num; }
151
152     qreal scaleFactor() const { return _scaleFactor; }
153     qreal rotationAngle() const { return _rotationAngle; }
154
155     QDeclarativeScriptString keepMouseFocus() const { return _keepMouseFocus; }
156     void setKeepMouseFocus(const QDeclarativeScriptString &keep) { _keepMouseFocus = keep; }
157
158     QList<QObject*> touches() { return _touches.values(); }
159     QList<QObject*> changedTouches() { return _changedTouches.values(); }
160     QList<QObject*> releasedTouches() { return _releasedTouches; }
161
162     QDeclarativeListProperty<QDeclarativeTouchPoint> touchPoints() {
163         return QDeclarativeListProperty<QDeclarativeTouchPoint>(this, 0, QDeclarativeTouchArea::touchPoint_append, QDeclarativeTouchArea::touchPoint_count, QDeclarativeTouchArea::touchPoint_at, 0);
164     }
165
166     static void touchPoint_append(QDeclarativeListProperty<QDeclarativeTouchPoint> *list, QDeclarativeTouchPoint* touch) {
167         QDeclarativeTouchArea *q = static_cast<QDeclarativeTouchArea*>(list->object);
168         q->addTouchPrototype(touch);
169     }
170
171     static int touchPoint_count(QDeclarativeListProperty<QDeclarativeTouchPoint> *list) {
172         QDeclarativeTouchArea *q = static_cast<QDeclarativeTouchArea*>(list->object);
173         return q->_touchPrototypes.count();
174     }
175
176     static QDeclarativeTouchPoint* touchPoint_at(QDeclarativeListProperty<QDeclarativeTouchPoint> *list, int index) {
177         QDeclarativeTouchArea *q = static_cast<QDeclarativeTouchArea*>(list->object);
178         return static_cast<QDeclarativeTouchPoint*>(q->_touchPrototypes[index]);
179     }
180
181
182 Q_SIGNALS:
183     void touchStart();
184     void touchMove();
185     void touchEnd();
186     void touchPointsChanged();
187     void scaleFactorChanged();
188     void rotationAngleChanged();
189
190 protected:
191     bool sceneEvent(QEvent *);
192     void updateTouchPoint(QDeclarativeTouchPoint*, const QTouchEvent::TouchPoint*);
193     void updatePinch(QList<QTouchEvent::TouchPoint> *touchPoints);
194     void addTouchPrototype(QDeclarativeTouchPoint* prototype);
195     void addTouchPoint(const QTouchEvent::TouchPoint *p);
196     void clearChangedAndReleasedTouches();
197     void updateTopTouchArea();
198     void updateTouchData(QEvent*);
199     bool event(QEvent *event);
200     bool sceneEventFilter(QGraphicsItem *i, QEvent *event);
201
202 private:
203     QMap<int,QObject*> _touchPrototypes;
204     QMap<int,QObject*> _touches;
205     QMap<int,QObject*> _changedTouches;
206     QList<QObject*> _releasedTouches;
207     int _minimumTouches;
208     int _maximumTouches;
209     qreal _scaleFactor;
210     qreal _rotationAngle;
211     QDeclarativeTouchArea* _parentTouchArea;
212     bool _active;
213     bool _stealMouse;
214     QDeclarativeScriptString _keepMouseFocus;
215
216 };
217
218 QML_DECLARE_TYPE(QDeclarativeTouchPoint)
219
220 #endif // QDECLARATIVETOUCHAREA_H