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 #include <QtMath>
26 
27 #include <qpa/qwindowsysteminterface.h>
28 
29 MousePointer::MousePointer(QQuickItem *parent)
30  : MirMousePointerInterface(parent)
31  , m_cursorName(QStringLiteral("left_ptr"))
32  , m_themeName(QStringLiteral("default"))
33 {
34 }
35 
36 void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
37  Qt::KeyboardModifiers modifiers)
38 {
39  if (!parentItem()) {
40  return;
41  }
42 
43  if (!movement.isNull()) {
44  Q_EMIT mouseMoved();
45  }
46 
47  m_accumulatedMovement += movement;
48  // don't apply the fractional part
49  QPointF appliedMovement(int(m_accumulatedMovement.x()), int(m_accumulatedMovement.y()));
50  m_accumulatedMovement -= appliedMovement;
51 
52  qreal newX = x() + appliedMovement.x();
53  qreal newY = y() + appliedMovement.y();
54  const qreal sceneWidth = parentItem()->width();
55  const qreal sceneHeight = parentItem()->height();
56 
57  if (newX <= 0 && newY < m_topBoundaryOffset) { // top left corner
58  const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-m_topBoundaryOffset, 2));
59  Q_EMIT pushedTopLeftCorner(qAbs(distance), buttons);
60  m_pushing = true;
61  } else if (newX >= sceneWidth-1 && newY < m_topBoundaryOffset) { // top right corner
62  const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-m_topBoundaryOffset, 2));
63  Q_EMIT pushedTopRightCorner(qAbs(distance), buttons);
64  m_pushing = true;
65  } else if (newX < 0 && newY >= sceneHeight-1) { // bottom left corner
66  const auto distance = qSqrt(qPow(newX, 2) + qPow(newY-sceneHeight, 2));
67  Q_EMIT pushedBottomLeftCorner(qAbs(distance), buttons);
68  m_pushing = true;
69  } else if (newX >= sceneWidth-1 && newY >= sceneHeight-1) { // bottom right corner
70  const auto distance = qSqrt(qPow(newX-sceneWidth, 2) + qPow(newY-sceneHeight, 2));
71  Q_EMIT pushedBottomRightCorner(qAbs(distance), buttons);
72  m_pushing = true;
73  } else if (newX < 0) { // left edge
74  Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
75  m_pushing = true;
76  } else if (newX >= sceneWidth) { // right edge
77  Q_EMIT pushedRightBoundary(newX - (sceneWidth - 1), buttons);
78  m_pushing = true;
79  } else if (newY < m_topBoundaryOffset) { // top edge
80  Q_EMIT pushedTopBoundary(qAbs(newY - m_topBoundaryOffset), buttons);
81  m_pushing = true;
82  } else if (Q_LIKELY(newX > 0 && newX < sceneWidth-1 && newY > 0 && newY < sceneHeight-1)) { // normal pos, not pushing
83  if (m_pushing) {
84  Q_EMIT pushStopped();
85  m_pushing = false;
86  }
87  }
88 
89  applyItemConfinement(newX, newY);
90 
91  setX(qBound(0.0, newX, sceneWidth - 1));
92  setY(qBound(0.0, newY, sceneHeight - 1));
93 
94  QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
95  QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
96  buttons, modifiers);
97 }
98 
99 void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
100 {
101  Q_ASSERT(parentItem() != nullptr);
102 
103  if (m_confiningItem.isNull()) {
104  return;
105  }
106 
107  QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
108 
109  QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
110 
111  if (newX < confiningRect.x()) {
112  newX = confiningRect.x();
113  } else if (newX > confiningRect.right()) {
114  newX = confiningRect.right();
115  }
116 
117  if (newY < confiningRect.y()) {
118  newY = confiningRect.y();
119  } else if (newY > confiningRect.bottom()) {
120  newY = confiningRect.bottom();
121  }
122 }
123 
124 void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
125 {
126  if (!parentItem()) {
127  return;
128  }
129 
130  QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
131  QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
132  QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
133 }
134 
135 int MousePointer::topBoundaryOffset() const
136 {
137  return m_topBoundaryOffset;
138 }
139 
140 void MousePointer::setTopBoundaryOffset(int topBoundaryOffset)
141 {
142  if (m_topBoundaryOffset == topBoundaryOffset)
143  return;
144 
145  m_topBoundaryOffset = topBoundaryOffset;
146  Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
147 }
148 
149 void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
150 {
151  if (change == ItemSceneChange) {
152  registerWindow(value.window);
153  }
154 }
155 
156 void MousePointer::registerWindow(QWindow *window)
157 {
158  if (window == m_registeredWindow) {
159  return;
160  }
161 
162  if (m_registeredWindow) {
163  m_registeredWindow->disconnect(this);
164  }
165 
166  m_registeredWindow = window;
167 
168  if (m_registeredWindow) {
169  connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
170  registerScreen(window->screen());
171  } else {
172  registerScreen(nullptr);
173  }
174 }
175 
176 void MousePointer::registerScreen(QScreen *screen)
177 {
178  if (m_registeredScreen == screen) {
179  return;
180  }
181 
182  if (m_registeredScreen) {
183  auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
184  if (previousCursor) {
185  previousCursor->setMousePointer(nullptr);
186  } else {
187  qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
188  }
189  }
190 
191  m_registeredScreen = screen;
192 
193  if (m_registeredScreen) {
194  auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
195  if (cursor) {
196  cursor->setMousePointer(this);
197  } else {
198  qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
199  }
200  }
201 }
202 
203 void MousePointer::setCursorName(const QString &cursorName)
204 {
205  if (cursorName != m_cursorName) {
206  m_cursorName = cursorName;
207  Q_EMIT cursorNameChanged(m_cursorName);
208  }
209 }
210 
211 void MousePointer::setThemeName(const QString &themeName)
212 {
213  if (m_themeName != themeName) {
214  m_themeName = themeName;
215  Q_EMIT themeNameChanged(m_themeName);
216  }
217 }
218 
219 void MousePointer::setCustomCursor(const QCursor &customCursor)
220 {
221  CursorImageProvider::instance()->setCustomCursor(customCursor);
222 }
223 
224 QQuickItem* MousePointer::confiningItem() const
225 {
226  return m_confiningItem.data();
227 }
228 
229 void MousePointer::setConfiningItem(QQuickItem *item)
230 {
231  if (item != m_confiningItem) {
232  m_confiningItem = item;
233  Q_EMIT confiningItemChanged();
234  }
235 }