1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtWaylandCompositor module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include "qwaylandmousetracker_p.h" |
31 | |
32 | #include <QtQuick/private/qquickitem_p.h> |
33 | |
34 | QT_BEGIN_NAMESPACE |
35 | |
36 | class QWaylandMouseTrackerPrivate : public QQuickItemPrivate |
37 | { |
38 | Q_DECLARE_PUBLIC(QWaylandMouseTracker) |
39 | public: |
40 | QWaylandMouseTrackerPrivate() |
41 | { |
42 | QImage cursorImage(64,64,QImage::Format_ARGB32); |
43 | cursorImage.fill(color: Qt::transparent); |
44 | cursorPixmap = QPixmap::fromImage(image: cursorImage); |
45 | } |
46 | void handleMousePos(const QPointF &mousePos) |
47 | { |
48 | Q_Q(QWaylandMouseTracker); |
49 | bool xChanged = mousePos.x() != this->mousePos.x(); |
50 | bool yChanged = mousePos.y() != this->mousePos.y(); |
51 | if (xChanged || yChanged) { |
52 | this->mousePos = mousePos; |
53 | if (xChanged) |
54 | emit q->mouseXChanged(); |
55 | if (yChanged) |
56 | emit q->mouseYChanged(); |
57 | } |
58 | } |
59 | |
60 | void setHovered(bool hovered) |
61 | { |
62 | Q_Q(QWaylandMouseTracker); |
63 | if (this->hovered == hovered) |
64 | return; |
65 | this->hovered = hovered; |
66 | emit q->hoveredChanged(); |
67 | } |
68 | |
69 | QPointF mousePos; |
70 | bool windowSystemCursorEnabled = false; |
71 | QPixmap cursorPixmap; |
72 | bool hovered = false; |
73 | }; |
74 | |
75 | QWaylandMouseTracker::QWaylandMouseTracker(QQuickItem *parent) |
76 | : QQuickItem(*(new QWaylandMouseTrackerPrivate), parent) |
77 | { |
78 | Q_D(QWaylandMouseTracker); |
79 | setFiltersChildMouseEvents(true); |
80 | setAcceptHoverEvents(true); |
81 | setAcceptedMouseButtons(Qt::AllButtons); |
82 | #if QT_CONFIG(cursor) |
83 | setCursor(QCursor(d->cursorPixmap)); |
84 | #endif |
85 | } |
86 | |
87 | qreal QWaylandMouseTracker::mouseX() const |
88 | { |
89 | Q_D(const QWaylandMouseTracker); |
90 | return d->mousePos.x(); |
91 | } |
92 | qreal QWaylandMouseTracker::mouseY() const |
93 | { |
94 | Q_D(const QWaylandMouseTracker); |
95 | return d->mousePos.y(); |
96 | } |
97 | |
98 | #if QT_CONFIG(cursor) |
99 | void QWaylandMouseTracker::setWindowSystemCursorEnabled(bool enable) |
100 | { |
101 | Q_D(QWaylandMouseTracker); |
102 | if (d->windowSystemCursorEnabled != enable) { |
103 | d->windowSystemCursorEnabled = enable; |
104 | if (enable) { |
105 | unsetCursor(); |
106 | } else { |
107 | setCursor(QCursor(d->cursorPixmap)); |
108 | } |
109 | emit windowSystemCursorEnabledChanged(); |
110 | } |
111 | } |
112 | |
113 | bool QWaylandMouseTracker::windowSystemCursorEnabled() const |
114 | { |
115 | Q_D(const QWaylandMouseTracker); |
116 | return d->windowSystemCursorEnabled; |
117 | } |
118 | #endif |
119 | |
120 | bool QWaylandMouseTracker::hovered() const |
121 | { |
122 | Q_D(const QWaylandMouseTracker); |
123 | return d->hovered; |
124 | } |
125 | |
126 | bool QWaylandMouseTracker::childMouseEventFilter(QQuickItem *item, QEvent *event) |
127 | { |
128 | Q_D(QWaylandMouseTracker); |
129 | if (event->type() == QEvent::MouseMove) { |
130 | QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); |
131 | d->handleMousePos(mousePos: mapFromItem(item, point: mouseEvent->localPos())); |
132 | } else if (event->type() == QEvent::HoverMove) { |
133 | QHoverEvent *hoverEvent = static_cast<QHoverEvent *>(event); |
134 | d->handleMousePos(mousePos: mapFromItem(item, point: hoverEvent->posF())); |
135 | } |
136 | return false; |
137 | } |
138 | |
139 | void QWaylandMouseTracker::mouseMoveEvent(QMouseEvent *event) |
140 | { |
141 | Q_D(QWaylandMouseTracker); |
142 | QQuickItem::mouseMoveEvent(event); |
143 | d->handleMousePos(mousePos: event->localPos()); |
144 | } |
145 | |
146 | void QWaylandMouseTracker::hoverMoveEvent(QHoverEvent *event) |
147 | { |
148 | Q_D(QWaylandMouseTracker); |
149 | QQuickItem::hoverMoveEvent(event); |
150 | d->handleMousePos(mousePos: event->posF()); |
151 | } |
152 | |
153 | void QWaylandMouseTracker::hoverEnterEvent(QHoverEvent *event) |
154 | { |
155 | Q_D(QWaylandMouseTracker); |
156 | Q_UNUSED(event); |
157 | d->setHovered(true); |
158 | } |
159 | |
160 | void QWaylandMouseTracker::hoverLeaveEvent(QHoverEvent *event) |
161 | { |
162 | Q_D(QWaylandMouseTracker); |
163 | Q_UNUSED(event); |
164 | d->setHovered(false); |
165 | } |
166 | |
167 | QT_END_NAMESPACE |
168 |