Unity 8
MousePointer.cpp
1 /*
2  * Copyright (C) 2015-2016 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(QStringLiteral("left_ptr"))
31  , m_themeName(QStringLiteral("default"))
32 {
33 }
34 
35 void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
36  Qt::KeyboardModifiers modifiers)
37 {
38  if (!parentItem()) {
39  return;
40  }
41 
42  if (!movement.isNull()) {
43  Q_EMIT mouseMoved();
44  }
45 
46  m_accumulatedMovement += movement;
47  // don't apply the fractional part
48  QPointF appliedMovement(int(m_accumulatedMovement.x()), int(m_accumulatedMovement.y()));
49  m_accumulatedMovement -= appliedMovement;
50 
51  qreal newX = x() + appliedMovement.x();
52  if (newX < 0) {
53  Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
54  newX = 0;
55  } else if (newX >= parentItem()->width()) {
56  Q_EMIT pushedRightBoundary(newX - (parentItem()->width() - 1), buttons);
57  newX = parentItem()->width() - 1;
58  }
59  setX(newX);
60 
61  qreal newY = y() + appliedMovement.y();
62  if (newY < 0) {
63  newY = 0;
64  } else if (newY >= parentItem()->height()) {
65  newY = parentItem()->height() - 1;
66  }
67  setY(newY);
68 
69  QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
70  QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
71  buttons, modifiers);
72 }
73 
74 void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
75 {
76  if (!parentItem()) {
77  return;
78  }
79 
80  QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
81  QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
82  QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
83 }
84 
85 void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
86 {
87  if (change == ItemSceneChange) {
88  registerWindow(value.window);
89  }
90 }
91 
92 void MousePointer::registerWindow(QWindow *window)
93 {
94  if (window == m_registeredWindow) {
95  return;
96  }
97 
98  if (m_registeredWindow) {
99  m_registeredWindow->disconnect(this);
100  }
101 
102  m_registeredWindow = window;
103 
104  if (m_registeredWindow) {
105  connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
106  registerScreen(window->screen());
107  } else {
108  registerScreen(nullptr);
109  }
110 }
111 
112 void MousePointer::registerScreen(QScreen *screen)
113 {
114  if (m_registeredScreen == screen) {
115  return;
116  }
117 
118  if (m_registeredScreen) {
119  auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
120  if (previousCursor) {
121  previousCursor->setMousePointer(nullptr);
122  } else {
123  qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
124  }
125  }
126 
127  m_registeredScreen = screen;
128 
129  if (m_registeredScreen) {
130  auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
131  if (cursor) {
132  cursor->setMousePointer(this);
133  } else {
134  qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
135  }
136  }
137 }
138 
139 void MousePointer::setCursorName(const QString &cursorName)
140 {
141  if (cursorName != m_cursorName) {
142  m_cursorName = cursorName;
143  Q_EMIT cursorNameChanged(m_cursorName);
144  }
145 }
146 
147 void MousePointer::setThemeName(const QString &themeName)
148 {
149  if (m_themeName != themeName) {
150  m_themeName = themeName;
151  Q_EMIT themeNameChanged(m_themeName);
152  }
153 }
154 
155 void MousePointer::setCustomCursor(const QCursor &customCursor)
156 {
157  CursorImageProvider::instance()->setCustomCursor(customCursor);
158 }