|
1 /* |
|
2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
|
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * 1. Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
|
11 * notice, this list of conditions and the following disclaimer in the |
|
12 * documentation and/or other materials provided with the distribution. |
|
13 * |
|
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
|
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
|
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|
24 * THE POSSIBILITY OF SUCH DAMAGE. |
|
25 */ |
|
26 |
|
27 #include "WebEventFactoryQt.h" |
|
28 |
|
29 #include <qgraphicssceneevent.h> |
|
30 #include <QApplication> |
|
31 #include <QKeyEvent> |
|
32 #include <WebCore/PlatformKeyboardEvent.h> |
|
33 #include <wtf/ASCIICType.h> |
|
34 #include <wtf/CurrentTime.h> |
|
35 |
|
36 using namespace WebCore; |
|
37 |
|
38 namespace WebKit { |
|
39 |
|
40 static WebMouseEvent::Button mouseButtonForEvent(QGraphicsSceneMouseEvent *event) |
|
41 { |
|
42 if (event->button() == Qt::LeftButton || (event->buttons() & Qt::LeftButton)) |
|
43 return WebMouseEvent::LeftButton; |
|
44 else if (event->button() == Qt::RightButton || (event->buttons() & Qt::RightButton)) |
|
45 return WebMouseEvent::RightButton; |
|
46 else if (event->button() == Qt::MidButton || (event->buttons() & Qt::MidButton)) |
|
47 return WebMouseEvent::MiddleButton; |
|
48 return WebMouseEvent::NoButton; |
|
49 } |
|
50 |
|
51 static WebEvent::Type webEventTypeForEvent(QEvent* event) |
|
52 { |
|
53 switch (event->type()) { |
|
54 case QEvent::GraphicsSceneMouseDoubleClick: |
|
55 case QEvent::GraphicsSceneMousePress: |
|
56 return WebEvent::MouseDown; |
|
57 case QEvent::GraphicsSceneMouseRelease: |
|
58 return WebEvent::MouseUp; |
|
59 case QEvent::GraphicsSceneMouseMove: |
|
60 return WebEvent::MouseMove; |
|
61 case QEvent::Wheel: |
|
62 return WebEvent::Wheel; |
|
63 case QEvent::KeyPress: |
|
64 return WebEvent::KeyDown; |
|
65 case QEvent::KeyRelease: |
|
66 return WebEvent::KeyUp; |
|
67 default: |
|
68 // assert |
|
69 return WebEvent::MouseMove; |
|
70 } |
|
71 } |
|
72 |
|
73 static inline WebEvent::Modifiers modifiersForEvent(Qt::KeyboardModifiers modifiers) |
|
74 { |
|
75 unsigned result = 0; |
|
76 if (modifiers & Qt::ShiftModifier) |
|
77 result |= WebEvent::ShiftKey; |
|
78 if (modifiers & Qt::ControlModifier) |
|
79 result |= WebEvent::ControlKey; |
|
80 if (modifiers & Qt::AltModifier) |
|
81 result |= WebEvent::AltKey; |
|
82 if (modifiers & Qt::MetaModifier) |
|
83 result |= WebEvent::MetaKey; |
|
84 return (WebEvent::Modifiers)result; |
|
85 } |
|
86 |
|
87 WebMouseEvent WebEventFactory::createWebMouseEvent(QGraphicsSceneMouseEvent* event, int eventClickCount) |
|
88 { |
|
89 IntPoint position(event->pos().toPoint()); |
|
90 IntPoint globalPosition(event->screenPos()); |
|
91 |
|
92 WebEvent::Type type = webEventTypeForEvent(event); |
|
93 WebMouseEvent::Button button = mouseButtonForEvent(event); |
|
94 int positionX = position.x(); |
|
95 int positionY = position.y(); |
|
96 int globalPositionX = globalPosition.x(); |
|
97 int globalPositionY = globalPosition.y(); |
|
98 int clickCount = eventClickCount; |
|
99 WebEvent::Modifiers modifiers = modifiersForEvent(event->modifiers()); |
|
100 double timestamp = WTF::currentTime(); |
|
101 |
|
102 return WebMouseEvent(type, button, positionX, positionY, globalPositionX, globalPositionY, clickCount, modifiers, timestamp); |
|
103 } |
|
104 |
|
105 WebWheelEvent WebEventFactory::createWebWheelEvent(QGraphicsSceneWheelEvent* e) |
|
106 { |
|
107 int x = e->pos().x(); |
|
108 int y = e->pos().y(); |
|
109 int globalX = e->screenPos().x(); |
|
110 int globalY = e->screenPos().y(); |
|
111 float deltaX = 0; |
|
112 float deltaY = 0; |
|
113 float wheelTicksX = 0; |
|
114 float wheelTicksY = 0; |
|
115 WebWheelEvent::Granularity granularity = WebWheelEvent::ScrollByPixelWheelEvent; |
|
116 WebEvent::Modifiers modifiers = modifiersForEvent(e->modifiers()); |
|
117 double timestamp = WTF::currentTime(); |
|
118 |
|
119 // A delta that is not mod 120 indicates a device that is sending |
|
120 // fine-resolution scroll events, so use the delta as number of wheel ticks |
|
121 // and number of pixels to scroll.See also webkit.org/b/29601 |
|
122 bool fullTick = !(e->delta() % 120); |
|
123 |
|
124 if (e->orientation() == Qt::Horizontal) { |
|
125 deltaX = (fullTick) ? e->delta() / 120.0f : e->delta(); |
|
126 wheelTicksX = deltaX; |
|
127 } else { |
|
128 deltaY = (fullTick) ? e->delta() / 120.0f : e->delta(); |
|
129 wheelTicksY = deltaY; |
|
130 } |
|
131 |
|
132 // Use the same single scroll step as QTextEdit |
|
133 // (in QTextEditPrivate::init [h,v]bar->setSingleStep) |
|
134 static const float cDefaultQtScrollStep = 20.f; |
|
135 #ifndef QT_NO_WHEELEVENT |
|
136 deltaX *= (fullTick) ? QApplication::wheelScrollLines() * cDefaultQtScrollStep : 1; |
|
137 deltaY *= (fullTick) ? QApplication::wheelScrollLines() * cDefaultQtScrollStep : 1; |
|
138 #endif |
|
139 |
|
140 return WebWheelEvent(WebEvent::Wheel, x, y, globalX, globalY, deltaX, deltaY, wheelTicksX, wheelTicksY, granularity, modifiers, timestamp); |
|
141 } |
|
142 |
|
143 WebKeyboardEvent WebEventFactory::createWebKeyboardEvent(QKeyEvent* event) |
|
144 { |
|
145 const int state = event->modifiers(); |
|
146 WebEvent::Type type = webEventTypeForEvent(event); |
|
147 const String text = event->text(); |
|
148 const String unmodifiedText = event->text(); |
|
149 bool isAutoRepeat = event->isAutoRepeat(); |
|
150 bool isSystemKey = false; // FIXME: No idea what that is. |
|
151 bool isKeypad = (state & Qt::KeypadModifier); |
|
152 const String keyIdentifier = keyIdentifierForQtKeyCode(event->key()); |
|
153 int windowsVirtualKeyCode = windowsKeyCodeForKeyEvent(event->key(), isKeypad); |
|
154 int nativeVirtualKeyCode = event->nativeVirtualKey(); |
|
155 WebEvent::Modifiers modifiers = modifiersForEvent(event->modifiers()); |
|
156 double timestamp = WTF::currentTime(); |
|
157 |
|
158 return WebKeyboardEvent(type, text, unmodifiedText, keyIdentifier, windowsVirtualKeyCode, nativeVirtualKeyCode, isAutoRepeat, isKeypad, isSystemKey, modifiers, timestamp); |
|
159 } |
|
160 |
|
161 } // namespace WebKit |