source: trunk/src/gui/kernel/qmultitouch_mac.mm

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <private/qmultitouch_mac_p.h>
43#include <qcursor.h>
44
45#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
46
47QT_BEGIN_NAMESPACE
48
49#ifdef QT_MAC_USE_COCOA
50
51QHash<qint64, QCocoaTouch*> QCocoaTouch::_currentTouches;
52QPointF QCocoaTouch::_screenReferencePos;
53QPointF QCocoaTouch::_trackpadReferencePos;
54int QCocoaTouch::_idAssignmentCount = 0;
55int QCocoaTouch::_touchCount = 0;
56bool QCocoaTouch::_updateInternalStateOnly = true;
57
58QCocoaTouch::QCocoaTouch(NSTouch *nstouch)
59{
60 if (_currentTouches.size() == 0)
61 _idAssignmentCount = 0;
62
63 _touchPoint.setId(_idAssignmentCount++);
64 _touchPoint.setPressure(1.0);
65 _identity = qint64([nstouch identity]);
66 _currentTouches.insert(_identity, this);
67 updateTouchData(nstouch, NSTouchPhaseBegan);
68}
69
70QCocoaTouch::~QCocoaTouch()
71{
72 _currentTouches.remove(_identity);
73}
74
75void QCocoaTouch::updateTouchData(NSTouch *nstouch, NSTouchPhase phase)
76{
77 if (_touchCount == 1)
78 _touchPoint.setState(toTouchPointState(phase) | Qt::TouchPointPrimary);
79 else
80 _touchPoint.setState(toTouchPointState(phase));
81
82 // From the normalized position on the trackpad, calculate
83 // where on screen the touchpoint should be according to the
84 // reference position:
85 NSPoint npos = [nstouch normalizedPosition];
86 QPointF qnpos = QPointF(npos.x, 1 - npos.y);
87 _touchPoint.setNormalizedPos(qnpos);
88
89 if (_touchPoint.id() == 0 && phase == NSTouchPhaseBegan) {
90 _trackpadReferencePos = qnpos;
91 _screenReferencePos = QCursor::pos();
92 }
93
94 NSSize dsize = [nstouch deviceSize];
95 float ppiX = (qnpos.x() - _trackpadReferencePos.x()) * dsize.width;
96 float ppiY = (qnpos.y() - _trackpadReferencePos.y()) * dsize.height;
97 QPointF relativePos = _trackpadReferencePos - QPointF(ppiX, ppiY);
98 _touchPoint.setScreenPos(_screenReferencePos - relativePos);
99}
100
101QCocoaTouch *QCocoaTouch::findQCocoaTouch(NSTouch *nstouch)
102{
103 qint64 identity = qint64([nstouch identity]);
104 if (_currentTouches.contains(identity))
105 return _currentTouches.value(identity);
106 return 0;
107}
108
109Qt::TouchPointState QCocoaTouch::toTouchPointState(NSTouchPhase nsState)
110{
111 Qt::TouchPointState qtState = Qt::TouchPointReleased;
112 switch (nsState) {
113 case NSTouchPhaseBegan:
114 qtState = Qt::TouchPointPressed;
115 break;
116 case NSTouchPhaseMoved:
117 qtState = Qt::TouchPointMoved;
118 break;
119 case NSTouchPhaseStationary:
120 qtState = Qt::TouchPointStationary;
121 break;
122 case NSTouchPhaseEnded:
123 case NSTouchPhaseCancelled:
124 qtState = Qt::TouchPointReleased;
125 break;
126 default:
127 break;
128 }
129 return qtState;
130}
131
132QList<QTouchEvent::TouchPoint>
133QCocoaTouch::getCurrentTouchPointList(NSEvent *event, bool acceptSingleTouch)
134{
135 QMap<int, QTouchEvent::TouchPoint> touchPoints;
136 NSSet *ended = [event touchesMatchingPhase:NSTouchPhaseEnded | NSTouchPhaseCancelled inView:nil];
137 NSSet *active = [event
138 touchesMatchingPhase:NSTouchPhaseBegan | NSTouchPhaseMoved | NSTouchPhaseStationary
139 inView:nil];
140 _touchCount = [active count];
141
142 // First: remove touches that were ended by the user. If we are
143 // currently not accepting single touches, a corresponding 'begin'
144 // has never been send to the app for these events.
145 // So should therefore not send the following removes either.
146
147 for (int i=0; i<int([ended count]); ++i) {
148 NSTouch *touch = [[ended allObjects] objectAtIndex:i];
149 QCocoaTouch *qcocoaTouch = findQCocoaTouch(touch);
150 if (qcocoaTouch) {
151 qcocoaTouch->updateTouchData(touch, [touch phase]);
152 if (!_updateInternalStateOnly)
153 touchPoints.insert(qcocoaTouch->_touchPoint.id(), qcocoaTouch->_touchPoint);
154 delete qcocoaTouch;
155 }
156 }
157
158 bool wasUpdateInternalStateOnly = _updateInternalStateOnly;
159 _updateInternalStateOnly = !acceptSingleTouch && _touchCount < 2;
160
161 // Next: update, or create, existing touches.
162 // We always keep track of all touch points, even
163 // when not accepting single touches.
164
165 for (int i=0; i<int([active count]); ++i) {
166 NSTouch *touch = [[active allObjects] objectAtIndex:i];
167 QCocoaTouch *qcocoaTouch = findQCocoaTouch(touch);
168 if (!qcocoaTouch)
169 qcocoaTouch = new QCocoaTouch(touch);
170 else
171 qcocoaTouch->updateTouchData(touch, wasUpdateInternalStateOnly ? NSTouchPhaseBegan : [touch phase]);
172 if (!_updateInternalStateOnly)
173 touchPoints.insert(qcocoaTouch->_touchPoint.id(), qcocoaTouch->_touchPoint);
174 }
175
176 // Next: sadly, we need to check that our touch hash is in
177 // sync with cocoa. This is typically not the case after a system
178 // gesture happend (like a four-finger-swipe to show expose).
179
180 if (_touchCount != _currentTouches.size()) {
181 // Remove all instances, and basically start from scratch:
182 touchPoints.clear();
183 foreach (QCocoaTouch *qcocoaTouch, _currentTouches.values()) {
184 if (!_updateInternalStateOnly) {
185 qcocoaTouch->_touchPoint.setState(Qt::TouchPointReleased);
186 touchPoints.insert(qcocoaTouch->_touchPoint.id(), qcocoaTouch->_touchPoint);
187 }
188 delete qcocoaTouch;
189 }
190 _currentTouches.clear();
191 _updateInternalStateOnly = !acceptSingleTouch;
192 return touchPoints.values();
193 }
194
195 // Finally: If this call _started_ to reject single
196 // touches, we need to fake a relase for the remaining
197 // touch now (and refake a begin for it later, if needed).
198
199 if (_updateInternalStateOnly && !wasUpdateInternalStateOnly && !_currentTouches.isEmpty()) {
200 QCocoaTouch *qcocoaTouch = _currentTouches.values().first();
201 qcocoaTouch->_touchPoint.setState(Qt::TouchPointReleased);
202 touchPoints.insert(qcocoaTouch->_touchPoint.id(), qcocoaTouch->_touchPoint);
203 // Since this last touch also will end up beeing the first
204 // touch (if the user adds a second finger without lifting
205 // the first), we promote it to be the primary touch:
206 qcocoaTouch->_touchPoint.setId(0);
207 _idAssignmentCount = 1;
208 }
209
210 return touchPoints.values();
211}
212
213#endif
214
215QT_END_NAMESPACE
216
217#endif // MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
218
Note: See TracBrowser for help on using the repository browser.