on 14bbc75 help sign in
unity8/plugins/Cursor/MousePointer.cpp
195 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 <QtMath>
27#include <qpa/qwindowsysteminterface.h>
29MousePointer::MousePointer(QQuickItem *parent)
30 : MirMousePointerInterface(parent)
31 , m_cursorName(QStringLiteral("left_ptr"))
32 , m_themeName(QStringLiteral("default"))
36void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
37 Qt::KeyboardModifiers modifiers)
39 if (!parentItem()) {
40 return;
41 }
43 if (!movement.isNull()) {
44 Q_EMIT mouseMoved();
45 }
47 m_accumulatedMovement += movement;
48 // don't apply the fractional part
49 QPointF appliedMovement(int(m_accumulatedMovement.x()), int(m_accumulatedMovement.y()));
50 m_accumulatedMovement -= appliedMovement;
52 const qreal newX = x() + appliedMovement.x();
53 const qreal newY = y() + appliedMovement.y();
54 const qreal sceneWidth = parentItem()->width();
55 const qreal sceneHeight = parentItem()->height();
57 if (newX <= 0 && newY < m_topBoundaryOffset) { // top left corner
58 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-m_topBoundaryOffset, 2));
59 Q_EMIT pushedTopLeftCorner(qAbs(distance), buttons);
60 m_pushing = true;
61 } else if (newX >= sceneWidth-1 && newY < m_topBoundaryOffset) { // top right corner
62 const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-m_topBoundaryOffset, 2));
63 Q_EMIT pushedTopRightCorner(qAbs(distance), buttons);
64 m_pushing = true;
65 } else if (newX < 0 && newY >= sceneHeight-1) { // bottom left corner
66 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-sceneHeight, 2));
67 Q_EMIT pushedBottomLeftCorner(qAbs(distance), buttons);
68 m_pushing = true;
69 } else if (newX >= sceneWidth-1 && newY >= sceneHeight-1) { // bottom right corner
70 const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-sceneHeight, 2));
71 Q_EMIT pushedBottomRightCorner(qAbs(distance), buttons);
72 m_pushing = true;
73 } else if (newX < 0) { // left edge
74 Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
75 m_pushing = true;
76 } else if (newX >= sceneWidth) { // right edge
77 Q_EMIT pushedRightBoundary(newX - (sceneWidth - 1), buttons);
78 m_pushing = true;
79 } else if (newY < m_topBoundaryOffset) { // top edge
80 Q_EMIT pushedTopBoundary(qAbs(newY - m_topBoundaryOffset), buttons);
81 m_pushing = true;
82 } else if (Q_LIKELY(newX > 0 && newX < sceneWidth-1 && newY > 0 && newY < sceneHeight-1)) { // normal pos, not pushing
83 if (m_pushing) {
84 Q_EMIT pushStopped();
85 m_pushing = false;
86 }
87 }
89 setX(qBound(0.0, newX, sceneWidth - 1));
90 setY(qBound(0.0, newY, sceneHeight - 1));
92 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
93 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
94 buttons, modifiers);
97void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
99 if (!parentItem()) {
100 return;
101 }
103 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
104 QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
105 QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
108int MousePointer::topBoundaryOffset() const
110 return m_topBoundaryOffset;
113void MousePointer::setTopBoundaryOffset(int topBoundaryOffset)
115 if (m_topBoundaryOffset == topBoundaryOffset)
116 return;
118 m_topBoundaryOffset = topBoundaryOffset;
119 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
122void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
124 if (change == ItemSceneChange) {
125 registerWindow(value.window);
126 }
129void MousePointer::registerWindow(QWindow *window)
131 if (window == m_registeredWindow) {
132 return;
133 }
135 if (m_registeredWindow) {
136 m_registeredWindow->disconnect(this);
137 }
139 m_registeredWindow = window;
141 if (m_registeredWindow) {
142 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
143 registerScreen(window->screen());
144 } else {
145 registerScreen(nullptr);
146 }
149void MousePointer::registerScreen(QScreen *screen)
151 if (m_registeredScreen == screen) {
152 return;
153 }
155 if (m_registeredScreen) {
156 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
157 if (previousCursor) {
158 previousCursor->setMousePointer(nullptr);
159 } else {
160 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
161 }
162 }
164 m_registeredScreen = screen;
166 if (m_registeredScreen) {
167 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
168 if (cursor) {
169 cursor->setMousePointer(this);
170 } else {
171 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
172 }
173 }
176void MousePointer::setCursorName(const QString &cursorName)
178 if (cursorName != m_cursorName) {
179 m_cursorName = cursorName;
180 Q_EMIT cursorNameChanged(m_cursorName);
181 }
184void MousePointer::setThemeName(const QString &themeName)
186 if (m_themeName != themeName) {
187 m_themeName = themeName;
188 Q_EMIT themeNameChanged(m_themeName);
189 }
192void MousePointer::setCustomCursor(const QCursor &customCursor)
194 CursorImageProvider::instance()->setCustomCursor(customCursor);