| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qhighdpiscaling.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 29 | ** | - | ||||||||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 31 | ** | - | ||||||||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | #include "qhighdpiscaling_p.h" | - | ||||||||||||||||||
| 35 | #include "qguiapplication.h" | - | ||||||||||||||||||
| 36 | #include "qscreen.h" | - | ||||||||||||||||||
| 37 | #include "qplatformintegration.h" | - | ||||||||||||||||||
| 38 | #include "private/qscreen_p.h" | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | #include <QtCore/qdebug.h> | - | ||||||||||||||||||
| 41 | - | |||||||||||||||||||
| 42 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 43 | - | |||||||||||||||||||
| 44 | Q_LOGGING_CATEGORY(lcScaling, "qt.scaling"); never executed: return category; | 0 | ||||||||||||||||||
| 45 | - | |||||||||||||||||||
| 46 | #ifndef QT_NO_HIGHDPISCALING | - | ||||||||||||||||||
| 47 | static const char legacyDevicePixelEnvVar[] = "QT_DEVICE_PIXEL_RATIO"; | - | ||||||||||||||||||
| 48 | static const char scaleFactorEnvVar[] = "QT_SCALE_FACTOR"; | - | ||||||||||||||||||
| 49 | static const char autoScreenEnvVar[] = "QT_AUTO_SCREEN_SCALE_FACTOR"; | - | ||||||||||||||||||
| 50 | static const char screenFactorsEnvVar[] = "QT_SCREEN_SCALE_FACTORS"; | - | ||||||||||||||||||
| 51 | - | |||||||||||||||||||
| 52 | static inline qreal initialGlobalScaleFactor() | - | ||||||||||||||||||
| 53 | { | - | ||||||||||||||||||
| 54 | - | |||||||||||||||||||
| 55 | qreal result = 1; | - | ||||||||||||||||||
| 56 | if (qEnvironmentVariableIsSet(scaleFactorEnvVar)) {
| 0 | ||||||||||||||||||
| 57 | bool ok; | - | ||||||||||||||||||
| 58 | const qreal f = qgetenv(scaleFactorEnvVar).toDouble(&ok); | - | ||||||||||||||||||
| 59 | if (ok && f > 0) {
| 0 | ||||||||||||||||||
| 60 | qCDebug(lcScaling) << "Apply " << scaleFactorEnvVar << f; never executed: QMessageLogger(__FILE__, 60, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() << "Apply " << scaleFactorEnvVar << f;
| 0 | ||||||||||||||||||
| 61 | result = f; | - | ||||||||||||||||||
| 62 | } never executed: end of block | 0 | ||||||||||||||||||
| 63 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 64 | if (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar)) {
| 0 | ||||||||||||||||||
| 65 | qWarning() << "Warning:" << legacyDevicePixelEnvVar << "is deprecated. Instead use:" << endl | - | ||||||||||||||||||
| 66 | << " " << autoScreenEnvVar << "to enable platform plugin controlled per-screen factors." << endl | - | ||||||||||||||||||
| 67 | << " " << screenFactorsEnvVar << "to set per-screen factors." << endl | - | ||||||||||||||||||
| 68 | << " " << scaleFactorEnvVar << "to set the application global scale factor."; | - | ||||||||||||||||||
| 69 | - | |||||||||||||||||||
| 70 | int dpr = qEnvironmentVariableIntValue(legacyDevicePixelEnvVar); | - | ||||||||||||||||||
| 71 | if (dpr > 0)
| 0 | ||||||||||||||||||
| 72 | result = dpr; never executed: result = dpr; | 0 | ||||||||||||||||||
| 73 | } never executed: end of block | 0 | ||||||||||||||||||
| 74 | } never executed: end of block | 0 | ||||||||||||||||||
| 75 | return result; never executed: return result; | 0 | ||||||||||||||||||
| 76 | } | - | ||||||||||||||||||
| 77 | - | |||||||||||||||||||
| 78 | /*! | - | ||||||||||||||||||
| 79 | \class QHighDpiScaling | - | ||||||||||||||||||
| 80 | \since 5.6 | - | ||||||||||||||||||
| 81 | \internal | - | ||||||||||||||||||
| 82 | \preliminary | - | ||||||||||||||||||
| 83 | \ingroup qpa | - | ||||||||||||||||||
| 84 | - | |||||||||||||||||||
| 85 | \brief Collection of utility functions for UI scaling. | - | ||||||||||||||||||
| 86 | - | |||||||||||||||||||
| 87 | QHighDpiScaling implements utility functions for high-dpi scaling for use | - | ||||||||||||||||||
| 88 | on operating systems that provide limited support for native scaling. In | - | ||||||||||||||||||
| 89 | addition this functionality can be used for simulation and testing purposes. | - | ||||||||||||||||||
| 90 | - | |||||||||||||||||||
| 91 | The functions support scaling between the device independent coordinate | - | ||||||||||||||||||
| 92 | system used by Qt applications and the native coordinate system used by | - | ||||||||||||||||||
| 93 | the platform plugins. Intended usage locations are the low level / platform | - | ||||||||||||||||||
| 94 | plugin interfacing parts of QtGui, for example the QWindow, QScreen and | - | ||||||||||||||||||
| 95 | QWindowSystemInterface implementation. | - | ||||||||||||||||||
| 96 | - | |||||||||||||||||||
| 97 | There are now up to three active coordinate systems in Qt: | - | ||||||||||||||||||
| 98 | - | |||||||||||||||||||
| 99 | --------------------------------------------------- | - | ||||||||||||||||||
| 100 | | Application Device Independent Pixels | devicePixelRatio | - | ||||||||||||||||||
| 101 | | Qt Widgets | = | - | ||||||||||||||||||
| 102 | | Qt Gui | | - | ||||||||||||||||||
| 103 | |---------------------------------------------------| Qt Scale Factor | - | ||||||||||||||||||
| 104 | | Qt Gui QPlatform* Native Pixels | * | - | ||||||||||||||||||
| 105 | | Qt platform plugin | | - | ||||||||||||||||||
| 106 | |---------------------------------------------------| OS Scale Factor | - | ||||||||||||||||||
| 107 | | Display Device Pixels | | - | ||||||||||||||||||
| 108 | | (Graphics Buffers) | | - | ||||||||||||||||||
| 109 | ----------------------------------------------------- | - | ||||||||||||||||||
| 110 | - | |||||||||||||||||||
| 111 | This is an simplification and shows the main coordinate system. All layers | - | ||||||||||||||||||
| 112 | may work with device pixels in specific cases: OpenGL, creating the backing | - | ||||||||||||||||||
| 113 | store, and QPixmap management. The "Native Pixels" coordinate system is | - | ||||||||||||||||||
| 114 | internal to Qt and should not be exposed to Qt users: Seen from the outside | - | ||||||||||||||||||
| 115 | there are only two coordinate systems: device independent pixels and device | - | ||||||||||||||||||
| 116 | pixels. | - | ||||||||||||||||||
| 117 | - | |||||||||||||||||||
| 118 | The devicePixelRatio seen by applications is the product of the Qt scale | - | ||||||||||||||||||
| 119 | factor and the OS scale factor. The value of the scale factors may be 1, | - | ||||||||||||||||||
| 120 | in which case two or more of the coordinate systems are equivalent. Platforms | - | ||||||||||||||||||
| 121 | that (may) have an OS scale factor include \macos, iOS and Wayland. | - | ||||||||||||||||||
| 122 | - | |||||||||||||||||||
| 123 | Note that the functions in this file do not work with the OS scale factor | - | ||||||||||||||||||
| 124 | directly and are limited to converting between device independent and native | - | ||||||||||||||||||
| 125 | pixels. The OS scale factor is accunted for by QWindow::devicePixelRatio() | - | ||||||||||||||||||
| 126 | and similar functions. | - | ||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | Configuration Examples: | - | ||||||||||||||||||
| 129 | - | |||||||||||||||||||
| 130 | 'Classic': Device Independent Pixels = Native Pixels = Device Pixels | - | ||||||||||||||||||
| 131 | --------------------------------------------------- devicePixelRatio: 1 | - | ||||||||||||||||||
| 132 | | Application / Qt Gui 100 x 100 | | - | ||||||||||||||||||
| 133 | | | Qt Scale Factor: 1 | - | ||||||||||||||||||
| 134 | | Qt Platform / OS 100 x 100 | | - | ||||||||||||||||||
| 135 | | | OS Scale Factor: 1 | - | ||||||||||||||||||
| 136 | | Display 100 x 100 | | - | ||||||||||||||||||
| 137 | ----------------------------------------------------- | - | ||||||||||||||||||
| 138 | - | |||||||||||||||||||
| 139 | 'Retina Device': Device Independent Pixels = Native Pixels | - | ||||||||||||||||||
| 140 | --------------------------------------------------- devicePixelRatio: 2 | - | ||||||||||||||||||
| 141 | | Application / Qt Gui 100 x 100 | | - | ||||||||||||||||||
| 142 | | | Qt Scale Factor: 1 | - | ||||||||||||||||||
| 143 | | Qt Platform / OS 100 x 100 | | - | ||||||||||||||||||
| 144 | |---------------------------------------------------| OS Scale Factor: 2 | - | ||||||||||||||||||
| 145 | | Display 200 x 200 | | - | ||||||||||||||||||
| 146 | ----------------------------------------------------- | - | ||||||||||||||||||
| 147 | - | |||||||||||||||||||
| 148 | '2x Qt Scaling': Native Pixels = Device Pixels | - | ||||||||||||||||||
| 149 | --------------------------------------------------- devicePixelRatio: 2 | - | ||||||||||||||||||
| 150 | | Application / Qt Gui 100 x 100 | | - | ||||||||||||||||||
| 151 | |---------------------------------------------------| Qt Scale Factor: 2 | - | ||||||||||||||||||
| 152 | | Qt Platform / OS 200 x 200 | | - | ||||||||||||||||||
| 153 | | | OS Scale Factor: 1 | - | ||||||||||||||||||
| 154 | | Display 200 x 200 | | - | ||||||||||||||||||
| 155 | ----------------------------------------------------- | - | ||||||||||||||||||
| 156 | - | |||||||||||||||||||
| 157 | The Qt Scale Factor is the product of two sub-scale factors, which | - | ||||||||||||||||||
| 158 | are independently either set or determined by the platform plugin. | - | ||||||||||||||||||
| 159 | Several APIs are offered for this, targeting both developers and | - | ||||||||||||||||||
| 160 | end users. All scale factors are of type qreal. | - | ||||||||||||||||||
| 161 | - | |||||||||||||||||||
| 162 | 1) A global scale factor | - | ||||||||||||||||||
| 163 | The QT_SCALE_FACTOR environment variable can be used to set | - | ||||||||||||||||||
| 164 | a global scale factor for all windows in the processs. This | - | ||||||||||||||||||
| 165 | is useful for testing and debugging (you can simulate any | - | ||||||||||||||||||
| 166 | devicePixelRatio without needing access to special hardware), | - | ||||||||||||||||||
| 167 | and perhaps also for targeting a specific application to | - | ||||||||||||||||||
| 168 | a specific display type (embedded use cases). | - | ||||||||||||||||||
| 169 | - | |||||||||||||||||||
| 170 | 2) Per-screen scale factors | - | ||||||||||||||||||
| 171 | Some platform plugins support providing a per-screen scale | - | ||||||||||||||||||
| 172 | factor based on display density information. These platforms | - | ||||||||||||||||||
| 173 | include X11, Windows, and Android. | - | ||||||||||||||||||
| 174 | - | |||||||||||||||||||
| 175 | There are two APIs for enabling or disabling this behavior: | - | ||||||||||||||||||
| 176 | - The QT_AUTO_SCREEN_SCALE_FACTOR environment variable. | - | ||||||||||||||||||
| 177 | - The AA_EnableHighDpiScaling and AA_DisableHighDpiScaling | - | ||||||||||||||||||
| 178 | application attributes | - | ||||||||||||||||||
| 179 | - | |||||||||||||||||||
| 180 | Enabling either will make QHighDpiScaling call QPlatformScreen::pixelDensity() | - | ||||||||||||||||||
| 181 | and use the value provided as the scale factor for the screen in | - | ||||||||||||||||||
| 182 | question. Disabling is done on a 'veto' basis where either the | - | ||||||||||||||||||
| 183 | environment or the application can disable the scaling. The intended use | - | ||||||||||||||||||
| 184 | cases are 'My system is not providing correct display density | - | ||||||||||||||||||
| 185 | information' and 'My application needs to work in display pixels', | - | ||||||||||||||||||
| 186 | respectively. | - | ||||||||||||||||||
| 187 | - | |||||||||||||||||||
| 188 | The QT_SCREEN_SCALE_FACTORS environment variable can be used to set the screen | - | ||||||||||||||||||
| 189 | scale factors manually. Set this to a semicolon-separated | - | ||||||||||||||||||
| 190 | list of scale factors (matching the order of QGuiApplications::screens()), | - | ||||||||||||||||||
| 191 | or to a list of name=value pairs (where name matches QScreen::name()). | - | ||||||||||||||||||
| 192 | - | |||||||||||||||||||
| 193 | Coordinate conversion functions must be used when writing code that passes | - | ||||||||||||||||||
| 194 | geometry across the Qt Gui / Platform plugin boundary. The main conversion | - | ||||||||||||||||||
| 195 | functions are: | - | ||||||||||||||||||
| 196 | T toNativePixels(T, QWindow *) | - | ||||||||||||||||||
| 197 | T fromNativePixels(T, QWindow*) | - | ||||||||||||||||||
| 198 | - | |||||||||||||||||||
| 199 | The following classes in QtGui use native pixels, for the convenience of the | - | ||||||||||||||||||
| 200 | plataform plugins: | - | ||||||||||||||||||
| 201 | QPlatformWindow | - | ||||||||||||||||||
| 202 | QPlatformScreen | - | ||||||||||||||||||
| 203 | QWindowSystemInterface (API only - Events are in device independent pixels) | - | ||||||||||||||||||
| 204 | - | |||||||||||||||||||
| 205 | As a special consideration platform plugin code should be careful about | - | ||||||||||||||||||
| 206 | calling QtGui geometry accessor functions: | - | ||||||||||||||||||
| 207 | QRect r = window->geometry(); | - | ||||||||||||||||||
| 208 | Here the returned geometry is in device independent pixels. Add a conversion call: | - | ||||||||||||||||||
| 209 | QRect r = QHighDpi::toNativePixels(window->geometry()); | - | ||||||||||||||||||
| 210 | (Avoiding calling QWindow and instead using the QPlatformWindow geometry | - | ||||||||||||||||||
| 211 | might be a better course of action in this case.) | - | ||||||||||||||||||
| 212 | */ | - | ||||||||||||||||||
| 213 | - | |||||||||||||||||||
| 214 | qreal QHighDpiScaling::m_factor = 1.0; | - | ||||||||||||||||||
| 215 | bool QHighDpiScaling::m_active = false; //"overall active" - is there any scale factor set. | - | ||||||||||||||||||
| 216 | bool QHighDpiScaling::m_usePixelDensity = false; // use scale factor from platform plugin | - | ||||||||||||||||||
| 217 | bool QHighDpiScaling::m_pixelDensityScalingActive = false; // pixel density scale factor > 1 | - | ||||||||||||||||||
| 218 | bool QHighDpiScaling::m_globalScalingActive = false; // global scale factor is active | - | ||||||||||||||||||
| 219 | bool QHighDpiScaling::m_screenFactorSet = false; // QHighDpiScaling::setScreenFactor has been used | - | ||||||||||||||||||
| 220 | QDpi QHighDpiScaling::m_logicalDpi = QDpi(-1,-1); // The scaled logical DPI of the primary screen | - | ||||||||||||||||||
| 221 | - | |||||||||||||||||||
| 222 | /* | - | ||||||||||||||||||
| 223 | Initializes the QHighDpiScaling global variables. Called before the | - | ||||||||||||||||||
| 224 | platform plugin is created. | - | ||||||||||||||||||
| 225 | */ | - | ||||||||||||||||||
| 226 | - | |||||||||||||||||||
| 227 | static inline bool usePixelDensity() | - | ||||||||||||||||||
| 228 | { | - | ||||||||||||||||||
| 229 | // Determine if we should set a scale factor based on the pixel density | - | ||||||||||||||||||
| 230 | // reported by the platform plugin. There are several enablers and several | - | ||||||||||||||||||
| 231 | // disablers. A single disable may veto all other enablers. | - | ||||||||||||||||||
| 232 | if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling))
| 0 | ||||||||||||||||||
| 233 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 234 | bool screenEnvValueOk; | - | ||||||||||||||||||
| 235 | const int screenEnvValue = qEnvironmentVariableIntValue(autoScreenEnvVar, &screenEnvValueOk); | - | ||||||||||||||||||
| 236 | if (screenEnvValueOk && screenEnvValue < 1)
| 0 | ||||||||||||||||||
| 237 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 238 | return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
| 0 | ||||||||||||||||||
| 239 | || (screenEnvValueOk && screenEnvValue > 0) never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
| 0 | ||||||||||||||||||
| 240 | || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto"); never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
| 0 | ||||||||||||||||||
| 241 | } | - | ||||||||||||||||||
| 242 | - | |||||||||||||||||||
| 243 | void QHighDpiScaling::initHighDpiScaling() | - | ||||||||||||||||||
| 244 | { | - | ||||||||||||||||||
| 245 | // Determine if there is a global scale factor set. | - | ||||||||||||||||||
| 246 | m_factor = initialGlobalScaleFactor(); | - | ||||||||||||||||||
| 247 | m_globalScalingActive = !qFuzzyCompare(m_factor, qreal(1)); | - | ||||||||||||||||||
| 248 | - | |||||||||||||||||||
| 249 | m_usePixelDensity = usePixelDensity(); | - | ||||||||||||||||||
| 250 | - | |||||||||||||||||||
| 251 | m_pixelDensityScalingActive = false; //set in updateHighDpiScaling below | - | ||||||||||||||||||
| 252 | - | |||||||||||||||||||
| 253 | // we update m_active in updateHighDpiScaling, but while we create the | - | ||||||||||||||||||
| 254 | // screens, we have to assume that m_usePixelDensity implies scaling | - | ||||||||||||||||||
| 255 | m_active = m_globalScalingActive || m_usePixelDensity;
| 0 | ||||||||||||||||||
| 256 | } never executed: end of block | 0 | ||||||||||||||||||
| 257 | - | |||||||||||||||||||
| 258 | void QHighDpiScaling::updateHighDpiScaling() | - | ||||||||||||||||||
| 259 | { | - | ||||||||||||||||||
| 260 | if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling))
| 0 | ||||||||||||||||||
| 261 | return; never executed: return; | 0 | ||||||||||||||||||
| 262 | - | |||||||||||||||||||
| 263 | if (m_usePixelDensity && !m_pixelDensityScalingActive) {
| 0 | ||||||||||||||||||
| 264 | Q_FOREACH (QScreen *screen, QGuiApplication::screens()) { | - | ||||||||||||||||||
| 265 | if (!qFuzzyCompare(screenSubfactor(screen->handle()), qreal(1))) {
| 0 | ||||||||||||||||||
| 266 | m_pixelDensityScalingActive = true; | - | ||||||||||||||||||
| 267 | break; never executed: break; | 0 | ||||||||||||||||||
| 268 | } | - | ||||||||||||||||||
| 269 | } never executed: end of block | 0 | ||||||||||||||||||
| 270 | } never executed: end of block | 0 | ||||||||||||||||||
| 271 | if (qEnvironmentVariableIsSet(screenFactorsEnvVar)) {
| 0 | ||||||||||||||||||
| 272 | int i = 0; | - | ||||||||||||||||||
| 273 | Q_FOREACH (const QByteArray &spec, qgetenv(screenFactorsEnvVar).split(';')) { | - | ||||||||||||||||||
| 274 | QScreen *screen = 0; | - | ||||||||||||||||||
| 275 | int equalsPos = spec.lastIndexOf('='); | - | ||||||||||||||||||
| 276 | double factor = 0; | - | ||||||||||||||||||
| 277 | if (equalsPos > 0) {
| 0 | ||||||||||||||||||
| 278 | // support "name=factor" | - | ||||||||||||||||||
| 279 | QByteArray name = spec.mid(0, equalsPos); | - | ||||||||||||||||||
| 280 | QByteArray f = spec.mid(equalsPos + 1); | - | ||||||||||||||||||
| 281 | bool ok; | - | ||||||||||||||||||
| 282 | factor = f.toDouble(&ok); | - | ||||||||||||||||||
| 283 | if (ok) {
| 0 | ||||||||||||||||||
| 284 | Q_FOREACH (QScreen *s, QGuiApplication::screens()) { | - | ||||||||||||||||||
| 285 | if (s->name() == QString::fromLocal8Bit(name)) {
| 0 | ||||||||||||||||||
| 286 | screen = s; | - | ||||||||||||||||||
| 287 | break; never executed: break; | 0 | ||||||||||||||||||
| 288 | } | - | ||||||||||||||||||
| 289 | } never executed: end of block | 0 | ||||||||||||||||||
| 290 | } never executed: end of block | 0 | ||||||||||||||||||
| 291 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 292 | // listing screens in order | - | ||||||||||||||||||
| 293 | bool ok; | - | ||||||||||||||||||
| 294 | factor = spec.toDouble(&ok); | - | ||||||||||||||||||
| 295 | if (ok && i < QGuiApplication::screens().count())
| 0 | ||||||||||||||||||
| 296 | screen = QGuiApplication::screens().at(i); never executed: screen = QGuiApplication::screens().at(i); | 0 | ||||||||||||||||||
| 297 | } never executed: end of block | 0 | ||||||||||||||||||
| 298 | if (screen)
| 0 | ||||||||||||||||||
| 299 | setScreenFactor(screen, factor); never executed: setScreenFactor(screen, factor); | 0 | ||||||||||||||||||
| 300 | ++i; | - | ||||||||||||||||||
| 301 | } never executed: end of block | 0 | ||||||||||||||||||
| 302 | } never executed: end of block | 0 | ||||||||||||||||||
| 303 | m_active = m_globalScalingActive || m_screenFactorSet || m_pixelDensityScalingActive;
| 0 | ||||||||||||||||||
| 304 | - | |||||||||||||||||||
| 305 | QPlatformScreen *primaryScreen = QGuiApplication::primaryScreen()->handle(); | - | ||||||||||||||||||
| 306 | qreal sf = screenSubfactor(primaryScreen); | - | ||||||||||||||||||
| 307 | QDpi primaryDpi = primaryScreen->logicalDpi(); | - | ||||||||||||||||||
| 308 | m_logicalDpi = QDpi(primaryDpi.first / sf, primaryDpi.second / sf); | - | ||||||||||||||||||
| 309 | } never executed: end of block | 0 | ||||||||||||||||||
| 310 | - | |||||||||||||||||||
| 311 | /* | - | ||||||||||||||||||
| 312 | Sets the global scale factor which is applied to all windows. | - | ||||||||||||||||||
| 313 | */ | - | ||||||||||||||||||
| 314 | void QHighDpiScaling::setGlobalFactor(qreal factor) | - | ||||||||||||||||||
| 315 | { | - | ||||||||||||||||||
| 316 | if (qFuzzyCompare(factor, m_factor))
| 0 | ||||||||||||||||||
| 317 | return; never executed: return; | 0 | ||||||||||||||||||
| 318 | if (!QGuiApplication::allWindows().isEmpty())
| 0 | ||||||||||||||||||
| 319 | qWarning("QHighDpiScaling::setFactor: Should only be called when no windows exist."); never executed: QMessageLogger(__FILE__, 319, __PRETTY_FUNCTION__).warning("QHighDpiScaling::setFactor: Should only be called when no windows exist."); | 0 | ||||||||||||||||||
| 320 | - | |||||||||||||||||||
| 321 | m_globalScalingActive = !qFuzzyCompare(factor, qreal(1)); | - | ||||||||||||||||||
| 322 | m_factor = m_globalScalingActive ? factor : qreal(1);
| 0 | ||||||||||||||||||
| 323 | m_active = m_globalScalingActive || m_screenFactorSet || m_pixelDensityScalingActive;
| 0 | ||||||||||||||||||
| 324 | Q_FOREACH (QScreen *screen, QGuiApplication::screens()) | - | ||||||||||||||||||
| 325 | screen->d_func()->updateHighDpi(); never executed: screen->d_func()->updateHighDpi(); | 0 | ||||||||||||||||||
| 326 | } never executed: end of block | 0 | ||||||||||||||||||
| 327 | - | |||||||||||||||||||
| 328 | static const char scaleFactorProperty[] = "_q_scaleFactor"; | - | ||||||||||||||||||
| 329 | - | |||||||||||||||||||
| 330 | /* | - | ||||||||||||||||||
| 331 | Sets a per-screen scale factor. | - | ||||||||||||||||||
| 332 | */ | - | ||||||||||||||||||
| 333 | void QHighDpiScaling::setScreenFactor(QScreen *screen, qreal factor) | - | ||||||||||||||||||
| 334 | { | - | ||||||||||||||||||
| 335 | if (!qFuzzyCompare(factor, qreal(1))) {
| 0 | ||||||||||||||||||
| 336 | m_screenFactorSet = true; | - | ||||||||||||||||||
| 337 | m_active = true; | - | ||||||||||||||||||
| 338 | } never executed: end of block | 0 | ||||||||||||||||||
| 339 | screen->setProperty(scaleFactorProperty, QVariant(factor)); | - | ||||||||||||||||||
| 340 | - | |||||||||||||||||||
| 341 | // hack to force re-evaluation of screen geometry | - | ||||||||||||||||||
| 342 | if (screen->handle())
| 0 | ||||||||||||||||||
| 343 | screen->d_func()->setPlatformScreen(screen->handle()); // updates geometries based on scale factor never executed: screen->d_func()->setPlatformScreen(screen->handle()); | 0 | ||||||||||||||||||
| 344 | } never executed: end of block | 0 | ||||||||||||||||||
| 345 | - | |||||||||||||||||||
| 346 | QPoint QHighDpiScaling::mapPositionToNative(const QPoint &pos, const QPlatformScreen *platformScreen) | - | ||||||||||||||||||
| 347 | { | - | ||||||||||||||||||
| 348 | if (!platformScreen)
| 0 | ||||||||||||||||||
| 349 | return pos; never executed: return pos; | 0 | ||||||||||||||||||
| 350 | const qreal scaleFactor = factor(platformScreen); | - | ||||||||||||||||||
| 351 | const QPoint topLeft = platformScreen->geometry().topLeft(); | - | ||||||||||||||||||
| 352 | return (pos - topLeft) * scaleFactor + topLeft; never executed: return (pos - topLeft) * scaleFactor + topLeft; | 0 | ||||||||||||||||||
| 353 | } | - | ||||||||||||||||||
| 354 | - | |||||||||||||||||||
| 355 | QPoint QHighDpiScaling::mapPositionFromNative(const QPoint &pos, const QPlatformScreen *platformScreen) | - | ||||||||||||||||||
| 356 | { | - | ||||||||||||||||||
| 357 | if (!platformScreen)
| 0 | ||||||||||||||||||
| 358 | return pos; never executed: return pos; | 0 | ||||||||||||||||||
| 359 | const qreal scaleFactor = factor(platformScreen); | - | ||||||||||||||||||
| 360 | const QPoint topLeft = platformScreen->geometry().topLeft(); | - | ||||||||||||||||||
| 361 | return (pos - topLeft) / scaleFactor + topLeft; never executed: return (pos - topLeft) / scaleFactor + topLeft; | 0 | ||||||||||||||||||
| 362 | } | - | ||||||||||||||||||
| 363 | - | |||||||||||||||||||
| 364 | qreal QHighDpiScaling::screenSubfactor(const QPlatformScreen *screen) | - | ||||||||||||||||||
| 365 | { | - | ||||||||||||||||||
| 366 | qreal factor = qreal(1.0); | - | ||||||||||||||||||
| 367 | if (screen) {
| 0 | ||||||||||||||||||
| 368 | if (m_usePixelDensity)
| 0 | ||||||||||||||||||
| 369 | factor *= screen->pixelDensity(); never executed: factor *= screen->pixelDensity(); | 0 | ||||||||||||||||||
| 370 | if (m_screenFactorSet) {
| 0 | ||||||||||||||||||
| 371 | QVariant screenFactor = screen->screen()->property(scaleFactorProperty); | - | ||||||||||||||||||
| 372 | if (screenFactor.isValid())
| 0 | ||||||||||||||||||
| 373 | factor *= screenFactor.toReal(); never executed: factor *= screenFactor.toReal(); | 0 | ||||||||||||||||||
| 374 | } never executed: end of block | 0 | ||||||||||||||||||
| 375 | } never executed: end of block | 0 | ||||||||||||||||||
| 376 | return factor; never executed: return factor; | 0 | ||||||||||||||||||
| 377 | } | - | ||||||||||||||||||
| 378 | - | |||||||||||||||||||
| 379 | QDpi QHighDpiScaling::logicalDpi() | - | ||||||||||||||||||
| 380 | { | - | ||||||||||||||||||
| 381 | return m_logicalDpi; never executed: return m_logicalDpi; | 0 | ||||||||||||||||||
| 382 | } | - | ||||||||||||||||||
| 383 | - | |||||||||||||||||||
| 384 | qreal QHighDpiScaling::factor(const QScreen *screen) | - | ||||||||||||||||||
| 385 | { | - | ||||||||||||||||||
| 386 | // Fast path for when scaling in Qt is not used at all. | - | ||||||||||||||||||
| 387 | if (!m_active)
| 0 | ||||||||||||||||||
| 388 | return qreal(1.0); never executed: return qreal(1.0); | 0 | ||||||||||||||||||
| 389 | - | |||||||||||||||||||
| 390 | // The effective factor for a given screen is the product of the | - | ||||||||||||||||||
| 391 | // screen and global sub-factors | - | ||||||||||||||||||
| 392 | qreal factor = m_factor; | - | ||||||||||||||||||
| 393 | if (screen)
| 0 | ||||||||||||||||||
| 394 | factor *= screenSubfactor(screen->handle()); never executed: factor *= screenSubfactor(screen->handle()); | 0 | ||||||||||||||||||
| 395 | return factor; never executed: return factor; | 0 | ||||||||||||||||||
| 396 | } | - | ||||||||||||||||||
| 397 | - | |||||||||||||||||||
| 398 | qreal QHighDpiScaling::factor(const QPlatformScreen *platformScreen) | - | ||||||||||||||||||
| 399 | { | - | ||||||||||||||||||
| 400 | if (!m_active)
| 0 | ||||||||||||||||||
| 401 | return qreal(1.0); never executed: return qreal(1.0); | 0 | ||||||||||||||||||
| 402 | - | |||||||||||||||||||
| 403 | return m_factor * screenSubfactor(platformScreen); never executed: return m_factor * screenSubfactor(platformScreen); | 0 | ||||||||||||||||||
| 404 | } | - | ||||||||||||||||||
| 405 | - | |||||||||||||||||||
| 406 | qreal QHighDpiScaling::factor(const QWindow *window) | - | ||||||||||||||||||
| 407 | { | - | ||||||||||||||||||
| 408 | if (!m_active)
| 0 | ||||||||||||||||||
| 409 | return qreal(1.0); never executed: return qreal(1.0); | 0 | ||||||||||||||||||
| 410 | - | |||||||||||||||||||
| 411 | return factor(window ? window->screen() : QGuiApplication::primaryScreen()); never executed: return factor(window ? window->screen() : QGuiApplication::primaryScreen()); | 0 | ||||||||||||||||||
| 412 | } | - | ||||||||||||||||||
| 413 | - | |||||||||||||||||||
| 414 | QPoint QHighDpiScaling::origin(const QScreen *screen) | - | ||||||||||||||||||
| 415 | { | - | ||||||||||||||||||
| 416 | return screen->geometry().topLeft(); never executed: return screen->geometry().topLeft(); | 0 | ||||||||||||||||||
| 417 | } | - | ||||||||||||||||||
| 418 | - | |||||||||||||||||||
| 419 | QPoint QHighDpiScaling::origin(const QPlatformScreen *platformScreen) | - | ||||||||||||||||||
| 420 | { | - | ||||||||||||||||||
| 421 | return platformScreen->geometry().topLeft(); never executed: return platformScreen->geometry().topLeft(); | 0 | ||||||||||||||||||
| 422 | } | - | ||||||||||||||||||
| 423 | - | |||||||||||||||||||
| 424 | #endif //QT_NO_HIGHDPISCALING | - | ||||||||||||||||||
| 425 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |