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>
37 #include <QGraphicsSceneContextMenuEvent>
38 #include <QGraphicsSceneMouseEvent>
43 #ifndef QT_NO_ANIMATION
44 #include <QAbstractAnimation>
45 #include <QAbstractTransition>
46 #include <QFinalState>
47 #include <QPropertyAnimation>
49 #include <QStateMachine>
52 WebViewGraphicsBased::WebViewGraphicsBased(QWidget* parent)
53 : QGraphicsView(parent)
54 , m_item(new GraphicsWebView)
56 , m_numPaintsSinceLastMeasure(0)
58 , m_resizesToContents(false)
61 setScene(new QGraphicsScene(this));
62 scene()->addItem(m_item);
63 scene()->setFocusItem(m_item);
65 setFrameShape(QFrame::NoFrame);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
69 m_updateTimer = new QTimer(this);
70 m_updateTimer->setInterval(1000);
71 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateFrameRate()));
74 void WebViewGraphicsBased::setPage(QWebPage* page)
76 connect(page->mainFrame(), SIGNAL(contentsSizeChanged(const QSize&)), SLOT(contentsSizeChanged(const QSize&)));
77 connect(page, SIGNAL(scrollRequested(int, int, const QRect&)), SLOT(scrollRequested(int, int)));
78 graphicsWebView()->setPage(page);
81 void WebViewGraphicsBased::scrollRequested(int x, int y)
83 if (!m_resizesToContents)
86 // Turn off interactive mode while scrolling, or QGraphicsView will replay the
87 // last mouse event which may cause WebKit to initiate a drag operation.
88 bool interactive = isInteractive();
89 setInteractive(false);
91 verticalScrollBar()->setValue(-y);
92 horizontalScrollBar()->setValue(-x);
94 setInteractive(interactive);
97 void WebViewGraphicsBased::contentsSizeChanged(const QSize& size)
99 if (m_resizesToContents)
100 scene()->setSceneRect(0, 0, size.width(), size.height());
103 void WebViewGraphicsBased::setResizesToContents(bool b)
105 if (b == m_resizesToContents)
108 m_resizesToContents = b;
109 graphicsWebView()->setResizesToContents(m_resizesToContents);
111 // When setting resizesToContents ON, our web view widget will always size as big as the
112 // web content being displayed, and so will the QWebPage's viewport. It implies that internally
113 // WebCore will work as if there was no content rendered offscreen, and then no scrollbars need
114 // drawing. In order to keep scrolling working, we:
116 // 1) Set QGraphicsView's scrollbars policy back to 'auto'.
117 // 2) Set scene's boundaries rect to an invalid size, which automatically makes it to be as big
118 // as it needs to enclose all items onto it. We do that because QGraphicsView also calculates
119 // the size of its scrollable area according to the amount of content in scene that is rendered
121 // 3) Set QWebPage's preferredContentsSize according to the size of QGraphicsView's viewport,
122 // so WebCore properly lays pages out.
124 // On the other hand, when toggling resizesToContents OFF, we set back the default values, as
125 // opposite as described above.
126 if (m_resizesToContents) {
127 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
128 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
129 graphicsWebView()->page()->setPreferredContentsSize(size());
130 QRectF itemRect(graphicsWebView()->geometry().topLeft(), graphicsWebView()->page()->mainFrame()->contentsSize());
131 graphicsWebView()->setGeometry(itemRect);
132 scene()->setSceneRect(itemRect);
134 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
135 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
136 graphicsWebView()->page()->setPreferredContentsSize(QSize());
137 QRect viewportRect(QPoint(0, 0), size());
138 graphicsWebView()->setGeometry(viewportRect);
139 scene()->setSceneRect(viewportRect);
143 void WebViewGraphicsBased::resizeEvent(QResizeEvent* event)
145 QGraphicsView::resizeEvent(event);
147 QSize size(event->size());
149 if (m_resizesToContents) {
150 graphicsWebView()->page()->setPreferredContentsSize(size);
154 QRectF rect(QPoint(0, 0), size);
155 graphicsWebView()->setGeometry(rect);
156 scene()->setSceneRect(rect);
159 void WebViewGraphicsBased::setFrameRateMeasurementEnabled(bool enabled)
161 m_measureFps = enabled;
163 m_lastConsultTime = m_startTime = QTime::currentTime();
165 m_updateTimer->start();
168 m_updateTimer->stop();
172 void WebViewGraphicsBased::updateFrameRate()
174 const QTime now = QTime::currentTime();
175 int interval = m_lastConsultTime.msecsTo(now);
176 int frames = m_fpsTimer.numFrames(interval);
177 int current = interval ? frames * 1000 / interval : 0;
179 emit currentFPSUpdated(current);
181 m_lastConsultTime = now;
184 void WebViewGraphicsBased::animatedFlip()
186 #ifndef QT_NO_ANIMATION
187 QSizeF center = graphicsWebView()->boundingRect().size() / 2;
188 QPointF centerPoint = QPointF(center.width(), center.height());
189 graphicsWebView()->setTransformOriginPoint(centerPoint);
191 QPropertyAnimation* animation = new QPropertyAnimation(graphicsWebView(), "rotation", this);
192 animation->setDuration(1000);
194 int rotation = int(graphicsWebView()->rotation());
196 animation->setStartValue(rotation);
197 animation->setEndValue(rotation + 180 - (rotation % 180));
199 animation->start(QAbstractAnimation::DeleteWhenStopped);
203 void WebViewGraphicsBased::animatedYFlip()
205 #ifndef QT_NO_ANIMATION
207 m_machine = new QStateMachine(this);
209 QState* s0 = new QState(m_machine);
210 s0->assignProperty(this, "yRotation", 0);
212 QState* s1 = new QState(m_machine);
213 s1->assignProperty(this, "yRotation", 90);
215 QAbstractTransition* t1 = s0->addTransition(s1);
216 QPropertyAnimation* yRotationAnim = new QPropertyAnimation(this, "yRotation", this);
217 t1->addAnimation(yRotationAnim);
219 QState* s2 = new QState(m_machine);
220 s2->assignProperty(this, "yRotation", -90);
221 s1->addTransition(s1, SIGNAL(propertiesAssigned()), s2);
223 QState* s3 = new QState(m_machine);
224 s3->assignProperty(this, "yRotation", 0);
226 QAbstractTransition* t2 = s2->addTransition(s3);
227 t2->addAnimation(yRotationAnim);
229 QFinalState* final = new QFinalState(m_machine);
230 s3->addTransition(s3, SIGNAL(propertiesAssigned()), final);
232 m_machine->setInitialState(s0);
233 yRotationAnim->setDuration(1000);
240 void WebViewGraphicsBased::paintEvent(QPaintEvent* event)
242 QGraphicsView::paintEvent(event);
247 static QMenu* createContextMenu(QWebPage* page, QPoint position)
249 QMenu* menu = page->createStandardContextMenu();
251 QWebHitTestResult r = page->mainFrame()->hitTestContent(position);
253 if (!r.linkUrl().isEmpty()) {
254 WebPage* webPage = qobject_cast<WebPage*>(page);
255 QAction* newTabAction = menu->addAction("Open in Default &Browser", webPage, SLOT(openUrlInDefaultBrowser()));
256 newTabAction->setData(r.linkUrl());
257 menu->insertAction(menu->actions().at(2), newTabAction);
262 void GraphicsWebView::mousePressEvent(QGraphicsSceneMouseEvent* event)
264 setProperty("mouseButtons", QVariant::fromValue(int(event->buttons())));
265 setProperty("keyboardModifiers", QVariant::fromValue(int(event->modifiers())));
267 QGraphicsWebView::mousePressEvent(event);
270 void WebViewTraditional::mousePressEvent(QMouseEvent* event)
272 setProperty("mouseButtons", QVariant::fromValue(int(event->buttons())));
273 setProperty("keyboardModifiers", QVariant::fromValue(int(event->modifiers())));
275 QWebView::mousePressEvent(event);
278 void GraphicsWebView::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
280 QMenu* menu = createContextMenu(page(), event->pos().toPoint());
281 menu->exec(event->screenPos());
285 void WebViewTraditional::contextMenuEvent(QContextMenuEvent* event)
287 QMenu* menu = createContextMenu(page(), event->pos());
288 menu->exec(event->globalPos());