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