on 999ef19 help sign in
unity8/plugins/Cursor/MousePointer.cpp
199 lines 5.7 kB history raw
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 */
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(QStringLiteral("left_ptr"))
31 , m_themeName(QStringLiteral("default"))
35void MousePointer::handleMouseEvent(ulong timestamp, QPointF movement, Qt::MouseButtons buttons,
36 Qt::KeyboardModifiers modifiers)
38 if (!parentItem()) {
39 return;
40 }
42 if (!movement.isNull()) {
43 Q_EMIT mouseMoved();
44 }
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;
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 }
60 qreal newY = y() + appliedMovement.y();
61 if (newY < 0) {
62 newY = 0;
63 } else if (newY >= parentItem()->height()) {
64 newY = parentItem()->height() - 1;
65 }
67 applyItemConfinement(newX, newY);
69 setX(newX);
70 setY(newY);
72 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
73 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
74 buttons, modifiers);
77void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
79 Q_ASSERT(parentItem() != nullptr);
81 if (m_confiningItem.isNull()) {
82 return;
83 }
85 QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
87 QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
89 if (newX < confiningRect.x()) {
90 newX = confiningRect.x();
91 } else if (newX > confiningRect.right()) {
92 newX = confiningRect.right();
93 }
95 if (newY < confiningRect.y()) {
96 newY = confiningRect.y();
97 } else if (newY > confiningRect.bottom()) {
98 newY = confiningRect.bottom();
99 }
102void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
104 if (!parentItem()) {
105 return;
106 }
108 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
109 QWindowSystemInterface::handleWheelEvent(window(), timestamp, scenePosition /* local */, scenePosition /* global */,
110 QPoint() /* pixelDelta */, angleDelta, modifiers, Qt::ScrollUpdate);
113void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
115 if (change == ItemSceneChange) {
116 registerWindow(value.window);
117 }
120void MousePointer::registerWindow(QWindow *window)
122 if (window == m_registeredWindow) {
123 return;
124 }
126 if (m_registeredWindow) {
127 m_registeredWindow->disconnect(this);
128 }
130 m_registeredWindow = window;
132 if (m_registeredWindow) {
133 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
134 registerScreen(window->screen());
135 } else {
136 registerScreen(nullptr);
137 }
140void MousePointer::registerScreen(QScreen *screen)
142 if (m_registeredScreen == screen) {
143 return;
144 }
146 if (m_registeredScreen) {
147 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
148 if (previousCursor) {
149 previousCursor->setMousePointer(nullptr);
150 } else {
151 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
152 }
153 }
155 m_registeredScreen = screen;
157 if (m_registeredScreen) {
158 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
159 if (cursor) {
160 cursor->setMousePointer(this);
161 } else {
162 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
163 }
164 }
167void MousePointer::setCursorName(const QString &cursorName)
169 if (cursorName != m_cursorName) {
170 m_cursorName = cursorName;
171 Q_EMIT cursorNameChanged(m_cursorName);
172 }
175void MousePointer::setThemeName(const QString &themeName)
177 if (m_themeName != themeName) {
178 m_themeName = themeName;
179 Q_EMIT themeNameChanged(m_themeName);
180 }
183void MousePointer::setCustomCursor(const QCursor &customCursor)
185 CursorImageProvider::instance()->setCustomCursor(customCursor);
188QQuickItem* MousePointer::confiningItem() const
190 return m_confiningItem.data();
193void MousePointer::setConfiningItem(QQuickItem *item)
195 if (item != m_confiningItem) {
196 m_confiningItem = item;
197 Q_EMIT confiningItemChanged();
198 }