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

Last change on this file since 642 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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];