| 1 |
// Copyright (C) 2020 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtCore/qtversion.h> |
| 5 |
#include <QtGui/qpa/qplatformscreen.h> |
| 6 |
#include <QtGui/qpa/qplatformwindow.h> |
| 7 |
#include <QtGui/private/qhighdpiscaling_p.h> |
| 8 |
#include <QtWidgets/QtWidgets> |
| 9 |
#include <iostream> |
| 10 |
|
| 11 |
/* |
| 12 |
DprGadget: The focused High-DPI settings debug utility |
| 13 |
|
| 14 |
DprGadget displays the device pixel ratio ("DPR") for the screen |
| 15 |
it's on in a large font, as well as the inputs (from the platform |
| 16 |
plugin or environment) currently in use for determinging the DPR. |
| 17 |
|
| 18 |
Non-relevant inputs are not displayed. See qttools/src/qtdiag for |
| 19 |
an utility which displays all inputs. |
| 20 |
*/ |
| 21 |
|
| 22 |
bool g_qtScaleFactor = false; |
| 23 |
bool g_qtUsePhysicalDpi = false; |
| 24 |
bool g_qtFontDpi = false; |
| 25 |
bool g_qtScaleFactorRoundingPolicy = false; |
| 26 |
bool g_qtHighDpiDownscale = false; |
| 27 |
bool g_displayEvents = false; |
| 28 |
|
| 29 |
|
| 30 |
class DprGadget : public QWidget |
| 31 |
{ |
| 32 |
public: |
| 33 |
std::function<void(void)> m_clearFn; |
| 34 |
std::function<void(void)> m_updateFn; |
| 35 |
qreal m_currentDpr = -1; |
| 36 |
QString m_eventsText; |
| 37 |
|
| 38 |
DprGadget() { |
| 39 |
setWindowTitle(QString("DprGadget - Qt %1").arg(qVersion())); |
| 40 |
|
| 41 |
QFont tinyFont; |
| 42 |
tinyFont.setPointSize(8); |
| 43 |
QFont smallFont; |
| 44 |
smallFont.setPointSize(12); |
| 45 |
QFont bigFont; |
| 46 |
bigFont.setPointSize(42); |
| 47 |
QFont biggerFont; |
| 48 |
biggerFont.setPointSize(80); |
| 49 |
|
| 50 |
QLabel *dprLabel = new QLabel("Device Pixel Ratio"); |
| 51 |
dprLabel->setFont(bigFont); |
| 52 |
dprLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 53 |
|
| 54 |
QLabel *dprValue = new QLabel(); |
| 55 |
dprValue->setFont(biggerFont); |
| 56 |
dprValue->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 57 |
|
| 58 |
QLabel *screenLabel = new QLabel("Current Screen:"); |
| 59 |
screenLabel->setFont(smallFont); |
| 60 |
|
| 61 |
QLabel *sizeLabel = new QLabel("Window size:"); |
| 62 |
sizeLabel->setFont(smallFont); |
| 63 |
sizeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 64 |
|
| 65 |
QLabel *nativeSizeLabel = new QLabel("Native:"); |
| 66 |
nativeSizeLabel->setFont(smallFont); |
| 67 |
nativeSizeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 68 |
|
| 69 |
QLabel *dpiLabel = new QLabel("Logical DPI:"); |
| 70 |
dpiLabel->setFont(smallFont); |
| 71 |
dpiLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 72 |
|
| 73 |
QLabel *windowDpiLabel = new QLabel("Window DPI:"); |
| 74 |
windowDpiLabel->setFont(smallFont); |
| 75 |
windowDpiLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 76 |
|
| 77 |
QLabel *platformDpiLabel = new QLabel("Native Device Pixel Ratio:"); |
| 78 |
platformDpiLabel->setFont(smallFont); |
| 79 |
platformDpiLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 80 |
|
| 81 |
QLabel *windowDprLabel = new QLabel("Window DPR:"); |
| 82 |
windowDprLabel->setFont(smallFont); |
| 83 |
windowDprLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 84 |
|
| 85 |
QLabel *plarformDprLabel = new QLabel("Native DPI:"); |
| 86 |
plarformDprLabel->setFont(smallFont); |
| 87 |
plarformDprLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 88 |
|
| 89 |
QLabel *qtScaleFactorLabel = new QLabel("Qt Internal Scale Factor:"); |
| 90 |
qtScaleFactorLabel->setFont(smallFont); |
| 91 |
|
| 92 |
QLabel *eventsLabel = new QLabel(m_eventsText); |
| 93 |
eventsLabel->setFont(tinyFont); |
| 94 |
eventsLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 95 |
|
| 96 |
QVBoxLayout *layout = new QVBoxLayout(); |
| 97 |
layout->addWidget(dprLabel); |
| 98 |
layout->setAlignment(dprLabel, Qt::AlignHCenter); |
| 99 |
layout->addWidget(dprValue); |
| 100 |
layout->setAlignment(dprValue, Qt::AlignHCenter); |
| 101 |
|
| 102 |
bool displayLogicalDpi = false; |
| 103 |
if (displayLogicalDpi) |
| 104 |
layout->addWidget(dpiLabel); |
| 105 |
|
| 106 |
layout->addStretch(); |
| 107 |
|
| 108 |
QHBoxLayout *screenLabelLayout = new QHBoxLayout(); |
| 109 |
screenLabelLayout->addStretch(); |
| 110 |
screenLabelLayout->addWidget(screenLabel); |
| 111 |
screenLabelLayout->addStretch(); |
| 112 |
layout->addLayout(screenLabelLayout); |
| 113 |
|
| 114 |
QHBoxLayout *windowSizeLayout = new QHBoxLayout(); |
| 115 |
windowSizeLayout->addWidget(sizeLabel); |
| 116 |
windowSizeLayout->addStretch(); |
| 117 |
windowSizeLayout->addWidget(nativeSizeLabel); |
| 118 |
layout->addLayout(windowSizeLayout); |
| 119 |
|
| 120 |
QHBoxLayout *dpiLayout = new QHBoxLayout(); |
| 121 |
dpiLayout->addWidget(windowDpiLabel); |
| 122 |
dpiLayout->addStretch(); |
| 123 |
dpiLayout->addWidget(platformDpiLabel); |
| 124 |
layout->addLayout(dpiLayout); |
| 125 |
|
| 126 |
QHBoxLayout *dprLayout = new QHBoxLayout(); |
| 127 |
dprLayout->addWidget(windowDprLabel); |
| 128 |
dprLayout->addStretch(); |
| 129 |
dprLayout->addWidget(plarformDprLabel); |
| 130 |
layout->addLayout(dprLayout); |
| 131 |
|
| 132 |
QHBoxLayout *qtScaleFactorLabelLayout = new QHBoxLayout(); |
| 133 |
qtScaleFactorLabelLayout->addStretch(); |
| 134 |
qtScaleFactorLabelLayout->addWidget(qtScaleFactorLabel); |
| 135 |
qtScaleFactorLabelLayout->addStretch(); |
| 136 |
layout->addLayout(qtScaleFactorLabelLayout); |
| 137 |
|
| 138 |
if (g_displayEvents) |
| 139 |
layout->addWidget(eventsLabel); |
| 140 |
|
| 141 |
bool activeEnvironment = g_qtScaleFactor || g_qtUsePhysicalDpi || g_qtFontDpi || g_qtScaleFactorRoundingPolicy || g_qtHighDpiDownscale; |
| 142 |
if (activeEnvironment) { |
| 143 |
layout->addWidget(new QLabel("Active Environment:")); |
| 144 |
if (g_qtScaleFactor) { |
| 145 |
QString text = QString("QT_SCALE_FACTOR=") + qgetenv("QT_SCALE_FACTOR"); |
| 146 |
layout->addWidget(new QLabel(text)); |
| 147 |
} |
| 148 |
if (g_qtUsePhysicalDpi) { |
| 149 |
QString text = QString("QT_USE_PHYSICAL_DPI=") + qgetenv("QT_USE_PHYSICAL_DPI"); |
| 150 |
layout->addWidget(new QLabel(text)); |
| 151 |
} |
| 152 |
if (g_qtFontDpi) { |
| 153 |
QString text = QString("QT_FONT_DPI=") + qgetenv("QT_FONT_DPI"); |
| 154 |
layout->addWidget(new QLabel(text)); |
| 155 |
} |
| 156 |
if (g_qtScaleFactorRoundingPolicy) { |
| 157 |
QString text = QString("QT_SCALE_FACTOR_ROUNDING_POLICY=") + qgetenv("QT_SCALE_FACTOR_ROUNDING_POLICY"); |
| 158 |
layout->addWidget(new QLabel(text)); |
| 159 |
} |
| 160 |
if (g_qtHighDpiDownscale) { |
| 161 |
QString text = QString("QT_WIDGETS_HIGHDPI_DOWNSCALE=") + qgetenv("QT_WIDGETS_HIGHDPI_DOWNSCALE"); |
| 162 |
layout->addWidget(new QLabel(text)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
auto updateValues = [=]() { |
| 167 |
dprValue->setText(QString("%1").arg(devicePixelRatioF())); |
| 168 |
windowDpiLabel->setText(QString("QWindow DPI: %1").arg(logicalDpiX())); |
| 169 |
dpiLabel->setText(QString("Logical DPI: %1").arg(logicalDpiX())); |
| 170 |
sizeLabel->setText(QString("QWindow size: %1 %2").arg(width()).arg(height())); |
| 171 |
|
| 172 |
QPlatformWindow *platformWindow = windowHandle()->handle(); |
| 173 |
nativeSizeLabel->setText(QString("native size %1 %2").arg(platformWindow->geometry().width()) |
| 174 |
.arg(platformWindow->geometry().height())); |
| 175 |
QPlatformScreen *pscreen = screen()->handle(); |
| 176 |
if (g_qtUsePhysicalDpi) { |
| 177 |
int physicalDpi = qRound(pscreen->geometry().width() / pscreen->physicalSize().width() * qreal(25.4)); |
| 178 |
platformDpiLabel->setText(QString("Native Physical DPI: %1").arg(physicalDpi)); |
| 179 |
} else { |
| 180 |
platformDpiLabel->setText(QString("native logical DPI: %1").arg(pscreen->logicalDpi().first)); |
| 181 |
} |
| 182 |
|
| 183 |
windowDprLabel->setText(QString("QWindow DPR: %1").arg(windowHandle()->devicePixelRatio())); |
| 184 |
plarformDprLabel->setText(QString("native DPR: %1").arg(pscreen->devicePixelRatio())); |
| 185 |
|
| 186 |
screenLabel->setText(QString("Current Screen: %1").arg(screen()->name())); |
| 187 |
qtScaleFactorLabel->setText(QString("Qt Internal Scale Factor: %1").arg(QHighDpiScaling::factor(windowHandle()))); |
| 188 |
eventsLabel->setText(QString(m_eventsText)); |
| 189 |
}; |
| 190 |
m_updateFn = updateValues; |
| 191 |
|
| 192 |
m_clearFn = [=]() { |
| 193 |
dprValue->setText(QString("")); |
| 194 |
m_eventsText.clear(); |
| 195 |
}; |
| 196 |
|
| 197 |
create(); |
| 198 |
|
| 199 |
QObject::connect(this->windowHandle(), &QWindow::screenChanged, [updateValues, this](QScreen *screen){ |
| 200 |
Q_UNUSED(screen); |
| 201 |
this->m_eventsText.prepend(QString("ScreenChange ")); |
| 202 |
this->m_eventsText.truncate(80); |
| 203 |
updateValues(); |
| 204 |
}); |
| 205 |
|
| 206 |
setLayout(layout); |
| 207 |
|
| 208 |
updateValues(); |
| 209 |
} |
| 210 |
|
| 211 |
void paintEvent(QPaintEvent *) override { |
| 212 |
|
| 213 |
// Update the UI in the paint event - normally not good |
| 214 |
// practice but it looks like we can get away with it here |
| 215 |
this->m_eventsText.prepend(QString("Paint ")); |
| 216 |
this->m_eventsText.truncate(80); |
| 217 |
|
| 218 |
// Dpr change should trigger a repaint, update display values here |
| 219 |
if (m_currentDpr == devicePixelRatioF()) |
| 220 |
return; |
| 221 |
m_currentDpr = devicePixelRatioF(); |
| 222 |
|
| 223 |
m_updateFn(); |
| 224 |
} |
| 225 |
|
| 226 |
void resizeEvent(QResizeEvent *event) override { |
| 227 |
QSize size = event->size(); |
| 228 |
m_eventsText.prepend(QString("Resize(%1 %2) ").arg(size.width()).arg(size.height())); |
| 229 |
m_eventsText.truncate(80); |
| 230 |
m_updateFn(); |
| 231 |
} |
| 232 |
|
| 233 |
void mousePressEvent(QMouseEvent *) override { |
| 234 |
m_clearFn(); |
| 235 |
QTimer::singleShot(500, this, [this](){ |
| 236 |
m_updateFn(); |
| 237 |
}); |
| 238 |
} |
| 239 |
}; |
| 240 |
|
| 241 |
int main(int argc, char **argv) { |
| 242 |
|
| 243 |
// Set sensible defaults |
| 244 |
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); |
| 245 |
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); |
| 246 |
|
| 247 |
// Display any set high-dpi eviornment variable. |
| 248 |
g_qtScaleFactor = qEnvironmentVariableIsSet("QT_SCALE_FACTOR"); |
| 249 |
g_qtUsePhysicalDpi = qgetenv("QT_USE_PHYSICAL_DPI") == QByteArray("1"); |
| 250 |
g_qtFontDpi = qEnvironmentVariableIsSet("QT_FONT_DPI"); |
| 251 |
g_qtScaleFactorRoundingPolicy = qEnvironmentVariableIsSet("QT_SCALE_FACTOR_ROUNDING_POLICY"); |
| 252 |
g_qtHighDpiDownscale = qEnvironmentVariableIsSet("QT_WIDGETS_HIGHDPI_DOWNSCALE"); |
| 253 |
|
| 254 |
QApplication app(argc, argv); |
| 255 |
|
| 256 |
DprGadget dprGadget; |
| 257 |
dprGadget.show(); |
| 258 |
|
| 259 |
return app.exec(); |
| 260 |
} |