Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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";
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";
35 return "ns-resize";
37 return "ew-resize";
39 return "nesw-resize";
41 return "nwse-resize";
43 return "move";
44 case Qt::BlankCursor:
45 return "none";
47 return "row-resize";
49 return "col-resize";
51 return "pointer";
53 return "not-allowed";
55 return "help";
56 case Qt::BusyCursor:
57 return "progress";
59 return "grab";
61 return "grabbing";
63 return "copy";
65 return "default";
67 return "alias";
68 case Qt::BitmapCursor: {
69 auto pixmap = cursor->pixmap();
70 QByteArray cursorAsPng;
71 QBuffer buffer(&cursorAsPng);
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
94{
95 if (!window)
96 return;
97 if (QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle()))
98 wasmWindow->setWindowCursor(windowCursor ? cursorToCss(windowCursor) : "default");
99}
100
\inmodule QtCore \reentrant
Definition qbuffer.h:16
\inmodule QtCore
Definition qbytearray.h:57
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition qcursor.h:45
QPixmap pixmap() const
Returns the cursor pixmap.
Definition qcursor.cpp:584
Qt::CursorShape shape() const
Returns the cursor shape identifier.
Definition qcursor.cpp:498
QPoint hotSpot() const
Returns the cursor hot spot, or (0, 0) if it is one of the standard cursors.
Definition qcursor.cpp:595
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8870
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
QByteArray toUtf8() const &
Definition qstring.h:634
void changeCursor(QCursor *windowCursor, QWindow *window) override
This method is called by Qt whenever the cursor graphic should be changed.
\inmodule QtGui
Definition qwindow.h:63
QCursor cursor
Combined button and popup list for selecting options.
@ BlankCursor
@ CrossCursor
@ DragCopyCursor
@ BitmapCursor
@ PointingHandCursor
@ SizeHorCursor
@ SizeAllCursor
@ CustomCursor
@ WaitCursor
@ SizeVerCursor
@ DragLinkCursor
@ OpenHandCursor
@ SizeFDiagCursor
@ WhatsThisCursor
@ ArrowCursor
@ SplitVCursor
@ UpArrowCursor
@ ClosedHandCursor
@ DragMoveCursor
@ IBeamCursor
@ SizeBDiagCursor
@ ForbiddenCursor
@ BusyCursor
@ SplitHCursor
#define qWarning
Definition qlogging.h:166
GLenum GLuint buffer
widget render & pixmap
aWidget window() -> setWindowTitle("New Window Title")
[2]