From add58edcdbd986e68cdd65a2a935b76d85d5b639 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?=
 <morten.sorvig@theqtcompany.com>
Date: Fri, 16 Oct 2015 12:24:38 +0200
Subject: [PATCH] Update high-DPI scaling enablers.

Make it possible to enable and disable both at the
environment variable level and at the source code
level. This applies to scaling done by Qt using display
density information provided by the operating system.

Disabling is done with a 'veto' system: both the environment
and source code my prevent the other for enabling
scaling. This covers use cases of 'my system does not
provide correct display metrics' and 'my application
needs access to display pixels', respectively.

On the environment, scaling is now enabled with
QT_AUTO_SCREEN_SCALE_FACTOR=1 and disabled with
QT_AUTO_SCREEN_SCALE_FACTOR=0. In source code the
corresponding application attributes are AA_EnableHighDpiScaling
and AA_DisapleHighDpiScaling. Not setting any of these
indicates 'no preference'.

The global scale factor set by QT_SCALE_FACTOR is
not affected by any if the disablers.

Task-number: QTBUG-46615
Change-Id: If18607d4b56ace1914a710e5aa60b2f0968e0010
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Reviewed-by: Paul Olav Tvete <paul.tvete@theqtcompany.com>
---
 src/corelib/global/qnamespace.h    |  3 ++-
 src/corelib/global/qnamespace.qdoc | 16 ++++++++++++++--
 src/gui/kernel/qhighdpiscaling.cpp | 36 ++++++++++++++++++++++++------------
 3 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/src/corelib/global/qnamespace.h b/src/corelib/global/qnamespace.h
index de8a17f..c4f5415 100644
--- a/src/corelib/global/qnamespace.h
+++ b/src/corelib/global/qnamespace.h
@@ -496,7 +496,8 @@ public:
         AA_UseSoftwareOpenGL = 17,
         AA_ShareOpenGLContexts = 18,
         AA_SetPalette = 19,
-        AA_NoHighDpiScaling = 20,
+        AA_EnableHighDpiScaling = 20,
+        AA_DisableHighDpiScaling = 21,
 
         // Add new attributes before this line
         AA_AttributeCount
diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc
index 443eae5..e789daa 100644
--- a/src/corelib/global/qnamespace.qdoc
+++ b/src/corelib/global/qnamespace.qdoc
@@ -200,10 +200,22 @@
     \value AA_SetPalette Indicates whether a palette was explicitly set on the
            QApplication/QGuiApplication. This value has been added in Qt 5.5.
 
-    \value AA_NoHighDpiScaling Disables all high-DPI scaling in Qt, exposing window
+    \value AA_EnableHighDpiScaling. Enables high-DPI scaling in Qt on supported
+           platforms (see also \l{High DPI Displays}). Supported platforms are
+           X11, Windows and Android. Enabling makes Qt scale the main (device
+           independent) coordinate system according to display scale factors
+           provided by the operating system. This corresponds to setting the
+           QT_AUTO_SCREEN_SCALE_FACTOR environment variable to 1. This value
+           has been added in Qt 5.6. This attribute must be set before
+           Q(Gui)Application is constructed.
+
+    \value AA_DisableHighDpiScaling Disables high-DPI scaling in Qt, exposing window
            system coordinates. Note that the window system may do its own scaling,
            so this does not guarantee that QPaintDevice::devicePixelRatio() will
-           be equal to 1. This value has been added in Qt 5.6.
+           be equal to 1. In addition, scale factors set by QT_SCALE_FACTOR will not
+           be affected. This corresponds to setting the QT_AUTO_SCREEN_SCALE_FACTOR
+           environment variable to 0. This value has been added in Qt 5.6. This
+           attribute must be set before Q(Gui)Application is constructed.
 
     The following values are obsolete:
 
diff --git a/src/gui/kernel/qhighdpiscaling.cpp b/src/gui/kernel/qhighdpiscaling.cpp
index daba9f9..a002b8c 100644
--- a/src/gui/kernel/qhighdpiscaling.cpp
+++ b/src/gui/kernel/qhighdpiscaling.cpp
@@ -49,7 +49,7 @@ static const char scaleFactorEnvVar[] = "QT_SCALE_FACTOR";
 static const char autoScreenEnvVar[] = "QT_AUTO_SCREEN_SCALE_FACTOR";
 static const char screenFactorsEnvVar[] = "QT_SCREEN_SCALE_FACTORS";
 
-static inline qreal initialScaleFactor()
+static inline qreal initialGlobalScaleFactor()
 {
 
     qreal result = 1;
@@ -134,19 +134,31 @@ QDpi QHighDpiScaling::m_logicalDpi = QDpi(-1,-1); // The scaled logical DPI of t
     Initializes the QHighDpiScaling global variables. Called before the
     platform plugin is created.
 */
-void QHighDpiScaling::initHighDpiScaling()
+
+static inline bool usePixelDensity()
 {
-    if (QCoreApplication::testAttribute(Qt::AA_NoHighDpiScaling)) {
-        m_factor = 1;
-        m_active = false;
-        return;
-    }
-    m_factor = initialScaleFactor();
-    bool usePlatformPluginPixelDensity = qEnvironmentVariableIsSet(autoScreenEnvVar)
-                                         || qgetenv(legacyDevicePixelEnvVar).toLower() == "auto";
+    // Determine if we should set a scale factor based on the pixel density
+    // reported by the platform plugin. There are several enablers and several
+    // disablers. A single disable may veto all other enablers.
+    if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling))
+        return false;
+    bool screenEnvValueOk;
+    const int screenEnvValue = qEnvironmentVariableIntValue(autoScreenEnvVar, &screenEnvValueOk);
+    if (screenEnvValueOk && screenEnvValue < 1)
+        return false;
+    return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling)
+        || (screenEnvValueOk && screenEnvValue > 0)
+        || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
+}
 
+void QHighDpiScaling::initHighDpiScaling()
+{
+    // Determine if there is a global scale factor set.
+    m_factor = initialGlobalScaleFactor();
     m_globalScalingActive = !qFuzzyCompare(m_factor, qreal(1));
-    m_usePixelDensity = usePlatformPluginPixelDensity;
+
+    m_usePixelDensity = usePixelDensity();
+
     m_pixelDensityScalingActive = false; //set in updateHighDpiScaling below
 
     // we update m_active in updateHighDpiScaling, but while we create the
@@ -156,7 +168,7 @@ void QHighDpiScaling::initHighDpiScaling()
 
 void QHighDpiScaling::updateHighDpiScaling()
 {
-    if (QCoreApplication::testAttribute(Qt::AA_NoHighDpiScaling))
+    if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling))
         return;
 
     if (m_usePixelDensity && !m_pixelDensityScalingActive) {
-- 
2.6.3

