on c7cb640 help sign in
unity8/plugins/Cursor/MousePointer.cpp
144 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 qreal newX = x() + movement.x();
46 if (newX < 0) {
47 Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
48 newX = 0;
49 } else if (newX > parentItem()->width()) {
50 Q_EMIT pushedRightBoundary(newX - parentItem()->width(), buttons);
51 newX = parentItem()->width();
52 }
53 setX(newX);
55 qreal newY = y() + movement.y();
56 if (newY < 0) {
57 newY = 0;
58 } else if (newY > parentItem()->height()) {
59 newY = parentItem()->height();
60 }
61 setY(newY);
63 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
64 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
65 buttons, modifiers);
68void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
70 if (!parentItem()) {
71 return;
72 }
74 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
75 QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
76 QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
79void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
81 if (change == ItemSceneChange) {
82 registerWindow(value.window);
83 }
86void MousePointer::registerWindow(QWindow *window)
88 if (m_registeredWindow && window != m_registeredWindow) {
89 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredWindow->screen()->handle()->cursor());
90 if (previousCursor) {
91 previousCursor->setMousePointer(nullptr);
92 } else {
93 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
94 }
95 }
97 m_registeredWindow = window;
99 if (m_registeredWindow) {
100 auto cursor = dynamic_cast<MirPlatformCursor*>(window->screen()->handle()->cursor());
101 if (cursor) {
102 cursor->setMousePointer(this);
103 } else {
104 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
105 }
106 }
109void MousePointer::setCursorName(const QString &cursorName)
111 if (cursorName != m_cursorName) {
112 m_cursorName = cursorName;
113 Q_EMIT cursorNameChanged(m_cursorName);
114 updateHotspot();
115 }
118void MousePointer::updateHotspot()
120 QPoint newHotspot = CursorImageProvider::instance()->hotspot(m_themeName, m_cursorName);
122 if (m_hotspotX != newHotspot.x()) {
123 m_hotspotX = newHotspot.x();
124 Q_EMIT hotspotXChanged(m_hotspotX);
125 }
127 if (m_hotspotY != newHotspot.y()) {
128 m_hotspotY = newHotspot.y();
129 Q_EMIT hotspotYChanged(m_hotspotY);
130 }
133void MousePointer::setThemeName(const QString &themeName)
135 if (m_themeName != themeName) {
136 m_themeName = themeName;
137 Q_EMIT themeNameChanged(m_themeName);
138 }
141void MousePointer::setCustomCursor(const QCursor &customCursor)
143 CursorImageProvider::instance()->setCustomCursor(customCursor);