Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwasmcursor.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qwasmcursor.h"
5#include "qwasmscreen.h"
6#include "qwasmwindow.h"
7
8#include <QtCore/qbuffer.h>
9#include <QtCore/qdebug.h>
10#include <QtCore/qstring.h>
11#include <QtGui/qwindow.h>
12
13#include <emscripten/emscripten.h>
14#include <emscripten/bind.h>
15
17using namespace emscripten;
18
19namespace {
20QByteArray cursorToCss(const QCursor *cursor)
21{
22 auto shape = cursor->shape();
23 switch (shape) {
24 case Qt::ArrowCursor:
25 return "default";
26 case Qt::UpArrowCursor:
27 return "n-resize";
28 case Qt::CrossCursor:
29 return "crosshair";
30 case Qt::WaitCursor:
31 return "wait";
32 case Qt::IBeamCursor:
33 return "text";
34 case Qt::SizeVerCursor:
35 return "ns-resize";
36 case Qt::SizeHorCursor:
37 return "ew-resize";
38 case Qt::SizeBDiagCursor:
39 return "nesw-resize";
40 case Qt::SizeFDiagCursor:
41 return "nwse-resize";
42 case Qt::SizeAllCursor:
43 return "move";
44 case Qt::BlankCursor:
45 return "none";
46 case Qt::SplitVCursor:
47 return "row-resize";
48 case Qt::SplitHCursor:
49 return "col-resize";
50 case Qt::PointingHandCursor:
51 return "pointer";
52 case Qt::ForbiddenCursor:
53 return "not-allowed";
54 case Qt::WhatsThisCursor:
55 return "help";
56 case Qt::BusyCursor:
57 return "progress";
58 case Qt::OpenHandCursor:
59 return "grab";
60 case Qt::ClosedHandCursor:
61 return "grabbing";
62 case Qt::DragCopyCursor:
63 return "copy";
64 case Qt::DragMoveCursor:
65 return "default";
66 case Qt::DragLinkCursor:
67 return "alias";
68 case Qt::BitmapCursor: {
69 auto pixmap = cursor->pixmap();
70 QByteArray cursorAsPng;
71 QBuffer buffer(&cursorAsPng);
72 buffer.open(QBuffer::WriteOnly);
73 pixmap.save(&buffer, "PNG");
74 buffer.close();
75 auto cursorAsBase64 = cursorAsPng.toBase64();
76 auto hotSpot = cursor->hotSpot();
77 auto encodedCursor =
78 QString("url(data:image/png;base64,%1) %2 %3, auto")
79 .arg(QString::fromUtf8(cursorAsBase64),
80 QString::number(hotSpot.x()),
81 QString::number(hotSpot.y()));
82 return encodedCursor.toUtf8();
83 }
84 default:
85 static_assert(Qt::CustomCursor == 25,
86 "New cursor type added, handle it");
87 qWarning() << "QWasmCursor: " << shape << " unsupported";
88 return "default";
89 }
90}
91} // namespace
92
93void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window)
94{
95 if (!window)
96 return;
97 if (QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle()))
98 wasmWindow->setWindowCursor(windowCursor ? cursorToCss(windowCursor) : "default");
99}
100
101QT_END_NAMESPACE
Combined button and popup list for selecting options.