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
qcursor.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qcursor.h"
5
6#include <qcoreapplication.h>
7#include <qbitmap.h>
8#include <qimage.h>
9#include <qdatastream.h>
10#include <qvariant.h>
11#include <private/qcursor_p.h>
12#include <qdebug.h>
13
14#include <qpa/qplatformcursor.h>
15#include <private/qguiapplication_p.h>
16#include <private/qhighdpiscaling_p.h>
17
19
158{
159 if (screen) {
160 if (const QPlatformCursor *cursor = screen->handle()->cursor()) {
161 const QPlatformScreen *ps = screen->handle();
162 QPoint nativePos = cursor->pos();
163 ps = ps->screenForPosition(nativePos);
164 return QHighDpi::fromNativePixels(nativePos, ps->screen());
165 }
166 }
168}
169
192
215{
216 if (screen) {
218 const QPoint pos(x, y);
220 // Need to check, since some X servers generate null mouse move
221 // events, causing looping in applications which call setPos() on
222 // every mouse move event.
223 if (devicePos != cursor->pos())
224 cursor->setPos(devicePos);
225 }
226 }
227}
228
244
245#ifndef QT_NO_CURSOR
246
265/*****************************************************************************
266 QCursor stream functions
267 *****************************************************************************/
268
269#ifndef QT_NO_DATASTREAM
270
271
282{
283 s << (qint16)c.shape(); // write shape id to stream
284 if (c.shape() == Qt::BitmapCursor) { // bitmap cursor
285 bool isPixmap = false;
286 if (s.version() >= 7) {
287 isPixmap = !c.pixmap().isNull();
288 s << isPixmap;
289 }
290 if (isPixmap)
291 s << c.pixmap();
292 else
293 s << c.bitmap() << c.mask();
294 s << c.hotSpot();
295 }
296 return s;
297}
298
309{
310 qint16 shape;
311 s >> shape; // read shape id from stream
312 if (shape == Qt::BitmapCursor) { // read bitmap cursor
313 bool isPixmap = false;
314 if (s.version() >= 7)
315 s >> isPixmap;
316 if (isPixmap) {
317 QPixmap pm;
318 QPoint hot;
319 s >> pm >> hot;
320 c = QCursor(pm, hot.x(), hot.y());
321 } else {
322 QBitmap bm, bmm;
323 QPoint hot;
324 s >> bm >> bmm >> hot;
325 c = QCursor(bm, bmm, hot.x(), hot.y());
326 }
327 } else {
328 c.setShape((Qt::CursorShape)shape); // create cursor with shape
329 }
330 return s;
331}
332#endif // QT_NO_DATASTREAM
333
334
353QCursor::QCursor(const QPixmap &pixmap, int hotX, int hotY)
354 : d(nullptr)
355{
358 QBitmap bmm = pixmap.mask();
359 if (!bmm.isNull()) {
360 QBitmap nullBm;
361 bm.setMask(nullBm);
362 }
363 else if (!pixmap.mask().isNull()) {
366 }
367 else {
368 bmm = QBitmap(bm.size());
369 bmm.fill(Qt::color1);
370 }
371
372 d = QCursorData::setBitmap(bm, bmm, hotX, hotY, pixmap.devicePixelRatio());
373 d->pixmap = pixmap;
374}
375
376
377
409QCursor::QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY)
410 : d(nullptr)
411{
412 d = QCursorData::setBitmap(bitmap, mask, hotX, hotY, 1.0);
413}
414
419{
422 d = nullptr;
423 return;
424 }
426 }
428 c->ref.ref();
429 d = c;
430}
431
445
462bool operator==(const QCursor &lhs, const QCursor &rhs) noexcept
463{
464 if (lhs.d == rhs.d)
465 return true; // Copy or same shape
466
467 // Check pixmaps or bitmaps cache keys. Notice that having BitmapCursor
468 // shape implies either non-null pixmap or non-null bitmap and mask
469 if (lhs.shape() == Qt::BitmapCursor && rhs.shape() == Qt::BitmapCursor
470 && lhs.hotSpot() == rhs.hotSpot()) {
471 if (!lhs.d->pixmap.isNull())
472 return lhs.d->pixmap.cacheKey() == rhs.d->pixmap.cacheKey();
473
474 if (!rhs.d->pixmap.isNull())
475 return false;
476
477 return lhs.d->bm->cacheKey() == rhs.d->bm->cacheKey()
478 && lhs.d->bmm->cacheKey() == rhs.d->bmm->cacheKey();
479 }
480
481 return false;
482}
483
499{
501 return d->cshape;
502}
503
512{
515 if (!c)
516 c = qt_cursorTable[0];
517 c->ref.ref();
518 if (!d) {
519 d = c;
520 } else {
521 if (!d->ref.deref())
522 delete d;
523 d = c;
524 }
525}
526
546{
548 if (d->bm)
549 return *(d->bm);
550 return QBitmap();
551}
552
572{
574 if (d->bmm)
575 return *(d->bmm);
576 return QBitmap();
577}
578
585{
587 return d->pixmap;
588}
589
596{
598 return QPoint(d->hx, d->hy);
599}
600
606{
608 d = c.d;
609 d->ref.ref();
610}
611
617{
618 if (d && !d->ref.deref())
619 delete d;
620}
621
622
629{
631 if (c.d)
632 c.d->ref.ref();
633 if (d && !d->ref.deref())
634 delete d;
635 d = c.d;
636 return *this;
637}
638
642QCursor::operator QVariant() const
643{
644 return QVariant::fromValue(*this);
645}
646
647#ifndef QT_NO_DEBUG_STREAM
649{
650 QDebugStateSaver saver(dbg);
651 dbg.nospace() << "QCursor(Qt::CursorShape(" << c.shape() << "))";
652 return dbg;
653}
654#endif
655
656/*****************************************************************************
657 Internal QCursorData class
658 *****************************************************************************/
659
661bool QCursorData::initialized = false;
662
664 : ref(1), cshape(s), bm(nullptr), bmm(nullptr), hx(0), hy(0)
665{
666}
667
669{
670 delete bm;
671 delete bmm;
672}
673
676{
678 return;
679
680 for (int shape = 0; shape <= Qt::LastCursor; ++shape) {
681 // In case someone has a static QCursor defined with this shape
682 if (!qt_cursorTable[shape]->ref.deref())
683 delete qt_cursorTable[shape];
684 qt_cursorTable[shape] = nullptr;
685 }
687}
688
691{
693 return;
694 for (int shape = 0; shape <= Qt::LastCursor; ++shape)
695 qt_cursorTable[shape] = new QCursorData((Qt::CursorShape)shape);
697}
698
699QCursorData *QCursorData::setBitmap(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY, qreal devicePixelRatio)
700{
702 if (bitmap.depth() != 1 || mask.depth() != 1 || bitmap.size() != mask.size()) {
703 qWarning("QCursor: Cannot create bitmap cursor; invalid bitmap(s)");
705 c->ref.ref();
706 return c;
707 }
709 d->bm = new QBitmap(bitmap);
710 d->bmm = new QBitmap(mask);
711 d->cshape = Qt::BitmapCursor;
712 d->hx = hotX >= 0 ? hotX : bitmap.width() / 2 / devicePixelRatio;
713 d->hy = hotY >= 0 ? hotY : bitmap.height() / 2 / devicePixelRatio;
714
715 return d;
716}
717
719{
720}
721
723#endif // QT_NO_CURSOR
724
bool ref() noexcept
bool deref() noexcept
\inmodule QtGui
Definition qbitmap.h:16
static QBitmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Returns a copy of the given image converted to a bitmap using the specified image conversion flags.
Definition qbitmap.cpp:170
static bool startingUp()
Returns true if an application object has not been created yet; otherwise returns false.
QBitmap * bm
Definition qcursor_p.h:38
static bool initialized
Definition qcursor_p.h:41
QBitmap * bmm
Definition qcursor_p.h:38
QPixmap pixmap
Definition qcursor_p.h:39
QCursorData(Qt::CursorShape s=Qt::ArrowCursor)
Definition qcursor.cpp:663
Qt::CursorShape cshape
Definition qcursor_p.h:37
static QCursorData * setBitmap(const QBitmap &bitmap, const QBitmap &mask, int hotX, int hotY, qreal devicePixelRatio)
Definition qcursor.cpp:699
void update()
Definition qcursor.cpp:718
static void initialize()
Definition qcursor.cpp:690
static void cleanup()
Definition qcursor.cpp:675
QAtomicInt ref
Definition qcursor_p.h:36
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition qcursor.h:45
QBitmap bitmap() const
Returns the cursor bitmap, or a null bitmap if it is one of the standard cursors.
Definition qcursor.cpp:545
QPixmap pixmap() const
Returns the cursor pixmap.
Definition qcursor.cpp:584
~QCursor()
Destroys the cursor.
Definition qcursor.cpp:616
static void setPos(int x, int y)
Moves the cursor (hot spot) of the primary screen to the global screen position (x,...
Definition qcursor.cpp:240
Qt::CursorShape shape() const
Returns the cursor shape identifier.
Definition qcursor.cpp:498
static QPoint pos()
Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates.
Definition qcursor.cpp:188
QCursor & operator=(const QCursor &cursor)
Move-assigns other to this QCursor instance.
Definition qcursor.cpp:628
QCursor()
Constructs a cursor with the default arrow shape.
Definition qcursor.cpp:418
QPoint hotSpot() const
Returns the cursor hot spot, or (0, 0) if it is one of the standard cursors.
Definition qcursor.cpp:595
void setShape(Qt::CursorShape newShape)
Sets the cursor to the shape identified by shape.
Definition qcursor.cpp:511
QBitmap mask() const
Returns the cursor bitmap mask, or a null bitmap if it is one of the standard cursors.
Definition qcursor.cpp:571
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
static struct QGuiApplicationPrivate::QLastCursorPosition lastCursorPosition
QScreen * primaryScreen
the primary (or default) screen of the application.
\inmodule QtGui
Definition qimage.h:37
@ Format_Indexed8
Definition qimage.h:45
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:125
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
QImage toImage() const
Converts the pixmap to a QImage.
Definition qpixmap.cpp:408
QSize size() const
Returns the size of the pixmap.
Definition qpixmap.cpp:493
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition qpixmap.cpp:456
QBitmap mask() const
Returns true if this pixmap has an alpha channel, or has a mask, otherwise returns false.
void setMask(const QBitmap &)
Sets a mask bitmap.
Definition qpixmap.cpp:543
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
Definition qpixmap.cpp:850
qreal devicePixelRatio() const
Returns the device pixel ratio for the pixmap.
Definition qpixmap.cpp:576
The QPlatformCursor class provides information about pointer device events (movement,...
The QPlatformScreen class provides an abstraction for visual displays.
const QPlatformScreen * screenForPosition(const QPoint &point) const
Find the sibling screen corresponding to globalPos.
virtual QPlatformCursor * cursor() const
Reimplement this function in subclass to return the cursor of the screen.
QScreen * screen() const
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
QScreen * virtualSiblingAt(QPoint point)
Returns the screen at point within the set of \l QScreen::virtualSiblings(), or nullptr if outside of...
Definition qscreen.cpp:629
QPlatformScreen * handle() const
Get the platform screen handle.
Definition qscreen.cpp:83
\inmodule QtCore
Definition qvariant.h:65
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
QCursor cursor
T toNativePixels(const T &value, const C *context)
T fromNativePixels(const T &value, const C *context)
Combined button and popup list for selecting options.
@ AvoidDither
Definition qnamespace.h:498
@ ThresholdDither
Definition qnamespace.h:492
CursorShape
@ BitmapCursor
@ LastCursor
@ color1
Definition qnamespace.h:29
QCursorData * qt_cursorTable[Qt::LastCursor+1]
Definition qcursor.cpp:660
QDataStream & operator<<(QDataStream &s, const QCursor &c)
Definition qcursor.cpp:281
bool operator==(const QCursor &lhs, const QCursor &rhs) noexcept
Definition qcursor.cpp:462
QDataStream & operator>>(QDataStream &s, QCursor &c)
Definition qcursor.cpp:308
QCursorData * qt_cursorTable[Qt::LastCursor+1]
Definition qcursor.cpp:660
#define qWarning
Definition qlogging.h:166
GLint GLint GLint GLint GLint x
[0]
GLint ref
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint y
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLint void * img
Definition qopenglext.h:233
GLsizei GLfixed GLfixed GLfixed GLfixed const GLubyte * bitmap
QScreen * screen
[1]
Definition main.cpp:29
short qint16
Definition qtypes.h:47
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
QObject::connect nullptr
widget render & pixmap