summaryrefslogtreecommitdiffstats
path: root/src/webengine
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
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')
-rw-r--r--src/webengine/api/qquickwebengineview.cpp69
-rw-r--r--src/webengine/api/qquickwebengineview_p_p.h34
-rw-r--r--src/webengine/plugin/experimental/plugin.cpp2
3 files changed, 102 insertions, 3 deletions
diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp
index 4cfff010..b7d9a2d7 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
diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h
index e7c87ce1..b356b888 100644
--- a/src/webengine/api/qquickwebengineview_p_p.h
+++ b/src/webengine/api/qquickwebengineview_p_p.h
@@ -45,7 +45,7 @@
#include "qquickwebengineview_p.h"
#include "web_contents_adapter_client.h"
-#include <QSharedData>
+#include <QScopedPointer>
#include <QtQuick/private/qquickitem_p.h>
class WebContentsAdapter;
@@ -53,9 +53,29 @@ class WebContentsAdapter;
QT_BEGIN_NAMESPACE
class QQuickWebEngineView;
+class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineViewport : public QObject {
+ Q_OBJECT
+ Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio WRITE setDevicePixelRatio NOTIFY devicePixelRatioChanged)
+public:
+ QQuickWebEngineViewport(QQuickWebEngineViewPrivate *viewPrivate);
+
+ qreal devicePixelRatio() const;
+ void setDevicePixelRatio(qreal);
+
+Q_SIGNALS:
+ void devicePixelRatioChanged();
+
+private:
+ QQuickWebEngineViewPrivate *d_ptr;
+
+ Q_DECLARE_PRIVATE(QQuickWebEngineView)
+};
+
class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineViewExperimental : public QObject {
Q_OBJECT
+ Q_PROPERTY(QQuickWebEngineViewport *viewport READ viewport)
public:
+ QQuickWebEngineViewport *viewport() const;
private:
QQuickWebEngineViewExperimental(QQuickWebEngineViewPrivate* viewPrivate);
@@ -73,6 +93,7 @@ public:
QQuickWebEngineViewPrivate();
QQuickWebEngineViewExperimental *experimental() const;
+ QQuickWebEngineViewport *viewport() const;
virtual RenderWidgetHostViewQtDelegate* CreateRenderWidgetHostViewQtDelegate(RenderWidgetHostViewQtDelegateClient *client, RenderingMode) Q_DECL_OVERRIDE;
virtual void titleChanged(const QString&) Q_DECL_OVERRIDE;
@@ -81,6 +102,7 @@ public:
virtual void loadingStateChanged() Q_DECL_OVERRIDE;
virtual void loadProgressChanged(int progress) Q_DECL_OVERRIDE;
virtual QRectF viewportRect() const Q_DECL_OVERRIDE;
+ virtual qreal dpiScale() const Q_DECL_OVERRIDE;
virtual void loadFinished(bool success) Q_DECL_OVERRIDE;
virtual void focusContainer() Q_DECL_OVERRIDE;
virtual void adoptNewWindow(WebContentsAdapter *newWebContents, WindowOpenDisposition disposition, const QRect &) Q_DECL_OVERRIDE;
@@ -88,15 +110,23 @@ public:
virtual bool contextMenuRequested(const WebEngineContextMenuData &) Q_DECL_OVERRIDE { return false;}
virtual bool javascriptDialog(JavascriptDialogType type, const QString &message, const QString &defaultValue = QString(), QString *result = 0) Q_DECL_OVERRIDE { return false; }
+ void setDevicePixelRatio(qreal);
+
QExplicitlySharedDataPointer<WebContentsAdapter> adapter;
- QQuickWebEngineViewExperimental *e;
+ QScopedPointer<QQuickWebEngineViewExperimental> e;
+ QScopedPointer<QQuickWebEngineViewport> v;
QUrl icon;
int loadProgress;
bool inspectable;
+ qreal devicePixelRatio;
+
+private:
+ qreal m_dpiScale;
};
QT_END_NAMESPACE
QML_DECLARE_TYPE(QQuickWebEngineViewExperimental)
+QML_DECLARE_TYPE(QQuickWebEngineViewport)
#endif // QQUICKWEBENGINEVIEW_P_P_H
diff --git a/src/webengine/plugin/experimental/plugin.cpp b/src/webengine/plugin/experimental/plugin.cpp
index 08d48250..6107147c 100644
--- a/src/webengine/plugin/experimental/plugin.cpp
+++ b/src/webengine/plugin/experimental/plugin.cpp
@@ -69,6 +69,8 @@ public:
qmlRegisterExtendedType<QQuickWebEngineView, QQuickWebEngineViewExperimentalExtension>(uri, 1, 0, "WebEngineView");
qmlRegisterUncreatableType<QQuickWebEngineViewExperimental>(uri, 1, 0, "WebEngineViewExperimental",
QObject::tr("Cannot create a separate instance of WebEngineViewExperimental"));
+ qmlRegisterUncreatableType<QQuickWebEngineViewport>(uri, 1, 0, "WebEngineViewport",
+ QObject::tr("Cannot create a separate instance of WebEngineViewport"));
}
};