2011-01-19 Aparna Nandyal <aparna.nand@wipro.com>
[WebKit.git] / Tools / QtTestBrowser / launcherwindow.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
4  * Copyright (C) 2006 George Staikos <staikos@kde.org>
5  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
6  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
7  * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "launcherwindow.h"
34
35 const int gExitClickArea = 80;
36 QVector<int> LauncherWindow::m_zoomLevels;
37
38 LauncherWindow::LauncherWindow(WindowOptions* data, QGraphicsScene* sharedScene)
39     : MainWindow()
40     , m_currentZoom(100)
41     , m_view(0)
42     , m_inspector(0)
43     , m_formatMenuAction(0)
44     , m_zoomAnimation(0)
45 {
46     if (data)
47         m_windowOptions = *data;
48
49     init();
50     if (sharedScene && data->useGraphicsView)
51         static_cast<QGraphicsView*>(m_view)->setScene(sharedScene);
52
53     createChrome();
54 }
55
56 LauncherWindow::~LauncherWindow()
57 {
58     grabZoomKeys(false);
59
60     if (page())
61         page()->setQnamThreaded(false);
62 }
63
64 void LauncherWindow::init()
65 {
66     QSplitter* splitter = new QSplitter(Qt::Vertical, this);
67     setCentralWidget(splitter);
68
69 #if defined(Q_OS_SYMBIAN)
70     setWindowState(Qt::WindowMaximized);
71 #else
72     setWindowState(Qt::WindowNoState);
73     resize(800, 600);
74 #endif
75
76     m_inspector = new WebInspector;
77 #ifndef QT_NO_PROPERTIES
78     if (!m_windowOptions.inspectorUrl.isEmpty())
79         m_inspector->setProperty("_q_inspectorUrl", m_windowOptions.inspectorUrl);
80 #endif
81     connect(this, SIGNAL(destroyed()), m_inspector, SLOT(deleteLater()));
82
83     // the zoom values are chosen to be like in Mozilla Firefox 3
84     if (!m_zoomLevels.count()) {
85         m_zoomLevels << 30 << 50 << 67 << 80 << 90;
86         m_zoomLevels << 100;
87         m_zoomLevels << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300;
88     }
89
90     grabZoomKeys(true);
91
92     initializeView();
93 }
94
95 void LauncherWindow::initializeView()
96 {
97     delete m_view;
98
99     QUrl url = page()->mainFrame()->url();
100     setPage(new WebPage(this));
101     page()->setQnamThreaded(m_windowOptions.useThreadedQnam);
102
103     QSplitter* splitter = static_cast<QSplitter*>(centralWidget());
104
105     if (!m_windowOptions.useGraphicsView) {
106         WebViewTraditional* view = new WebViewTraditional(splitter);
107         view->setPage(page());
108
109         view->installEventFilter(this);
110
111         m_view = view;
112     } else {
113         WebViewGraphicsBased* view = new WebViewGraphicsBased(splitter);
114         view->setPage(page());
115
116         connect(view, SIGNAL(currentFPSUpdated(int)), this, SLOT(updateFPS(int)));
117
118         view->installEventFilter(this);
119         // The implementation of QAbstractScrollArea::eventFilter makes us need
120         // to install the event filter also on the viewport of a QGraphicsView.
121         view->viewport()->installEventFilter(this);
122
123         m_view = view;
124     }
125
126     m_touchMocking = false;
127
128     connect(page(), SIGNAL(loadStarted()), this, SLOT(loadStarted()));
129     connect(page(), SIGNAL(loadFinished(bool)), this, SLOT(loadFinished()));
130     connect(page(), SIGNAL(linkHovered(const QString&, const QString&, const QString&)),
131             this, SLOT(showLinkHover(const QString&, const QString&)));
132     connect(this, SIGNAL(enteredFullScreenMode(bool)), this, SLOT(toggleFullScreenMode(bool)));
133
134     applyPrefs();
135
136     splitter->addWidget(m_inspector);
137     m_inspector->setPage(page());
138     m_inspector->hide();
139
140     if (m_windowOptions.remoteInspectorPort)
141         page()->setProperty("_q_webInspectorServerPort", m_windowOptions.remoteInspectorPort);
142
143     if (url.isValid())
144         page()->mainFrame()->load(url);
145 }
146
147 void LauncherWindow::applyPrefs()
148 {
149     QWebSettings* settings = page()->settings();
150     settings->setAttribute(QWebSettings::AcceleratedCompositingEnabled, m_windowOptions.useCompositing);
151     settings->setAttribute(QWebSettings::TiledBackingStoreEnabled, m_windowOptions.useTiledBackingStore);
152     settings->setAttribute(QWebSettings::FrameFlatteningEnabled, m_windowOptions.useFrameFlattening);
153     settings->setAttribute(QWebSettings::WebGLEnabled, m_windowOptions.useWebGL);
154
155     if (!isGraphicsBased())
156         return;
157
158     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
159     view->setViewportUpdateMode(m_windowOptions.viewportUpdateMode);
160     view->setFrameRateMeasurementEnabled(m_windowOptions.showFrameRate);
161     view->setItemCacheMode(m_windowOptions.cacheWebView ? QGraphicsItem::DeviceCoordinateCache : QGraphicsItem::NoCache);
162
163     if (m_windowOptions.resizesToContents)
164         toggleResizesToContents(m_windowOptions.resizesToContents);
165 }
166
167 void LauncherWindow::createChrome()
168 {
169 #ifndef QT_NO_SHORTCUT
170     QMenu* fileMenu = menuBar()->addMenu("&File");
171     fileMenu->addAction("New Window", this, SLOT(newWindow()), QKeySequence::New);
172     fileMenu->addAction(tr("Open File..."), this, SLOT(openFile()), QKeySequence::Open);
173     fileMenu->addAction(tr("Open Location..."), this, SLOT(openLocation()), QKeySequence(Qt::CTRL | Qt::Key_L));
174     fileMenu->addAction("Close Window", this, SLOT(close()), QKeySequence::Close);
175     fileMenu->addSeparator();
176     fileMenu->addAction("Take Screen Shot...", this, SLOT(screenshot()));
177 #ifndef QT_NO_PRINTER
178     fileMenu->addAction(tr("Print..."), this, SLOT(print()), QKeySequence::Print);
179 #endif
180     fileMenu->addSeparator();
181     fileMenu->addAction("Quit", QApplication::instance(), SLOT(closeAllWindows()), QKeySequence(Qt::CTRL | Qt::Key_Q));
182
183     QMenu* editMenu = menuBar()->addMenu("&Edit");
184     editMenu->addAction(page()->action(QWebPage::Undo));
185     editMenu->addAction(page()->action(QWebPage::Redo));
186     editMenu->addSeparator();
187     editMenu->addAction(page()->action(QWebPage::Cut));
188     editMenu->addAction(page()->action(QWebPage::Copy));
189     editMenu->addAction(page()->action(QWebPage::Paste));
190     editMenu->addSeparator();
191     QAction* setEditable = editMenu->addAction("Set Editable", this, SLOT(setEditable(bool)));
192     setEditable->setCheckable(true);
193
194     QMenu* viewMenu = menuBar()->addMenu("&View");
195     viewMenu->addAction(page()->action(QWebPage::Stop));
196     viewMenu->addAction(page()->action(QWebPage::Reload));
197     viewMenu->addSeparator();
198     QAction* zoomIn = viewMenu->addAction("Zoom &In", this, SLOT(zoomIn()));
199     QAction* zoomOut = viewMenu->addAction("Zoom &Out", this, SLOT(zoomOut()));
200     QAction* resetZoom = viewMenu->addAction("Reset Zoom", this, SLOT(resetZoom()));
201     QAction* zoomTextOnly = viewMenu->addAction("Zoom Text Only", this, SLOT(toggleZoomTextOnly(bool)));
202     zoomTextOnly->setCheckable(true);
203     zoomTextOnly->setChecked(false);
204     viewMenu->addSeparator();
205     viewMenu->addAction("Dump HTML", this, SLOT(dumpHtml()));
206     // viewMenu->addAction("Dump plugins", this, SLOT(dumpPlugins()));
207
208     zoomIn->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Plus));
209     zoomOut->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Minus));
210     resetZoom->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0));
211
212     QMenu* formatMenu = new QMenu("F&ormat", this);
213     m_formatMenuAction = menuBar()->addMenu(formatMenu);
214     m_formatMenuAction->setVisible(false);
215     formatMenu->addAction(page()->action(QWebPage::ToggleBold));
216     formatMenu->addAction(page()->action(QWebPage::ToggleItalic));
217     formatMenu->addAction(page()->action(QWebPage::ToggleUnderline));
218     QMenu* writingMenu = formatMenu->addMenu(tr("Writing Direction"));
219     writingMenu->addAction(page()->action(QWebPage::SetTextDirectionDefault));
220     writingMenu->addAction(page()->action(QWebPage::SetTextDirectionLeftToRight));
221     writingMenu->addAction(page()->action(QWebPage::SetTextDirectionRightToLeft));
222
223     QMenu* windowMenu = menuBar()->addMenu("&Window");
224     QAction* toggleFullScreen = windowMenu->addAction("Toggle FullScreen", this, SIGNAL(enteredFullScreenMode(bool)));
225     toggleFullScreen->setShortcut(Qt::Key_F11);
226     toggleFullScreen->setCheckable(true);
227     toggleFullScreen->setChecked(false);
228     // When exit fullscreen mode by clicking on the exit area (bottom right corner) we must
229     // uncheck the Toggle FullScreen action.
230     toggleFullScreen->connect(this, SIGNAL(enteredFullScreenMode(bool)), SLOT(setChecked(bool)));
231
232     QWebSettings* settings = page()->settings();
233
234     QMenu* toolsMenu = menuBar()->addMenu("&Develop");
235     QMenu* graphicsViewMenu = toolsMenu->addMenu("QGraphicsView");
236     QAction* toggleGraphicsView = graphicsViewMenu->addAction("Toggle use of QGraphicsView", this, SLOT(toggleWebView(bool)));
237     toggleGraphicsView->setCheckable(true);
238     toggleGraphicsView->setChecked(isGraphicsBased());
239
240     QAction* toggleWebGL = toolsMenu->addAction("Toggle WebGL", this, SLOT(toggleWebGL(bool)));
241     toggleWebGL->setCheckable(true);
242     toggleWebGL->setChecked(settings->testAttribute(QWebSettings::WebGLEnabled));
243
244     QAction* toggleThreadedQnam = toolsMenu->addAction("Toggle threaded network", this, SLOT(toggleThreadedQnam(bool)));
245     toggleThreadedQnam->setCheckable(true);
246     toggleThreadedQnam->setChecked(m_windowOptions.useThreadedQnam);
247
248     QAction* spatialNavigationAction = toolsMenu->addAction("Toggle Spatial Navigation", this, SLOT(toggleSpatialNavigation(bool)));
249     spatialNavigationAction->setCheckable(true);
250     spatialNavigationAction->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_S));
251
252     QAction* toggleFrameFlattening = toolsMenu->addAction("Toggle Frame Flattening", this, SLOT(toggleFrameFlattening(bool)));
253     toggleFrameFlattening->setCheckable(true);
254     toggleFrameFlattening->setChecked(settings->testAttribute(QWebSettings::FrameFlatteningEnabled));
255
256     QAction* touchMockAction = toolsMenu->addAction("Toggle touch mocking", this, SLOT(setTouchMocking(bool)));
257     touchMockAction->setCheckable(true);
258     touchMockAction->setShortcut(QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_T));
259
260     toolsMenu->addSeparator();
261
262     QAction* toggleInterruptingJavaScripteEnabled = toolsMenu->addAction("Enable interrupting js scripts", this, SLOT(toggleInterruptingJavaScriptEnabled(bool)));
263     toggleInterruptingJavaScripteEnabled->setCheckable(true);
264     toggleInterruptingJavaScripteEnabled->setChecked(false);
265
266     QAction* toggleJavascriptCanOpenWindows = toolsMenu->addAction("Enable js popup windows", this, SLOT(toggleJavascriptCanOpenWindows(bool)));
267     toggleJavascriptCanOpenWindows->setCheckable(true);
268     toggleJavascriptCanOpenWindows->setChecked(false);
269
270     toolsMenu->addSeparator();
271
272     QAction* userAgentAction = toolsMenu->addAction("Change User Agent", this, SLOT(showUserAgentDialog()));
273     userAgentAction->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_U));
274
275     toolsMenu->addAction("Select Elements...", this, SLOT(selectElements()));
276
277     QAction* showInspectorAction = toolsMenu->addAction("Show Web Inspector", m_inspector, SLOT(setVisible(bool)), QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_I));
278     showInspectorAction->setCheckable(true);
279     showInspectorAction->connect(m_inspector, SIGNAL(visibleChanged(bool)), SLOT(setChecked(bool)));
280
281     // GraphicsView sub menu.
282     QAction* toggleAcceleratedCompositing = graphicsViewMenu->addAction("Toggle Accelerated Compositing", this, SLOT(toggleAcceleratedCompositing(bool)));
283     toggleAcceleratedCompositing->setCheckable(true);
284     toggleAcceleratedCompositing->setChecked(settings->testAttribute(QWebSettings::AcceleratedCompositingEnabled));
285     toggleAcceleratedCompositing->setEnabled(isGraphicsBased());
286     toggleAcceleratedCompositing->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
287
288     QAction* toggleResizesToContents = graphicsViewMenu->addAction("Toggle Resizes To Contents Mode", this, SLOT(toggleResizesToContents(bool)));
289     toggleResizesToContents->setCheckable(true);
290     toggleResizesToContents->setChecked(m_windowOptions.resizesToContents);
291     toggleResizesToContents->setEnabled(isGraphicsBased());
292     toggleResizesToContents->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
293
294     QAction* toggleTiledBackingStore = graphicsViewMenu->addAction("Toggle Tiled Backing Store", this, SLOT(toggleTiledBackingStore(bool)));
295     toggleTiledBackingStore->setCheckable(true);
296     toggleTiledBackingStore->setChecked(m_windowOptions.useTiledBackingStore);
297     toggleTiledBackingStore->setEnabled(isGraphicsBased());
298     toggleTiledBackingStore->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
299
300 #if defined(QT_CONFIGURED_WITH_OPENGL)
301     QAction* toggleQGLWidgetViewport = graphicsViewMenu->addAction("Toggle use of QGLWidget Viewport", this, SLOT(toggleQGLWidgetViewport(bool)));
302     toggleQGLWidgetViewport->setCheckable(true);
303     toggleQGLWidgetViewport->setChecked(m_windowOptions.useQGLWidgetViewport);
304     toggleQGLWidgetViewport->setEnabled(isGraphicsBased());
305     toggleQGLWidgetViewport->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
306 #endif
307
308     QMenu* viewportUpdateMenu = graphicsViewMenu->addMenu("Change Viewport Update Mode");
309     viewportUpdateMenu->setEnabled(isGraphicsBased());
310     viewportUpdateMenu->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
311
312     QAction* fullUpdate = viewportUpdateMenu->addAction("FullViewportUpdate");
313     fullUpdate->setCheckable(true);
314     fullUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::FullViewportUpdate) ? true : false);
315
316     QAction* minimalUpdate = viewportUpdateMenu->addAction("MinimalViewportUpdate");
317     minimalUpdate->setCheckable(true);
318     minimalUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::MinimalViewportUpdate) ? true : false);
319
320     QAction* smartUpdate = viewportUpdateMenu->addAction("SmartViewportUpdate");
321     smartUpdate->setCheckable(true);
322     smartUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::SmartViewportUpdate) ? true : false);
323
324     QAction* boundingRectUpdate = viewportUpdateMenu->addAction("BoundingRectViewportUpdate");
325     boundingRectUpdate->setCheckable(true);
326     boundingRectUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate) ? true : false);
327
328     QAction* noUpdate = viewportUpdateMenu->addAction("NoViewportUpdate");
329     noUpdate->setCheckable(true);
330     noUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::NoViewportUpdate) ? true : false);
331
332     QSignalMapper* signalMapper = new QSignalMapper(viewportUpdateMenu);
333     signalMapper->setMapping(fullUpdate, QGraphicsView::FullViewportUpdate);
334     signalMapper->setMapping(minimalUpdate, QGraphicsView::MinimalViewportUpdate);
335     signalMapper->setMapping(smartUpdate, QGraphicsView::SmartViewportUpdate);
336     signalMapper->setMapping(boundingRectUpdate, QGraphicsView::BoundingRectViewportUpdate);
337     signalMapper->setMapping(noUpdate, QGraphicsView::NoViewportUpdate);
338
339     connect(fullUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
340     connect(minimalUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
341     connect(smartUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
342     connect(boundingRectUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
343     connect(noUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
344
345     connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(changeViewportUpdateMode(int)));
346
347     QActionGroup* viewportUpdateModeActions = new QActionGroup(viewportUpdateMenu);
348     viewportUpdateModeActions->addAction(fullUpdate);
349     viewportUpdateModeActions->addAction(minimalUpdate);
350     viewportUpdateModeActions->addAction(smartUpdate);
351     viewportUpdateModeActions->addAction(boundingRectUpdate);
352     viewportUpdateModeActions->addAction(noUpdate);
353
354     graphicsViewMenu->addSeparator();
355
356     QAction* flipAnimated = graphicsViewMenu->addAction("Animated Flip");
357     flipAnimated->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
358     flipAnimated->setEnabled(isGraphicsBased());
359     connect(flipAnimated, SIGNAL(triggered()), SLOT(animatedFlip()));
360
361     QAction* flipYAnimated = graphicsViewMenu->addAction("Animated Y-Flip");
362     flipYAnimated->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
363     flipYAnimated->setEnabled(isGraphicsBased());
364     connect(flipYAnimated, SIGNAL(triggered()), SLOT(animatedYFlip()));
365
366     QAction* cloneWindow = graphicsViewMenu->addAction("Clone Window", this, SLOT(cloneWindow()));
367     cloneWindow->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
368     cloneWindow->setEnabled(isGraphicsBased());
369
370     graphicsViewMenu->addSeparator();
371
372     QAction* showFPS = graphicsViewMenu->addAction("Show FPS", this, SLOT(showFPS(bool)));
373     showFPS->setCheckable(true);
374     showFPS->setEnabled(isGraphicsBased());
375     showFPS->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
376     showFPS->setChecked(m_windowOptions.showFrameRate);
377 #endif
378 }
379
380 bool LauncherWindow::isGraphicsBased() const
381 {
382     return bool(qobject_cast<QGraphicsView*>(m_view));
383 }
384
385 void LauncherWindow::keyPressEvent(QKeyEvent* event)
386 {
387 #ifdef Q_WS_MAEMO_5
388     switch (event->key()) {
389     case Qt::Key_F7:
390         zoomIn();
391         event->accept();
392         break;
393     case Qt::Key_F8:
394         zoomOut();
395         event->accept();
396         break;
397     }
398 #endif
399     MainWindow::keyPressEvent(event);
400 }
401
402 void LauncherWindow::grabZoomKeys(bool grab)
403 {
404 #ifdef Q_WS_MAEMO_5
405     if (!winId()) {
406         qWarning("Can't grab keys unless we have a window id");
407         return;
408     }
409
410     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
411     if (!atom) {
412         qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM");
413         return;
414     }
415
416     unsigned long val = (grab) ? 1 : 0;
417     XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&val), 1);
418 #endif
419 }
420
421 void LauncherWindow::sendTouchEvent()
422 {
423     if (m_touchPoints.isEmpty())
424         return;
425
426     QEvent::Type type = QEvent::TouchUpdate;
427     if (m_touchPoints.size() == 1) {
428         if (m_touchPoints[0].state() == Qt::TouchPointReleased)
429             type = QEvent::TouchEnd;
430         else if (m_touchPoints[0].state() == Qt::TouchPointPressed)
431             type = QEvent::TouchBegin;
432     }
433
434     QTouchEvent touchEv(type);
435     touchEv.setTouchPoints(m_touchPoints);
436     QCoreApplication::sendEvent(page(), &touchEv);
437
438     // After sending the event, remove all touchpoints that were released
439     if (m_touchPoints[0].state() == Qt::TouchPointReleased)
440         m_touchPoints.removeAt(0);
441     if (m_touchPoints.size() > 1 && m_touchPoints[1].state() == Qt::TouchPointReleased)
442         m_touchPoints.removeAt(1);
443 }
444
445 bool LauncherWindow::eventFilter(QObject* obj, QEvent* event)
446 {
447     // If click pos is the bottom right corner (square with size defined by gExitClickArea)
448     // and the window is on FullScreen, the window must return to its original state.
449     if (event->type() == QEvent::MouseButtonRelease) {
450         QMouseEvent* ev = static_cast<QMouseEvent*>(event);
451         if (windowState() == Qt::WindowFullScreen
452             && ev->pos().x() > (width() - gExitClickArea)
453             && ev->pos().y() > (height() - gExitClickArea)) {
454
455             emit enteredFullScreenMode(false);
456         }
457     }
458
459     if (!m_touchMocking)
460         return QObject::eventFilter(obj, event);
461
462     if (event->type() == QEvent::MouseButtonPress
463         || event->type() == QEvent::MouseButtonRelease
464         || event->type() == QEvent::MouseButtonDblClick
465         || event->type() == QEvent::MouseMove) {
466
467         QMouseEvent* ev = static_cast<QMouseEvent*>(event);
468         if (ev->type() == QEvent::MouseMove
469             && !(ev->buttons() & Qt::LeftButton))
470             return false;
471
472         QTouchEvent::TouchPoint touchPoint;
473         touchPoint.setState(Qt::TouchPointMoved);
474         if ((ev->type() == QEvent::MouseButtonPress
475              || ev->type() == QEvent::MouseButtonDblClick))
476             touchPoint.setState(Qt::TouchPointPressed);
477         else if (ev->type() == QEvent::MouseButtonRelease)
478             touchPoint.setState(Qt::TouchPointReleased);
479
480         touchPoint.setId(0);
481         touchPoint.setScreenPos(ev->globalPos());
482         touchPoint.setPos(ev->pos());
483         touchPoint.setPressure(1);
484
485         // If the point already exists, update it. Otherwise create it.
486         if (m_touchPoints.size() > 0 && !m_touchPoints[0].id())
487             m_touchPoints[0] = touchPoint;
488         else if (m_touchPoints.size() > 1 && !m_touchPoints[1].id())
489             m_touchPoints[1] = touchPoint;
490         else
491             m_touchPoints.append(touchPoint);
492
493         sendTouchEvent();
494     } else if (event->type() == QEvent::KeyPress
495         && static_cast<QKeyEvent*>(event)->key() == Qt::Key_F
496         && static_cast<QKeyEvent*>(event)->modifiers() == Qt::ControlModifier) {
497
498         // If the keyboard point is already pressed, release it.
499         // Otherwise create it and append to m_touchPoints.
500         if (m_touchPoints.size() > 0 && m_touchPoints[0].id() == 1) {
501             m_touchPoints[0].setState(Qt::TouchPointReleased);
502             sendTouchEvent();
503         } else if (m_touchPoints.size() > 1 && m_touchPoints[1].id() == 1) {
504             m_touchPoints[1].setState(Qt::TouchPointReleased);
505             sendTouchEvent();
506         } else {
507             QTouchEvent::TouchPoint touchPoint;
508             touchPoint.setState(Qt::TouchPointPressed);
509             touchPoint.setId(1);
510             touchPoint.setScreenPos(QCursor::pos());
511             touchPoint.setPos(m_view->mapFromGlobal(QCursor::pos()));
512             touchPoint.setPressure(1);
513             m_touchPoints.append(touchPoint);
514             sendTouchEvent();
515
516             // After sending the event, change the touchpoint state to stationary
517             m_touchPoints.last().setState(Qt::TouchPointStationary);
518         }
519     }
520
521     return false;
522 }
523
524 void LauncherWindow::loadStarted()
525 {
526     m_view->setFocus(Qt::OtherFocusReason);
527 }
528
529 void LauncherWindow::loadFinished()
530 {
531     QUrl url = page()->mainFrame()->url();
532     setAddressUrl(url.toString(QUrl::RemoveUserInfo));
533     addCompleterEntry(url);
534 }
535
536 void LauncherWindow::showLinkHover(const QString &link, const QString &toolTip)
537 {
538 #ifndef Q_WS_MAEMO_5
539     statusBar()->showMessage(link);
540 #endif
541 #ifndef QT_NO_TOOLTIP
542     if (!toolTip.isEmpty())
543         QToolTip::showText(QCursor::pos(), toolTip);
544 #endif
545 }
546
547 void LauncherWindow::zoomAnimationFinished()
548 {
549     if (!isGraphicsBased())
550         return;
551     QGraphicsWebView* view = static_cast<WebViewGraphicsBased*>(m_view)->graphicsWebView();
552     view->setTiledBackingStoreFrozen(false);
553 }
554
555 void LauncherWindow::applyZoom()
556 {
557 #ifndef QT_NO_ANIMATION
558     if (isGraphicsBased() && page()->settings()->testAttribute(QWebSettings::TiledBackingStoreEnabled)) {
559         QGraphicsWebView* view = static_cast<WebViewGraphicsBased*>(m_view)->graphicsWebView();
560         view->setTiledBackingStoreFrozen(true);
561         if (!m_zoomAnimation) {
562             m_zoomAnimation = new QPropertyAnimation(view, "scale");
563             m_zoomAnimation->setStartValue(view->scale());
564             connect(m_zoomAnimation, SIGNAL(finished()), this, SLOT(zoomAnimationFinished()));
565         } else {
566             m_zoomAnimation->stop();
567             m_zoomAnimation->setStartValue(m_zoomAnimation->currentValue());
568         }
569
570         m_zoomAnimation->setDuration(300);
571         m_zoomAnimation->setEndValue(qreal(m_currentZoom) / 100.);
572         m_zoomAnimation->start();
573         return;
574     }
575 #endif
576     page()->mainFrame()->setZoomFactor(qreal(m_currentZoom) / 100.0);
577 }
578
579 void LauncherWindow::zoomIn()
580 {
581     int i = m_zoomLevels.indexOf(m_currentZoom);
582     Q_ASSERT(i >= 0);
583     if (i < m_zoomLevels.count() - 1)
584         m_currentZoom = m_zoomLevels[i + 1];
585
586     applyZoom();
587 }
588
589 void LauncherWindow::zoomOut()
590 {
591     int i = m_zoomLevels.indexOf(m_currentZoom);
592     Q_ASSERT(i >= 0);
593     if (i > 0)
594         m_currentZoom = m_zoomLevels[i - 1];
595
596     applyZoom();
597 }
598
599 void LauncherWindow::resetZoom()
600 {
601     m_currentZoom = 100;
602     applyZoom();
603 }
604
605 void LauncherWindow::toggleZoomTextOnly(bool b)
606 {
607     page()->settings()->setAttribute(QWebSettings::ZoomTextOnly, b);
608 }
609
610 void LauncherWindow::print()
611 {
612 #if !defined(QT_NO_PRINTER)
613     QPrintPreviewDialog dlg(this);
614     connect(&dlg, SIGNAL(paintRequested(QPrinter*)),
615             page()->mainFrame(), SLOT(print(QPrinter*)));
616     dlg.exec();
617 #endif
618 }
619
620 void LauncherWindow::screenshot()
621 {
622     QPixmap pixmap = QPixmap::grabWidget(m_view);
623     QLabel* label = 0;
624 #if !defined(Q_OS_SYMBIAN)
625     label = new QLabel;
626     label->setAttribute(Qt::WA_DeleteOnClose);
627     label->setWindowTitle("Screenshot - Preview");
628     label->setPixmap(pixmap);
629     label->show();
630 #endif
631
632 #ifndef QT_NO_FILEDIALOG
633     QString fileName = QFileDialog::getSaveFileName(label, "Screenshot");
634     if (!fileName.isEmpty()) {
635         pixmap.save(fileName, "png");
636         if (label)
637             label->setWindowTitle(QString("Screenshot - Saved at %1").arg(fileName));
638     }
639 #endif
640
641 #if defined(QT_CONFIGURED_WITH_OPENGL)
642     toggleQGLWidgetViewport(m_windowOptions.useQGLWidgetViewport);
643 #endif
644 }
645
646 void LauncherWindow::setEditable(bool on)
647 {
648     page()->setContentEditable(on);
649     m_formatMenuAction->setVisible(on);
650 }
651
652 /*
653 void LauncherWindow::dumpPlugins() {
654     QList<QWebPluginInfo> plugins = QWebSettings::pluginDatabase()->plugins();
655     foreach (const QWebPluginInfo plugin, plugins) {
656         qDebug() << "Plugin:" << plugin.name();
657         foreach (const QWebPluginInfo::MimeType mime, plugin.mimeTypes()) {
658             qDebug() << "   " << mime.name;
659         }
660     }
661 }
662 */
663
664 void LauncherWindow::dumpHtml()
665 {
666     qDebug() << "HTML: " << page()->mainFrame()->toHtml();
667 }
668
669 void LauncherWindow::selectElements()
670 {
671 #ifndef QT_NO_INPUTDIALOG
672     bool ok;
673     QString str = QInputDialog::getText(this, "Select elements", "Choose elements",
674                                         QLineEdit::Normal, "a", &ok);
675
676     if (ok && !str.isEmpty()) {
677         QWebElementCollection result =  page()->mainFrame()->findAllElements(str);
678         foreach (QWebElement e, result)
679             e.setStyleProperty("background-color", "yellow");
680 #ifndef Q_WS_MAEMO_5
681         statusBar()->showMessage(QString("%1 element(s) selected").arg(result.count()), 5000);
682 #endif
683     }
684 #endif
685 }
686
687 void LauncherWindow::setTouchMocking(bool on)
688 {
689     m_touchMocking = on;
690 }
691
692 void LauncherWindow::toggleWebView(bool graphicsBased)
693 {
694     m_windowOptions.useGraphicsView = graphicsBased;
695     initializeView();
696     menuBar()->clear();
697     createChrome();
698 }
699
700 void LauncherWindow::toggleAcceleratedCompositing(bool toggle)
701 {
702     m_windowOptions.useCompositing = toggle;
703     page()->settings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, toggle);
704 }
705
706 void LauncherWindow::toggleTiledBackingStore(bool toggle)
707 {
708     page()->settings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, toggle);
709 }
710
711 void LauncherWindow::toggleResizesToContents(bool toggle)
712 {
713     m_windowOptions.resizesToContents = toggle;
714     static_cast<WebViewGraphicsBased*>(m_view)->setResizesToContents(toggle);
715 }
716
717 void LauncherWindow::toggleWebGL(bool toggle)
718 {
719     m_windowOptions.useWebGL = toggle;
720     page()->settings()->setAttribute(QWebSettings::WebGLEnabled, toggle);
721 }
722
723 void LauncherWindow::toggleThreadedQnam(bool toggle)
724 {
725     m_windowOptions.useThreadedQnam = toggle;
726     page()->setQnamThreaded(toggle);
727 }
728
729 void LauncherWindow::animatedFlip()
730 {
731     qobject_cast<WebViewGraphicsBased*>(m_view)->animatedFlip();
732 }
733
734 void LauncherWindow::animatedYFlip()
735 {
736     qobject_cast<WebViewGraphicsBased*>(m_view)->animatedYFlip();
737 }
738 void LauncherWindow::toggleSpatialNavigation(bool b)
739 {
740     page()->settings()->setAttribute(QWebSettings::SpatialNavigationEnabled, b);
741 }
742
743 void LauncherWindow::toggleFullScreenMode(bool enable)
744 {
745     if (enable)
746         setWindowState(Qt::WindowFullScreen);
747     else {
748 #if defined(Q_OS_SYMBIAN)
749         setWindowState(Qt::WindowMaximized);
750 #else
751         setWindowState(Qt::WindowNoState);
752 #endif
753     }
754 }
755
756 void LauncherWindow::toggleFrameFlattening(bool toggle)
757 {
758     m_windowOptions.useFrameFlattening = toggle;
759     page()->settings()->setAttribute(QWebSettings::FrameFlatteningEnabled, toggle);
760 }
761
762 void LauncherWindow::toggleInterruptingJavaScriptEnabled(bool enable)
763 {
764     page()->setInterruptingJavaScriptEnabled(enable);
765 }
766
767 void LauncherWindow::toggleJavascriptCanOpenWindows(bool enable)
768 {
769     page()->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, enable);
770 }
771
772 #if defined(QT_CONFIGURED_WITH_OPENGL)
773 void LauncherWindow::toggleQGLWidgetViewport(bool enable)
774 {
775     if (!isGraphicsBased())
776         return;
777
778     m_windowOptions.useQGLWidgetViewport = enable;
779     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
780
781     view->setViewport(enable ? new QGLWidget() : 0);
782 }
783 #endif
784
785 void LauncherWindow::changeViewportUpdateMode(int mode)
786 {
787     m_windowOptions.viewportUpdateMode = QGraphicsView::ViewportUpdateMode(mode);
788
789     if (!isGraphicsBased())
790         return;
791
792     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
793     view->setViewportUpdateMode(m_windowOptions.viewportUpdateMode);
794 }
795
796 void LauncherWindow::showFPS(bool enable)
797 {
798     if (!isGraphicsBased())
799         return;
800
801     m_windowOptions.showFrameRate = enable;
802     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
803     view->setFrameRateMeasurementEnabled(enable);
804
805     if (!enable) {
806 #if defined(Q_WS_MAEMO_5) && defined(Q_OS_SYMBIAN)
807         setWindowTitle("");
808 #else
809         statusBar()->clearMessage();
810 #endif
811     }
812 }
813
814 void LauncherWindow::showUserAgentDialog()
815 {
816     QStringList items;
817     QFile file(":/useragentlist.txt");
818     if (file.open(QIODevice::ReadOnly)) {
819          while (!file.atEnd())
820             items << file.readLine().trimmed();
821         file.close();
822     }
823
824     QSettings settings;
825     QString customUserAgent = settings.value("CustomUserAgent").toString();
826     if (!items.contains(customUserAgent) && !customUserAgent.isEmpty())
827         items << customUserAgent;
828
829     QDialog* dialog = new QDialog(this);
830     dialog->resize(size().width() * 0.7, dialog->size().height());
831     dialog->setMaximumHeight(dialog->size().height());
832     dialog->setWindowTitle("Change User Agent");
833
834     QVBoxLayout* layout = new QVBoxLayout(dialog);
835     dialog->setLayout(layout);
836
837 #ifndef QT_NO_COMBOBOX
838     QComboBox* combo = new QComboBox(dialog);
839     combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
840     combo->setEditable(true);
841     combo->insertItems(0, items);
842     layout->addWidget(combo);
843
844     int index = combo->findText(page()->userAgentForUrl(QUrl()));
845     combo->setCurrentIndex(index);
846 #endif
847
848     QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
849             | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
850     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
851     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
852     layout->addWidget(buttonBox);
853
854 #ifndef QT_NO_COMBOBOX
855     if (dialog->exec() && !combo->currentText().isEmpty()) {
856         page()->setUserAgent(combo->currentText());
857         if (!items.contains(combo->currentText()))
858             settings.setValue("CustomUserAgent", combo->currentText());
859     }
860 #endif
861
862     delete dialog;
863 }
864
865 void LauncherWindow::updateFPS(int fps)
866 {
867     QString fpsStatusText = QString("Current FPS: %1").arg(fps);
868
869 #if defined(Q_WS_MAEMO_5) && defined(Q_OS_SYMBIAN)
870     setWindowTitle(fpsStatusText);
871 #else
872     statusBar()->showMessage(fpsStatusText);
873 #endif
874 }
875
876 LauncherWindow* LauncherWindow::newWindow()
877 {
878     LauncherWindow* mw = new LauncherWindow(&m_windowOptions);
879     mw->show();
880     return mw;
881 }
882
883 LauncherWindow* LauncherWindow::cloneWindow()
884 {
885     LauncherWindow* mw = new LauncherWindow(&m_windowOptions, qobject_cast<QGraphicsView*>(m_view)->scene());
886     mw->show();
887     return mw;
888 }
889