OSDN Git Service

Merge changes I347b2c80,I9e3185de
[android-x86/external-webkit.git] / WebKitTools / QtLauncher / webview.cpp
1 /*
2  * Copyright (C) 2009 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 "webview.h"
34
35 #include <QtGui>
36 #include <QGraphicsScene>
37
38 WebViewGraphicsBased::WebViewGraphicsBased(QWidget* parent)
39     : QGraphicsView(parent)
40     , m_item(new GraphicsWebView)
41     , m_numPaintsTotal(0)
42     , m_numPaintsSinceLastMeasure(0)
43     , m_measureFps(false)
44 {
45     setScene(new QGraphicsScene(this));
46     scene()->addItem(m_item);
47
48     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50
51 }
52
53 void WebViewGraphicsBased::resizeEvent(QResizeEvent* event)
54 {
55     QGraphicsView::resizeEvent(event);
56     QRectF rect(QPoint(0, 0), event->size());
57     m_item->setGeometry(rect);
58 }
59
60 void WebViewGraphicsBased::enableFrameRateMeasurement()
61 {
62     m_measureFps = true;
63     m_lastConsultTime = m_startTime = QTime::currentTime();
64     QTimer* updateTimer = new QTimer(this);
65     updateTimer->setInterval(1000);
66     connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateFrameRate()));
67     updateTimer->start();
68 }
69
70 void WebViewGraphicsBased::updateFrameRate()
71 {
72     QTime now = QTime::currentTime();
73
74     int interval = m_lastConsultTime.msecsTo(now);
75     int total = m_startTime.msecsTo(now);
76
77     int average = total ? m_numPaintsTotal * 1000 / total : 0;
78     int current = interval ? m_numPaintsSinceLastMeasure * 1000 / interval : 0;
79
80     qDebug("[FPS] average: %d, current: %d", average, current);
81
82     m_lastConsultTime = now;
83     m_numPaintsSinceLastMeasure = 0;
84 }
85
86 void WebViewGraphicsBased::paintEvent(QPaintEvent* event)
87 {
88     QGraphicsView::paintEvent(event);
89     if (!m_measureFps)
90         return;
91     m_numPaintsSinceLastMeasure++;
92     m_numPaintsTotal++;
93 }
94
95 static QMenu* createContextMenu(QWebPage* page, QPoint position)
96 {
97     QMenu* menu = page->createStandardContextMenu();
98
99     QWebHitTestResult r = page->mainFrame()->hitTestContent(position);
100
101     if (!r.linkUrl().isEmpty()) {
102         WebPage* webPage = qobject_cast<WebPage*>(page);
103         QAction* newTabAction = menu->addAction("Open in Default &Browser", webPage, SLOT(openUrlInDefaultBrowser()));
104         newTabAction->setData(r.linkUrl());
105         menu->insertAction(menu->actions().at(2), newTabAction);
106     }
107     return menu;
108 }
109
110 void GraphicsWebView::mousePressEvent(QGraphicsSceneMouseEvent* event)
111 {
112     setProperty("mouseButtons", QVariant::fromValue(int(event->buttons())));
113     setProperty("keyboardModifiers", QVariant::fromValue(int(event->modifiers())));
114
115     QGraphicsWebView::mousePressEvent(event);
116 }
117
118 void WebViewTraditional::mousePressEvent(QMouseEvent* event)
119 {
120     setProperty("mouseButtons", QVariant::fromValue(int(event->buttons())));
121     setProperty("keyboardModifiers", QVariant::fromValue(int(event->modifiers())));
122
123     QWebView::mousePressEvent(event);
124 }
125
126 void GraphicsWebView::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
127 {
128     QMenu* menu = createContextMenu(page(), event->pos().toPoint());
129     menu->exec(mapToScene(event->pos()).toPoint());
130     delete menu;
131 }
132
133 void WebViewTraditional::contextMenuEvent(QContextMenuEvent* event)
134 {
135     QMenu* menu = createContextMenu(page(), event->pos());
136     menu->exec(mapToGlobal(event->pos()));
137     delete menu;
138 }
139