summaryrefslogtreecommitdiffstats
path: root/src/webengine/api/qquickwebengineview.cpp
diff options
context:
space:
mode:
authorArvid Nilsson <anilsson@blackberry.com>2013-11-28 15:29:40 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-12-04 14:23:33 +0100
commit5d66b66df1bc205a9ce3d25cfaaa84c961fa7a29 (patch)
treeb99b54d09ba93dbe66a65313c50ac6d831e5a8d0 /src/webengine/api/qquickwebengineview.cpp
parent5d92938e5bb830b8ca5a711fd4c1999dd09fd0e3 (diff)
Quick: Add experimental.viewport.devicePixelRatio
This specifies a devicePixelRatio to be used by web content instead of the QScreen::devicePixelRatio(). This is necessary on non-iOS mobile devices to remain compatible with the mobile web which assumes devicePixelRatio is computed as the ratio of actual dpi to 160 dpi. Non-iOS mobile platforms may use different criteria to determine the QScreen::devicePixelRatio(), depending on the history of the platform, or simply leave it at 1.0. For QNX, this setting gets a reasonable default value so developers don't have to regularly use this experimental API. These changes were inspired by the Android Chromium port which uses a GetDpiScale() to accomplish the same in content/browser/android/content_view_core_impl.cc. Change-Id: I1bc8878a47dabcdb6986c4fe5c8c4ac230ae2514 Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com>
Diffstat (limited to 'src/webengine/api/qquickwebengineview.cpp')
-rw-r--r--src/webengine/api/qquickwebengineview.cpp69
1 files changed, 68 insertions, 1 deletions
diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp
index 4cfff0102..b7d9a2d7c 100644
--- a/src/webengine/api/qquickwebengineview.cpp
+++ b/src/webengine/api/qquickwebengineview.cpp
@@ -45,6 +45,7 @@
#include "web_contents_adapter.h"
#include "render_widget_host_view_qt_delegate_quick.h"
+#include <QScreen>
#include <QUrl>
QT_BEGIN_NAMESPACE
@@ -52,15 +53,40 @@ QT_BEGIN_NAMESPACE
QQuickWebEngineViewPrivate::QQuickWebEngineViewPrivate()
: adapter(new WebContentsAdapter(qApp->property("QQuickWebEngineView_DisableHardwareAcceleration").toBool() ? SoftwareRenderingMode : HardwareAccelerationMode))
, e(new QQuickWebEngineViewExperimental(this))
+ , v(new QQuickWebEngineViewport(this))
, loadProgress(0)
, inspectable(false)
+ , devicePixelRatio(QGuiApplication::primaryScreen()->devicePixelRatio())
+ , m_dpiScale(1.0)
{
+ // The gold standard for mobile web content is 160 dpi, and the devicePixelRatio expected
+ // is the (possibly quantized) ratio of device dpi to 160 dpi.
+ // However GUI toolkits on non-iOS platforms may be using different criteria than relative
+ // DPI (depending on the history of that platform), dictating the choice of
+ // QScreen::devicePixelRatio().
+ // Where applicable (i.e. non-iOS mobile platforms), override QScreen::devicePixelRatio
+ // and instead use a reasonable default value for viewport.devicePixelRatio to avoid every
+ // app having to use this experimental API.
+ QString platform = qApp->platformName().toLower();
+ if (platform == QStringLiteral("qnx")) {
+ qreal webPixelRatio = QGuiApplication::primaryScreen()->physicalDotsPerInch() / 160;
+
+ // Quantize devicePixelRatio to increments of 1 to allow JS and media queries to select
+ // 1x, 2x, 3x etc assets that fit an integral number of pixels.
+ setDevicePixelRatio(qMax(1, qRound(webPixelRatio)));
+ }
+
adapter->initialize(this);
}
QQuickWebEngineViewExperimental *QQuickWebEngineViewPrivate::experimental() const
{
- return e;
+ return e.data();
+}
+
+QQuickWebEngineViewport *QQuickWebEngineViewPrivate::viewport() const
+{
+ return v.data();
}
RenderWidgetHostViewQtDelegate *QQuickWebEngineViewPrivate::CreateRenderWidgetHostViewQtDelegate(RenderWidgetHostViewQtDelegateClient *client, RenderingMode mode)
@@ -112,6 +138,11 @@ QRectF QQuickWebEngineViewPrivate::viewportRect() const
return QRectF(q->x(), q->y(), q->width(), q->height());
}
+qreal QQuickWebEngineViewPrivate::dpiScale() const
+{
+ return m_dpiScale;
+}
+
void QQuickWebEngineViewPrivate::loadFinished(bool success)
{
Q_Q(QQuickWebEngineView);
@@ -137,6 +168,13 @@ void QQuickWebEngineViewPrivate::close()
Q_UNREACHABLE();
}
+void QQuickWebEngineViewPrivate::setDevicePixelRatio(qreal devicePixelRatio)
+{
+ this->devicePixelRatio = devicePixelRatio;
+ QScreen *screen = window ? window->screen() : QGuiApplication::primaryScreen();
+ m_dpiScale = devicePixelRatio / screen->devicePixelRatio();
+}
+
QQuickWebEngineView::QQuickWebEngineView(QQuickItem *parent)
: QQuickItem(*(new QQuickWebEngineViewPrivate), parent)
{
@@ -253,4 +291,33 @@ QQuickWebEngineViewExperimental::QQuickWebEngineViewExperimental(QQuickWebEngine
{
}
+QQuickWebEngineViewport *QQuickWebEngineViewExperimental::viewport() const
+{
+ Q_D(const QQuickWebEngineView);
+ return d->viewport();
+}
+
+QQuickWebEngineViewport::QQuickWebEngineViewport(QQuickWebEngineViewPrivate *viewPrivate)
+ : d_ptr(viewPrivate)
+{
+}
+
+qreal QQuickWebEngineViewport::devicePixelRatio() const
+{
+ Q_D(const QQuickWebEngineView);
+ return d->devicePixelRatio;
+}
+
+void QQuickWebEngineViewport::setDevicePixelRatio(qreal devicePixelRatio)
+{
+ Q_D(QQuickWebEngineView);
+ // Valid range is [1, inf)
+ devicePixelRatio = qMax(1.0, devicePixelRatio);
+ if (d->devicePixelRatio == devicePixelRatio)
+ return;
+ d->setDevicePixelRatio(devicePixelRatio);
+ d->adapter->dpiScaleChanged();
+ Q_EMIT devicePixelRatioChanged();
+}
+
QT_END_NAMESPACE