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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
36 #include <QGraphicsScene>
38 WebViewGraphicsBased::WebViewGraphicsBased(QWidget* parent)
39 : QGraphicsView(parent)
40 , m_item(new GraphicsWebView)
42 , m_numPaintsSinceLastMeasure(0)
45 setScene(new QGraphicsScene(this));
46 scene()->addItem(m_item);
48 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53 void WebViewGraphicsBased::resizeEvent(QResizeEvent* event)
55 QGraphicsView::resizeEvent(event);
56 QRectF rect(QPoint(0, 0), event->size());
57 m_item->setGeometry(rect);
60 void WebViewGraphicsBased::enableFrameRateMeasurement()
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()));
70 void WebViewGraphicsBased::updateFrameRate()
72 QTime now = QTime::currentTime();
74 int interval = m_lastConsultTime.msecsTo(now);
75 int total = m_startTime.msecsTo(now);
77 int average = total ? m_numPaintsTotal * 1000 / total : 0;
78 int current = interval ? m_numPaintsSinceLastMeasure * 1000 / interval : 0;
80 qDebug("[FPS] average: %d, current: %d", average, current);
82 m_lastConsultTime = now;
83 m_numPaintsSinceLastMeasure = 0;
86 void WebViewGraphicsBased::paintEvent(QPaintEvent* event)
88 QGraphicsView::paintEvent(event);
91 m_numPaintsSinceLastMeasure++;
95 static QMenu* createContextMenu(QWebPage* page, QPoint position)
97 QMenu* menu = page->createStandardContextMenu();
99 QWebHitTestResult r = page->mainFrame()->hitTestContent(position);
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);
110 void GraphicsWebView::mousePressEvent(QGraphicsSceneMouseEvent* event)
112 setProperty("mouseButtons", QVariant::fromValue(int(event->buttons())));
113 setProperty("keyboardModifiers", QVariant::fromValue(int(event->modifiers())));
115 QGraphicsWebView::mousePressEvent(event);
118 void WebViewTraditional::mousePressEvent(QMouseEvent* event)
120 setProperty("mouseButtons", QVariant::fromValue(int(event->buttons())));
121 setProperty("keyboardModifiers", QVariant::fromValue(int(event->modifiers())));
123 QWebView::mousePressEvent(event);
126 void GraphicsWebView::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
128 QMenu* menu = createContextMenu(page(), event->pos().toPoint());
129 menu->exec(mapToScene(event->pos()).toPoint());
133 void WebViewTraditional::contextMenuEvent(QContextMenuEvent* event)
135 QMenu* menu = createContextMenu(page(), event->pos());
136 menu->exec(mapToGlobal(event->pos()));