Doc: Enabling Qt QML linking to Qt Quick.
[profile/ivi/qtdeclarative.git] / src / qmltest / quicktestevent.cpp
index 67f1eef..2fccf3c 100644 (file)
@@ -1,38 +1,38 @@
 /****************************************************************************
 **
-** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the test suite of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
 ** GNU Lesser General Public License Usage
-** This file may be used under the terms of the GNU Lesser General Public
-** License version 2.1 as published by the Free Software Foundation and
-** appearing in the file LICENSE.LGPL included in the packaging of this
-** file. Please review the following information to ensure the GNU Lesser
-** General Public License version 2.1 requirements will be met:
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
-** In addition, as a special exception, Nokia gives you certain additional
-** rights. These rights are described in the Nokia Qt LGPL Exception
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU General
-** Public License version 3.0 as published by the Free Software Foundation
-** and appearing in the file LICENSE.GPL included in the packaging of this
-** file. Please review the following information to ensure the GNU General
-** Public License version 3.0 requirements will be met:
-** http://www.gnu.org/copyleft/gpl.html.
-**
-** Other Usage
-** Alternatively, this file may be used in accordance with the terms and
-** conditions contained in a signed written agreement between you and Nokia.
-**
-**
-**
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
 **
 **
 ** $QT_END_LICENSE$
 
 #include "quicktestevent_p.h"
 #include <QtTest/qtestkeyboard.h>
-#include <QtDeclarative/qdeclarative.h>
-#include <QtQuick1/qdeclarativeitem.h>
-#include <QtQuick1/qdeclarativeview.h>
-#if defined(QML_VERSION) && QML_VERSION >= 0x020000
-#include <QtDeclarative/qsgitem.h>
-#include <QtDeclarative/qsgcanvas.h>
-#define QUICK_TEST_SCENEGRAPH 1
-#endif
-#include <QtWidgets/qgraphicsscene.h>
+#include <QtQml/qqml.h>
+#include <QtQuick/qquickitem.h>
+#include <QtQuick/qquickwindow.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -64,28 +58,28 @@ QuickTestEvent::~QuickTestEvent()
 
 bool QuickTestEvent::keyPress(int key, int modifiers, int delay)
 {
-    QWidget *widget = eventWidget();
-    if (!widget)
+    QWindow *window = eventWindow();
+    if (!window)
         return false;
-    QTest::keyPress(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
+    QTest::keyPress(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
     return true;
 }
 
 bool QuickTestEvent::keyRelease(int key, int modifiers, int delay)
 {
-    QWidget *widget = eventWidget();
-    if (!widget)
+    QWindow *window = eventWindow();
+    if (!window)
         return false;
-    QTest::keyRelease(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
+    QTest::keyRelease(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
     return true;
 }
 
 bool QuickTestEvent::keyClick(int key, int modifiers, int delay)
 {
-    QWidget *widget = eventWidget();
-    if (!widget)
+    QWindow *window = eventWindow();
+    if (!window)
         return false;
-    QTest::keyClick(widget, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
+    QTest::keyClick(window, Qt::Key(key), Qt::KeyboardModifiers(modifiers), delay);
     return true;
 }
 
@@ -97,11 +91,11 @@ namespace QtQuickTest
 {
     enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDoubleClick, MouseMove };
 
-    static void mouseEvent(MouseAction action, QWidget *widget,
+    static void mouseEvent(MouseAction action, QWindow *window,
                            QObject *item, Qt::MouseButton button,
                            Qt::KeyboardModifiers stateKey, QPointF _pos, int delay=-1)
     {
-        QTEST_ASSERT(widget);
+        QTEST_ASSERT(window);
         QTEST_ASSERT(item);
 
         if (delay == -1 || delay < QTest::defaultMouseDelay())
@@ -110,30 +104,15 @@ namespace QtQuickTest
             QTest::qWait(delay);
 
         if (action == MouseClick) {
-            mouseEvent(MousePress, widget, item, button, stateKey, _pos);
-            mouseEvent(MouseRelease, widget, item, button, stateKey, _pos);
+            mouseEvent(MousePress, window, item, button, stateKey, _pos);
+            mouseEvent(MouseRelease, window, item, button, stateKey, _pos);
             return;
         }
 
         QPoint pos;
-        QDeclarativeView *view = qobject_cast<QDeclarativeView *>(widget);
-        QWidget *eventWidget = widget;
-#ifdef QUICK_TEST_SCENEGRAPH
-        QSGItem *sgitem = qobject_cast<QSGItem *>(item);
-        if (sgitem) {
+        QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
+        if (sgitem)
             pos = sgitem->mapToScene(_pos).toPoint();
-        } else
-#endif
-        {
-            QDeclarativeItem *ditem = qobject_cast<QDeclarativeItem *>(item);
-            if (!ditem) {
-                qWarning("Mouse event target is not an Item");
-                return;
-            }
-            pos = view->mapFromScene(ditem->mapToScene(_pos));
-            eventWidget = view->viewport();
-        }
-
         QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
         QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
 
@@ -143,36 +122,63 @@ namespace QtQuickTest
         switch (action)
         {
             case MousePress:
-                me = QMouseEvent(QEvent::MouseButtonPress, pos, widget->mapToGlobal(pos), button, button, stateKey);
+                me = QMouseEvent(QEvent::MouseButtonPress, pos, window->mapToGlobal(pos), button, button, stateKey);
                 break;
             case MouseRelease:
-                me = QMouseEvent(QEvent::MouseButtonRelease, pos, widget->mapToGlobal(pos), button, 0, stateKey);
+                me = QMouseEvent(QEvent::MouseButtonRelease, pos, window->mapToGlobal(pos), button, 0, stateKey);
                 break;
             case MouseDoubleClick:
-                me = QMouseEvent(QEvent::MouseButtonDblClick, pos, widget->mapToGlobal(pos), button, button, stateKey);
+                me = QMouseEvent(QEvent::MouseButtonDblClick, pos, window->mapToGlobal(pos), button, button, stateKey);
                 break;
             case MouseMove:
-                QCursor::setPos(widget->mapToGlobal(pos));
-                qApp->processEvents();
-                return;
+                // with move event the button is NoButton, but 'buttons' holds the currently pressed buttons
+                me = QMouseEvent(QEvent::MouseMove, pos, window->mapToGlobal(pos), Qt::NoButton, button, stateKey);
+                break;
             default:
                 QTEST_ASSERT(false);
         }
         QSpontaneKeyEvent::setSpontaneous(&me);
-        if (!qApp->notify(eventWidget, &me)) {
+        if (!qApp->notify(window, &me)) {
             static const char *mouseActionNames[] =
                 { "MousePress", "MouseRelease", "MouseClick", "MouseDoubleClick", "MouseMove" };
-            QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
-            QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data());
+            QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving window");
+            QWARN(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toLatin1().data());
         }
     }
+
+    static void mouseWheel(QWindow* window, QObject* item, Qt::MouseButtons buttons,
+                                Qt::KeyboardModifiers stateKey,
+                                QPointF _pos, int xDelta, int yDelta, int delay = -1)
+    {
+        QTEST_ASSERT(window);
+        QTEST_ASSERT(item);
+        if (delay == -1 || delay < QTest::defaultMouseDelay())
+            delay = QTest::defaultMouseDelay();
+        if (delay > 0)
+            QTest::qWait(delay);
+
+        QPoint pos;
+        QQuickItem *sgitem = qobject_cast<QQuickItem *>(item);
+        if (sgitem)
+            pos = sgitem->mapToScene(_pos).toPoint();
+
+        QTEST_ASSERT(buttons == Qt::NoButton || buttons & Qt::MouseButtonMask);
+        QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
+
+        stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
+        QWheelEvent we(pos, window->mapToGlobal(pos), QPoint(0, 0), QPoint(xDelta, yDelta), 0, Qt::Vertical, buttons, stateKey);
+
+        QSpontaneKeyEvent::setSpontaneous(&we); // hmmmm
+        if (!qApp->notify(window, &we))
+            QTest::qWarn("Wheel event not accepted by receiving window");
+    }
 };
 
 bool QuickTestEvent::mousePress
     (QObject *item, qreal x, qreal y, int button,
      int modifiers, int delay)
 {
-    QWidget *view = eventWidget();
+    QWindow *view = eventWindow();
     if (!view)
         return false;
     QtQuickTest::mouseEvent(QtQuickTest::MousePress, view, item,
@@ -182,11 +188,24 @@ bool QuickTestEvent::mousePress
     return true;
 }
 
+bool QuickTestEvent::mouseWheel(
+    QObject *item, qreal x, qreal y, int buttons,
+    int modifiers, int xDelta, int yDelta, int delay)
+{
+    QWindow *view = eventWindow();
+    if (!view)
+        return false;
+    QtQuickTest::mouseWheel(view, item, Qt::MouseButtons(buttons),
+                            Qt::KeyboardModifiers(modifiers),
+                            QPointF(x, y), xDelta, yDelta, delay);
+    return true;
+}
+
 bool QuickTestEvent::mouseRelease
     (QObject *item, qreal x, qreal y, int button,
      int modifiers, int delay)
 {
-    QWidget *view = eventWidget();
+    QWindow *view = eventWindow();
     if (!view)
         return false;
     QtQuickTest::mouseEvent(QtQuickTest::MouseRelease, view, item,
@@ -200,7 +219,7 @@ bool QuickTestEvent::mouseClick
     (QObject *item, qreal x, qreal y, int button,
      int modifiers, int delay)
 {
-    QWidget *view = eventWidget();
+    QWindow *view = eventWindow();
     if (!view)
         return false;
     QtQuickTest::mouseEvent(QtQuickTest::MouseClick, view, item,
@@ -214,7 +233,7 @@ bool QuickTestEvent::mouseDoubleClick
     (QObject *item, qreal x, qreal y, int button,
      int modifiers, int delay)
 {
-    QWidget *view = eventWidget();
+    QWindow *view = eventWindow();
     if (!view)
         return false;
     QtQuickTest::mouseEvent(QtQuickTest::MouseDoubleClick, view, item,
@@ -225,34 +244,23 @@ bool QuickTestEvent::mouseDoubleClick
 }
 
 bool QuickTestEvent::mouseMove
-    (QObject *item, qreal x, qreal y, int delay)
+    (QObject *item, qreal x, qreal y, int delay, int buttons)
 {
-    QWidget *view = eventWidget();
+    QWindow *view = eventWindow();
     if (!view)
         return false;
     QtQuickTest::mouseEvent(QtQuickTest::MouseMove, view, item,
-                            Qt::NoButton, Qt::NoModifier,
+                            Qt::MouseButton(buttons), Qt::NoModifier,
                             QPointF(x, y), delay);
     return true;
 }
 
-QWidget *QuickTestEvent::eventWidget()
+QWindow *QuickTestEvent::eventWindow()
 {
-#ifdef QUICK_TEST_SCENEGRAPH
-    QSGItem *sgitem = qobject_cast<QSGItem *>(parent());
+    QQuickItem *sgitem = qobject_cast<QQuickItem *>(parent());
     if (sgitem)
-        return sgitem->canvas();
-#endif
-    QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(parent());
-    if (!item)
-        return 0;
-    QGraphicsScene *s = item->scene();
-    if (!s)
-        return 0;
-    QList<QGraphicsView *> views = s->views();
-    if (views.isEmpty())
-        return 0;
-    return views.at(0);
+        return sgitem->window();
+    return 0;
 }
 
 QT_END_NAMESPACE