2 * Copyright (C) 2015-2016 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>
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)
42 if (!movement.isNull()) {
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();
53 Q_EMIT pushedLeftBoundary(qAbs(newX), buttons);
55 } else if (newX >= parentItem()->width()) {
56 Q_EMIT pushedRightBoundary(newX - (parentItem()->width() - 1), buttons);
57 newX = parentItem()->width() - 1;
60 qreal newY = y() + appliedMovement.y();
63 } else if (newY >= parentItem()->height()) {
64 newY = parentItem()->height() - 1;
67 applyItemConfinement(newX, newY);
72 QPointF scenePosition = mapToItem(nullptr, QPointF(0, 0));
73 QWindowSystemInterface::handleMouseEvent(window(), timestamp, scenePosition /*local*/, scenePosition /*global*/,
77void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
79 Q_ASSERT(parentItem() != nullptr);
81 if (m_confiningItem.isNull()) {
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();
95 if (newY < confiningRect.y()) {
96 newY = confiningRect.y();
97 } else if (newY > confiningRect.bottom()) {
98 newY = confiningRect.bottom();
102void MousePointer::handleWheelEvent(ulong timestamp, QPoint angleDelta, Qt::KeyboardModifiers modifiers)
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);
120void MousePointer::registerWindow(QWindow *window)
122 if (window == m_registeredWindow) {
126 if (m_registeredWindow) {
127 m_registeredWindow->disconnect(this);
130 m_registeredWindow = window;
132 if (m_registeredWindow) {
133 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
134 registerScreen(window->screen());
136 registerScreen(nullptr);
140void MousePointer::registerScreen(QScreen *screen)
142 if (m_registeredScreen == screen) {
146 if (m_registeredScreen) {
147 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
148 if (previousCursor) {
149 previousCursor->setMousePointer(nullptr);
151 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
155 m_registeredScreen = screen;
157 if (m_registeredScreen) {
158 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
160 cursor->setMousePointer(this);
162 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
167void MousePointer::setCursorName(const QString &cursorName)
169 if (cursorName != m_cursorName) {
170 m_cursorName = cursorName;
171 Q_EMIT cursorNameChanged(m_cursorName);
175void MousePointer::setThemeName(const QString &themeName)
177 if (m_themeName != themeName) {
178 m_themeName = themeName;
179 Q_EMIT themeNameChanged(m_themeName);
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();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list