on f940ff8 help sign in
unity8/plugins/Cursor/MousePointer.cpp
181 lines 6.2 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_panelHeight) { // top left corner
59 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-m_panelHeight, 2));
60 Q_EMIT pushedTopLeftCorner(qAbs(distance), buttons);
61 } else if (newX >= sceneWidth && newY < m_panelHeight) { // top right corner
62 const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-m_panelHeight, 2));
63 Q_EMIT pushedTopRightCorner(qAbs(distance), buttons);
64 } else if (newX < 0 && newY >= sceneHeight) { // bottom left corner
65 const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-sceneHeight, 2));
66 Q_EMIT pushedBottomLeftCorner(qAbs(distance), buttons);
67 } else if (newX >= sceneWidth && newY >= sceneHeight) { // bottom right corner
68 const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-sceneHeight, 2));
69 Q_EMIT pushedBottomRightCorner(qAbs(distance), buttons);
70 } else if (newX < 0) { // left edge
71 Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
72 } else if (newX >= sceneWidth) { // right edge
73 Q_EMIT pushedRightBoundary(newX - (sceneWidth - 1), buttons);
74 } else if (newY < m_panelHeight) { // top edge
75 Q_EMIT pushedTopBoundary(qAbs(newY - m_panelHeight), buttons);
76 } else if (Q_LIKELY(newX > 0 && newX < sceneWidth-1 && newY > 0 && newY < sceneHeight-1)) { // normal pos, not pushing
77 Q_EMIT pushStopped();
78 }
80 setX(qBound(0.0, newX, sceneWidth - 1));
81 setY(qBound(0.0, newY, sceneHeight - 1));
83 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
84 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
85 buttons, modifiers);
88void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
90 if (!parentItem()) {
91 return;
92 }
94 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
95 QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
96 QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
99void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
101 if (change == ItemSceneChange) {
102 registerWindow(value.window);
103 if (value.window) {
104 m_panelHeight = QQmlProperty::read(this, "panelHeight").toInt(); // when we move e.g. to a different screen (with possible different DGU)
105 }
106 }
109void MousePointer::componentComplete()
111 MirMousePointerInterface::componentComplete();
112 m_panelHeight = QQmlProperty::read(this, "panelHeight").toInt();
115void MousePointer::registerWindow(QWindow *window)
117 if (window == m_registeredWindow) {
118 return;
119 }
121 if (m_registeredWindow) {
122 m_registeredWindow->disconnect(this);
123 }
125 m_registeredWindow = window;
127 if (m_registeredWindow) {
128 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
129 registerScreen(window->screen());
130 } else {
131 registerScreen(nullptr);
132 }
135void MousePointer::registerScreen(QScreen *screen)
137 if (m_registeredScreen == screen) {
138 return;
139 }
141 if (m_registeredScreen) {
142 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
143 if (previousCursor) {
144 previousCursor->setMousePointer(nullptr);
145 } else {
146 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
147 }
148 }
150 m_registeredScreen = screen;
152 if (m_registeredScreen) {
153 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
154 if (cursor) {
155 cursor->setMousePointer(this);
156 } else {
157 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
158 }
159 }
162void MousePointer::setCursorName(const QString &cursorName)
164 if (cursorName != m_cursorName) {
165 m_cursorName = cursorName;
166 Q_EMIT cursorNameChanged(m_cursorName);
167 }
170void MousePointer::setThemeName(const QString &themeName)
172 if (m_themeName != themeName) {
173 m_themeName = themeName;
174 Q_EMIT themeNameChanged(m_themeName);
175 }
178void MousePointer::setCustomCursor(const QCursor &customCursor)
180 CursorImageProvider::instance()->setCustomCursor(customCursor);