2
* Copyright (C) 2015 Canonical, Ltd.
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.
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.
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/>.
17
#include "MousePointer.h"
18
#include "CursorImageProvider.h"
21
#include <unity/shell/application/MirPlatformCursor.h>
23
#include <QQuickWindow>
24
#include <QGuiApplication>
26
#include <qpa/qwindowsysteminterface.h>
28
MousePointer::MousePointer(QQuickItem *parent)
29
: MirMousePointerInterface(parent)
30
, m_cursorName("left_ptr")
31
, m_themeName("default")
37
void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
38
Qt::KeyboardModifiers modifiers)
44
qreal newX = x() + movement.x();
47
} else if (newX > parentItem()->width()) {
48
newX = parentItem()->width();
52
qreal newY = y() + movement.y();
55
} else if (newY > parentItem()->height()) {
56
newY = parentItem()->height();
60
QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
61
QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
65
void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
67
if (change == ItemSceneChange) {
68
registerWindow(value.window);
72
void MousePointer::registerWindow(QWindow *window)
74
if (m_registeredWindow && window != m_registeredWindow) {
75
auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredWindow->screen()->handle()->cursor());
77
previousCursor->setMousePointer(nullptr);
79
qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
83
m_registeredWindow = window;
85
if (m_registeredWindow) {
86
auto cursor = dynamic_cast<MirPlatformCursor*>(window->screen()->handle()->cursor());
88
cursor->setMousePointer(this);
90
qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
95
void MousePointer::setCursorName(const QString &cursorName)
97
if (cursorName != m_cursorName) {
98
m_cursorName = cursorName;
99
Q_EMIT cursorNameChanged(m_cursorName);
104
void 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);
113
if (m_hotspotY != newHotspot.y()) {
114
m_hotspotY = newHotspot.y();
115
Q_EMIT hotspotYChanged(m_hotspotY);
119
void MousePointer::setThemeName(const QString &themeName)
121
if (m_themeName != themeName) {
122
m_themeName = themeName;
123
Q_EMIT themeNameChanged(m_themeName);