| 1 |
// Copyright (C) 2021 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtGui> |
| 5 |
#include <QtWidgets> |
| 6 |
|
| 7 |
#include <QtGui/qpa/qplatformscreen.h> |
| 8 |
#include <QtGui/qpa/qplatformwindow.h> |
| 9 |
#include <QtGui/qpa/qplatformcursor.h> |
| 10 |
|
| 11 |
// This test app optionally doubles as a manual test for QScreen::mapToNative() |
| 12 |
// #define TEST_MAP_TO_NATIVE |
| 13 |
|
| 14 |
// ScreenDisplayer based on original impl from qtbase/tests/manual/highdpi |
| 15 |
class ScreenDisplayer : public QWidget |
| 16 |
{ |
| 17 |
public: |
| 18 |
ScreenDisplayer() = default; |
| 19 |
|
| 20 |
void setTitle() { |
| 21 |
|
| 22 |
QPoint deviceIndependentPos = pos(); |
| 23 |
QPlatformWindow *platformWindow = windowHandle()->handle(); |
| 24 |
QPoint nativePos = platformWindow->geometry().topLeft(); |
| 25 |
|
| 26 |
QString windowDebug = QString("pos ") |
| 27 |
+ QString("device independent %1 %2 ").arg(deviceIndependentPos.x()).arg(deviceIndependentPos.y()) |
| 28 |
+ QString("native %1 %2 ").arg(nativePos.x()).arg(nativePos.y()) |
| 29 |
; |
| 30 |
|
| 31 |
setWindowTitle(windowDebug); |
| 32 |
} |
| 33 |
|
| 34 |
void updateMapToNative(QPointF pos) |
| 35 |
{ |
| 36 |
#ifdef TEST_MAP_TO_NATIVE |
| 37 |
mappedToNative = QGuiApplication::mapToNative(QRectF(pos, QPointF(1,1))).topLeft(); // there |
| 38 |
mappedFromNative = QGuiApplication::mapFromNative(QRectF(mappedToNative, QPointF(1,1))).topLeft(); // and back again |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
void timerEvent(QTimerEvent *) override |
| 43 |
{ |
| 44 |
update(); |
| 45 |
setTitle(); |
| 46 |
} |
| 47 |
|
| 48 |
void mousePressEvent(QMouseEvent *) override |
| 49 |
{ |
| 50 |
displayMappedToNativeCursor = true; |
| 51 |
} |
| 52 |
|
| 53 |
void mouseMoveEvent(QMouseEvent *e) override |
| 54 |
{ |
| 55 |
setTitle(); |
| 56 |
updateMapToNative(e->globalPosition()); |
| 57 |
} |
| 58 |
|
| 59 |
void mouseReleaseEvent(QMouseEvent *) override |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
void showEvent(QShowEvent *) override |
| 64 |
{ |
| 65 |
refreshTimer.start(60, this); |
| 66 |
} |
| 67 |
|
| 68 |
void hideEvent(QHideEvent *) override |
| 69 |
{ |
| 70 |
refreshTimer.stop(); |
| 71 |
} |
| 72 |
|
| 73 |
void paintScreensInRect(QPainter &p, QRect rect, bool native) { |
| 74 |
p.save(); |
| 75 |
|
| 76 |
QRectF total; |
| 77 |
const auto screens = QGuiApplication::screens(); |
| 78 |
for (const QScreen *screen : screens) |
| 79 |
total |= native ? screen->handle()->geometry() : screen->geometry(); |
| 80 |
if (total.isEmpty()) |
| 81 |
return; |
| 82 |
|
| 83 |
qreal scaleMargin = 0.9; |
| 84 |
scaleFactor = scaleMargin * qMin(rect.width()/total.width(), rect.height()/total.height()); |
| 85 |
|
| 86 |
// p.fillRect(rect, Qt::black); |
| 87 |
int margin = 20; |
| 88 |
p.translate(margin, margin + rect.y()); |
| 89 |
p.scale(scaleFactor, scaleFactor); |
| 90 |
p.translate(-total.topLeft()); |
| 91 |
p.setPen(QPen(Qt::white, 10)); |
| 92 |
p.setBrush(Qt::gray); |
| 93 |
|
| 94 |
for (const QScreen *screen : screens) { |
| 95 |
QRect geometry = native ? screen->handle()->geometry() : screen->geometry(); |
| 96 |
p.drawRect(geometry); |
| 97 |
QFont f("Courier New"); |
| 98 |
f.setPixelSize(geometry.height() / 16); |
| 99 |
p.setFont(f); |
| 100 |
|
| 101 |
if (displayInfo) { |
| 102 |
QString text = "Name: " + screen->name() + "\n"; |
| 103 |
text += QString("\nGeometry: %1 %2 %3 %4 \n ").arg(geometry.x()).arg(geometry.y()).arg(geometry.width()).arg(geometry.height()); |
| 104 |
p.save(); |
| 105 |
p.translate(20, 0); |
| 106 |
p.drawText(geometry, Qt::AlignLeft | Qt::AlignVCenter, text); |
| 107 |
p.restore(); |
| 108 |
} |
| 109 |
} |
| 110 |
p.setBrush(QColor(200,220,255,127)); |
| 111 |
|
| 112 |
const auto topLevels = QApplication::topLevelWidgets(); |
| 113 |
for (QWidget *widget : topLevels) { |
| 114 |
if (!widget->isHidden()) |
| 115 |
p.drawRect(native ? widget->windowHandle()->handle()->geometry() : widget->geometry()); |
| 116 |
} |
| 117 |
|
| 118 |
QPolygon cursorShape; |
| 119 |
cursorShape << QPoint(0,0) << QPoint(20, 60) |
| 120 |
<< QPoint(30, 50) << QPoint(60, 80) |
| 121 |
<< QPoint(80, 60) << QPoint(50, 30) |
| 122 |
<< QPoint(60, 20); |
| 123 |
|
| 124 |
p.save(); |
| 125 |
p.translate(native ? qApp->primaryScreen()->handle()->cursor()->pos() : QCursor::pos()); |
| 126 |
p.drawPolygon(cursorShape); |
| 127 |
p.restore(); |
| 128 |
|
| 129 |
#ifdef TEST_MAP_TO_NATIVE |
| 130 |
// Draw red "mapped to native" cursor. We expect this |
| 131 |
// cursor to track the normal blue cursor if everything |
| 132 |
// works out ok. |
| 133 |
if (displayMappedToNativeCursor) { |
| 134 |
p.save(); |
| 135 |
p.setBrush(QColor(230,120,155,127)); |
| 136 |
p.translate(native ? mappedToNative: mappedFromNative); |
| 137 |
p.drawPolygon(cursorShape); |
| 138 |
p.restore(); |
| 139 |
} |
| 140 |
#endif |
| 141 |
|
| 142 |
p.restore(); |
| 143 |
} |
| 144 |
|
| 145 |
void paintEvent(QPaintEvent *) override |
| 146 |
{ |
| 147 |
QPainter p(this); |
| 148 |
|
| 149 |
QRect g = geometry(); |
| 150 |
int halfHeight = g.height() / 2; |
| 151 |
QRect topHalf = QRect(0, 0, g.width(), halfHeight); |
| 152 |
QRect bottomHalf = QRect(0, halfHeight, g.width(), halfHeight); |
| 153 |
|
| 154 |
if (displayDeviceIndependentGeometry) |
| 155 |
paintScreensInRect(p, topHalf, false); |
| 156 |
if (displayNativeGeometry) |
| 157 |
paintScreensInRect(p, bottomHalf, true); |
| 158 |
} |
| 159 |
|
| 160 |
bool displayInfo = false; |
| 161 |
bool displayDeviceIndependentGeometry = true; |
| 162 |
bool displayNativeGeometry = false; |
| 163 |
private: |
| 164 |
bool displayMappedToNativeCursor = false; |
| 165 |
QPointF mappedToNative; |
| 166 |
QPointF mappedFromNative; |
| 167 |
qreal scaleFactor = 1; |
| 168 |
QBasicTimer refreshTimer; |
| 169 |
}; |
| 170 |
|
| 171 |
class Controller : public QWidget { |
| 172 |
public: |
| 173 |
Controller(ScreenDisplayer *displayer) { |
| 174 |
setWindowTitle("Controller"); |
| 175 |
|
| 176 |
QVBoxLayout *layout = new QVBoxLayout(); |
| 177 |
setLayout(layout); |
| 178 |
|
| 179 |
layout->addWidget(new QLabel("Coordinate System:")); |
| 180 |
|
| 181 |
QCheckBox *deviceIndpendentGeometry = new QCheckBox("Device Independent"); |
| 182 |
deviceIndpendentGeometry->setChecked(true); |
| 183 |
connect(deviceIndpendentGeometry, &QCheckBox::stateChanged, [displayer](int checked){ displayer->displayDeviceIndependentGeometry = checked > 0; }); |
| 184 |
layout->addWidget(deviceIndpendentGeometry); |
| 185 |
|
| 186 |
QCheckBox *nativeGeometry = new QCheckBox("Native"); |
| 187 |
nativeGeometry->setChecked(false); |
| 188 |
connect(nativeGeometry, &QCheckBox::stateChanged, [displayer](int checked){ displayer->displayNativeGeometry = checked > 0; }); |
| 189 |
layout->addWidget(nativeGeometry); |
| 190 |
|
| 191 |
layout->addSpacing(40); |
| 192 |
|
| 193 |
QCheckBox *debug = new QCheckBox("Debug!"); |
| 194 |
debug->setChecked(false); |
| 195 |
connect(debug, &QCheckBox::stateChanged, [displayer](int checked){ displayer->displayInfo = checked > 0; }); |
| 196 |
layout->addWidget(debug); |
| 197 |
|
| 198 |
layout->addStretch(); |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
int main(int argc, char **argv) { |
| 203 |
|
| 204 |
QApplication app(argc, argv); |
| 205 |
|
| 206 |
ScreenDisplayer displayer; |
| 207 |
displayer.resize(300, 200); |
| 208 |
displayer.show(); |
| 209 |
|
| 210 |
Controller controller(&displayer); |
| 211 |
controller.resize(100, 200); |
| 212 |
|
| 213 |
QTimer::singleShot(300, [&controller, &displayer](){ |
| 214 |
controller.move(displayer.pos() + QPoint(displayer.width(), 0) + QPoint(50, 0)); |
| 215 |
controller.show(); |
| 216 |
}); |
| 217 |
|
| 218 |
return app.exec(); |
| 219 |
} |