1 | // Copyright (C) 2017 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qwaylandmousetracker_p.h" |
5 | |
6 | #include <QtQuick/private/qquickitem_p.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | class QWaylandMouseTrackerPrivate : public QQuickItemPrivate |
11 | { |
12 | Q_DECLARE_PUBLIC(QWaylandMouseTracker) |
13 | public: |
14 | QWaylandMouseTrackerPrivate() |
15 | { |
16 | QImage cursorImage(64,64,QImage::Format_ARGB32); |
17 | cursorImage.fill(color: Qt::transparent); |
18 | cursorPixmap = QPixmap::fromImage(image: cursorImage); |
19 | } |
20 | void handleMousePos(const QPointF &mousePos) |
21 | { |
22 | Q_Q(QWaylandMouseTracker); |
23 | bool xChanged = mousePos.x() != this->mousePos.x(); |
24 | bool yChanged = mousePos.y() != this->mousePos.y(); |
25 | if (xChanged || yChanged) { |
26 | this->mousePos = mousePos; |
27 | if (xChanged) |
28 | emit q->mouseXChanged(); |
29 | if (yChanged) |
30 | emit q->mouseYChanged(); |
31 | } |
32 | } |
33 | |
34 | void setHovered(bool hovered) |
35 | { |
36 | Q_Q(QWaylandMouseTracker); |
37 | if (this->hovered == hovered) |
38 | return; |
39 | this->hovered = hovered; |
40 | emit q->hoveredChanged(); |
41 | } |
42 | |
43 | QPointF mousePos; |
44 | bool windowSystemCursorEnabled = false; |
45 | QPixmap cursorPixmap; |
46 | bool hovered = false; |
47 | }; |
48 | |
49 | QWaylandMouseTracker::QWaylandMouseTracker(QQuickItem *parent) |
50 | : QQuickItem(*(new QWaylandMouseTrackerPrivate), parent) |
51 | { |
52 | Q_D(QWaylandMouseTracker); |
53 | setFiltersChildMouseEvents(true); |
54 | setAcceptHoverEvents(true); |
55 | setAcceptedMouseButtons(Qt::AllButtons); |
56 | #if QT_CONFIG(cursor) |
57 | setCursor(QCursor(d->cursorPixmap)); |
58 | #endif |
59 | } |
60 | |
61 | qreal QWaylandMouseTracker::mouseX() const |
62 | { |
63 | Q_D(const QWaylandMouseTracker); |
64 | return d->mousePos.x(); |
65 | } |
66 | qreal QWaylandMouseTracker::mouseY() const |
67 | { |
68 | Q_D(const QWaylandMouseTracker); |
69 | return d->mousePos.y(); |
70 | } |
71 | |
72 | #if QT_CONFIG(cursor) |
73 | void QWaylandMouseTracker::setWindowSystemCursorEnabled(bool enable) |
74 | { |
75 | Q_D(QWaylandMouseTracker); |
76 | if (d->windowSystemCursorEnabled != enable) { |
77 | d->windowSystemCursorEnabled = enable; |
78 | if (enable) { |
79 | unsetCursor(); |
80 | } else { |
81 | setCursor(QCursor(d->cursorPixmap)); |
82 | } |
83 | emit windowSystemCursorEnabledChanged(); |
84 | } |
85 | } |
86 | |
87 | bool QWaylandMouseTracker::windowSystemCursorEnabled() const |
88 | { |
89 | Q_D(const QWaylandMouseTracker); |
90 | return d->windowSystemCursorEnabled; |
91 | } |
92 | #endif |
93 | |
94 | bool QWaylandMouseTracker::hovered() const |
95 | { |
96 | Q_D(const QWaylandMouseTracker); |
97 | return d->hovered; |
98 | } |
99 | |
100 | bool QWaylandMouseTracker::childMouseEventFilter(QQuickItem *item, QEvent *event) |
101 | { |
102 | Q_D(QWaylandMouseTracker); |
103 | if (event->type() == QEvent::MouseMove) { |
104 | QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); |
105 | d->handleMousePos(mousePos: mapFromItem(item, point: mouseEvent->position())); |
106 | } else if (event->type() == QEvent::HoverMove) { |
107 | QHoverEvent *hoverEvent = static_cast<QHoverEvent *>(event); |
108 | d->handleMousePos(mousePos: mapFromItem(item, point: hoverEvent->position())); |
109 | } |
110 | return false; |
111 | } |
112 | |
113 | void QWaylandMouseTracker::mouseMoveEvent(QMouseEvent *event) |
114 | { |
115 | Q_D(QWaylandMouseTracker); |
116 | QQuickItem::mouseMoveEvent(event); |
117 | d->handleMousePos(mousePos: event->position()); |
118 | } |
119 | |
120 | void QWaylandMouseTracker::hoverMoveEvent(QHoverEvent *event) |
121 | { |
122 | Q_D(QWaylandMouseTracker); |
123 | QQuickItem::hoverMoveEvent(event); |
124 | d->handleMousePos(mousePos: event->position()); |
125 | } |
126 | |
127 | void QWaylandMouseTracker::hoverEnterEvent(QHoverEvent *event) |
128 | { |
129 | Q_D(QWaylandMouseTracker); |
130 | Q_UNUSED(event); |
131 | d->setHovered(true); |
132 | } |
133 | |
134 | void QWaylandMouseTracker::hoverLeaveEvent(QHoverEvent *event) |
135 | { |
136 | Q_D(QWaylandMouseTracker); |
137 | Q_UNUSED(event); |
138 | d->setHovered(false); |
139 | } |
140 | |
141 | QT_END_NAMESPACE |
142 | |
143 | #include "moc_qwaylandmousetracker_p.cpp" |
144 |