initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / API / qt / qtouchwebview.cpp
1 /*
2  * Copyright (C) 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 "qtouchwebview.h"
23 #include "qtouchwebview_p.h"
24
25 #include "TouchViewInterface.h"
26 #include "qtouchwebpage_p.h"
27 #include "QtWebPageProxy.h"
28 #include "WebPageGroup.h"
29 #include "WebPreferences.h"
30 #include <qgraphicssceneevent.h>
31
32 QTouchWebViewPrivate::QTouchWebViewPrivate(QTouchWebView* q)
33     : q(q)
34     , pageView(new QTouchWebPage(q))
35     , viewInterface(q, pageView.data())
36     , interactionEngine(q, pageView.data())
37     , page(&viewInterface, &interactionEngine)
38 {
39     QTouchWebPagePrivate* const pageViewPrivate = pageView.data()->d;
40     pageViewPrivate->setPage(&page);
41
42     QObject::connect(&interactionEngine, SIGNAL(viewportUpdateRequested()), q, SLOT(_q_viewportUpdated()));
43     QObject::connect(&interactionEngine, SIGNAL(viewportTrajectoryVectorChanged(const QPointF&)), q, SLOT(_q_viewportTrajectoryVectorChanged(const QPointF&)));
44 }
45
46 void QTouchWebViewPrivate::loadDidCommit()
47 {
48     interactionEngine.reset();
49 }
50
51 void QTouchWebViewPrivate::_q_viewportUpdated()
52 {
53     const QRectF visibleRectInPageViewCoordinate = q->mapRectToItem(pageView.data(), q->boundingRect()).intersected(pageView->boundingRect());
54     float scale = pageView->scale();
55     page.setVisibleContentRectAndScale(visibleRectInPageViewCoordinate, scale);
56 }
57
58 void QTouchWebViewPrivate::_q_viewportTrajectoryVectorChanged(const QPointF& trajectoryVector)
59 {
60     page.setVisibleContentRectTrajectoryVector(trajectoryVector);
61 }
62
63 void QTouchWebViewPrivate::updateViewportConstraints()
64 {
65     QSize availableSize = q->boundingRect().size().toSize();
66
67     if (availableSize.isEmpty())
68         return;
69
70     WebPageProxy* wkPage = toImpl(page.pageRef());
71     WebPreferences* wkPrefs = wkPage->pageGroup()->preferences();
72
73     WebCore::ViewportAttributes attr = WebCore::computeViewportAttributes(viewportArguments, wkPrefs->layoutFallbackWidth(), wkPrefs->deviceWidth(), wkPrefs->deviceHeight(), wkPrefs->deviceDPI(), availableSize);
74
75     ViewportInteractionEngine::Constraints newConstraints;
76     newConstraints.initialScale = attr.initialScale;
77     newConstraints.minimumScale = attr.minimumScale;
78     newConstraints.maximumScale = attr.maximumScale;
79     newConstraints.isUserScalable = !!attr.userScalable;
80     interactionEngine.setConstraints(newConstraints);
81
82     // Overwrite minimum scale value with fit-to-view value, unless the the content author
83     // explicitly says no. NB: We can only do this when we know we have a valid size, ie.
84     // after initial layout has completed.
85
86     // FIXME: Do this on the web process side.
87     wkPage->setResizesToContentsUsingLayoutSize(attr.layoutSize);
88 }
89
90 void QTouchWebViewPrivate::setViewportArguments(const WebCore::ViewportArguments& args)
91 {
92     viewportArguments = args;
93     updateViewportConstraints();
94 }
95
96 QTouchWebView::QTouchWebView(QSGItem* parent)
97     : QSGItem(parent)
98     , d(new QTouchWebViewPrivate(this))
99 {
100     setFlags(QSGItem::ItemClipsChildrenToShape);
101 }
102
103 QTouchWebView::~QTouchWebView()
104 {
105     delete d;
106 }
107
108 QTouchWebPage* QTouchWebView::page()
109 {
110     return d->pageView.data();
111 }
112
113 void QTouchWebView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
114 {
115     QSGItem::geometryChanged(newGeometry, oldGeometry);
116     if (newGeometry.size() != oldGeometry.size()) {
117         d->updateViewportConstraints();
118         d->_q_viewportUpdated();
119     }
120 }
121
122 #include "moc_qtouchwebview.cpp"