EventSenderQt.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
  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. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "config.h"
  30. #include "EventSenderQt.h"
  31. #include <QGestureEvent>
  32. #include <QGraphicsSceneMouseEvent>
  33. #include <QtTest/QtTest>
  34. #include <qpa/qwindowsysteminterface.h>
  35. #define KEYCODE_DEL 127
  36. #define KEYCODE_BACKSPACE 8
  37. #define KEYCODE_LEFTARROW 0xf702
  38. #define KEYCODE_RIGHTARROW 0xf703
  39. #define KEYCODE_UPARROW 0xf700
  40. #define KEYCODE_DOWNARROW 0xf701
  41. // Ports like Gtk and Windows expose a different approach for their zooming
  42. // API if compared to Qt: they have specific methods for zooming in and out,
  43. // as well as a settable zoom factor, while Qt has only a 'setZoomValue' method.
  44. // Hence Qt DRT adopts a fixed zoom-factor (1.2) for compatibility.
  45. #define ZOOM_STEP 1.2
  46. #define DRT_MESSAGE_DONE (QEvent::User + 1)
  47. struct DRTEventQueue {
  48. QEvent* m_event;
  49. int m_delay;
  50. };
  51. static DRTEventQueue eventQueue[1024];
  52. static unsigned endOfQueue;
  53. static unsigned startOfQueue;
  54. EventSender::EventSender(QWebPage* parent)
  55. : QObject(parent)
  56. #ifndef QT_NO_GESTURES
  57. , m_tapGesture(parent)
  58. , m_tapAndHoldGesture(parent)
  59. #endif
  60. {
  61. m_page = parent;
  62. m_mouseButtonPressed = false;
  63. m_drag = false;
  64. memset(eventQueue, 0, sizeof(eventQueue));
  65. endOfQueue = 0;
  66. startOfQueue = 0;
  67. m_eventLoop = 0;
  68. m_currentButton = 0;
  69. m_currentDragActionsAllowed = 0;
  70. resetClickCount();
  71. m_page->view()->installEventFilter(this);
  72. // This is a hack that works because we normally scroll 60 pixels (3*20) per tick, but Apple scrolls 120.
  73. // But Apple also has a bug where they report lines instead of ticks in PlatformWheelEvent, making 2 lines = 40 pixels match.
  74. QApplication::setWheelScrollLines(2);
  75. }
  76. static Qt::KeyboardModifiers getModifiers(const QStringList& modifiers)
  77. {
  78. Qt::KeyboardModifiers modifs = 0;
  79. for (int i = 0; i < modifiers.size(); ++i) {
  80. const QString& m = modifiers.at(i);
  81. if (m == "ctrlKey")
  82. modifs |= Qt::ControlModifier;
  83. else if (m == "shiftKey")
  84. modifs |= Qt::ShiftModifier;
  85. else if (m == "altKey")
  86. modifs |= Qt::AltModifier;
  87. else if (m == "metaKey")
  88. modifs |= Qt::MetaModifier;
  89. }
  90. return modifs;
  91. }
  92. void EventSender::mouseDown(int button, const QStringList& modifiers)
  93. {
  94. Qt::KeyboardModifiers modifs = getModifiers(modifiers);
  95. Qt::MouseButton mouseButton;
  96. switch (button) {
  97. case 0:
  98. mouseButton = Qt::LeftButton;
  99. break;
  100. case 1:
  101. mouseButton = Qt::MidButton;
  102. break;
  103. case 2:
  104. mouseButton = Qt::RightButton;
  105. break;
  106. case 3:
  107. // fast/events/mouse-click-events expects the 4th button to be treated as the middle button
  108. mouseButton = Qt::MidButton;
  109. break;
  110. default:
  111. mouseButton = Qt::LeftButton;
  112. break;
  113. }
  114. // only consider a click to count, an event originated by the
  115. // same previous button and at the same position.
  116. if (m_currentButton == button
  117. && m_mousePos == m_clickPos
  118. && m_clickTimer.isActive())
  119. m_clickCount++;
  120. else
  121. m_clickCount = 1;
  122. m_currentButton = button;
  123. m_clickPos = m_mousePos;
  124. m_mouseButtons |= mouseButton;
  125. // qDebug() << "EventSender::mouseDown" << frame;
  126. QEvent* event;
  127. if (isGraphicsBased()) {
  128. event = createGraphicsSceneMouseEvent((m_clickCount == 2) ?
  129. QEvent::GraphicsSceneMouseDoubleClick : QEvent::GraphicsSceneMousePress,
  130. m_mousePos, m_mousePos, mouseButton, m_mouseButtons, modifs);
  131. } else {
  132. event = new QMouseEvent((m_clickCount == 2) ? QEvent::MouseButtonDblClick :
  133. QEvent::MouseButtonPress, m_mousePos, m_mousePos,
  134. mouseButton, m_mouseButtons, modifs);
  135. }
  136. sendOrQueueEvent(event);
  137. m_clickTimer.start(QApplication::doubleClickInterval(), this);
  138. }
  139. void EventSender::mouseUp(int button)
  140. {
  141. Qt::MouseButton mouseButton;
  142. switch (button) {
  143. case 0:
  144. mouseButton = Qt::LeftButton;
  145. break;
  146. case 1:
  147. mouseButton = Qt::MidButton;
  148. break;
  149. case 2:
  150. mouseButton = Qt::RightButton;
  151. break;
  152. case 3:
  153. // fast/events/mouse-click-events expects the 4th button to be treated as the middle button
  154. mouseButton = Qt::MidButton;
  155. break;
  156. default:
  157. mouseButton = Qt::LeftButton;
  158. break;
  159. }
  160. m_mouseButtons &= ~mouseButton;
  161. // qDebug() << "EventSender::mouseUp" << frame;
  162. QEvent* event;
  163. if (isGraphicsBased()) {
  164. event = createGraphicsSceneMouseEvent(QEvent::GraphicsSceneMouseRelease,
  165. m_mousePos, m_mousePos, mouseButton, m_mouseButtons, Qt::NoModifier);
  166. } else {
  167. event = new QMouseEvent(QEvent::MouseButtonRelease,
  168. m_mousePos, m_mousePos, mouseButton, m_mouseButtons, Qt::NoModifier);
  169. }
  170. sendOrQueueEvent(event);
  171. if (m_currentDragData.urls().isEmpty())
  172. return;
  173. event = new QDropEvent(m_mousePos, m_currentDragActionsAllowed, &m_currentDragData, m_mouseButtons, Qt::NoModifier);
  174. sendEvent(m_page, event);
  175. m_currentDragData.clear();
  176. }
  177. void EventSender::mouseMoveTo(int x, int y)
  178. {
  179. // qDebug() << "EventSender::mouseMoveTo" << x << y;
  180. m_mousePos = QPoint(x, y);
  181. QEvent* event;
  182. if (isGraphicsBased()) {
  183. event = createGraphicsSceneMouseEvent(QEvent::GraphicsSceneMouseMove,
  184. m_mousePos, m_mousePos, Qt::NoButton, m_mouseButtons, Qt::NoModifier);
  185. } else {
  186. event = new QMouseEvent(QEvent::MouseMove,
  187. m_mousePos, m_mousePos, Qt::NoButton, m_mouseButtons, Qt::NoModifier);
  188. }
  189. sendOrQueueEvent(event);
  190. if (m_currentDragData.urls().isEmpty())
  191. return;
  192. Qt::MouseButtons mouseButtons = m_mouseButtons | Qt::LeftButton;
  193. event = new QDragMoveEvent(m_mousePos, m_currentDragActionsAllowed, &m_currentDragData, mouseButtons, Qt::NoModifier);
  194. sendEvent(m_page, event);
  195. }
  196. // Simulates a mouse down event for drag without sending an actual mouse down event.
  197. void EventSender::beginDragWithFiles(const QStringList& files)
  198. {
  199. m_currentDragData.clear();
  200. QList<QUrl> fileUrls;
  201. QUrl baseUrl = m_page->mainFrame()->baseUrl();
  202. foreach (const QString& file, files) {
  203. QUrl resolvedUrl = baseUrl.resolved(file);
  204. fileUrls.append(resolvedUrl);
  205. }
  206. m_currentDragData.setUrls(fileUrls);
  207. m_currentDragActionsAllowed = Qt::CopyAction;
  208. Qt::MouseButtons mouseButtons = m_mouseButtons | Qt::LeftButton;
  209. QDragEnterEvent* event = new QDragEnterEvent(m_mousePos, m_currentDragActionsAllowed, &m_currentDragData, mouseButtons, Qt::NoModifier);
  210. sendEvent(m_page, event);
  211. }
  212. #ifndef QT_NO_WHEELEVENT
  213. void EventSender::mouseScrollBy(int ticksX, int ticksY)
  214. {
  215. const int tickStep = QApplication::wheelScrollLines() * 20;
  216. continuousMouseScrollBy((ticksX * tickStep), (ticksY * tickStep));
  217. }
  218. void EventSender::continuousMouseScrollBy(int x, int y)
  219. {
  220. // continuousMouseScrollBy() mimics devices that send fine-grained scroll events where the 'delta' specified is not the usual
  221. // multiple of 120. See http://doc.qt.nokia.com/4.6/qwheelevent.html#delta for a good explanation of this.
  222. // A wheel delta of 120 (in 1/8 degrees) corresponds to one wheel tick, and we scroll tickStep pixels per wheel tick.
  223. const int tickStep = QApplication::wheelScrollLines() * 20;
  224. if (x) {
  225. QEvent* event;
  226. if (isGraphicsBased()) {
  227. event = createGraphicsSceneWheelEvent(QEvent::GraphicsSceneWheel,
  228. m_mousePos, m_mousePos, (x * 120) / tickStep, Qt::NoModifier, Qt::Horizontal);
  229. } else
  230. event = new QWheelEvent(m_mousePos, m_mousePos, (x * 120) / tickStep, m_mouseButtons, Qt::NoModifier, Qt::Horizontal);
  231. sendOrQueueEvent(event);
  232. }
  233. if (y) {
  234. QEvent* event;
  235. if (isGraphicsBased()) {
  236. event = createGraphicsSceneWheelEvent(QEvent::GraphicsSceneWheel,
  237. m_mousePos, m_mousePos, (y * 120) / tickStep, Qt::NoModifier, Qt::Vertical);
  238. } else
  239. event = new QWheelEvent(m_mousePos, m_mousePos, (y * 120) / tickStep, m_mouseButtons, Qt::NoModifier, Qt::Vertical);
  240. sendOrQueueEvent(event);
  241. }
  242. }
  243. #endif
  244. void EventSender::leapForward(int ms)
  245. {
  246. eventQueue[endOfQueue].m_delay = ms;
  247. //qDebug() << "EventSender::leapForward" << ms;
  248. }
  249. void EventSender::keyDown(const QString& string, const QStringList& modifiers, unsigned int location)
  250. {
  251. QString s = string;
  252. Qt::KeyboardModifiers modifs = getModifiers(modifiers);
  253. if (location == 3)
  254. modifs |= Qt::KeypadModifier;
  255. int code = 0;
  256. if (string.length() == 1) {
  257. code = string.unicode()->unicode();
  258. //qDebug() << ">>>>>>>>> keyDown" << code << (char)code;
  259. // map special keycodes used by the tests to something that works for Qt/X11
  260. if (code == '\r') {
  261. code = Qt::Key_Return;
  262. } else if (code == '\t') {
  263. code = Qt::Key_Tab;
  264. if (modifs == Qt::ShiftModifier)
  265. code = Qt::Key_Backtab;
  266. s = QString();
  267. } else if (code == KEYCODE_DEL || code == KEYCODE_BACKSPACE) {
  268. code = Qt::Key_Backspace;
  269. if (modifs == Qt::AltModifier)
  270. modifs = Qt::ControlModifier;
  271. s = QString();
  272. } else if (code == 'o' && modifs == Qt::ControlModifier) {
  273. // Mimic the emacs ctrl-o binding on Mac by inserting a paragraph
  274. // separator and then putting the cursor back to its original
  275. // position. Allows us to pass emacs-ctrl-o.html
  276. s = QLatin1String("\n");
  277. code = '\n';
  278. modifs = 0;
  279. QKeyEvent event(QEvent::KeyPress, code, modifs, s);
  280. sendEvent(m_page, &event);
  281. QKeyEvent event2(QEvent::KeyRelease, code, modifs, s);
  282. sendEvent(m_page, &event2);
  283. s = QString();
  284. code = Qt::Key_Left;
  285. } else if (code == 'y' && modifs == Qt::ControlModifier) {
  286. s = QLatin1String("c");
  287. code = 'c';
  288. } else if (code == 'k' && modifs == Qt::ControlModifier) {
  289. s = QLatin1String("x");
  290. code = 'x';
  291. } else if (code == 'a' && modifs == Qt::ControlModifier) {
  292. s = QString();
  293. code = Qt::Key_Home;
  294. modifs = 0;
  295. } else if (code == KEYCODE_LEFTARROW) {
  296. s = QString();
  297. code = Qt::Key_Left;
  298. if (modifs & Qt::MetaModifier) {
  299. code = Qt::Key_Home;
  300. modifs &= ~Qt::MetaModifier;
  301. }
  302. } else if (code == KEYCODE_RIGHTARROW) {
  303. s = QString();
  304. code = Qt::Key_Right;
  305. if (modifs & Qt::MetaModifier) {
  306. code = Qt::Key_End;
  307. modifs &= ~Qt::MetaModifier;
  308. }
  309. } else if (code == KEYCODE_UPARROW) {
  310. s = QString();
  311. code = Qt::Key_Up;
  312. if (modifs & Qt::MetaModifier) {
  313. code = Qt::Key_PageUp;
  314. modifs &= ~Qt::MetaModifier;
  315. }
  316. } else if (code == KEYCODE_DOWNARROW) {
  317. s = QString();
  318. code = Qt::Key_Down;
  319. if (modifs & Qt::MetaModifier) {
  320. code = Qt::Key_PageDown;
  321. modifs &= ~Qt::MetaModifier;
  322. }
  323. } else if (code == 'a' && modifs == Qt::ControlModifier) {
  324. s = QString();
  325. code = Qt::Key_Home;
  326. modifs = 0;
  327. } else
  328. code = string.unicode()->toUpper().unicode();
  329. } else {
  330. //qDebug() << ">>>>>>>>> keyDown" << string;
  331. if (string.startsWith(QLatin1Char('F')) && string.count() <= 3) {
  332. s = s.mid(1);
  333. int functionKey = s.toInt();
  334. Q_ASSERT(functionKey >= 1 && functionKey <= 35);
  335. code = Qt::Key_F1 + (functionKey - 1);
  336. // map special keycode strings used by the tests to something that works for Qt/X11
  337. } else if (string == QLatin1String("leftArrow")) {
  338. s = QString();
  339. code = Qt::Key_Left;
  340. } else if (string == QLatin1String("rightArrow")) {
  341. s = QString();
  342. code = Qt::Key_Right;
  343. } else if (string == QLatin1String("upArrow")) {
  344. s = QString();
  345. code = Qt::Key_Up;
  346. } else if (string == QLatin1String("downArrow")) {
  347. s = QString();
  348. code = Qt::Key_Down;
  349. } else if (string == QLatin1String("pageUp")) {
  350. s = QString();
  351. code = Qt::Key_PageUp;
  352. } else if (string == QLatin1String("pageDown")) {
  353. s = QString();
  354. code = Qt::Key_PageDown;
  355. } else if (string == QLatin1String("home")) {
  356. s = QString();
  357. code = Qt::Key_Home;
  358. } else if (string == QLatin1String("end")) {
  359. s = QString();
  360. code = Qt::Key_End;
  361. } else if (string == QLatin1String("insert")) {
  362. s = QString();
  363. code = Qt::Key_Insert;
  364. } else if (string == QLatin1String("delete")) {
  365. s = QString();
  366. code = Qt::Key_Delete;
  367. } else if (string == QLatin1String("printScreen")) {
  368. s = QString();
  369. code = Qt::Key_Print;
  370. } else if (string == QLatin1String("menu")) {
  371. s = QString();
  372. code = Qt::Key_Menu;
  373. }
  374. }
  375. QKeyEvent event(QEvent::KeyPress, code, modifs, s);
  376. sendEvent(m_page, &event);
  377. QKeyEvent event2(QEvent::KeyRelease, code, modifs, s);
  378. sendEvent(m_page, &event2);
  379. }
  380. QStringList EventSender::contextClick()
  381. {
  382. QMouseEvent event(QEvent::MouseButtonPress, m_mousePos, Qt::RightButton, Qt::RightButton, Qt::NoModifier);
  383. sendEvent(m_page, &event);
  384. QMouseEvent event2(QEvent::MouseButtonRelease, m_mousePos, Qt::RightButton, Qt::RightButton, Qt::NoModifier);
  385. sendEvent(m_page, &event2);
  386. if (isGraphicsBased()) {
  387. QGraphicsSceneContextMenuEvent ctxEvent(QEvent::GraphicsSceneContextMenu);
  388. ctxEvent.setReason(QGraphicsSceneContextMenuEvent::Mouse);
  389. ctxEvent.setPos(m_mousePos);
  390. WebViewGraphicsBased* view = qobject_cast<WebViewGraphicsBased*>(m_page->view());
  391. if (view)
  392. sendEvent(view->graphicsView(), &ctxEvent);
  393. } else {
  394. QContextMenuEvent ctxEvent(QContextMenuEvent::Mouse, m_mousePos);
  395. sendEvent(m_page->view(), &ctxEvent);
  396. }
  397. return DumpRenderTreeSupportQt::contextMenu(m_page->handle());
  398. }
  399. void EventSender::scheduleAsynchronousClick()
  400. {
  401. QMouseEvent* event = new QMouseEvent(QEvent::MouseButtonPress, m_mousePos, Qt::LeftButton, Qt::RightButton, Qt::NoModifier);
  402. postEvent(m_page, event);
  403. QMouseEvent* event2 = new QMouseEvent(QEvent::MouseButtonRelease, m_mousePos, Qt::LeftButton, Qt::RightButton, Qt::NoModifier);
  404. postEvent(m_page, event2);
  405. }
  406. void EventSender::addTouchPoint(int x, int y)
  407. {
  408. const int id = m_touchPoints.isEmpty() ? 0 : m_touchPoints.last().id() + 1;
  409. const QPointF pos(x, y);
  410. QTouchEvent::TouchPoint point(id);
  411. point.setPos(pos);
  412. point.setStartPos(pos);
  413. point.setState(Qt::TouchPointPressed);
  414. if (!m_touchPointRadius.isNull())
  415. point.setRect(QRectF(pos - m_touchPointRadius, pos + m_touchPointRadius));
  416. m_touchPoints.append(point);
  417. }
  418. void EventSender::updateTouchPoint(int index, int x, int y)
  419. {
  420. if (index < 0 || index >= m_touchPoints.count())
  421. return;
  422. const QPointF pos(x, y);
  423. QTouchEvent::TouchPoint &point = m_touchPoints[index];
  424. point.setPos(pos);
  425. point.setState(Qt::TouchPointMoved);
  426. if (!m_touchPointRadius.isNull())
  427. point.setRect(QRectF(pos - m_touchPointRadius, pos + m_touchPointRadius));
  428. }
  429. void EventSender::setTouchModifier(const QString &modifier, bool enable)
  430. {
  431. Qt::KeyboardModifier mod = Qt::NoModifier;
  432. if (!modifier.compare(QLatin1String("shift"), Qt::CaseInsensitive))
  433. mod = Qt::ShiftModifier;
  434. else if (!modifier.compare(QLatin1String("alt"), Qt::CaseInsensitive))
  435. mod = Qt::AltModifier;
  436. else if (!modifier.compare(QLatin1String("meta"), Qt::CaseInsensitive))
  437. mod = Qt::MetaModifier;
  438. else if (!modifier.compare(QLatin1String("ctrl"), Qt::CaseInsensitive))
  439. mod = Qt::ControlModifier;
  440. if (enable)
  441. m_touchModifiers |= mod;
  442. else
  443. m_touchModifiers &= ~mod;
  444. }
  445. void EventSender::setTouchPointRadius(int radiusX, int radiusY)
  446. {
  447. m_touchPointRadius = QPoint(radiusX, radiusY);
  448. }
  449. void EventSender::touchStart()
  450. {
  451. if (!m_touchActive) {
  452. sendTouchEvent(QEvent::TouchBegin);
  453. m_touchActive = true;
  454. } else
  455. sendTouchEvent(QEvent::TouchUpdate);
  456. }
  457. void EventSender::touchMove()
  458. {
  459. sendTouchEvent(QEvent::TouchUpdate);
  460. }
  461. void EventSender::touchEnd()
  462. {
  463. for (int i = 0; i < m_touchPoints.count(); ++i)
  464. if (m_touchPoints[i].state() != Qt::TouchPointReleased) {
  465. sendTouchEvent(QEvent::TouchUpdate);
  466. return;
  467. }
  468. sendTouchEvent(QEvent::TouchEnd);
  469. m_touchActive = false;
  470. }
  471. void EventSender::touchCancel()
  472. {
  473. sendTouchEvent(QEvent::TouchCancel);
  474. m_touchActive = false;
  475. }
  476. void EventSender::clearTouchPoints()
  477. {
  478. m_touchPoints.clear();
  479. m_touchModifiers = Qt::KeyboardModifiers();
  480. m_touchActive = false;
  481. m_touchPointRadius = QPoint();
  482. }
  483. void EventSender::releaseTouchPoint(int index)
  484. {
  485. if (index < 0 || index >= m_touchPoints.count())
  486. return;
  487. m_touchPoints[index].setState(Qt::TouchPointReleased);
  488. }
  489. void EventSender::cancelTouchPoint(int index)
  490. {
  491. // FIXME: No cancellation state in Qt 5, mapped to release instead.
  492. // PlatformTouchEvent conversion later will map all touch points to
  493. // cancelled.
  494. releaseTouchPoint(index);
  495. }
  496. void EventSender::sendTouchEvent(QEvent::Type type)
  497. {
  498. static QTouchDevice* device = 0;
  499. if (!device) {
  500. device = new QTouchDevice;
  501. device->setType(QTouchDevice::TouchScreen);
  502. QWindowSystemInterface::registerTouchDevice(device);
  503. }
  504. QTouchEvent event(type, device, m_touchModifiers);
  505. event.setTouchPoints(m_touchPoints);
  506. sendEvent(m_page, &event);
  507. QList<QTouchEvent::TouchPoint>::Iterator it = m_touchPoints.begin();
  508. while (it != m_touchPoints.end()) {
  509. if (it->state() == Qt::TouchPointReleased)
  510. it = m_touchPoints.erase(it);
  511. else {
  512. it->setState(Qt::TouchPointStationary);
  513. ++it;
  514. }
  515. }
  516. }
  517. #ifndef QT_NO_GESTURES
  518. void EventSender::gestureTap(int x, int y)
  519. {
  520. m_tapGesture.setPosition(QPointF(x, y));
  521. m_gestures.clear();
  522. m_gestures.append(&m_tapGesture);
  523. QGestureEvent event(m_gestures);
  524. sendEvent(m_page, &event);
  525. }
  526. void EventSender::gestureLongPress(int x, int y)
  527. {
  528. m_tapAndHoldGesture.setPosition(QPointF(x, y));
  529. m_gestures.clear();
  530. m_gestures.append(&m_tapAndHoldGesture);
  531. QGestureEvent event(m_gestures);
  532. sendEvent(m_page, &event);
  533. }
  534. #endif
  535. void EventSender::zoomPageIn()
  536. {
  537. if (QWebFrame* frame = m_page->mainFrame())
  538. frame->setZoomFactor(frame->zoomFactor() * ZOOM_STEP);
  539. }
  540. void EventSender::zoomPageOut()
  541. {
  542. if (QWebFrame* frame = m_page->mainFrame())
  543. frame->setZoomFactor(frame->zoomFactor() / ZOOM_STEP);
  544. }
  545. void EventSender::textZoomIn()
  546. {
  547. if (QWebFrame* frame = m_page->mainFrame())
  548. frame->setTextSizeMultiplier(frame->textSizeMultiplier() * ZOOM_STEP);
  549. }
  550. void EventSender::textZoomOut()
  551. {
  552. if (QWebFrame* frame = m_page->mainFrame())
  553. frame->setTextSizeMultiplier(frame->textSizeMultiplier() / ZOOM_STEP);
  554. }
  555. void EventSender::scalePageBy(float scaleFactor, float x, float y)
  556. {
  557. if (QWebFrame* frame = m_page->mainFrame())
  558. DumpRenderTreeSupportQt::scalePageBy(frame->handle(), scaleFactor, QPoint(x, y));
  559. }
  560. QWebFrame* EventSender::frameUnderMouse() const
  561. {
  562. QWebFrame* frame = m_page->mainFrame();
  563. redo:
  564. QList<QWebFrame*> children = frame->childFrames();
  565. for (int i = 0; i < children.size(); ++i) {
  566. if (children.at(i)->geometry().contains(m_mousePos)) {
  567. frame = children.at(i);
  568. goto redo;
  569. }
  570. }
  571. if (frame->geometry().contains(m_mousePos))
  572. return frame;
  573. return 0;
  574. }
  575. void EventSender::sendOrQueueEvent(QEvent* event)
  576. {
  577. // Mouse move events are queued if
  578. // 1. A previous event was queued.
  579. // 2. A delay was set-up by leapForward().
  580. // 3. A call to mouseMoveTo while the mouse button is pressed could initiate a drag operation, and that does not return until mouseUp is processed.
  581. // To be safe and avoid a deadlock, this event is queued.
  582. if (endOfQueue == startOfQueue && !eventQueue[endOfQueue].m_delay && (!(m_mouseButtonPressed && (m_eventLoop && event->type() == QEvent::MouseButtonRelease)))) {
  583. sendEvent(m_page->view(), event);
  584. delete event;
  585. return;
  586. }
  587. eventQueue[endOfQueue++].m_event = event;
  588. eventQueue[endOfQueue].m_delay = 0;
  589. replaySavedEvents(event->type() != QEvent::MouseMove);
  590. }
  591. void EventSender::replaySavedEvents(bool flush)
  592. {
  593. if (startOfQueue < endOfQueue) {
  594. // First send all the events that are ready to be sent
  595. while (!eventQueue[startOfQueue].m_delay && startOfQueue < endOfQueue) {
  596. QEvent* ev = eventQueue[startOfQueue++].m_event;
  597. postEvent(m_page->view(), ev);
  598. }
  599. if (startOfQueue == endOfQueue) {
  600. // Reset the queue
  601. startOfQueue = 0;
  602. endOfQueue = 0;
  603. } else {
  604. QTest::qWait(eventQueue[startOfQueue].m_delay);
  605. eventQueue[startOfQueue].m_delay = 0;
  606. }
  607. }
  608. if (!flush)
  609. return;
  610. // Send a marker event, it will tell us when it is safe to exit the new event loop
  611. QEvent* drtEvent = new QEvent((QEvent::Type)DRT_MESSAGE_DONE);
  612. QApplication::postEvent(m_page->view(), drtEvent);
  613. // Start an event loop for async handling of Drag & Drop
  614. m_eventLoop = new QEventLoop;
  615. m_eventLoop->exec();
  616. delete m_eventLoop;
  617. m_eventLoop = 0;
  618. }
  619. bool EventSender::eventFilter(QObject* watched, QEvent* event)
  620. {
  621. if (watched != m_page->view())
  622. return false;
  623. switch (event->type()) {
  624. case QEvent::Leave:
  625. return true;
  626. case QEvent::MouseButtonPress:
  627. case QEvent::GraphicsSceneMousePress:
  628. m_mouseButtonPressed = true;
  629. break;
  630. case QEvent::MouseMove:
  631. case QEvent::GraphicsSceneMouseMove:
  632. if (m_mouseButtonPressed)
  633. m_drag = true;
  634. break;
  635. case QEvent::MouseButtonRelease:
  636. case QEvent::GraphicsSceneMouseRelease:
  637. m_mouseButtonPressed = false;
  638. m_drag = false;
  639. break;
  640. case DRT_MESSAGE_DONE:
  641. m_eventLoop->exit();
  642. return true;
  643. }
  644. return false;
  645. }
  646. void EventSender::timerEvent(QTimerEvent* ev)
  647. {
  648. m_clickTimer.stop();
  649. }
  650. QGraphicsSceneMouseEvent* EventSender::createGraphicsSceneMouseEvent(QEvent::Type type, const QPoint& pos, const QPoint& screenPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
  651. {
  652. QGraphicsSceneMouseEvent* event;
  653. event = new QGraphicsSceneMouseEvent(type);
  654. event->setPos(pos);
  655. event->setScreenPos(screenPos);
  656. event->setButton(button);
  657. event->setButtons(buttons);
  658. event->setModifiers(modifiers);
  659. return event;
  660. }
  661. QGraphicsSceneWheelEvent* EventSender::createGraphicsSceneWheelEvent(QEvent::Type type, const QPoint& pos, const QPoint& screenPos, int delta, Qt::KeyboardModifiers modifiers, Qt::Orientation orientation)
  662. {
  663. QGraphicsSceneWheelEvent* event;
  664. event = new QGraphicsSceneWheelEvent(type);
  665. event->setPos(pos);
  666. event->setScreenPos(screenPos);
  667. event->setDelta(delta);
  668. event->setModifiers(modifiers);
  669. event->setOrientation(orientation);
  670. return event;
  671. }
  672. void EventSender::sendEvent(QObject* receiver, QEvent* event)
  673. {
  674. if (WebViewGraphicsBased* view = qobject_cast<WebViewGraphicsBased*>(receiver))
  675. view->scene()->sendEvent(view->graphicsView(), event);
  676. else
  677. QApplication::sendEvent(receiver, event);
  678. }
  679. void EventSender::postEvent(QObject* receiver, QEvent* event)
  680. {
  681. // QGraphicsScene does not have a postEvent method, so send the event in this case
  682. // and delete it after that.
  683. if (WebViewGraphicsBased* view = qobject_cast<WebViewGraphicsBased*>(receiver)) {
  684. view->scene()->sendEvent(view->graphicsView(), event);
  685. delete event;
  686. } else
  687. QApplication::postEvent(receiver, event); // event deleted by the system
  688. }
  689. #include "moc_EventSenderQt.cpp"