Unity 8
MousePointer.cpp
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  */
16 
17 #include "MousePointer.h"
18 #include "CursorImageProvider.h"
19 
20 // Unity API
21 #include <unity/shell/application/MirPlatformCursor.h>
22 
23 #include <QQuickWindow>
24 #include <QGuiApplication>
25 
26 #include <qpa/qwindowsysteminterface.h>
27 
28 MousePointer::MousePointer(QQuickItem *parent)
29  : MirMousePointerInterface(parent)
30  , m_cursorName("left_ptr")
31  , m_themeName("default")
32  , m_hotspotX(0)
33  , m_hotspotY(0)
34 {
35 }
36 
37 void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
38  Qt::KeyboardModifiers modifiers)
39 {
40  if (!parentItem()) {
41  return;
42  }
43 
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);
51 
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);
59 
60  QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
61  QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
62  buttons, modifiers);
63 }
64 
65 void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
66 {
67  if (change == ItemSceneChange) {
68  registerWindow(value.window);
69  }
70 }
71 
72 void MousePointer::registerWindow(QWindow *window)
73 {
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  }
82 
83  m_registeredWindow = window;
84 
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  }
93 }
94 
95 void MousePointer::setCursorName(const QString &cursorName)
96 {
97  if (cursorName != m_cursorName) {
98  m_cursorName = cursorName;
99  Q_EMIT cursorNameChanged(m_cursorName);
100  updateHotspot();
101  }
102 }
103 
104 void MousePointer::updateHotspot()
105 {
106  QPoint newHotspot = CursorImageProvider::instance()->hotspot(m_themeName, m_cursorName);
107 
108  if (m_hotspotX != newHotspot.x()) {
109  m_hotspotX = newHotspot.x();
110  Q_EMIT hotspotXChanged(m_hotspotX);
111  }
112 
113  if (m_hotspotY != newHotspot.y()) {
114  m_hotspotY = newHotspot.y();
115  Q_EMIT hotspotYChanged(m_hotspotY);
116  }
117 }
118 
119 void MousePointer::setThemeName(const QString &themeName)
120 {
121  if (m_themeName != themeName) {
122  m_themeName = themeName;
123  Q_EMIT themeNameChanged(m_themeName);
124  }
125 }