summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmcursor.cpp
blob: c258befa772de5a93761fc5a8285e1d2b4c6a763 (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
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qwasmcursor.h"
#include "qwasmscreen.h"
#include "qwasmwindow.h"

#include <QtCore/qbuffer.h>
#include <QtCore/qdebug.h>
#include <QtCore/qstring.h>
#include <QtGui/qwindow.h>

#include <emscripten/emscripten.h>
#include <emscripten/bind.h>

QT_BEGIN_NAMESPACE
using namespace emscripten;

namespace {
QByteArray cursorToCss(const QCursor *cursor)
{
    auto shape = cursor->shape();
    switch (shape) {
    case Qt::ArrowCursor:
        return "default";
    case Qt::UpArrowCursor:
        return "n-resize";
    case Qt::CrossCursor:
        return "crosshair";
    case Qt::WaitCursor:
        return "wait";
    case Qt::IBeamCursor:
        return "text";
    case Qt::SizeVerCursor:
        return "ns-resize";
    case Qt::SizeHorCursor:
        return "ew-resize";
    case Qt::SizeBDiagCursor:
        return "nesw-resize";
    case Qt::SizeFDiagCursor:
        return "nwse-resize";
    case Qt::SizeAllCursor:
        return "move";
    case Qt::BlankCursor:
        return "none";
    case Qt::SplitVCursor:
        return "row-resize";
    case Qt::SplitHCursor:
        return "col-resize";
    case Qt::PointingHandCursor:
        return "pointer";
    case Qt::ForbiddenCursor:
        return "not-allowed";
    case Qt::WhatsThisCursor:
        return "help";
    case Qt::BusyCursor:
        return "progress";
    case Qt::OpenHandCursor:
        return "grab";
    case Qt::ClosedHandCursor:
        return "grabbing";
    case Qt::DragCopyCursor:
        return "copy";
    case Qt::DragMoveCursor:
        return "default";
    case Qt::DragLinkCursor:
        return "alias";
    case Qt::BitmapCursor: {
        auto pixmap = cursor->pixmap();
        QByteArray cursorAsPng;
        QBuffer buffer(&cursorAsPng);
        buffer.open(QBuffer::WriteOnly);
        pixmap.save(&buffer, "PNG");
        buffer.close();
        auto cursorAsBase64 = cursorAsPng.toBase64();
        auto hotSpot = cursor->hotSpot();
        auto encodedCursor =
            QString("url(data:image/png;base64,%1) %2 %3, auto")
                .arg(QString::fromUtf8(cursorAsBase64),
                     QString::number(hotSpot.x()),
                     QString::number(hotSpot.y()));
        return encodedCursor.toUtf8();
        }
    default:
        static_assert(Qt::CustomCursor == 25,
                      "New cursor type added, handle it");
        qWarning() << "QWasmCursor: " << shape << " unsupported";
        return "default";
    }
}
} // namespace

void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window)
{
    if (!window)
        return;
    if (QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle()))
        wasmWindow->setWindowCursor(windowCursor ? cursorToCss(windowCursor) : "default");
}

QT_END_NAMESPACE