9#include "private/qscreen_p.h"
10#include <private/qguiapplication_p.h>
12#include <QtCore/qdebug.h>
13#include <QtCore/qmetaobject.h>
22#ifndef QT_NO_HIGHDPISCALING
51 auto opt =
ok ? std::optional(
value) : std::nullopt;
62 return ok ? std::optional(
value) : std::nullopt;
240qreal QHighDpiScaling::m_factor = 1.0;
241bool QHighDpiScaling::m_active =
false;
242bool QHighDpiScaling::m_usePlatformPluginDpi =
false;
243bool QHighDpiScaling::m_platformPluginDpiScalingActive =
false;
244bool QHighDpiScaling::m_globalScalingActive =
false;
245bool QHighDpiScaling::m_screenFactorSet =
false;
246bool QHighDpiScaling::m_usePhysicalDpi =
false;
255 QDpi platformBaseDpi =
screen->logicalBaseDpi();
256 if (QHighDpiScaling::m_usePhysicalDpi) {
269template <
class EnumType>
276template <
class EnumType>
282template <
class EnumType>
286 for (; i1 < i2; ++i1) {
345 qreal roundedFactor = rawFactor;
346 switch (scaleFactorRoundingPolicy) {
348 roundedFactor =
qRound(rawFactor);
351 roundedFactor =
qCeil(rawFactor);
354 roundedFactor =
qFloor(rawFactor);
358 roundedFactor = rawFactor -
qFloor(rawFactor) < 0.75
368 roundedFactor =
qMax(roundedFactor,
qreal(1));
370 return roundedFactor;
384 const QDpi baseDpi =
screen->logicalBaseDpi();
385 const qreal dpiAdjustmentFactor = rawFactor / roundedFactor;
393 return QDpi(baseDpi.first * dpiAdjustmentFactor, baseDpi.second * dpiAdjustmentFactor);
404 qCDebug(lcHighDpi) <<
"Initializing high-DPI scaling";
407 static const char* envDebugStr =
"environment variable set:";
409 if (envEnableHighDpiScaling.has_value())
413 if (envScaleFactor.has_value())
417 if (envScreenFactors.has_value())
421 if (envUsePhysicalDpi.has_value())
425 if (envScaleFactorRoundingPolicy.has_value())
429 if (envDpiAdjustmentPolicy.has_value())
433 m_usePlatformPluginDpi = envEnableHighDpiScaling.value_or(1) > 0;
434 m_platformPluginDpiScalingActive =
false;
437 m_factor = envScaleFactor.value_or(
qreal(1));
444 m_screenFactors = parseScreenScaleFactorsSpec(
QStringView{screenFactorsSpec});
445 m_namedScreenScaleFactors.
clear();
447 m_usePhysicalDpi = envUsePhysicalDpi.value_or(0) > 0;
450 if (envScaleFactorRoundingPolicy.has_value()) {
451 QByteArray policyText = envScaleFactorRoundingPolicy.value();
458 qWarning(
"Unknown scale factor rounding policy: %s. Supported values are: %s.",
464 if (envDpiAdjustmentPolicy.has_value()) {
465 QByteArray policyText = envDpiAdjustmentPolicy.value();
468 QHighDpiScaling::m_dpiAdjustmentPolicy = policyEnumValue;
472 qWarning(
"Unknown DPI adjustment policy: %s. Supported values are: %s.",
478 m_active = m_globalScalingActive || m_usePlatformPluginDpi;
480 qCDebug(lcHighDpi) <<
"Initialization done, high-DPI scaling is"
481 << (m_active ?
"active" :
"inactive");
490 qCDebug(lcHighDpi) <<
"Updating high-DPI scaling";
493 if (m_screenFactors.size() > 0) {
494 qCDebug(lcHighDpi) <<
"Applying screen factors" << m_screenFactors;
497 for (
const auto &[
name, rawFactor]: m_screenFactors) {
501 if (
i < screens.size())
516 if (m_usePlatformPluginDpi && !m_platformPluginDpiScalingActive ) {
520 m_platformPluginDpiScalingActive =
true;
526 m_active = m_globalScalingActive || m_screenFactorSet || m_platformPluginDpiScalingActive;
528 qCDebug(lcHighDpi) <<
"Update done, high-DPI scaling is"
529 << (m_active ?
"active" :
"inactive");
537 qCDebug(lcHighDpi) <<
"Setting global scale factor to" <<
factor;
542 qWarning(
"QHighDpiScaling::setFactor: Should only be called when no windows exist.");
546 std::vector<QScreenPrivate::UpdateEmitter> updateEmitters;
548 updateEmitters.emplace_back(
screen);
551 m_factor = m_globalScalingActive ?
factor :
qreal(1);
552 m_active = m_globalScalingActive || m_screenFactorSet || m_platformPluginDpiScalingActive ;
554 screen->d_func()->updateGeometry();
567 m_screenFactorSet =
true;
581 screen->d_func()->updateGeometry();
590 return (
pos - topLeft) * scaleFactor + topLeft;
599 return (
pos - topLeft) / scaleFactor + topLeft;
612 bool screenPropertyUsed =
false;
613 if (m_screenFactorSet) {
617 if (
auto qScreen =
screen->screen()) {
619 if (screenPropertyUsed)
623 if (!screenPropertyUsed) {
625 if ((screenPropertyUsed = byNameIt != QHighDpiScaling::m_namedScreenScaleFactors.
cend()))
630 if (!screenPropertyUsed && m_usePlatformPluginDpi)
642 if (!m_usePlatformPluginDpi) {
645 return QDpi{ dpi.first / screenScaleFactor, dpi.second / screenScaleFactor };
650 return effectiveLogicalDpi(
screen->
handle(), scaleFactor, roundedScaleFactor);
663 return maybeScreen->handle();
665 return primary->handle();
675 ? platformScreen->geometry().contains(
position.point)
676 : platformScreen->screen()->geometry().contains(
position.point);
680 if (onScreen(
position, platformGuess))
681 return platformGuess->
screen();
700 const auto specs = screenScaleFactors.
split(u
';');
701 for (
const auto &spec : specs) {
703 if (equalsPos == -1) {
715 screenFactors.
append(QHighDpiScaling::ScreenFactor(spec.left(equalsPos).toString(),
factor));
720 return screenFactors;
729 return { m_factor,
QPoint() };
730 return { m_factor * screenSubfactor(platformScreen), platformScreen->
geometry().
topLeft() };
739 return { m_factor,
QPoint() };
752 QScreen *targetScreen = overrideScreen ? overrideScreen :
screen;
756#ifndef QT_NO_DEBUG_STREAM
761 if (!factor.name.isEmpty())
762 debug << factor.name <<
"=";
763 debug << factor.factor;
789#include "moc_qhighdpiscaling_p.cpp"
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
static void setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy policy)
static QWindowList allWindows()
Returns a list of all the windows in the application.
QScreen * primaryScreen
the primary (or default) screen of the application.
static QList< QScreen * > screens()
Returns a list of all the screens associated with the windowing system the application is connected t...
static Qt::HighDpiScaleFactorRoundingPolicy highDpiScaleFactorRoundingPolicy()
const_iterator constFind(const Key &key) const noexcept
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
static qreal roundScaleFactor(qreal rawFactor)
static void setGlobalFactor(qreal factor)
static qreal factor(C *context)
static ScaleAndOrigin scaleAndOrigin(const QPlatformScreen *platformScreen, Point position=Point{ Point::Invalid, QPoint() })
static QPoint mapPositionToNative(const QPoint &pos, const QPlatformScreen *platformScreen)
static QDpi logicalDpi(const QScreen *screen)
static QPoint mapPositionFromNative(const QPoint &pos, const QPlatformScreen *platformScreen)
static void updateHighDpiScaling()
static void initHighDpiScaling()
static void setScreenFactor(QScreen *screen, qreal factor)
void append(parameter_type t)
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
\inmodule QtCore\reentrant
constexpr QPoint topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
constexpr QSize size() const noexcept
Returns the size of the rectangle.
The QScreen class is used to query screen properties. \inmodule QtGui.
QSizeF physicalSize
the screen's physical size (in millimeters)
QRect geometry
the screen's geometry in pixels
QString name
a user presentable string representing the screen
QPlatformScreen * handle() const
Get the platform screen handle.
constexpr qreal width() const noexcept
Returns the width.
constexpr qreal height() const noexcept
Returns the height.
constexpr int height() const noexcept
Returns the height.
constexpr int width() const noexcept
Returns the width.
Q_CORE_EXPORT QList< QStringView > split(QStringView sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the view into substring views wherever sep occurs, and returns the list of those string views.
\macro QT_RESTRICTED_CAST_FROM_ASCII
double toDouble(bool *ok=nullptr) const
Returns the string converted to a double value.
QSet< QString >::iterator it
const PluginKeyMapConstIterator cend
Combined button and popup list for selecting options.
HighDpiScaleFactorRoundingPolicy
#define QByteArrayLiteral(str)
Q_CORE_EXPORT int qstricmp(const char *, const char *)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
int qRound(qfloat16 d) noexcept
static const char usePhysicalDpiEnvVar[]
static bool operator==(const EnumLookup< EnumType > &e1, const EnumLookup< EnumType > &e2)
static std::optional< qreal > qEnvironmentVariableOptionalReal(const char *name)
static Qt::HighDpiScaleFactorRoundingPolicy lookupScaleFactorRoundingPolicy(const QByteArray &v)
static const char scaleFactorEnvVar[]
static const char enableHighDpiScalingEnvVar[]
static const DpiAdjustmentPolicyLookup dpiAdjustmentPolicyLookup[]
static const char dpiAdjustmentPolicyEnvVar[]
static const ScaleFactorRoundingPolicyLookup scaleFactorRoundingPolicyLookup[]
static const char scaleFactorRoundingPolicyEnvVar[]
QDebug operator<<(QDebug debug, const QHighDpiScaling::ScreenFactor &factor)
static std::optional< QString > qEnvironmentVariableOptionalString(const char *name)
static std::optional< int > qEnvironmentVariableOptionalInt(const char *name)
static const char screenFactorsEnvVar[]
static std::optional< QByteArray > qEnvironmentVariableOptionalByteArray(const char *name)
static QByteArray joinEnumValues(const EnumLookup< EnumType > *i1, const EnumLookup< EnumType > *i2)
static const char scaleFactorProperty[]
static QHighDpiScaling::DpiAdjustmentPolicy lookupDpiAdjustmentPolicy(const QByteArray &v)
QPair< qreal, qreal > QDpi
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
constexpr const T & qMax(const T &a, const T &b)
GLenum GLsizei GLsizei GLint * values
[15]
GLsizei const GLfloat * v
[13]
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
QString qEnvironmentVariable(const char *varName, const QString &defaultValue)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
qsizetype lastIndexOf(const AT &t, qsizetype from=-1) const noexcept