2011-01-25 Mansi Mithal <mansi.mithal@nokia.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* userAgentAction = toolsMenu->addAction("Change User Agent", this, SLOT(showUserAgentDialog()));
263     userAgentAction->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_U));
264
265     toolsMenu->addAction("Select Elements...", this, SLOT(selectElements()));
266
267     QAction* showInspectorAction = toolsMenu->addAction("Show Web Inspector", m_inspector, SLOT(setVisible(bool)), QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_I));
268     showInspectorAction->setCheckable(true);
269     showInspectorAction->connect(m_inspector, SIGNAL(visibleChanged(bool)), SLOT(setChecked(bool)));
270
271     // GraphicsView sub menu.
272     QAction* toggleAcceleratedCompositing = graphicsViewMenu->addAction("Toggle Accelerated Compositing", this, SLOT(toggleAcceleratedCompositing(bool)));
273     toggleAcceleratedCompositing->setCheckable(true);
274     toggleAcceleratedCompositing->setChecked(settings->testAttribute(QWebSettings::AcceleratedCompositingEnabled));
275     toggleAcceleratedCompositing->setEnabled(isGraphicsBased());
276     toggleAcceleratedCompositing->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
277
278     QAction* toggleResizesToContents = graphicsViewMenu->addAction("Toggle Resizes To Contents Mode", this, SLOT(toggleResizesToContents(bool)));
279     toggleResizesToContents->setCheckable(true);
280     toggleResizesToContents->setChecked(m_windowOptions.resizesToContents);
281     toggleResizesToContents->setEnabled(isGraphicsBased());
282     toggleResizesToContents->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
283
284     QAction* toggleTiledBackingStore = graphicsViewMenu->addAction("Toggle Tiled Backing Store", this, SLOT(toggleTiledBackingStore(bool)));
285     toggleTiledBackingStore->setCheckable(true);
286     toggleTiledBackingStore->setChecked(m_windowOptions.useTiledBackingStore);
287     toggleTiledBackingStore->setEnabled(isGraphicsBased());
288     toggleTiledBackingStore->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
289
290 #if defined(QT_CONFIGURED_WITH_OPENGL)
291     QAction* toggleQGLWidgetViewport = graphicsViewMenu->addAction("Toggle use of QGLWidget Viewport", this, SLOT(toggleQGLWidgetViewport(bool)));
292     toggleQGLWidgetViewport->setCheckable(true);
293     toggleQGLWidgetViewport->setChecked(m_windowOptions.useQGLWidgetViewport);
294     toggleQGLWidgetViewport->setEnabled(isGraphicsBased());
295     toggleQGLWidgetViewport->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
296 #endif
297
298     QMenu* viewportUpdateMenu = graphicsViewMenu->addMenu("Change Viewport Update Mode");
299     viewportUpdateMenu->setEnabled(isGraphicsBased());
300     viewportUpdateMenu->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
301
302     QAction* fullUpdate = viewportUpdateMenu->addAction("FullViewportUpdate");
303     fullUpdate->setCheckable(true);
304     fullUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::FullViewportUpdate) ? true : false);
305
306     QAction* minimalUpdate = viewportUpdateMenu->addAction("MinimalViewportUpdate");
307     minimalUpdate->setCheckable(true);
308     minimalUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::MinimalViewportUpdate) ? true : false);
309
310     QAction* smartUpdate = viewportUpdateMenu->addAction("SmartViewportUpdate");
311     smartUpdate->setCheckable(true);
312     smartUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::SmartViewportUpdate) ? true : false);
313
314     QAction* boundingRectUpdate = viewportUpdateMenu->addAction("BoundingRectViewportUpdate");
315     boundingRectUpdate->setCheckable(true);
316     boundingRectUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate) ? true : false);
317
318     QAction* noUpdate = viewportUpdateMenu->addAction("NoViewportUpdate");
319     noUpdate->setCheckable(true);
320     noUpdate->setChecked((m_windowOptions.viewportUpdateMode == QGraphicsView::NoViewportUpdate) ? true : false);
321
322     QSignalMapper* signalMapper = new QSignalMapper(viewportUpdateMenu);
323     signalMapper->setMapping(fullUpdate, QGraphicsView::FullViewportUpdate);
324     signalMapper->setMapping(minimalUpdate, QGraphicsView::MinimalViewportUpdate);
325     signalMapper->setMapping(smartUpdate, QGraphicsView::SmartViewportUpdate);
326     signalMapper->setMapping(boundingRectUpdate, QGraphicsView::BoundingRectViewportUpdate);
327     signalMapper->setMapping(noUpdate, QGraphicsView::NoViewportUpdate);
328
329     connect(fullUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
330     connect(minimalUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
331     connect(smartUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
332     connect(boundingRectUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
333     connect(noUpdate, SIGNAL(triggered()), signalMapper, SLOT(map()));
334
335     connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(changeViewportUpdateMode(int)));
336
337     QActionGroup* viewportUpdateModeActions = new QActionGroup(viewportUpdateMenu);
338     viewportUpdateModeActions->addAction(fullUpdate);
339     viewportUpdateModeActions->addAction(minimalUpdate);
340     viewportUpdateModeActions->addAction(smartUpdate);
341     viewportUpdateModeActions->addAction(boundingRectUpdate);
342     viewportUpdateModeActions->addAction(noUpdate);
343
344     graphicsViewMenu->addSeparator();
345
346     QAction* flipAnimated = graphicsViewMenu->addAction("Animated Flip");
347     flipAnimated->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
348     flipAnimated->setEnabled(isGraphicsBased());
349     connect(flipAnimated, SIGNAL(triggered()), SLOT(animatedFlip()));
350
351     QAction* flipYAnimated = graphicsViewMenu->addAction("Animated Y-Flip");
352     flipYAnimated->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
353     flipYAnimated->setEnabled(isGraphicsBased());
354     connect(flipYAnimated, SIGNAL(triggered()), SLOT(animatedYFlip()));
355
356     QAction* cloneWindow = graphicsViewMenu->addAction("Clone Window", this, SLOT(cloneWindow()));
357     cloneWindow->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
358     cloneWindow->setEnabled(isGraphicsBased());
359
360     graphicsViewMenu->addSeparator();
361
362     QAction* showFPS = graphicsViewMenu->addAction("Show FPS", this, SLOT(showFPS(bool)));
363     showFPS->setCheckable(true);
364     showFPS->setEnabled(isGraphicsBased());
365     showFPS->connect(toggleGraphicsView, SIGNAL(toggled(bool)), SLOT(setEnabled(bool)));
366     showFPS->setChecked(m_windowOptions.showFrameRate);
367
368     QMenu* settingsMenu = menuBar()->addMenu("&Settings");
369
370     QAction* toggleAutoLoadImages = settingsMenu->addAction("Disable Auto Load Images", this, SLOT(toggleAutoLoadImages(bool)));
371     toggleAutoLoadImages->setCheckable(true);
372     toggleAutoLoadImages->setChecked(false);
373
374     QAction* toggleInterruptingJavaScripteEnabled = settingsMenu->addAction("Enable interrupting js scripts", this, SLOT(toggleInterruptingJavaScriptEnabled(bool)));
375     toggleInterruptingJavaScripteEnabled->setCheckable(true);
376     toggleInterruptingJavaScripteEnabled->setChecked(false);
377
378     QAction* toggleJavascriptCanOpenWindows = settingsMenu->addAction("Enable js popup windows", this, SLOT(toggleJavascriptCanOpenWindows(bool)));
379     toggleJavascriptCanOpenWindows->setCheckable(true);
380     toggleJavascriptCanOpenWindows->setChecked(false);
381
382 #endif
383 }
384
385 bool LauncherWindow::isGraphicsBased() const
386 {
387     return bool(qobject_cast<QGraphicsView*>(m_view));
388 }
389
390 void LauncherWindow::keyPressEvent(QKeyEvent* event)
391 {
392 #ifdef Q_WS_MAEMO_5
393     switch (event->key()) {
394     case Qt::Key_F7:
395         zoomIn();
396         event->accept();
397         break;
398     case Qt::Key_F8:
399         zoomOut();
400         event->accept();
401         break;
402     }
403 #endif
404     MainWindow::keyPressEvent(event);
405 }
406
407 void LauncherWindow::grabZoomKeys(bool grab)
408 {
409 #ifdef Q_WS_MAEMO_5
410     if (!winId()) {
411         qWarning("Can't grab keys unless we have a window id");
412         return;
413     }
414
415     Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
416     if (!atom) {
417         qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM");
418         return;
419     }
420
421     unsigned long val = (grab) ? 1 : 0;
422     XChangeProperty(QX11Info::display(), winId(), atom, XA_INTEGER, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&val), 1);
423 #endif
424 }
425
426 void LauncherWindow::sendTouchEvent()
427 {
428     if (m_touchPoints.isEmpty())
429         return;
430
431     QEvent::Type type = QEvent::TouchUpdate;
432     if (m_touchPoints.size() == 1) {
433         if (m_touchPoints[0].state() == Qt::TouchPointReleased)
434             type = QEvent::TouchEnd;
435         else if (m_touchPoints[0].state() == Qt::TouchPointPressed)
436             type = QEvent::TouchBegin;
437     }
438
439     QTouchEvent touchEv(type);
440     touchEv.setTouchPoints(m_touchPoints);
441     QCoreApplication::sendEvent(page(), &touchEv);
442
443     // After sending the event, remove all touchpoints that were released
444     if (m_touchPoints[0].state() == Qt::TouchPointReleased)
445         m_touchPoints.removeAt(0);
446     if (m_touchPoints.size() > 1 && m_touchPoints[1].state() == Qt::TouchPointReleased)
447         m_touchPoints.removeAt(1);
448 }
449
450 bool LauncherWindow::eventFilter(QObject* obj, QEvent* event)
451 {
452     // If click pos is the bottom right corner (square with size defined by gExitClickArea)
453     // and the window is on FullScreen, the window must return to its original state.
454     if (event->type() == QEvent::MouseButtonRelease) {
455         QMouseEvent* ev = static_cast<QMouseEvent*>(event);
456         if (windowState() == Qt::WindowFullScreen
457             && ev->pos().x() > (width() - gExitClickArea)
458             && ev->pos().y() > (height() - gExitClickArea)) {
459
460             emit enteredFullScreenMode(false);
461         }
462     }
463
464     if (!m_touchMocking)
465         return QObject::eventFilter(obj, event);
466
467     if (event->type() == QEvent::MouseButtonPress
468         || event->type() == QEvent::MouseButtonRelease
469         || event->type() == QEvent::MouseButtonDblClick
470         || event->type() == QEvent::MouseMove) {
471
472         QMouseEvent* ev = static_cast<QMouseEvent*>(event);
473         if (ev->type() == QEvent::MouseMove
474             && !(ev->buttons() & Qt::LeftButton))
475             return false;
476
477         QTouchEvent::TouchPoint touchPoint;
478         touchPoint.setState(Qt::TouchPointMoved);
479         if ((ev->type() == QEvent::MouseButtonPress
480              || ev->type() == QEvent::MouseButtonDblClick))
481             touchPoint.setState(Qt::TouchPointPressed);
482         else if (ev->type() == QEvent::MouseButtonRelease)
483             touchPoint.setState(Qt::TouchPointReleased);
484
485         touchPoint.setId(0);
486         touchPoint.setScreenPos(ev->globalPos());
487         touchPoint.setPos(ev->pos());
488         touchPoint.setPressure(1);
489
490         // If the point already exists, update it. Otherwise create it.
491         if (m_touchPoints.size() > 0 && !m_touchPoints[0].id())
492             m_touchPoints[0] = touchPoint;
493         else if (m_touchPoints.size() > 1 && !m_touchPoints[1].id())
494             m_touchPoints[1] = touchPoint;
495         else
496             m_touchPoints.append(touchPoint);
497
498         sendTouchEvent();
499     } else if (event->type() == QEvent::KeyPress
500         && static_cast<QKeyEvent*>(event)->key() == Qt::Key_F
501         && static_cast<QKeyEvent*>(event)->modifiers() == Qt::ControlModifier) {
502
503         // If the keyboard point is already pressed, release it.
504         // Otherwise create it and append to m_touchPoints.
505         if (m_touchPoints.size() > 0 && m_touchPoints[0].id() == 1) {
506             m_touchPoints[0].setState(Qt::TouchPointReleased);
507             sendTouchEvent();
508         } else if (m_touchPoints.size() > 1 && m_touchPoints[1].id() == 1) {
509             m_touchPoints[1].setState(Qt::TouchPointReleased);
510             sendTouchEvent();
511         } else {
512             QTouchEvent::TouchPoint touchPoint;
513             touchPoint.setState(Qt::TouchPointPressed);
514             touchPoint.setId(1);
515             touchPoint.setScreenPos(QCursor::pos());
516             touchPoint.setPos(m_view->mapFromGlobal(QCursor::pos()));
517             touchPoint.setPressure(1);
518             m_touchPoints.append(touchPoint);
519             sendTouchEvent();
520
521             // After sending the event, change the touchpoint state to stationary
522             m_touchPoints.last().setState(Qt::TouchPointStationary);
523         }
524     }
525
526     return false;
527 }
528
529 void LauncherWindow::loadStarted()
530 {
531     m_view->setFocus(Qt::OtherFocusReason);
532 }
533
534 void LauncherWindow::loadFinished()
535 {
536     QUrl url = page()->mainFrame()->url();
537     setAddressUrl(url.toString(QUrl::RemoveUserInfo));
538     addCompleterEntry(url);
539 }
540
541 void LauncherWindow::showLinkHover(const QString &link, const QString &toolTip)
542 {
543 #ifndef Q_WS_MAEMO_5
544     statusBar()->showMessage(link);
545 #endif
546 #ifndef QT_NO_TOOLTIP
547     if (!toolTip.isEmpty())
548         QToolTip::showText(QCursor::pos(), toolTip);
549 #endif
550 }
551
552 void LauncherWindow::zoomAnimationFinished()
553 {
554     if (!isGraphicsBased())
555         return;
556     QGraphicsWebView* view = static_cast<WebViewGraphicsBased*>(m_view)->graphicsWebView();
557     view->setTiledBackingStoreFrozen(false);
558 }
559
560 void LauncherWindow::applyZoom()
561 {
562 #ifndef QT_NO_ANIMATION
563     if (isGraphicsBased() && page()->settings()->testAttribute(QWebSettings::TiledBackingStoreEnabled)) {
564         QGraphicsWebView* view = static_cast<WebViewGraphicsBased*>(m_view)->graphicsWebView();
565         view->setTiledBackingStoreFrozen(true);
566         if (!m_zoomAnimation) {
567             m_zoomAnimation = new QPropertyAnimation(view, "scale");
568             m_zoomAnimation->setStartValue(view->scale());
569             connect(m_zoomAnimation, SIGNAL(finished()), this, SLOT(zoomAnimationFinished()));
570         } else {
571             m_zoomAnimation->stop();
572             m_zoomAnimation->setStartValue(m_zoomAnimation->currentValue());
573         }
574
575         m_zoomAnimation->setDuration(300);
576         m_zoomAnimation->setEndValue(qreal(m_currentZoom) / 100.);
577         m_zoomAnimation->start();
578         return;
579     }
580 #endif
581     page()->mainFrame()->setZoomFactor(qreal(m_currentZoom) / 100.0);
582 }
583
584 void LauncherWindow::zoomIn()
585 {
586     int i = m_zoomLevels.indexOf(m_currentZoom);
587     Q_ASSERT(i >= 0);
588     if (i < m_zoomLevels.count() - 1)
589         m_currentZoom = m_zoomLevels[i + 1];
590
591     applyZoom();
592 }
593
594 void LauncherWindow::zoomOut()
595 {
596     int i = m_zoomLevels.indexOf(m_currentZoom);
597     Q_ASSERT(i >= 0);
598     if (i > 0)
599         m_currentZoom = m_zoomLevels[i - 1];
600
601     applyZoom();
602 }
603
604 void LauncherWindow::resetZoom()
605 {
606     m_currentZoom = 100;
607     applyZoom();
608 }
609
610 void LauncherWindow::toggleZoomTextOnly(bool b)
611 {
612     page()->settings()->setAttribute(QWebSettings::ZoomTextOnly, b);
613 }
614
615 void LauncherWindow::print()
616 {
617 #if !defined(QT_NO_PRINTER)
618     QPrintPreviewDialog dlg(this);
619     connect(&dlg, SIGNAL(paintRequested(QPrinter*)),
620             page()->mainFrame(), SLOT(print(QPrinter*)));
621     dlg.exec();
622 #endif
623 }
624
625 void LauncherWindow::screenshot()
626 {
627     QPixmap pixmap = QPixmap::grabWidget(m_view);
628     QLabel* label = 0;
629 #if !defined(Q_OS_SYMBIAN)
630     label = new QLabel;
631     label->setAttribute(Qt::WA_DeleteOnClose);
632     label->setWindowTitle("Screenshot - Preview");
633     label->setPixmap(pixmap);
634     label->show();
635 #endif
636
637 #ifndef QT_NO_FILEDIALOG
638     QString fileName = QFileDialog::getSaveFileName(label, "Screenshot");
639     if (!fileName.isEmpty()) {
640         pixmap.save(fileName, "png");
641         if (label)
642             label->setWindowTitle(QString("Screenshot - Saved at %1").arg(fileName));
643     }
644 #endif
645
646 #if defined(QT_CONFIGURED_WITH_OPENGL)
647     toggleQGLWidgetViewport(m_windowOptions.useQGLWidgetViewport);
648 #endif
649 }
650
651 void LauncherWindow::setEditable(bool on)
652 {
653     page()->setContentEditable(on);
654     m_formatMenuAction->setVisible(on);
655 }
656
657 /*
658 void LauncherWindow::dumpPlugins() {
659     QList<QWebPluginInfo> plugins = QWebSettings::pluginDatabase()->plugins();
660     foreach (const QWebPluginInfo plugin, plugins) {
661         qDebug() << "Plugin:" << plugin.name();
662         foreach (const QWebPluginInfo::MimeType mime, plugin.mimeTypes()) {
663             qDebug() << "   " << mime.name;
664         }
665     }
666 }
667 */
668
669 void LauncherWindow::dumpHtml()
670 {
671     qDebug() << "HTML: " << page()->mainFrame()->toHtml();
672 }
673
674 void LauncherWindow::selectElements()
675 {
676 #ifndef QT_NO_INPUTDIALOG
677     bool ok;
678     QString str = QInputDialog::getText(this, "Select elements", "Choose elements",
679                                         QLineEdit::Normal, "a", &ok);
680
681     if (ok && !str.isEmpty()) {
682         QWebElementCollection result =  page()->mainFrame()->findAllElements(str);
683         foreach (QWebElement e, result)
684             e.setStyleProperty("background-color", "yellow");
685 #ifndef Q_WS_MAEMO_5
686         statusBar()->showMessage(QString("%1 element(s) selected").arg(result.count()), 5000);
687 #endif
688     }
689 #endif
690 }
691
692 void LauncherWindow::setTouchMocking(bool on)
693 {
694     m_touchMocking = on;
695 }
696
697 void LauncherWindow::toggleWebView(bool graphicsBased)
698 {
699     m_windowOptions.useGraphicsView = graphicsBased;
700     initializeView();
701     menuBar()->clear();
702     createChrome();
703 }
704
705 void LauncherWindow::toggleAcceleratedCompositing(bool toggle)
706 {
707     m_windowOptions.useCompositing = toggle;
708     page()->settings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, toggle);
709 }
710
711 void LauncherWindow::toggleTiledBackingStore(bool toggle)
712 {
713     page()->settings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, toggle);
714 }
715
716 void LauncherWindow::toggleResizesToContents(bool toggle)
717 {
718     m_windowOptions.resizesToContents = toggle;
719     static_cast<WebViewGraphicsBased*>(m_view)->setResizesToContents(toggle);
720 }
721
722 void LauncherWindow::toggleWebGL(bool toggle)
723 {
724     m_windowOptions.useWebGL = toggle;
725     page()->settings()->setAttribute(QWebSettings::WebGLEnabled, toggle);
726 }
727
728 void LauncherWindow::toggleThreadedQnam(bool toggle)
729 {
730     m_windowOptions.useThreadedQnam = toggle;
731     page()->setQnamThreaded(toggle);
732 }
733
734 void LauncherWindow::animatedFlip()
735 {
736     qobject_cast<WebViewGraphicsBased*>(m_view)->animatedFlip();
737 }
738
739 void LauncherWindow::animatedYFlip()
740 {
741     qobject_cast<WebViewGraphicsBased*>(m_view)->animatedYFlip();
742 }
743 void LauncherWindow::toggleSpatialNavigation(bool b)
744 {
745     page()->settings()->setAttribute(QWebSettings::SpatialNavigationEnabled, b);
746 }
747
748 void LauncherWindow::toggleFullScreenMode(bool enable)
749 {
750     if (enable)
751         setWindowState(Qt::WindowFullScreen);
752     else {
753 #if defined(Q_OS_SYMBIAN)
754         setWindowState(Qt::WindowMaximized);
755 #else
756         setWindowState(Qt::WindowNoState);
757 #endif
758     }
759 }
760
761 void LauncherWindow::toggleFrameFlattening(bool toggle)
762 {
763     m_windowOptions.useFrameFlattening = toggle;
764     page()->settings()->setAttribute(QWebSettings::FrameFlatteningEnabled, toggle);
765 }
766
767 void LauncherWindow::toggleInterruptingJavaScriptEnabled(bool enable)
768 {
769     page()->setInterruptingJavaScriptEnabled(enable);
770 }
771
772 void LauncherWindow::toggleJavascriptCanOpenWindows(bool enable)
773 {
774     page()->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, enable);
775 }
776
777 void LauncherWindow::toggleAutoLoadImages(bool enable)
778 {
779     page()->settings()->setAttribute(QWebSettings::AutoLoadImages, !enable);
780 }
781
782 #if defined(QT_CONFIGURED_WITH_OPENGL)
783 void LauncherWindow::toggleQGLWidgetViewport(bool enable)
784 {
785     if (!isGraphicsBased())
786         return;
787
788     m_windowOptions.useQGLWidgetViewport = enable;
789     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
790
791     view->setViewport(enable ? new QGLWidget() : 0);
792 }
793 #endif
794
795 void LauncherWindow::changeViewportUpdateMode(int mode)
796 {
797     m_windowOptions.viewportUpdateMode = QGraphicsView::ViewportUpdateMode(mode);
798
799     if (!isGraphicsBased())
800         return;
801
802     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
803     view->setViewportUpdateMode(m_windowOptions.viewportUpdateMode);
804 }
805
806 void LauncherWindow::showFPS(bool enable)
807 {
808     if (!isGraphicsBased())
809         return;
810
811     m_windowOptions.showFrameRate = enable;
812     WebViewGraphicsBased* view = static_cast<WebViewGraphicsBased*>(m_view);
813     view->setFrameRateMeasurementEnabled(enable);
814
815     if (!enable) {
816 #if defined(Q_WS_MAEMO_5) && defined(Q_OS_SYMBIAN)
817         setWindowTitle("");
818 #else
819         statusBar()->clearMessage();
820 #endif
821     }
822 }
823
824 void LauncherWindow::showUserAgentDialog()
825 {
826     QStringList items;
827     QFile file(":/useragentlist.txt");
828     if (file.open(QIODevice::ReadOnly)) {
829          while (!file.atEnd())
830             items << file.readLine().trimmed();
831         file.close();
832     }
833
834     QSettings settings;
835     QString customUserAgent = settings.value("CustomUserAgent").toString();
836     if (!items.contains(customUserAgent) && !customUserAgent.isEmpty())
837         items << customUserAgent;
838
839     QDialog* dialog = new QDialog(this);
840     dialog->resize(size().width() * 0.7, dialog->size().height());
841     dialog->setMaximumHeight(dialog->size().height());
842     dialog->setWindowTitle("Change User Agent");
843
844     QVBoxLayout* layout = new QVBoxLayout(dialog);
845     dialog->setLayout(layout);
846
847 #ifndef QT_NO_COMBOBOX
848     QComboBox* combo = new QComboBox(dialog);
849     combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
850     combo->setEditable(true);
851     combo->insertItems(0, items);
852     layout->addWidget(combo);
853
854     int index = combo->findText(page()->userAgentForUrl(QUrl()));
855     combo->setCurrentIndex(index);
856 #endif
857
858     QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
859             | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
860     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
861     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
862     layout->addWidget(buttonBox);
863
864 #ifndef QT_NO_COMBOBOX
865     if (dialog->exec() && !combo->currentText().isEmpty()) {
866         page()->setUserAgent(combo->currentText());
867         if (!items.contains(combo->currentText()))
868             settings.setValue("CustomUserAgent", combo->currentText());
869     }
870 #endif
871
872     delete dialog;
873 }
874
875 void LauncherWindow::updateFPS(int fps)
876 {
877     QString fpsStatusText = QString("Current FPS: %1").arg(fps);
878
879 #if defined(Q_WS_MAEMO_5) && defined(Q_OS_SYMBIAN)
880     setWindowTitle(fpsStatusText);
881 #else
882     statusBar()->showMessage(fpsStatusText);
883 #endif
884 }
885
886 LauncherWindow* LauncherWindow::newWindow()
887 {
888     LauncherWindow* mw = new LauncherWindow(&m_windowOptions);
889     mw->show();
890     return mw;
891 }
892
893 LauncherWindow* LauncherWindow::cloneWindow()
894 {
895     LauncherWindow* mw = new LauncherWindow(&m_windowOptions, qobject_cast<QGraphicsView*>(m_view)->scene());
896     mw->show();
897     return mw;
898 }
899