on 03ff1bd help sign in
unity8/plugins/Cursor/MousePointer.cpp
125 lines 3.6 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("left_ptr")
31 , m_themeName("default")
32 , m_hotspotX(0)
33 , m_hotspotY(0)
37void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
38 Qt::KeyboardModifiers modifiers)
40 if (!parentItem()) {
41 return;
42 }
44 qreal newX = x() + movement.x();
45 if (newX < 0) {
46 newX = 0;
47 } else if (newX > parentItem()->width()) {
48 newX = parentItem()->width();
49 }
50 setX(newX);
52 qreal newY = y() + movement.y();
53 if (newY < 0) {
54 newY = 0;
55 } else if (newY > parentItem()->height()) {
56 newY = parentItem()->height();
57 }
58 setY(newY);
60 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
61 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
62 buttons, modifiers);
65void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
67 if (change == ItemSceneChange) {
68 registerWindow(value.window);
69 }
72void MousePointer::registerWindow(QWindow *window)
74 if (m_registeredWindow && window != m_registeredWindow) {
75 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredWindow->screen()->handle()->cursor());
76 if (previousCursor) {
77 previousCursor->setMousePointer(nullptr);
78 } else {
79 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
80 }
81 }
83 m_registeredWindow = window;
85 if (m_registeredWindow) {
86 auto cursor = dynamic_cast<MirPlatformCursor*>(window->screen()->handle()->cursor());
87 if (cursor) {
88 cursor->setMousePointer(this);
89 } else {
90 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
91 }
92 }
95void MousePointer::setCursorName(const QString &cursorName)
97 if (cursorName != m_cursorName) {
98 m_cursorName = cursorName;
99 Q_EMIT cursorNameChanged(m_cursorName);
100 updateHotspot();
101 }
104void MousePointer::updateHotspot()
106 QPoint newHotspot = CursorImageProvider::instance()->hotspot(m_themeName, m_cursorName);
108 if (m_hotspotX != newHotspot.x()) {
109 m_hotspotX = newHotspot.x();
110 Q_EMIT hotspotXChanged(m_hotspotX);
111 }
113 if (m_hotspotY != newHotspot.y()) {
114 m_hotspotY = newHotspot.y();
115 Q_EMIT hotspotYChanged(m_hotspotY);
116 }
119void MousePointer::setThemeName(const QString &themeName)
121 if (m_themeName != themeName) {
122 m_themeName = themeName;
123 Q_EMIT themeNameChanged(m_themeName);
124 }