2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "PageClientQt.h"
24 #include <QGraphicsScene>
25 #include <QGraphicsView>
30 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
31 #include "TextureMapperQt.h"
32 #include "texmap/TextureMapperNode.h"
34 #if USE(TEXTURE_MAPPER_GL)
35 #include "opengl/TextureMapperGL.h"
41 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
42 TextureMapperNodeClientQt::TextureMapperNodeClientQt(QWebFrame* frame, GraphicsLayer* layer)
44 , m_rootGraphicsLayer(GraphicsLayer::create(0))
46 m_frame->d->rootTextureMapperNode = rootNode();
47 m_rootGraphicsLayer->addChild(layer);
48 m_rootGraphicsLayer->setDrawsContent(false);
49 m_rootGraphicsLayer->setMasksToBounds(false);
50 m_rootGraphicsLayer->setSize(IntSize(1, 1));
53 void TextureMapperNodeClientQt::setTextureMapper(const PassOwnPtr<TextureMapper>& textureMapper)
55 m_frame->d->textureMapper = textureMapper;
56 m_frame->d->rootTextureMapperNode->setTextureMapper(m_frame->d->textureMapper.get());
59 TextureMapperNodeClientQt::~TextureMapperNodeClientQt()
61 m_frame->d->rootTextureMapperNode = 0;
64 void TextureMapperNodeClientQt::syncRootLayer()
66 m_rootGraphicsLayer->syncCompositingStateForThisLayerOnly();
69 TextureMapperNode* TextureMapperNodeClientQt::rootNode()
71 return toTextureMapperNode(m_rootGraphicsLayer.get());
75 void PageClientQWidget::setRootGraphicsLayer(GraphicsLayer* layer)
78 textureMapperNodeClient = adoptPtr(new TextureMapperNodeClientQt(page->mainFrame(), layer));
79 textureMapperNodeClient->setTextureMapper(adoptPtr(new TextureMapperQt));
80 textureMapperNodeClient->syncRootLayer();
83 textureMapperNodeClient.clear();
86 void PageClientQWidget::markForSync(bool scheduleSync)
88 syncTimer.startOneShot(0);
91 void PageClientQWidget::syncLayers(Timer<PageClientQWidget>*)
93 if (textureMapperNodeClient)
94 textureMapperNodeClient->syncRootLayer();
95 QWebFramePrivate::core(page->mainFrame())->view()->syncCompositingStateIncludingSubframes();
96 if (!textureMapperNodeClient)
98 if (textureMapperNodeClient->rootNode()->descendantsOrSelfHaveRunningAnimations())
99 syncTimer.startOneShot(1.0 / 60.0);
100 update(view->rect());
104 void PageClientQWidget::scroll(int dx, int dy, const QRect& rectToScroll)
106 view->scroll(qreal(dx), qreal(dy), rectToScroll);
109 void PageClientQWidget::update(const QRect & dirtyRect)
111 view->update(dirtyRect);
114 void PageClientQWidget::setInputMethodEnabled(bool enable)
116 view->setAttribute(Qt::WA_InputMethodEnabled, enable);
119 bool PageClientQWidget::inputMethodEnabled() const
121 return view->testAttribute(Qt::WA_InputMethodEnabled);
124 void PageClientQWidget::setInputMethodHints(Qt::InputMethodHints hints)
126 view->setInputMethodHints(hints);
129 PageClientQWidget::~PageClientQWidget()
134 QCursor PageClientQWidget::cursor() const
136 return view->cursor();
139 void PageClientQWidget::updateCursor(const QCursor& cursor)
141 view->setCursor(cursor);
145 QPalette PageClientQWidget::palette() const
147 return view->palette();
150 int PageClientQWidget::screenNumber() const
152 #if defined(Q_WS_X11)
153 return view->x11Info().screen();
158 QWidget* PageClientQWidget::ownerWidget() const
163 QRect PageClientQWidget::geometryRelativeToOwnerWidget() const
165 return view->geometry();
168 QObject* PageClientQWidget::pluginParent() const
173 QStyle* PageClientQWidget::style() const
175 return view->style();
178 QRectF PageClientQWidget::windowRect() const
180 return QRectF(view->window()->geometry());
183 #if !defined(QT_NO_GRAPHICSVIEW)
184 PageClientQGraphicsWidget::~PageClientQGraphicsWidget()
187 #if USE(ACCELERATED_COMPOSITING) && !USE(TEXTURE_MAPPER)
188 if (!rootGraphicsLayer)
190 // we don't need to delete the root graphics layer. The lifecycle is managed in GraphicsLayerQt.cpp.
191 rootGraphicsLayer.data()->setParentItem(0);
192 view->scene()->removeItem(rootGraphicsLayer.data());
196 void PageClientQGraphicsWidget::scroll(int dx, int dy, const QRect& rectToScroll)
198 view->scroll(qreal(dx), qreal(dy), rectToScroll);
201 void PageClientQGraphicsWidget::update(const QRect& dirtyRect)
203 view->update(dirtyRect);
205 createOrDeleteOverlay();
207 overlay->update(QRectF(dirtyRect));
208 #if USE(ACCELERATED_COMPOSITING) && !USE(TEXTURE_MAPPER)
213 void PageClientQGraphicsWidget::createOrDeleteOverlay()
215 // We don't use an overlay with TextureMapper. Instead, the overlay is drawn inside QWebFrame.
216 #if !USE(TEXTURE_MAPPER)
217 bool useOverlay = false;
218 if (!viewResizesToContents) {
219 #if USE(ACCELERATED_COMPOSITING)
220 useOverlay = useOverlay || rootGraphicsLayer;
222 #if ENABLE(TILED_BACKING_STORE)
223 useOverlay = useOverlay || QWebFramePrivate::core(page->mainFrame())->tiledBackingStore();
226 if (useOverlay == !!overlay)
230 overlay = new QGraphicsItemOverlay(view, page);
231 overlay->setZValue(OverlayZValue);
233 // Changing the overlay might be done inside paint events.
234 overlay->deleteLater();
237 #endif // !USE(TEXTURE_MAPPER)
240 #if USE(ACCELERATED_COMPOSITING)
241 void PageClientQGraphicsWidget::syncLayers()
243 #if USE(TEXTURE_MAPPER)
244 if (textureMapperNodeClient)
245 textureMapperNodeClient->syncRootLayer();
248 QWebFramePrivate::core(page->mainFrame())->view()->syncCompositingStateIncludingSubframes();
250 #if USE(TEXTURE_MAPPER)
251 if (!textureMapperNodeClient)
254 if (textureMapperNodeClient->rootNode()->descendantsOrSelfHaveRunningAnimations())
255 syncTimer.startOneShot(1.0 / 60.0);
256 update(view->boundingRect().toAlignedRect());
263 #if USE(TEXTURE_MAPPER)
264 void PageClientQGraphicsWidget::setRootGraphicsLayer(GraphicsLayer* layer)
267 textureMapperNodeClient = adoptPtr(new TextureMapperNodeClientQt(page->mainFrame(), layer));
268 #if USE(TEXTURE_MAPPER_GL)
269 QGraphicsView* graphicsView = view->scene()->views()[0];
270 if (graphicsView && graphicsView->viewport() && graphicsView->viewport()->inherits("QGLWidget")) {
271 textureMapperNodeClient->setTextureMapper(TextureMapperGL::create());
275 textureMapperNodeClient->setTextureMapper(TextureMapperQt::create());
278 textureMapperNodeClient.clear();
281 void PageClientQGraphicsWidget::setRootGraphicsLayer(GraphicsLayer* layer)
283 if (rootGraphicsLayer) {
284 rootGraphicsLayer.data()->setParentItem(0);
285 view->scene()->removeItem(rootGraphicsLayer.data());
286 QWebFramePrivate::core(page->mainFrame())->view()->syncCompositingStateIncludingSubframes();
289 rootGraphicsLayer = layer ? layer->platformLayer() : 0;
291 if (rootGraphicsLayer) {
292 rootGraphicsLayer.data()->setParentItem(view);
293 rootGraphicsLayer.data()->setZValue(RootGraphicsLayerZValue);
295 createOrDeleteOverlay();
299 void PageClientQGraphicsWidget::markForSync(bool scheduleSync)
302 syncTimer.startOneShot(0);
307 #if ENABLE(TILED_BACKING_STORE)
308 void PageClientQGraphicsWidget::updateTiledBackingStoreScale()
310 WebCore::TiledBackingStore* backingStore = QWebFramePrivate::core(page->mainFrame())->tiledBackingStore();
313 backingStore->setContentsScale(view->scale());
317 void PageClientQGraphicsWidget::setInputMethodEnabled(bool enable)
319 view->setFlag(QGraphicsItem::ItemAcceptsInputMethod, enable);
322 bool PageClientQGraphicsWidget::inputMethodEnabled() const
324 return view->flags() & QGraphicsItem::ItemAcceptsInputMethod;
327 void PageClientQGraphicsWidget::setInputMethodHints(Qt::InputMethodHints hints)
329 view->setInputMethodHints(hints);
333 QCursor PageClientQGraphicsWidget::cursor() const
335 return view->cursor();
338 void PageClientQGraphicsWidget::updateCursor(const QCursor& cursor)
340 view->setCursor(cursor);
344 QPalette PageClientQGraphicsWidget::palette() const
346 return view->palette();
349 int PageClientQGraphicsWidget::screenNumber() const
351 #if defined(Q_WS_X11)
352 if (QGraphicsScene* scene = view->scene()) {
353 const QList<QGraphicsView*> views = scene->views();
355 if (!views.isEmpty())
356 return views.at(0)->x11Info().screen();
363 QWidget* PageClientQGraphicsWidget::ownerWidget() const
365 if (QGraphicsScene* scene = view->scene()) {
366 const QList<QGraphicsView*> views = scene->views();
367 return views.value(0);
372 QRect PageClientQGraphicsWidget::geometryRelativeToOwnerWidget() const
377 QList<QGraphicsView*> views = view->scene()->views();
381 QGraphicsView* graphicsView = views.at(0);
382 return graphicsView->mapFromScene(view->boundingRect()).boundingRect();
385 #if ENABLE(TILED_BACKING_STORE)
386 QRectF PageClientQGraphicsWidget::graphicsItemVisibleRect() const
391 QList<QGraphicsView*> views = view->scene()->views();
395 QGraphicsView* graphicsView = views.at(0);
396 int xOffset = graphicsView->horizontalScrollBar()->value();
397 int yOffset = graphicsView->verticalScrollBar()->value();
398 return view->mapRectFromScene(QRectF(QPointF(xOffset, yOffset), graphicsView->viewport()->size()));
402 QObject* PageClientQGraphicsWidget::pluginParent() const
407 QStyle* PageClientQGraphicsWidget::style() const
409 return view->style();
412 QRectF PageClientQGraphicsWidget::windowRect() const
417 // The sceneRect is a good approximation of the size of the application, independent of the view.
418 return view->scene()->sceneRect();
420 #endif // QT_NO_GRAPHICSVIEW
422 } // namespace WebCore