initial import
[vuplus_webkit] / Tools / MiniBrowser / qt / MiniBrowserApplication.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2010 University of Szeged
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "MiniBrowserApplication.h"
30
31 #include "utils.h"
32 #include <QRegExp>
33 #include <QEvent>
34 #include <QMouseEvent>
35 #include <QTouchEvent>
36
37 extern Q_GUI_EXPORT void qt_translateRawTouchEvent(QWidget*, QTouchEvent::DeviceType, const QList<QTouchEvent::TouchPoint>&);
38
39 static inline bool isTouchEvent(const QEvent* event)
40 {
41     switch (event->type()) {
42     case QEvent::TouchBegin:
43     case QEvent::TouchUpdate:
44     case QEvent::TouchEnd:
45         return true;
46     default:
47         return false;
48     }
49 }
50
51 static inline bool isMouseEvent(const QEvent* event)
52 {
53     switch (event->type()) {
54     case QEvent::MouseButtonPress:
55     case QEvent::MouseMove:
56     case QEvent::MouseButtonRelease:
57     case QEvent::MouseButtonDblClick:
58         return true;
59     default:
60         return false;
61     }
62 }
63
64 MiniBrowserApplication::MiniBrowserApplication(int& argc, char** argv)
65     : QApplication(argc, argv, QApplication::GuiServer)
66     , m_windowOptions()
67     , m_spontaneousTouchEventReceived(false)
68     , m_sendingFakeTouchEvent(false)
69     , m_isRobotized(false)
70     , m_robotTimeoutSeconds(0)
71     , m_robotExtraTimeSeconds(0)
72 {
73     setOrganizationName("Nokia");
74     setApplicationName("QtMiniBrowser");
75     setApplicationVersion("0.1");
76
77     handleUserOptions();
78 }
79
80 bool MiniBrowserApplication::notify(QObject* target, QEvent* event)
81 {
82     // We try to be smart, if we received real touch event, we are probably on a device
83     // with touch screen, and we should not have touch mocking.
84
85     if (!event->spontaneous() || m_sendingFakeTouchEvent || m_spontaneousTouchEventReceived)
86         return QApplication::notify(target, event);
87     if (isTouchEvent(event) && static_cast<QTouchEvent*>(event)->deviceType() == QTouchEvent::TouchScreen) {
88         m_spontaneousTouchEventReceived = true;
89         return QApplication::notify(target, event);
90     }
91     if (isMouseEvent(event)) {
92         const QMouseEvent* const mouseEvent = static_cast<QMouseEvent*>(event);
93
94         QTouchEvent::TouchPoint touchPoint;
95         touchPoint.setScreenPos(mouseEvent->globalPos());
96         touchPoint.setPos(mouseEvent->pos());
97
98         switch (mouseEvent->type()) {
99         case QEvent::MouseButtonPress:
100         case QEvent::MouseButtonDblClick:
101             touchPoint.setId(mouseEvent->button());
102             if (m_touchPoints.contains(touchPoint.id()))
103                 touchPoint.setState(Qt::TouchPointMoved);
104             else
105                 touchPoint.setState(Qt::TouchPointPressed);
106             break;
107         case QEvent::MouseMove:
108             if (!mouseEvent->buttons() || !m_touchPoints.contains(mouseEvent->buttons()))
109                 return QApplication::notify(target, event);
110             touchPoint.setState(Qt::TouchPointMoved);
111             touchPoint.setId(mouseEvent->buttons());
112             break;
113         case QEvent::MouseButtonRelease:
114             if (mouseEvent->modifiers().testFlag(Qt::ControlModifier))
115                 return QApplication::notify(target, event);
116             touchPoint.setState(Qt::TouchPointReleased);
117             touchPoint.setId(mouseEvent->button());
118             break;
119         default:
120             Q_ASSERT_X(false, "multi-touch mocking", "unhandled event type");
121         }
122
123         // Update current touch-point
124         m_touchPoints.insert(touchPoint.id(), touchPoint);
125
126         // Update states for all other touch-points
127         for (QHash<int, QTouchEvent::TouchPoint>::iterator it = m_touchPoints.begin(); it != m_touchPoints.end(); ++it) {
128             if (it.value().id() != touchPoint.id())
129                 it.value().setState(Qt::TouchPointStationary);
130         }
131
132         QList<QTouchEvent::TouchPoint> touchPoints = m_touchPoints.values();
133         QTouchEvent::TouchPoint& firstPoint = touchPoints.first();
134         firstPoint.setState(firstPoint.state() | Qt::TouchPointPrimary);
135         m_sendingFakeTouchEvent = true;
136         qt_translateRawTouchEvent(0, QTouchEvent::TouchScreen, touchPoints);
137         m_sendingFakeTouchEvent = false;
138
139         // Get rid of touch-points that are no longer valid
140         foreach (const QTouchEvent::TouchPoint& touchPoint, m_touchPoints) {
141             if (touchPoint.state() ==  Qt::TouchPointReleased)
142                 m_touchPoints.remove(touchPoint.id());
143         }
144     }
145
146     return QApplication::notify(target, event);
147 }
148
149 void MiniBrowserApplication::handleUserOptions()
150 {
151     QStringList args = arguments();
152     QFileInfo program(args.at(0));
153     QString programName("MiniBrowser");
154     if (program.exists())
155         programName = program.baseName();
156
157     if (args.contains("-help")) {
158         qDebug() << "Usage:" << programName.toLatin1().data()
159              << "[-touch]"
160              << "[-maximize]"
161              << "[-r list]"
162              << "[-robot-timeout seconds]"
163              << "[-robot-extra-time seconds]"
164              << "[-chunked-drawing-area]"
165              << "[-print-loaded-urls]"
166 #if defined(QT_CONFIGURED_WITH_OPENGL)
167              << "[-gl-viewport]"
168 #endif
169              << "URLs";
170         appQuit(0);
171     }
172
173     if (args.contains("-touch"))
174         m_windowOptions.useTouchWebView = true;
175
176     if (args.contains("-maximize"))
177         m_windowOptions.startMaximized = true;
178
179     int robotIndex = args.indexOf("-r");
180     if (robotIndex != -1) {
181         QString listFile = takeOptionValue(&args, robotIndex);
182         if (listFile.isEmpty())
183             appQuit(1, "-r needs a list file to start in robotized mode");
184         if (!QFile::exists(listFile))
185             appQuit(1, "The list file supplied to -r does not exist.");
186
187         m_isRobotized = true;
188         m_urls = QStringList(listFile);
189     } else {
190         int lastArg = args.lastIndexOf(QRegExp("^-.*"));
191         m_urls = (lastArg != -1) ? args.mid(++lastArg) : args.mid(1);
192     }
193
194     int robotTimeoutIndex = args.indexOf("-robot-timeout");
195     if (robotTimeoutIndex != -1)
196         m_robotTimeoutSeconds = takeOptionValue(&args, robotTimeoutIndex).toInt();
197
198     int robotExtraTimeIndex = args.indexOf("-robot-extra-time");
199     if (robotExtraTimeIndex != -1)
200         m_robotExtraTimeSeconds = takeOptionValue(&args, robotExtraTimeIndex).toInt();
201
202     if (args.contains("-print-loaded-urls"))
203         m_windowOptions.printLoadedUrls = true;
204
205 #if defined(QT_CONFIGURED_WITH_OPENGL)
206     if (args.contains("-gl-viewport"))
207         m_windowOptions.useQGLWidgetViewport = true;
208 #endif
209 }