summaryrefslogtreecommitdiffstats
path: root/tests/manual/qcursor/qcursorhighdpi/main.cpp
blob: 55ba67d8251a9c822dc8761c95ced5f0357e5c25 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QAction>
#include <QApplication>
#include <QGridLayout>
#include <QLabel>
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QSharedPointer>
#include <QToolBar>

#include <QBitmap>
#include <QCursor>
#include <QDrag>
#include <QPainter>
#include <QPixmap>

#include <QDebug>
#include <QMimeData>
#include <QStringList>
#include <QTextStream>

#include <QScreen>
#include <QWindow>
#include <private/qhighdpiscaling_p.h>
#include <qpa/qplatformwindow.h>

#ifdef Q_OS_WIN
#  include <qt_windows.h>
#endif

#include <algorithm>
#include <iterator>

// High DPI cursor test for testing cursor sizes in multi-screen setups.
// It creates one widget per screen with a grid of standard cursors,
// pixmap / bitmap cursors and pixmap / bitmap cursors with device pixel ratio 2.
// On the left, there is a ruler with 10 DIP marks.
// The code is meant to compile with Qt 4 also.

static QString screenInfo(const QWidget *w)
{
    QString result;
    QTextStream str(&result);
    QScreen *screen = nullptr;
    if (const QWindow *window = w->windowHandle())
        screen = window->screen();
    if (screen) {
        str << '"' << screen->name() << "\" " << screen->size().width() << 'x'
            << screen->size().height() << ", DPR=" << screen->devicePixelRatio()
            << ", " << screen->logicalDotsPerInchX() << "DPI ";
        if (QHighDpiScaling::isActive())
            str << ", factor=" << QHighDpiScaling::factor(screen);
        else
            str << ", no scaling";
    } else {
        str << "<null>";
    }
#ifdef Q_OS_WIN
    str << ", SM_C_CURSOR: " << GetSystemMetrics(SM_CXCURSOR) << 'x' << GetSystemMetrics(SM_CYCURSOR);
#endif
    return result;
}

// Helpers for painting pixmaps and creating cursors
static QPixmap paintPixmap(int size, QColor c)
{
    QPixmap result(size, size);
    result.fill(c);
    QPainter p(&result);
    p.drawRect(QRect(QPoint(0, 0), result.size() - QSize(1, 1)));
    p.drawLine(0, 0, size, size);
    p.drawLine(0, size, size, 0);
    return result;
}

static QCursor pixmapCursor(int size)
{
    QCursor result(paintPixmap(size, Qt::red), size / 2, size / 2);
    return result;
}

static QPair<QBitmap, QBitmap> paintBitmaps(int size)
{
    QBitmap bitmap(size, size);
    bitmap.fill(Qt::color1);
    QBitmap mask(size, size);
    mask.fill(Qt::color1);
    {
        QPainter mp(&mask);
        mp.fillRect(QRect(0, 0, size / 2, size / 2), Qt::color0);
    }
    return QPair<QBitmap, QBitmap>(bitmap, mask);
}

static QCursor bitmapCursor(int size)
{
    QPair<QBitmap, QBitmap> bitmaps = paintBitmaps(size);
    return QCursor(bitmaps.first, bitmaps.second, size / 2, size / 2);
}

static QCursor pixmapCursorDevicePixelRatio(int size, int dpr)
{
    QPixmap pixmap = paintPixmap(dpr * size, Qt::yellow);
    pixmap.setDevicePixelRatio(dpr);
    return QCursor(pixmap, size / 2, size / 2);
}

static QCursor bitmapCursorDevicePixelRatio(int size, int dpr)
{
    QPair<QBitmap, QBitmap> bitmaps = paintBitmaps(dpr * size);
    bitmaps.first.setDevicePixelRatio(dpr);
    bitmaps.second.setDevicePixelRatio(dpr);
    return QCursor(bitmaps.first, bitmaps.second, size / 2, size / 2);
}

// A label from which a pixmap can be dragged for testing drag with pixmaps/DPR.
class DraggableLabel : public QLabel {
public:
    explicit DraggableLabel(const QPixmap &p, const QString &text, QWidget *parent = Q_NULLPTR)
        : QLabel(text, parent), m_pixmap(p)
    {
        setToolTip(QLatin1String("Click to drag away the pixmap. Press Shift to set a circular mask."));
    }

protected:
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

private:
    const QPixmap m_pixmap;
};

void DraggableLabel::mousePressEvent(QMouseEvent *)
{
    QMimeData *mimeData = new QMimeData;
    mimeData->setImageData(QVariant::fromValue(m_pixmap));
    QDrag *drag = new QDrag(this);
    QPixmap pixmap = m_pixmap;
    if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
        QBitmap mask(pixmap.width(), pixmap.height());
        mask.clear();
        QPainter painter(&mask);
        painter.setBrush(Qt::color1);
        const int hx = pixmap.width() / 2;
        const int hy = pixmap.width() / 2;
        painter.drawEllipse(QPoint(hx, hy), hx, hy);
        pixmap.setMask(mask);
    }
    drag->setMimeData(mimeData);
    drag->setPixmap(pixmap);
    QPoint sizeP = QPoint(m_pixmap.width(), m_pixmap.height());
    sizeP /= int(m_pixmap.devicePixelRatio());
    drag->setHotSpot(sizeP / 2);
    qDebug() << "Dragging:" << m_pixmap;
    drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
}

// Vertical ruler widget with 10 px marks
class VerticalRuler : public QWidget {
public:
    VerticalRuler(QWidget *parent = Q_NULLPTR);

protected:
    void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
};

VerticalRuler::VerticalRuler(QWidget *parent) : QWidget(parent)
{
    const int screenWidth = screen()->geometry().width();
    setFixedWidth(screenWidth / 48); // 1920 pixel monitor ->40
}

void VerticalRuler::paintEvent(QPaintEvent *)
{
    const QSize sizeS(size());
    const QPoint sizeP(sizeS.width(), sizeS.height());
    const QPoint center = sizeP / 2;
    QPainter painter(this);
    painter.fillRect(QRect(QPoint(0, 0), sizeS), Qt::white);
    painter.drawLine(center.x(), 0, center.x(), sizeP.y());
    for (int y = 0; y < sizeP.y(); y += 10)
        painter.drawLine(center.x() - 5, y, center.x() + 5, y);
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = Q_NULLPTR);
    void updateScreenInfo() { m_screenInfoLabel->setText(screenInfo(this)); }

public slots:
    void screenChanged() { updateScreenInfo(); }

private:
    QLabel *m_screenInfoLabel;
};

static QLabel *createCursorLabel(const QCursor &cursor, const QString &additionalText = QString())
{
    QString labelText;
    QDebug(&labelText).nospace() << cursor.shape();
    labelText.remove(0, labelText.indexOf('(') + 1);
    labelText.chop(1);
    if (!additionalText.isEmpty())
        labelText += ' ' + additionalText;
    const QPixmap cursorPixmap = cursor.pixmap();
    QLabel *result = Q_NULLPTR;
    if (cursorPixmap.size().isEmpty()) {
        result = new QLabel(labelText);
        result->setFrameShape(QFrame::Box);
    } else {
        result = new DraggableLabel(cursor.pixmap(), labelText);
        result->setFrameShape(QFrame::StyledPanel);
    }
    result->setCursor(cursor);
    return result;
}

static void addToGrid(QWidget *w, QGridLayout *gridLayout, int columnCount, int &row, int &col)
{
    gridLayout->addWidget(w, row, col);
    if (col >= columnCount) {
        col = 0;
        row++;
    } else {
        col++;
    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_screenInfoLabel(new QLabel)
{
    QString title = "Cursors ";
    title += '(' + QGuiApplication::platformName() + ") ";
    title += QT_VERSION_STR;
    setWindowTitle(title);

    QMenu *fileMenu = menuBar()->addMenu("File");
    QAction *quitAction = fileMenu->addAction("Quit");
    quitAction->setShortcut(Qt::CTRL | Qt::Key_Q);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

    QToolBar *fileToolBar = addToolBar("File");
    fileToolBar->addAction(quitAction);

    QWidget *cw = new QWidget;
    QHBoxLayout *hLayout = new QHBoxLayout(cw);
    hLayout->addWidget(new VerticalRuler(cw));
    QGridLayout *gridLayout = new QGridLayout;
    hLayout->addLayout(gridLayout);

    const int columnCount = 5;
    const int size = 32;

    int row = 0;
    int col = 0;
    for (int i = 0; i < Qt::BitmapCursor; ++i)
        addToGrid(createCursorLabel(QCursor(static_cast<Qt::CursorShape>(i))), gridLayout, columnCount, row, col);

    addToGrid(createCursorLabel(QCursor(pixmapCursor(size)),
                                QLatin1String("Plain PX ") + QString::number(size)),
                                gridLayout, columnCount, row, col);

    addToGrid(createCursorLabel(bitmapCursor(size),
                                QLatin1String("Plain BM ") + QString::number(size)),
                                gridLayout, columnCount, row, col);

    addToGrid(createCursorLabel(QCursor(pixmapCursorDevicePixelRatio(size, 2)),
                                "PX with DPR 2 " + QString::number(size)),
                                gridLayout, columnCount, row, col);

    addToGrid(createCursorLabel(QCursor(bitmapCursorDevicePixelRatio(size, 2)),
                                "BM with DPR 2 " + QString::number(size)),
                                gridLayout, columnCount, row, col);

    gridLayout->addWidget(m_screenInfoLabel, row + 1, 0, 1, columnCount);

    setCentralWidget(cw);
}

typedef QSharedPointer<MainWindow> MainWindowPtr;
typedef QList<MainWindowPtr> MainWindowPtrList;

int main(int argc, char *argv[])
{
    QStringList arguments;
    std::copy(argv + 1, argv + argc, std::back_inserter(arguments));

    QApplication app(argc, argv);

    MainWindowPtrList windows;
    const int lastScreen = arguments.contains("-p")
        ? 0  // Primary screen only
        : QGuiApplication::screens().size() - 1; // All screens
    for (int s = lastScreen; s >= 0; --s) {
        MainWindowPtr window(new MainWindow());
        const QPoint pos = QGuiApplication::screens().at(s)->geometry().center() - QPoint(200, 100);
        window->move(pos);
        windows.append(window);
        window->show();
        window->updateScreenInfo();
        QObject::connect(window->windowHandle(), &QWindow::screenChanged,
                         window.data(), &MainWindow::updateScreenInfo);
    }
    return app.exec();
}
#include "main.moc"