| Line | Source | Count |
| 1 | | - |
| 2 | | - |
| 3 | | - |
| 4 | | - |
| 5 | | - |
| 6 | | - |
| 7 | | - |
| 8 | | - |
| 9 | | - |
| 10 | | - |
| 11 | | - |
| 12 | | - |
| 13 | | - |
| 14 | | - |
| 15 | | - |
| 16 | | - |
| 17 | | - |
| 18 | | - |
| 19 | | - |
| 20 | | - |
| 21 | | - |
| 22 | | - |
| 23 | | - |
| 24 | | - |
| 25 | | - |
| 26 | | - |
| 27 | | - |
| 28 | | - |
| 29 | | - |
| 30 | | - |
| 31 | | - |
| 32 | | - |
| 33 | | - |
| 34 | | - |
| 35 | | - |
| 36 | | - |
| 37 | | - |
| 38 | | - |
| 39 | | - |
| 40 | #include "qhighdpiscaling_p.h" | - |
| 41 | #include "qguiapplication.h" | - |
| 42 | #include "qscreen.h" | - |
| 43 | #include "qplatformintegration.h" | - |
| 44 | #include "private/qscreen_p.h" | - |
| 45 | | - |
| 46 | #include <QtCore/qdebug.h> | - |
| 47 | | - |
| 48 | QT_BEGIN_NAMESPACE | - |
| 49 | | - |
| 50 | Q_LOGGING_CATEGORY(lcScaling, "qt.scaling"); | - |
| 51 | | - |
| 52 | #ifndef QT_NO_HIGHDPISCALING | - |
| 53 | static const char legacyDevicePixelEnvVar[] = "QT_DEVICE_PIXEL_RATIO"; | - |
| 54 | static const char scaleFactorEnvVar[] = "QT_SCALE_FACTOR"; | - |
| 55 | static const char autoScreenEnvVar[] = "QT_AUTO_SCREEN_SCALE_FACTOR"; | - |
| 56 | static const char screenFactorsEnvVar[] = "QT_SCREEN_SCALE_FACTORS"; | - |
| 57 | | - |
| 58 | static inline qreal initialGlobalScaleFactor() | - |
| 59 | { | - |
| 60 | | - |
| 61 | qreal result = 1; | - |
| 62 | if (qEnvironmentVariableIsSet(scaleFactorEnvVar)) { | - |
| 63 | bool ok; | - |
| 64 | const qreal f = qgetenv(scaleFactorEnvVar).toDouble(&ok); | - |
| 65 | if (ok && f > 0) { | - |
| 66 | qCDebug(lcScaling) << "Apply " << scaleFactorEnvVar << f; | - |
| 67 | result = f; | - |
| 68 | } | - |
| 69 | } else { | - |
| 70 | if (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar)) { | - |
| 71 | qWarning() << "Warning:" << legacyDevicePixelEnvVar << "is deprecated. Instead use:" << endl | - |
| 72 | << " " << autoScreenEnvVar << "to enable platform plugin controlled per-screen factors." << endl | - |
| 73 | << " " << screenFactorsEnvVar << "to set per-screen factors." << endl | - |
| 74 | << " " << scaleFactorEnvVar << "to set the application global scale factor."; | - |
| 75 | | - |
| 76 | int dpr = qEnvironmentVariableIntValue(legacyDevicePixelEnvVar); | - |
| 77 | if (dpr > 0) | - |
| 78 | result = dpr; | - |
| 79 | } | - |
| 80 | } | - |
| 81 | return result; | - |
| 82 | } | - |
| 83 | | - |
| 84 | | - |
| 85 | | - |
| 86 | | - |
| 87 | | - |
| 88 | | - |
| 89 | | - |
| 90 | | - |
| 91 | | - |
| 92 | | - |
| 93 | | - |
| 94 | | - |
| 95 | | - |
| 96 | | - |
| 97 | | - |
| 98 | | - |
| 99 | | - |
| 100 | | - |
| 101 | | - |
| 102 | | - |
| 103 | | - |
| 104 | | - |
| 105 | | - |
| 106 | | - |
| 107 | | - |
| 108 | | - |
| 109 | | - |
| 110 | | - |
| 111 | | - |
| 112 | | - |
| 113 | | - |
| 114 | | - |
| 115 | | - |
| 116 | | - |
| 117 | | - |
| 118 | | - |
| 119 | | - |
| 120 | | - |
| 121 | | - |
| 122 | | - |
| 123 | | - |
| 124 | | - |
| 125 | | - |
| 126 | | - |
| 127 | | - |
| 128 | | - |
| 129 | | - |
| 130 | | - |
| 131 | | - |
| 132 | | - |
| 133 | | - |
| 134 | | - |
| 135 | | - |
| 136 | | - |
| 137 | | - |
| 138 | | - |
| 139 | | - |
| 140 | | - |
| 141 | | - |
| 142 | | - |
| 143 | | - |
| 144 | | - |
| 145 | | - |
| 146 | | - |
| 147 | | - |
| 148 | | - |
| 149 | | - |
| 150 | | - |
| 151 | | - |
| 152 | | - |
| 153 | | - |
| 154 | | - |
| 155 | | - |
| 156 | | - |
| 157 | | - |
| 158 | | - |
| 159 | | - |
| 160 | | - |
| 161 | | - |
| 162 | | - |
| 163 | | - |
| 164 | | - |
| 165 | | - |
| 166 | | - |
| 167 | | - |
| 168 | | - |
| 169 | | - |
| 170 | | - |
| 171 | | - |
| 172 | | - |
| 173 | | - |
| 174 | | - |
| 175 | | - |
| 176 | | - |
| 177 | | - |
| 178 | | - |
| 179 | | - |
| 180 | | - |
| 181 | | - |
| 182 | | - |
| 183 | | - |
| 184 | | - |
| 185 | | - |
| 186 | | - |
| 187 | | - |
| 188 | | - |
| 189 | | - |
| 190 | | - |
| 191 | | - |
| 192 | | - |
| 193 | | - |
| 194 | | - |
| 195 | | - |
| 196 | | - |
| 197 | | - |
| 198 | | - |
| 199 | | - |
| 200 | | - |
| 201 | | - |
| 202 | | - |
| 203 | | - |
| 204 | | - |
| 205 | | - |
| 206 | | - |
| 207 | | - |
| 208 | | - |
| 209 | | - |
| 210 | | - |
| 211 | | - |
| 212 | | - |
| 213 | | - |
| 214 | | - |
| 215 | | - |
| 216 | | - |
| 217 | | - |
| 218 | | - |
| 219 | | - |
| 220 | qreal QHighDpiScaling::m_factor = 1.0; | - |
| 221 | bool QHighDpiScaling::m_active = false; | - |
| 222 | bool QHighDpiScaling::m_usePixelDensity = false; | - |
| 223 | bool QHighDpiScaling::m_pixelDensityScalingActive = false; | - |
| 224 | bool QHighDpiScaling::m_globalScalingActive = false; | - |
| 225 | bool QHighDpiScaling::m_screenFactorSet = false; | - |
| 226 | QDpi QHighDpiScaling::m_logicalDpi = QDpi(-1,-1); | - |
| 227 | | - |
| 228 | | - |
| 229 | | - |
| 230 | | - |
| 231 | | - |
| 232 | | - |
| 233 | static inline bool usePixelDensity() | - |
| 234 | { | - |
| 235 | | - |
| 236 | | - |
| 237 | | - |
| 238 | if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling)) | - |
| 239 | return false; | - |
| 240 | bool screenEnvValueOk; | - |
| 241 | const int screenEnvValue = qEnvironmentVariableIntValue(autoScreenEnvVar, &screenEnvValueOk); | - |
| 242 | if (screenEnvValueOk && screenEnvValue < 1) | - |
| 243 | return false; | - |
| 244 | return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) | - |
| 245 | || (screenEnvValueOk && screenEnvValue > 0) | - |
| 246 | || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto"); | - |
| 247 | } | - |
| 248 | | - |
| 249 | void QHighDpiScaling::initHighDpiScaling() | - |
| 250 | { | - |
| 251 | | - |
| 252 | m_factor = initialGlobalScaleFactor(); | - |
| 253 | m_globalScalingActive = !qFuzzyCompare(m_factor, qreal(1)); | - |
| 254 | | - |
| 255 | m_usePixelDensity = usePixelDensity(); | - |
| 256 | | - |
| 257 | m_pixelDensityScalingActive = false; | - |
| 258 | | - |
| 259 | | - |
| 260 | | - |
| 261 | m_active = m_globalScalingActive || m_usePixelDensity; | - |
| 262 | } | - |
| 263 | | - |
| 264 | void QHighDpiScaling::updateHighDpiScaling() | - |
| 265 | { | - |
| 266 | if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling))| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 267 | return; never executed: return; | 0 |
| 268 | | - |
| 269 | if (m_usePixelDensity && !m_pixelDensityScalingActive) {| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 270 | Q_FOREACHconst auto screens = QGuiApplication::screens(); | - |
| 271 | for (QScreen *screen , QGuiApplication::: screens())) { | - |
| 272 | if (!qFuzzyCompare(screenSubfactor(screen->handle()), qreal(1))) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 273 | m_pixelDensityScalingActive = true; | - |
| 274 | break; never executed: break; | 0 |
| 275 | } | - |
| 276 | } never executed: end of block | 0 |
| 277 | } never executed: end of block | 0 |
| 278 | if (qEnvironmentVariableIsSet(screenFactorsEnvVar)) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 279 | int i = 0; | - |
| 280 | Q_FOREACH (const QByteArray &spec,auto specs = qgetenv(screenFactorsEnvVar).split(';'))); | - |
| 281 | for (const QByteArray &spec : specs) { | - |
| 282 | QScreen *screen = 0; | - |
| 283 | int equalsPos = spec.lastIndexOf('='); | - |
| 284 | double factor = 0; | - |
| 285 | if (equalsPos > 0) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 286 | | - |
| 287 | QByteArray name = spec.mid(0, equalsPos); | - |
| 288 | QByteArray f = spec.mid(equalsPos + 1); | - |
| 289 | bool ok; | - |
| 290 | factor = f.toDouble(&ok); | - |
| 291 | if (ok) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 292 | Q_FOREACHconst auto screens = QGuiApplication::screens(); | - |
| 293 | for (QScreen *s , QGuiApplication::: screens())) { | - |
| 294 | if (s->name() == QString::fromLocal8Bit(name)) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 295 | screen = s; | - |
| 296 | break; never executed: break; | 0 |
| 297 | } | - |
| 298 | } never executed: end of block | 0 |
| 299 | } never executed: end of block | 0 |
| 300 | } else { never executed: end of block | 0 |
| 301 | | - |
| 302 | bool ok; | - |
| 303 | factor = spec.toDouble(&ok); | - |
| 304 | if (ok && i < QGuiApplication::screens().count())| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 305 | screen = QGuiApplication::screens().at(i); never executed: screen = QGuiApplication::screens().at(i); | 0 |
| 306 | } never executed: end of block | 0 |
| 307 | if (screen)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 308 | setScreenFactor(screen, factor); never executed: setScreenFactor(screen, factor); | 0 |
| 309 | ++i; | - |
| 310 | } never executed: end of block | 0 |
| 311 | } never executed: end of block | 0 |
| 312 | m_active = m_globalScalingActive || m_screenFactorSet || m_pixelDensityScalingActive;| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 313 | | - |
| 314 | QPlatformScreen *primaryScreen = QGuiApplication::primaryScreen()->handle(); | - |
| 315 | qreal sf = screenSubfactor(primaryScreen); | - |
| 316 | QDpi primaryDpi = primaryScreen->logicalDpi(); | - |
| 317 | m_logicalDpi = QDpi(primaryDpi.first / sf, primaryDpi.second / sf); | - |
| 318 | } never executed: end of block | 0 |
| 319 | | - |
| 320 | | - |
| 321 | | - |
| 322 | | - |
| 323 | void QHighDpiScaling::setGlobalFactor(qreal factor) | - |
| 324 | { | - |
| 325 | if (qFuzzyCompare(factor, m_factor))| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 326 | return; never executed: return; | 0 |
| 327 | if (!QGuiApplication::allWindows().isEmpty())| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 328 | qWarning("QHighDpiScaling::setFactor: Should only be called when no windows exist."); never executed: QMessageLogger(__FILE__, 328, __PRETTY_FUNCTION__).warning("QHighDpiScaling::setFactor: Should only be called when no windows exist."); | 0 |
| 329 | | - |
| 330 | m_globalScalingActive = !qFuzzyCompare(factor, qreal(1)); | - |
| 331 | m_factor = m_globalScalingActive ? factor : qreal(1);| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 332 | m_active = m_globalScalingActive || m_screenFactorSet || m_pixelDensityScalingActive;| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 333 | Q_FOREACHconst auto screens = QGuiApplication::screens(); | - |
| 334 | for (QScreen *screen , QGuiApplication::: screens())) | - |
| 335 | screen->d_func()->updateHighDpi(); never executed: screen->d_func()->updateHighDpi(); | 0 |
| 336 | } never executed: end of block | 0 |
| 337 | | - |
| 338 | static const char scaleFactorProperty[] = "_q_scaleFactor"; | - |
| 339 | | - |
| 340 | | - |
| 341 | | - |
| 342 | | - |
| 343 | void QHighDpiScaling::setScreenFactor(QScreen *screen, qreal factor) | - |
| 344 | { | - |
| 345 | if (!qFuzzyCompare(factor, qreal(1))) { | - |
| 346 | m_screenFactorSet = true; | - |
| 347 | m_active = true; | - |
| 348 | } | - |
| 349 | screen->setProperty(scaleFactorProperty, QVariant(factor)); | - |
| 350 | | - |
| 351 | | - |
| 352 | if (screen->handle()) | - |
| 353 | screen->d_func()->setPlatformScreen(screen->handle()); | - |
| 354 | } | - |
| 355 | | - |
| 356 | QPoint QHighDpiScaling::mapPositionToNative(const QPoint &pos, const QPlatformScreen *platformScreen) | - |
| 357 | { | - |
| 358 | if (!platformScreen) | - |
| 359 | return pos; | - |
| 360 | const qreal scaleFactor = factor(platformScreen); | - |
| 361 | const QPoint topLeft = platformScreen->geometry().topLeft(); | - |
| 362 | return (pos - topLeft) * scaleFactor + topLeft; | - |
| 363 | } | - |
| 364 | | - |
| 365 | QPoint QHighDpiScaling::mapPositionFromNative(const QPoint &pos, const QPlatformScreen *platformScreen) | - |
| 366 | { | - |
| 367 | if (!platformScreen) | - |
| 368 | return pos; | - |
| 369 | const qreal scaleFactor = factor(platformScreen); | - |
| 370 | const QPoint topLeft = platformScreen->geometry().topLeft(); | - |
| 371 | return (pos - topLeft) / scaleFactor + topLeft; | - |
| 372 | } | - |
| 373 | | - |
| 374 | qreal QHighDpiScaling::screenSubfactor(const QPlatformScreen *screen) | - |
| 375 | { | - |
| 376 | qreal factor = qreal(1.0); | - |
| 377 | if (screen) { | - |
| 378 | if (m_usePixelDensity) | - |
| 379 | factor *= screen->pixelDensity(); | - |
| 380 | if (m_screenFactorSet) { | - |
| 381 | QVariant screenFactor = screen->screen()->property(scaleFactorProperty); | - |
| 382 | if (screenFactor.isValid()) | - |
| 383 | factor *= screenFactor.toReal(); | - |
| 384 | } | - |
| 385 | } | - |
| 386 | return factor; | - |
| 387 | } | - |
| 388 | | - |
| 389 | QDpi QHighDpiScaling::logicalDpi() | - |
| 390 | { | - |
| 391 | return m_logicalDpi; | - |
| 392 | } | - |
| 393 | | - |
| 394 | qreal QHighDpiScaling::factor(const QScreen *screen) | - |
| 395 | { | - |
| 396 | | - |
| 397 | if (!m_active) | - |
| 398 | return qreal(1.0); | - |
| 399 | | - |
| 400 | | - |
| 401 | | - |
| 402 | qreal factor = m_factor; | - |
| 403 | if (screen) | - |
| 404 | factor *= screenSubfactor(screen->handle()); | - |
| 405 | return factor; | - |
| 406 | } | - |
| 407 | | - |
| 408 | qreal QHighDpiScaling::factor(const QPlatformScreen *platformScreen) | - |
| 409 | { | - |
| 410 | if (!m_active) | - |
| 411 | return qreal(1.0); | - |
| 412 | | - |
| 413 | return m_factor * screenSubfactor(platformScreen); | - |
| 414 | } | - |
| 415 | | - |
| 416 | qreal QHighDpiScaling::factor(const QWindow *window) | - |
| 417 | { | - |
| 418 | if (!m_active) | - |
| 419 | return qreal(1.0); | - |
| 420 | | - |
| 421 | return factor(window ? window->screen() : QGuiApplication::primaryScreen()); | - |
| 422 | } | - |
| 423 | | - |
| 424 | QPoint QHighDpiScaling::origin(const QScreen *screen) | - |
| 425 | { | - |
| 426 | return screen->geometry().topLeft(); | - |
| 427 | } | - |
| 428 | | - |
| 429 | QPoint QHighDpiScaling::origin(const QPlatformScreen *platformScreen) | - |
| 430 | { | - |
| 431 | return platformScreen->geometry().topLeft(); | - |
| 432 | } | - |
| 433 | | - |
| 434 | #endif //QT_NO_HIGHDPISCALING | - |
| 435 | QT_END_NAMESPACE | - |
| | |