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#ifndef EventSenderQt_h
30#define EventSenderQt_h
31
32
33#include "DumpRenderTreeQt.h"
34
35#include <QApplication>
36#include <QBasicTimer>
37#include <QEvent>
38#include <QEventLoop>
39#include <QMouseEvent>
40#include <QObject>
41#include <QPoint>
42#include <QString>
43#include <QStringList>
44#include <QTouchEvent>
45
46#include <qwebpage.h>
47#include <qwebframe.h>
48
49
50class EventSender : public QObject {
51    Q_OBJECT
52public:
53    EventSender(QWebPage* parent);
54    virtual bool eventFilter(QObject* watched, QEvent* event);
55    void resetClickCount() { m_clickCount = 0; }
56
57public slots:
58    void mouseDown(int button = 0);
59    void mouseUp(int button = 0);
60    void mouseMoveTo(int x, int y);
61#ifndef QT_NO_WHEELEVENT
62    void mouseScrollBy(int x, int y);
63    void continuousMouseScrollBy(int x, int y);
64#endif
65    void leapForward(int ms);
66    void keyDown(const QString& string, const QStringList& modifiers = QStringList(), unsigned int location = 0);
67    void clearKillRing() {}
68    QStringList contextClick();
69    void scheduleAsynchronousClick();
70    void addTouchPoint(int x, int y);
71    void updateTouchPoint(int index, int x, int y);
72    void setTouchModifier(const QString &modifier, bool enable);
73    void touchStart();
74    void touchMove();
75    void touchEnd();
76    void zoomPageIn();
77    void zoomPageOut();
78    void textZoomIn();
79    void textZoomOut();
80    void clearTouchPoints();
81    void releaseTouchPoint(int index);
82
83protected:
84    void timerEvent(QTimerEvent*);
85
86private:
87    bool isGraphicsBased() const { return qobject_cast<WebCore::WebViewGraphicsBased*>(m_page->view()); }
88    QGraphicsSceneMouseEvent* createGraphicsSceneMouseEvent(QEvent::Type, const QPoint& pos, const QPoint& screenPos, Qt::MouseButton, Qt::MouseButtons, Qt::KeyboardModifiers);
89    QGraphicsSceneWheelEvent* createGraphicsSceneWheelEvent(QEvent::Type, const QPoint& pos, const QPoint& screenPos, int delta, Qt::KeyboardModifiers, Qt::Orientation);
90    void sendEvent(QObject* receiver, QEvent* event);
91    void postEvent(QObject* receiver, QEvent* event);
92
93private:
94    void sendTouchEvent(QEvent::Type);
95    void sendOrQueueEvent(QEvent*);
96    void replaySavedEvents(bool flush);
97    QPoint m_mousePos;
98    QPoint m_clickPos;
99    Qt::MouseButtons m_mouseButtons;
100    QWebPage* m_page;
101    int m_clickCount;
102    int m_currentButton;
103    bool m_mouseButtonPressed;
104    bool m_drag;
105    QEventLoop* m_eventLoop;
106    QWebFrame* frameUnderMouse() const;
107    QBasicTimer m_clickTimer;
108    QList<QTouchEvent::TouchPoint> m_touchPoints;
109    Qt::KeyboardModifiers m_touchModifiers;
110    bool m_touchActive;
111};
112#endif //  EventSenderQt_h
113