on 86231ce help sign in
unity8/plugins/Cursor/MousePointer.cpp
148 lines 4.4 kB history raw
1/*
2 * Copyright (C) 2015 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>
26#include <qpa/qwindowsysteminterface.h>
28MousePointer::MousePointer(QQuickItem *parent)
29 : MirMousePointerInterface(parent)
30 , m_cursorName(QStringLiteral("left_ptr"))
31 , m_themeName(QStringLiteral("default"))
32 , m_hotspotX(0)
33 , m_hotspotY(0)
35 updateHotspot();
38void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
39 Qt::KeyboardModifiers modifiers)
41 if (!parentItem()) {
42 return;
43 }
45 if (!movement.isNull()) {
46 Q_EMIT mouseMoved();
47 }
49 qreal newX = x() + movement.x();
50 if (newX < 0) {
51 Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
52 newX = 0;
53 } else if (newX > parentItem()->width()) {
54 Q_EMIT pushedRightBoundary(newX - parentItem()->width(), buttons);
55 newX = parentItem()->width();
56 }
57 setX(newX);
59 qreal newY = y() + movement.y();
60 if (newY < 0) {
61 newY = 0;
62 } else if (newY > parentItem()->height()) {
63 newY = parentItem()->height();
64 }
65 setY(newY);
67 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
68 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
69 buttons, modifiers);
72void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
74 if (!parentItem()) {
75 return;
76 }
78 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
79 QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
80 QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
83void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
85 if (change == ItemSceneChange) {
86 registerWindow(value.window);
87 }
90void MousePointer::registerWindow(QWindow *window)
92 if (m_registeredWindow && window != m_registeredWindow) {
93 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredWindow->screen()->handle()->cursor());
94 if (previousCursor) {
95 previousCursor->setMousePointer(nullptr);
96 } else {
97 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
98 }
99 }
101 m_registeredWindow = window;
103 if (m_registeredWindow) {
104 auto cursor = dynamic_cast<MirPlatformCursor*>(window->screen()->handle()->cursor());
105 if (cursor) {
106 cursor->setMousePointer(this);
107 } else {
108 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
109 }
110 }
113void MousePointer::setCursorName(const QString &cursorName)
115 if (cursorName != m_cursorName) {
116 m_cursorName = cursorName;
117 Q_EMIT cursorNameChanged(m_cursorName);
118 updateHotspot();
119 }
122void MousePointer::updateHotspot()
124 QPoint newHotspot = CursorImageProvider::instance()->hotspot(m_themeName, m_cursorName);
126 if (m_hotspotX != newHotspot.x()) {
127 m_hotspotX = newHotspot.x();
128 Q_EMIT hotspotXChanged(m_hotspotX);
129 }
131 if (m_hotspotY != newHotspot.y()) {
132 m_hotspotY = newHotspot.y();
133 Q_EMIT hotspotYChanged(m_hotspotY);
134 }
137void MousePointer::setThemeName(const QString &themeName)
139 if (m_themeName != themeName) {
140 m_themeName = themeName;
141 Q_EMIT themeNameChanged(m_themeName);
142 }
145void MousePointer::setCustomCursor(const QCursor &customCursor)
147 CursorImageProvider::instance()->setCustomCursor(customCursor);