qhighdpiscaling.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qhighdpiscaling.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5const QLoggingCategory &lcScaling() { static const QLoggingCategory category("qt.scaling"); return
never executed: return category;
category;
never executed: return category;
};
0
6-
7-
8static const char legacyDevicePixelEnvVar[] = "QT_DEVICE_PIXEL_RATIO";-
9static const char scaleFactorEnvVar[] = "QT_SCALE_FACTOR";-
10static const char autoScreenEnvVar[] = "QT_AUTO_SCREEN_SCALE_FACTOR";-
11static const char screenFactorsEnvVar[] = "QT_SCREEN_SCALE_FACTORS";-
12-
13static inline qreal initialGlobalScaleFactor()-
14{-
15-
16 qreal result = 1;-
17 if (qEnvironmentVariableIsSet(scaleFactorEnvVar)
qEnvironmentVa...eFactorEnvVar)Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
18 bool ok;-
19 const qreal f = qgetenv(scaleFactorEnvVar).toDouble(&ok);-
20 if (ok
okDescription
TRUEnever evaluated
FALSEnever evaluated
&& f > 0
f > 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
21 for (bool qt_category_enabled = lcScaling().isDebugEnabled(); qt_category_enabled
qt_category_enabledDescription
TRUEnever evaluated
FALSEnever evaluated
; qt_category_enabled = false) QMessageLogger(__FILE__, 66, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() << "Apply " << scaleFactorEnvVar << f;
never executed: QMessageLogger(__FILE__, 66, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() << "Apply " << scaleFactorEnvVar << f;
0
22 result = f;-
23 }
never executed: end of block
0
24 }
never executed: end of block
else {
0
25 if (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar)
qEnvironmentVa...cePixelEnvVar)Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
26 QMessageLogger(__FILE__, 71, __PRETTY_FUNCTION__).warning() << "Warning:" << legacyDevicePixelEnvVar << "is deprecated. Instead use:" << endl-
27 << " " << autoScreenEnvVar << "to enable platform plugin controlled per-screen factors." << endl-
28 << " " << screenFactorsEnvVar << "to set per-screen factors." << endl-
29 << " " << scaleFactorEnvVar << "to set the application global scale factor.";-
30-
31 int dpr = qEnvironmentVariableIntValue(legacyDevicePixelEnvVar);-
32 if (dpr > 0
dpr > 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
33 result = dpr;
never executed: result = dpr;
0
34 }
never executed: end of block
0
35 }
never executed: end of block
0
36 return
never executed: return result;
result;
never executed: return result;
0
37}-
38qreal QHighDpiScaling::m_factor = 1.0;-
39bool QHighDpiScaling::m_active = false;-
40bool QHighDpiScaling::m_usePixelDensity = false;-
41bool QHighDpiScaling::m_pixelDensityScalingActive = false;-
42bool QHighDpiScaling::m_globalScalingActive = false;-
43bool QHighDpiScaling::m_screenFactorSet = false;-
44QDpi QHighDpiScaling::m_logicalDpi = QDpi(-1,-1);-
45-
46-
47-
48-
49-
50-
51static inline bool usePixelDensity()-
52{-
53-
54-
55-
56 if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling)
QCoreApplicati...ighDpiScaling)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
57 return
never executed: return false;
false;
never executed: return false;
0
58 bool screenEnvValueOk;-
59 const int screenEnvValue = qEnvironmentVariableIntValue(autoScreenEnvVar, &screenEnvValueOk);-
60 if (screenEnvValueOk
screenEnvValueOkDescription
TRUEnever evaluated
FALSEnever evaluated
&& screenEnvValue < 1
screenEnvValue < 1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
61 return
never executed: return false;
false;
never executed: return false;
0
62 return
never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling)
never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
0
63 || (screenEnvValueOk && screenEnvValue > 0)
never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
0
64 || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
never executed: return QCoreApplication::testAttribute(Qt::AA_EnableHighDpiScaling) || (screenEnvValueOk && screenEnvValue > 0) || (qEnvironmentVariableIsSet(legacyDevicePixelEnvVar) && qgetenv(legacyDevicePixelEnvVar).toLower() == "auto");
0
65}-
66-
67void QHighDpiScaling::initHighDpiScaling()-
68{-
69-
70 m_factor = initialGlobalScaleFactor();-
71 m_globalScalingActive = !qFuzzyCompare(m_factor, qreal(1));-
72-
73 m_usePixelDensity = usePixelDensity();-
74-
75 m_pixelDensityScalingActive = false;-
76-
77-
78-
79 m_active = m_globalScalingActive
m_globalScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
|| m_usePixelDensity
m_usePixelDensityDescription
TRUEnever evaluated
FALSEnever evaluated
;
0
80}
never executed: end of block
0
81-
82void QHighDpiScaling::updateHighDpiScaling()-
83{-
84 if (QCoreApplication::testAttribute(Qt::AA_DisableHighDpiScaling)
QCoreApplicati...ighDpiScaling)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
85 return;
never executed: return;
0
86-
87 if (m_usePixelDensity
m_usePixelDensityDescription
TRUEnever evaluated
FALSEnever evaluated
&& !m_pixelDensityScalingActive
!m_pixelDensityScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
88 const auto screens = QGuiApplication::screens();-
89 for (QScreen *screen : screens) {-
90 if (!qFuzzyCompare(screenSubfactor(screen->handle()), qreal(1))
!qFuzzyCompare...()), qreal(1))Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
91 m_pixelDensityScalingActive = true;-
92 break;
never executed: break;
0
93 }-
94 }
never executed: end of block
0
95 }
never executed: end of block
0
96 if (qEnvironmentVariableIsSet(screenFactorsEnvVar)
qEnvironmentVa...FactorsEnvVar)Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
97 int i = 0;-
98 const auto specs = qgetenv(screenFactorsEnvVar).split(';');-
99 for (const QByteArray &spec : specs) {-
100 QScreen *screen = 0;-
101 int equalsPos = spec.lastIndexOf('=');-
102 double factor = 0;-
103 if (equalsPos > 0
equalsPos > 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
104-
105 QByteArray name = spec.mid(0, equalsPos);-
106 QByteArray f = spec.mid(equalsPos + 1);-
107 bool ok;-
108 factor = f.toDouble(&ok);-
109 if (ok
okDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
110 const auto screens = QGuiApplication::screens();-
111 for (QScreen *s : screens) {-
112 if (s->name() == QString::fromLocal8Bit(name)
s->name() == Q...ocal8Bit(name)Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
113 screen = s;-
114 break;
never executed: break;
0
115 }-
116 }
never executed: end of block
0
117 }
never executed: end of block
0
118 }
never executed: end of block
else {
0
119-
120 bool ok;-
121 factor = spec.toDouble(&ok);-
122 if (ok
okDescription
TRUEnever evaluated
FALSEnever evaluated
&& i < QGuiApplication::screens().count()
i < QGuiApplic...eens().count()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
123 screen = QGuiApplication::screens().at(i);
never executed: screen = QGuiApplication::screens().at(i);
0
124 }
never executed: end of block
0
125 if (screen
screenDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
126 setScreenFactor(screen, factor);
never executed: setScreenFactor(screen, factor);
0
127 ++i;-
128 }
never executed: end of block
0
129 }
never executed: end of block
0
130 m_active = m_globalScalingActive
m_globalScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
|| m_screenFactorSet
m_screenFactorSetDescription
TRUEnever evaluated
FALSEnever evaluated
|| m_pixelDensityScalingActive
m_pixelDensityScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
;
0
131-
132 QPlatformScreen *primaryScreen = QGuiApplication::primaryScreen()->handle();-
133 qreal sf = screenSubfactor(primaryScreen);-
134 QDpi primaryDpi = primaryScreen->logicalDpi();-
135 m_logicalDpi = QDpi(primaryDpi.first / sf, primaryDpi.second / sf);-
136}
never executed: end of block
0
137-
138-
139-
140-
141void QHighDpiScaling::setGlobalFactor(qreal factor)-
142{-
143 if (qFuzzyCompare(factor, m_factor)
qFuzzyCompare(...tor, m_factor)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
144 return;
never executed: return;
0
145 if (!QGuiApplication::allWindows().isEmpty()
!QGuiApplicati...ws().isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
146 QMessageLogger(__FILE__, 328, __PRETTY_FUNCTION__).warning("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
147-
148 m_globalScalingActive = !qFuzzyCompare(factor, qreal(1));-
149 m_factor = m_globalScalingActive
m_globalScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
? factor : qreal(1);
0
150 m_active = m_globalScalingActive
m_globalScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
|| m_screenFactorSet
m_screenFactorSetDescription
TRUEnever evaluated
FALSEnever evaluated
|| m_pixelDensityScalingActive
m_pixelDensityScalingActiveDescription
TRUEnever evaluated
FALSEnever evaluated
;
0
151 const auto screens = QGuiApplication::screens();-
152 for (QScreen *screen : screens)-
153 screen->d_func()->updateHighDpi();
never executed: screen->d_func()->updateHighDpi();
0
154}
never executed: end of block
0
155-
156static const char scaleFactorProperty[] = "_q_scaleFactor";-
157-
158-
159-
160-
161void QHighDpiScaling::setScreenFactor(QScreen *screen, qreal factor)-
162{-
163 if (!qFuzzyCompare(factor, qreal(1))
!qFuzzyCompare...tor, qreal(1))Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
164 m_screenFactorSet = true;-
165 m_active = true;-
166 }
never executed: end of block
0
167 screen->setProperty(scaleFactorProperty, QVariant(factor));-
168-
169-
170 if (screen->handle()
screen->handle()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
171 screen->d_func()->setPlatformScreen(screen->handle());
never executed: screen->d_func()->setPlatformScreen(screen->handle());
0
172}
never executed: end of block
0
173-
174QPoint QHighDpiScaling::mapPositionToNative(const QPoint &pos, const QPlatformScreen *platformScreen)-
175{-
176 if (!platformScreen
!platformScreenDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
177 return
never executed: return pos;
pos;
never executed: return pos;
0
178 const qreal scaleFactor = factor(platformScreen);-
179 const QPoint topLeft = platformScreen->geometry().topLeft();-
180 return
never executed: return (pos - topLeft) * scaleFactor + topLeft;
(pos - topLeft) * scaleFactor + topLeft;
never executed: return (pos - topLeft) * scaleFactor + topLeft;
0
181}-
182-
183QPoint QHighDpiScaling::mapPositionFromNative(const QPoint &pos, const QPlatformScreen *platformScreen)-
184{-
185 if (!platformScreen
!platformScreenDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
186 return
never executed: return pos;
pos;
never executed: return pos;
0
187 const qreal scaleFactor = factor(platformScreen);-
188 const QPoint topLeft = platformScreen->geometry().topLeft();-
189 return
never executed: return (pos - topLeft) / scaleFactor + topLeft;
(pos - topLeft) / scaleFactor + topLeft;
never executed: return (pos - topLeft) / scaleFactor + topLeft;
0
190}-
191-
192qreal QHighDpiScaling::screenSubfactor(const QPlatformScreen *screen)-
193{-
194 qreal factor = qreal(1.0);-
195 if (screen
screenDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
196 if (m_usePixelDensity
m_usePixelDensityDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
197 factor *= screen->pixelDensity();
never executed: factor *= screen->pixelDensity();
0
198 if (m_screenFactorSet
m_screenFactorSetDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
199 QVariant screenFactor = screen->screen()->property(scaleFactorProperty);-
200 if (screenFactor.isValid()
screenFactor.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
201 factor *= screenFactor.toReal();
never executed: factor *= screenFactor.toReal();
0
202 }
never executed: end of block
0
203 }
never executed: end of block
0
204 return
never executed: return factor;
factor;
never executed: return factor;
0
205}-
206-
207QDpi QHighDpiScaling::logicalDpi()-
208{-
209 return
never executed: return m_logicalDpi;
m_logicalDpi;
never executed: return m_logicalDpi;
0
210}-
211-
212qreal QHighDpiScaling::factor(const QScreen *screen)-
213{-
214-
215 if (!m_active
!m_activeDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
216 return
never executed: return qreal(1.0);
qreal(1.0);
never executed: return qreal(1.0);
0
217-
218-
219-
220 qreal factor = m_factor;-
221 if (screen
screenDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
222 factor *= screenSubfactor(screen->handle());
never executed: factor *= screenSubfactor(screen->handle());
0
223 return
never executed: return factor;
factor;
never executed: return factor;
0
224}-
225-
226qreal QHighDpiScaling::factor(const QPlatformScreen *platformScreen)-
227{-
228 if (!m_active
!m_activeDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
229 return
never executed: return qreal(1.0);
qreal(1.0);
never executed: return qreal(1.0);
0
230-
231 return
never executed: return m_factor * screenSubfactor(platformScreen);
m_factor * screenSubfactor(platformScreen);
never executed: return m_factor * screenSubfactor(platformScreen);
0
232}-
233-
234qreal QHighDpiScaling::factor(const QWindow *window)-
235{-
236 if (!m_active
!m_activeDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
237 return
never executed: return qreal(1.0);
qreal(1.0);
never executed: return qreal(1.0);
0
238-
239 return
never executed: return factor(window ? window->screen() : QGuiApplication::primaryScreen());
factor(window ? window->screen() : QGuiApplication::primaryScreen());
never executed: return factor(window ? window->screen() : QGuiApplication::primaryScreen());
0
240}-
241-
242QPoint QHighDpiScaling::origin(const QScreen *screen)-
243{-
244 return
never executed: return screen->geometry().topLeft();
screen->geometry().topLeft();
never executed: return screen->geometry().topLeft();
0
245}-
246-
247QPoint QHighDpiScaling::origin(const QPlatformScreen *platformScreen)-
248{-
249 return
never executed: return platformScreen->geometry().topLeft();
platformScreen->geometry().topLeft();
never executed: return platformScreen->geometry().topLeft();
0
250}-
251-
252-
253-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9