[Qt][WK2] Move non-api classes to WebKit namespace at WebKit2/UiProcess/qt
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / qt / qquickwebpage.cpp
1 /*
2  * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
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.
8  *
9  * This program 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.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; 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.
18  *
19  */
20
21 #include "config.h"
22 #include "qquickwebpage_p.h"
23
24 #include "LayerTreeHostProxy.h"
25 #include "QtWebPageEventHandler.h"
26 #include "QtWebPageSGNode.h"
27 #include "TransformationMatrix.h"
28 #include "WebLayerTreeRenderer.h"
29 #include "WebPageProxy.h"
30 #include "qquickwebpage_p_p.h"
31 #include "qquickwebview_p.h"
32 #include <QtQuick/QQuickCanvas>
33
34 using namespace WebKit;
35
36 QQuickWebPage::QQuickWebPage(QQuickWebView* viewportItem)
37     : QQuickItem(viewportItem->contentItem())
38     , d(new QQuickWebPagePrivate(this, viewportItem))
39 {
40     setFlag(ItemHasContents);
41     setClip(true);
42
43     // We do the transform from the top left so the viewport can assume the position 0, 0
44     // is always where rendering starts.
45     setTransformOrigin(TopLeft);
46 }
47
48 QQuickWebPage::~QQuickWebPage()
49 {
50     delete d;
51 }
52
53 QQuickWebPagePrivate::QQuickWebPagePrivate(QQuickWebPage* q, QQuickWebView* viewportItem)
54     : q(q)
55     , viewportItem(viewportItem)
56     , webPageProxy(0)
57     , paintingIsInitialized(false)
58     , m_paintNode(0)
59     , contentsScale(1)
60 {
61 }
62
63 void QQuickWebPagePrivate::initialize(WebKit::WebPageProxy* webPageProxy)
64 {
65     this->webPageProxy = webPageProxy;
66     eventHandler.reset(new QtWebPageEventHandler(toAPI(webPageProxy), q, viewportItem));
67 }
68
69 void QQuickWebPagePrivate::paint(QPainter* painter)
70 {
71     if (!webPageProxy->drawingArea())
72         return;
73
74     LayerTreeHostProxy* layerTreeHostProxy = webPageProxy->drawingArea()->layerTreeHostProxy();
75     if (layerTreeHostProxy->layerTreeRenderer())
76         layerTreeHostProxy->layerTreeRenderer()->paintToGraphicsContext(painter);
77 }
78
79 QSGNode* QQuickWebPage::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
80 {
81     if (!d->webPageProxy->drawingArea())
82         return oldNode;
83
84     LayerTreeHostProxy* layerTreeHostProxy = d->webPageProxy->drawingArea()->layerTreeHostProxy();
85     WebLayerTreeRenderer* renderer = layerTreeHostProxy->layerTreeRenderer();
86
87     QtWebPageSGNode* node = static_cast<QtWebPageSGNode*>(oldNode);
88     if (!node)
89         node = new QtWebPageSGNode();
90     node->setRenderer(renderer);
91     renderer->syncRemoteContent();
92
93     node->setScale(d->contentsScale);
94     QColor backgroundColor = d->webPageProxy->drawsTransparentBackground() ? Qt::transparent : Qt::white;
95     QRectF backgroundRect(QPointF(0, 0), d->contentsSize);
96     node->setBackground(backgroundRect, backgroundColor);
97
98     return node;
99 }
100
101 QtWebPageEventHandler* QQuickWebPage::eventHandler() const
102 {
103     return d->eventHandler.data();
104 }
105
106 void QQuickWebPage::setContentsSize(const QSizeF& size)
107 {
108     if (size.isEmpty() || d->contentsSize == size)
109         return;
110
111     d->contentsSize = size;
112     d->updateSize();
113 }
114
115 const QSizeF& QQuickWebPage::contentsSize() const
116 {
117     return d->contentsSize;
118 }
119
120 void QQuickWebPage::setContentsScale(qreal scale)
121 {
122     ASSERT(scale > 0);
123     d->contentsScale = scale;
124     d->updateSize();
125 }
126
127 qreal QQuickWebPage::contentsScale() const
128 {
129     ASSERT(d->contentsScale > 0);
130     return d->contentsScale;
131 }
132
133 QTransform QQuickWebPage::transformFromItem() const
134 {
135     return transformToItem().inverted();
136 }
137
138 QTransform QQuickWebPage::transformToItem() const
139 {
140     QPointF pos = d->viewportItem->pageItemPos();
141     return QTransform(d->contentsScale, 0, 0, 0, d->contentsScale, 0, pos.x(), pos.y(), 1);
142 }
143
144 void QQuickWebPagePrivate::updateSize()
145 {
146     QSizeF scaledSize = contentsSize * contentsScale;
147     q->setSize(scaledSize);
148     viewportItem->updateContentsSize(scaledSize);
149     DrawingAreaProxy* drawingArea = webPageProxy->drawingArea();
150     if (drawingArea && drawingArea->layerTreeHostProxy())
151         drawingArea->layerTreeHostProxy()->setContentsSize(WebCore::FloatSize(contentsSize.width(), contentsSize.height()));
152 }
153
154 QQuickWebPagePrivate::~QQuickWebPagePrivate()
155 {
156 }
157
158 #include "moc_qquickwebpage_p.cpp"