on 5e6ca45 help sign in
unity8/plugins/Cursor/MousePointer.cpp
196 lines 6.5 kB history raw
1/*
2 * Copyright (C) 2015-2016 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "MousePointer.h"
18#include "CursorImageProvider.h"
20// Unity API
21#include <unity/shell/application/MirPlatformCursor.h>
23#include <QQuickWindow>
24#include <QGuiApplication>
25#include <QQmlProperty>
26#include <QtMath>
28#include <qpa/qwindowsysteminterface.h>
30MousePointer::MousePointer(QQuickItem *parent)
31 : MirMousePointerInterface(parent)
32 , m_cursorName(QStringLiteral("left_ptr"))
33 , m_themeName(QStringLiteral("default"))
37void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
38 Qt::KeyboardModifiers modifiers)
40 if (!parentItem()) {
41 return;
42 }
44 if (!movement.isNull()) {
45 Q_EMIT mouseMoved();
46 }
48 m_accumulatedMovement += movement;
49 // don't apply the fractional part
50 QPointF appliedMovement(int(m_accumulatedMovement.x()), int(m_accumulatedMovement.y()));
51 m_accumulatedMovement -= appliedMovement;
53 const qreal newX = x() + appliedMovement.x();
54 const qreal newY = y() + appliedMovement.y();
55 const qreal sceneWidth = parentItem()->width();
56 const qreal sceneHeight = parentItem()->height();
58 if (newX <= 0 && newY < m_topBoundaryOffset) { // top left corner
59 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-m_topBoundaryOffset, 2));
60 Q_EMIT pushedTopLeftCorner(qAbs(distance), buttons);
61 m_pushing = true;
62 } else if (newX >= sceneWidth-1 && newY < m_topBoundaryOffset) { // top right corner
63 const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-m_topBoundaryOffset, 2));
64 Q_EMIT pushedTopRightCorner(qAbs(distance), buttons);
65 m_pushing = true;
66 } else if (newX < 0 && newY >= sceneHeight-1) { // bottom left corner
67 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-sceneHeight, 2));
68 Q_EMIT pushedBottomLeftCorner(qAbs(distance), buttons);
69 m_pushing = true;
70 } else if (newX >= sceneWidth-1 && newY >= sceneHeight-1) { // bottom right corner
71 const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-sceneHeight, 2));
72 Q_EMIT pushedBottomRightCorner(qAbs(distance), buttons);
73 m_pushing = true;
74 } else if (newX < 0) { // left edge
75 Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
76 m_pushing = true;
77 } else if (newX >= sceneWidth) { // right edge
78 Q_EMIT pushedRightBoundary(newX - (sceneWidth - 1), buttons);
79 m_pushing = true;
80 } else if (newY < m_topBoundaryOffset) { // top edge
81 Q_EMIT pushedTopBoundary(qAbs(newY - m_topBoundaryOffset), buttons);
82 m_pushing = true;
83 } else if (Q_LIKELY(newX > 0 && newX < sceneWidth-1 && newY > 0 && newY < sceneHeight-1)) { // normal pos, not pushing
84 if (m_pushing) {
85 Q_EMIT pushStopped();
86 m_pushing = false;
87 }
88 }
90 setX(qBound(0.0, newX, sceneWidth - 1));
91 setY(qBound(0.0, newY, sceneHeight - 1));
93 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
94 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
95 buttons, modifiers);
98void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
100 if (!parentItem()) {
101 return;
102 }
104 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
105 QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
106 QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
109int MousePointer::topBoundaryOffset() const
111 return m_topBoundaryOffset;
114void MousePointer::setTopBoundaryOffset(int topBoundaryOffset)
116 if (m_topBoundaryOffset == topBoundaryOffset)
117 return;
119 m_topBoundaryOffset = topBoundaryOffset;
120 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
123void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
125 if (change == ItemSceneChange) {
126 registerWindow(value.window);
127 }
130void MousePointer::registerWindow(QWindow *window)
132 if (window == m_registeredWindow) {
133 return;
134 }
136 if (m_registeredWindow) {
137 m_registeredWindow->disconnect(this);
138 }
140 m_registeredWindow = window;
142 if (m_registeredWindow) {
143 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
144 registerScreen(window->screen());
145 } else {
146 registerScreen(nullptr);
147 }
150void MousePointer::registerScreen(QScreen *screen)
152 if (m_registeredScreen == screen) {
153 return;
154 }
156 if (m_registeredScreen) {
157 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
158 if (previousCursor) {
159 previousCursor->setMousePointer(nullptr);
160 } else {
161 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
162 }
163 }
165 m_registeredScreen = screen;
167 if (m_registeredScreen) {
168 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
169 if (cursor) {
170 cursor->setMousePointer(this);
171 } else {
172 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
173 }
174 }
177void MousePointer::setCursorName(const QString &cursorName)
179 if (cursorName != m_cursorName) {
180 m_cursorName = cursorName;
181 Q_EMIT cursorNameChanged(m_cursorName);
182 }
185void MousePointer::setThemeName(const QString &themeName)
187 if (m_themeName != themeName) {
188 m_themeName = themeName;
189 Q_EMIT themeNameChanged(m_themeName);
190 }
193void MousePointer::setCustomCursor(const QCursor &customCursor)
195 CursorImageProvider::instance()->setCustomCursor(customCursor);