Add touchEvent() virtual to QWindow.
authorLaszlo Agocs <laszlo.p.agocs@nokia.com>
Mon, 12 Dec 2011 15:24:33 +0000 (17:24 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 12 Dec 2011 16:27:19 +0000 (17:27 +0100)
Unlike keyPressEvent(), mousePressEvent(), etc. the touch events had
no equivalent so one had to fall back to reimplementing event() or
using an event filter. This is now corrected by introducing
touchEvent(). Touch events are finally becoming a first-class citizen
in Qt 5.

Change-Id: Ia2044030154fd5b1b5384f08a3cb1749b798435f
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
src/gui/kernel/qwindow.cpp
src/gui/kernel/qwindow.h
tests/auto/gui/kernel/qwindow/tst_qwindow.cpp

index 55329f5..d4f5e1d 100644 (file)
@@ -813,6 +813,12 @@ bool QWindow::event(QEvent *event)
         mouseDoubleClickEvent(static_cast<QMouseEvent*>(event));
         break;
 
+    case QEvent::TouchBegin:
+    case QEvent::TouchUpdate:
+    case QEvent::TouchEnd:
+        touchEvent(static_cast<QTouchEvent *>(event));
+        break;
+
     case QEvent::Move:
         moveEvent(static_cast<QMoveEvent*>(event));
         break;
@@ -907,6 +913,9 @@ void QWindow::wheelEvent(QWheelEvent *)
 }
 #endif //QT_NO_WHEELEVENT
 
+void QWindow::touchEvent(QTouchEvent *)
+{
+}
 
 
 /*!
index 11ff6b1..2a50248 100644 (file)
@@ -72,6 +72,7 @@ class QMouseEvent;
 #ifndef QT_NO_WHEELEVENT
 class QWheelEvent;
 #endif
+class QTouchEvent;
 
 class QPlatformSurface;
 class QPlatformWindow;
@@ -285,6 +286,7 @@ protected:
 #ifndef QT_NO_WHEELEVENT
     virtual void wheelEvent(QWheelEvent *);
 #endif
+    virtual void touchEvent(QTouchEvent *);
 
     QWindow(QWindowPrivate &dd, QWindow *parent);
 
index 4171f0f..13b196c 100644 (file)
@@ -43,6 +43,8 @@
 
 #include <QtTest/QtTest>
 
+#include <QEvent>
+
 class tst_QWindow: public QObject
 {
     Q_OBJECT
@@ -51,6 +53,7 @@ private slots:
     void mapGlobal();
     void positioning();
     void isActive();
+    void testInputEvents();
 };
 
 
@@ -221,5 +224,84 @@ void tst_QWindow::isActive()
     QVERIFY(child.isActive());
 }
 
+class InputTestWindow : public QWindow
+{
+public:
+    void keyPressEvent(QKeyEvent *event) {
+        keyPressCode = event->key();
+    }
+    void keyReleaseEvent(QKeyEvent *event) {
+        keyReleaseCode = event->key();
+    }
+    void mousePressEvent(QMouseEvent *event) {
+        mousePressButton = event->button();
+    }
+    void mouseReleaseEvent(QMouseEvent *event) {
+        mouseReleaseButton = event->button();
+    }
+    void touchEvent(QTouchEvent *event) {
+        QList<QTouchEvent::TouchPoint> points = event->touchPoints();
+        for (int i = 0; i < points.count(); ++i) {
+            switch (points.at(i).state()) {
+            case Qt::TouchPointPressed:
+                ++touchPressedCount;
+                break;
+            case Qt::TouchPointReleased:
+                ++touchReleasedCount;
+                break;
+            }
+        }
+    }
+
+    InputTestWindow() {
+        keyPressCode = keyReleaseCode = 0;
+        mousePressButton = mouseReleaseButton = 0;
+        touchPressedCount = touchReleasedCount = 0;
+    }
+
+    int keyPressCode, keyReleaseCode;
+    int mousePressButton, mouseReleaseButton;
+    int touchPressedCount, touchReleasedCount;
+};
+
+void tst_QWindow::testInputEvents()
+{
+    InputTestWindow window;
+    window.setGeometry(80, 80, 40, 40);
+    window.show();
+    QTest::qWaitForWindowShown(&window);
+
+    QWindowSystemInterface::handleKeyEvent(&window, QEvent::KeyPress, Qt::Key_A, Qt::NoModifier);
+    QWindowSystemInterface::handleKeyEvent(&window, QEvent::KeyRelease, Qt::Key_A, Qt::NoModifier);
+    QCoreApplication::processEvents();
+    QCOMPARE(window.keyPressCode, int(Qt::Key_A));
+    QCOMPARE(window.keyReleaseCode, int(Qt::Key_A));
+
+    QPointF local(12, 34);
+    QWindowSystemInterface::handleMouseEvent(&window, local, local, Qt::LeftButton);
+    QWindowSystemInterface::handleMouseEvent(&window, local, local, Qt::NoButton);
+    QCoreApplication::processEvents();
+    QCOMPARE(window.mousePressButton, int(Qt::LeftButton));
+    QCOMPARE(window.mouseReleaseButton, int(Qt::LeftButton));
+
+    QTouchDevice *device = new QTouchDevice;
+    device->setType(QTouchDevice::TouchScreen);
+    QWindowSystemInterface::registerTouchDevice(device);
+    QList<QWindowSystemInterface::TouchPoint> points;
+    QWindowSystemInterface::TouchPoint tp1, tp2;
+    tp1.id = 1;
+    tp1.state = Qt::TouchPointPressed;
+    tp2.id = 2;
+    tp2.state = Qt::TouchPointPressed;
+    points << tp1 << tp2;
+    QWindowSystemInterface::handleTouchEvent(&window, device, points);
+    points[0].state = Qt::TouchPointReleased;
+    points[1].state = Qt::TouchPointReleased;
+    QWindowSystemInterface::handleTouchEvent(&window, device, points);
+    QCoreApplication::processEvents();
+    QCOMPARE(window.touchPressedCount, 2);
+    QCOMPARE(window.touchReleasedCount, 2);
+}
+
 #include <tst_qwindow.moc>
 QTEST_MAIN(tst_QWindow);