2 * Copyright (C) 2011 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 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.
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.
22 #include "qtouchwebview.h"
23 #include "qtouchwebview_p.h"
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>
32 QTouchWebViewPrivate::QTouchWebViewPrivate(QTouchWebView* q)
34 , pageView(new QTouchWebPage(q))
35 , viewInterface(q, pageView.data())
36 , interactionEngine(q, pageView.data())
37 , page(&viewInterface, &interactionEngine)
39 QTouchWebPagePrivate* const pageViewPrivate = pageView.data()->d;
40 pageViewPrivate->setPage(&page);
42 QObject::connect(&interactionEngine, SIGNAL(viewportUpdateRequested()), q, SLOT(_q_viewportUpdated()));
43 QObject::connect(&interactionEngine, SIGNAL(viewportTrajectoryVectorChanged(const QPointF&)), q, SLOT(_q_viewportTrajectoryVectorChanged(const QPointF&)));
46 void QTouchWebViewPrivate::loadDidCommit()
48 interactionEngine.reset();
51 void QTouchWebViewPrivate::_q_viewportUpdated()
53 const QRectF visibleRectInPageViewCoordinate = q->mapRectToItem(pageView.data(), q->boundingRect()).intersected(pageView->boundingRect());
54 float scale = pageView->scale();
55 page.setVisibleContentRectAndScale(visibleRectInPageViewCoordinate, scale);
58 void QTouchWebViewPrivate::_q_viewportTrajectoryVectorChanged(const QPointF& trajectoryVector)
60 page.setVisibleContentRectTrajectoryVector(trajectoryVector);
63 void QTouchWebViewPrivate::updateViewportConstraints()
65 QSize availableSize = q->boundingRect().size().toSize();
67 if (availableSize.isEmpty())
70 WebPageProxy* wkPage = toImpl(page.pageRef());
71 WebPreferences* wkPrefs = wkPage->pageGroup()->preferences();
73 WebCore::ViewportAttributes attr = WebCore::computeViewportAttributes(viewportArguments, wkPrefs->layoutFallbackWidth(), wkPrefs->deviceWidth(), wkPrefs->deviceHeight(), wkPrefs->deviceDPI(), availableSize);
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);
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.
86 // FIXME: Do this on the web process side.
87 wkPage->setResizesToContentsUsingLayoutSize(attr.layoutSize);
90 void QTouchWebViewPrivate::setViewportArguments(const WebCore::ViewportArguments& args)
92 viewportArguments = args;
93 updateViewportConstraints();
96 QTouchWebView::QTouchWebView(QSGItem* parent)
98 , d(new QTouchWebViewPrivate(this))
100 setFlags(QSGItem::ItemClipsChildrenToShape);
103 QTouchWebView::~QTouchWebView()
108 QTouchWebPage* QTouchWebView::page()
110 return d->pageView.data();
113 void QTouchWebView::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
115 QSGItem::geometryChanged(newGeometry, oldGeometry);
116 if (newGeometry.size() != oldGeometry.size()) {
117 d->updateViewportConstraints();
118 d->_q_viewportUpdated();
122 #include "moc_qtouchwebview.cpp"