2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2010 University of Szeged
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include "MiniBrowserApplication.h"
34 #include <QMouseEvent>
35 #include <QTouchEvent>
37 extern Q_GUI_EXPORT void qt_translateRawTouchEvent(QWidget*, QTouchEvent::DeviceType, const QList<QTouchEvent::TouchPoint>&);
39 static inline bool isTouchEvent(const QEvent* event)
41 switch (event->type()) {
42 case QEvent::TouchBegin:
43 case QEvent::TouchUpdate:
44 case QEvent::TouchEnd:
51 static inline bool isMouseEvent(const QEvent* event)
53 switch (event->type()) {
54 case QEvent::MouseButtonPress:
55 case QEvent::MouseMove:
56 case QEvent::MouseButtonRelease:
57 case QEvent::MouseButtonDblClick:
64 MiniBrowserApplication::MiniBrowserApplication(int& argc, char** argv)
65 : QApplication(argc, argv, QApplication::GuiServer)
67 , m_spontaneousTouchEventReceived(false)
68 , m_sendingFakeTouchEvent(false)
69 , m_isRobotized(false)
70 , m_robotTimeoutSeconds(0)
71 , m_robotExtraTimeSeconds(0)
73 setOrganizationName("Nokia");
74 setApplicationName("QtMiniBrowser");
75 setApplicationVersion("0.1");
80 bool MiniBrowserApplication::notify(QObject* target, QEvent* event)
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.
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);
91 if (isMouseEvent(event)) {
92 const QMouseEvent* const mouseEvent = static_cast<QMouseEvent*>(event);
94 QTouchEvent::TouchPoint touchPoint;
95 touchPoint.setScreenPos(mouseEvent->globalPos());
96 touchPoint.setPos(mouseEvent->pos());
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);
105 touchPoint.setState(Qt::TouchPointPressed);
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());
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());
120 Q_ASSERT_X(false, "multi-touch mocking", "unhandled event type");
123 // Update current touch-point
124 m_touchPoints.insert(touchPoint.id(), touchPoint);
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);
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;
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());
146 return QApplication::notify(target, event);
149 void MiniBrowserApplication::handleUserOptions()
151 QStringList args = arguments();
152 QFileInfo program(args.at(0));
153 QString programName("MiniBrowser");
154 if (program.exists())
155 programName = program.baseName();
157 if (args.contains("-help")) {
158 qDebug() << "Usage:" << programName.toLatin1().data()
162 << "[-robot-timeout seconds]"
163 << "[-robot-extra-time seconds]"
164 << "[-chunked-drawing-area]"
165 << "[-print-loaded-urls]"
166 #if defined(QT_CONFIGURED_WITH_OPENGL)
173 if (args.contains("-touch"))
174 m_windowOptions.useTouchWebView = true;
176 if (args.contains("-maximize"))
177 m_windowOptions.startMaximized = true;
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.");
187 m_isRobotized = true;
188 m_urls = QStringList(listFile);
190 int lastArg = args.lastIndexOf(QRegExp("^-.*"));
191 m_urls = (lastArg != -1) ? args.mid(++lastArg) : args.mid(1);
194 int robotTimeoutIndex = args.indexOf("-robot-timeout");
195 if (robotTimeoutIndex != -1)
196 m_robotTimeoutSeconds = takeOptionValue(&args, robotTimeoutIndex).toInt();
198 int robotExtraTimeIndex = args.indexOf("-robot-extra-time");
199 if (robotExtraTimeIndex != -1)
200 m_robotExtraTimeSeconds = takeOptionValue(&args, robotExtraTimeIndex).toInt();
202 if (args.contains("-print-loaded-urls"))
203 m_windowOptions.printLoadedUrls = true;
205 #if defined(QT_CONFIGURED_WITH_OPENGL)
206 if (args.contains("-gl-viewport"))
207 m_windowOptions.useQGLWidgetViewport = true;