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"
19#include "InputDispatcherFilter.h"
21#include <QQuickWindow>
24#include <unity/shell/application/MirPlatformCursor.h>
26MousePointer::MousePointer(QQuickItem *parent)
27 : MirMousePointerInterface(parent)
28 , m_cursorName(QStringLiteral("left_ptr"))
29 , m_themeName(QStringLiteral("default"))
31 InputDispatcherFilter::instance()->registerPointer(this);
33 auto mouseMovedFunc = [this]() {
34 if (!isEnabled() || !window()) return;
35 QPointF globalPosition = mapToItem(nullptr, QPointF(0, 0));
36 InputDispatcherFilter::instance()->setPosition(globalPosition);
39 connect(this, &QQuickItem::xChanged, this, mouseMovedFunc);
40 connect(this, &QQuickItem::yChanged, this, mouseMovedFunc);
42 connect(this, &QQuickItem::enabledChanged, this, [this]() {
43 if (!isEnabled()) setVisible(false);
46 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedLeftBoundary,
47 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
48 if (window() && window()->screen() == screen) {
49 Q_EMIT pushedLeftBoundary(amount, buttons);
53 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedRightBoundary,
54 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
55 if (window() && window()->screen() == screen) {
56 Q_EMIT pushedRightBoundary(amount, buttons);
61MousePointer::~MousePointer()
63 registerScreen(nullptr);
64 InputDispatcherFilter::instance()->unregisterPointer(this);
67void MousePointer::handleMouseEvent(ulong /*timestamp*/, QPointF /*movement*/, Qt::MouseButtons /*buttons*/,
68 Qt::KeyboardModifiers /*modifiers*/)
72void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
74 Q_ASSERT(parentItem() != nullptr);
76 if (m_confiningItem.isNull()) {
80 QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
82 QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
84 if (newX < confiningRect.x()) {
85 newX = confiningRect.x();
86 } else if (newX > confiningRect.right()) {
87 newX = confiningRect.right();
90 if (newY < confiningRect.y()) {
91 newY = confiningRect.y();
92 } else if (newY > confiningRect.bottom()) {
93 newY = confiningRect.bottom();
97void MousePointer::handleWheelEvent(ulong /*timestamp*/, QPoint /*angleDelta*/, Qt::KeyboardModifiers /*modifiers*/)
101int MousePointer::topBoundaryOffset() const
103 return m_topBoundaryOffset;
106void MousePointer::setTopBoundaryOffset(int topBoundaryOffset)
108 if (m_topBoundaryOffset == topBoundaryOffset)
111 m_topBoundaryOffset = topBoundaryOffset;
112 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
115void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
117 if (change == ItemSceneChange) {
118 registerWindow(value.window);
122void MousePointer::registerWindow(QWindow *window)
124 if (window == m_registeredWindow) {
128 if (m_registeredWindow) {
129 m_registeredWindow->disconnect(this);
132 m_registeredWindow = window;
134 if (m_registeredWindow) {
135 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
136 registerScreen(window->screen());
138 registerScreen(nullptr);
142void MousePointer::registerScreen(QScreen *screen)
144 if (m_registeredScreen == screen) {
148 if (m_registeredScreen) {
149 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
150 if (previousCursor) {
151 previousCursor->unregisterMousePointer(this);
153 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
157 m_registeredScreen = screen;
159 if (m_registeredScreen) {
160 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
162 cursor->registerMousePointer(this);
164 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
169void MousePointer::setCursorName(const QString &cursorName)
171 if (cursorName != m_cursorName) {
172 m_cursorName = cursorName;
173 Q_EMIT cursorNameChanged(m_cursorName);
177void MousePointer::setThemeName(const QString &themeName)
179 if (m_themeName != themeName) {
180 m_themeName = themeName;
181 Q_EMIT themeNameChanged(m_themeName);
185void MousePointer::setCustomCursor(const QCursor &customCursor)
187 CursorImageProvider::instance()->setCustomCursor(customCursor);
190QQuickItem* MousePointer::confiningItem() const
192 return m_confiningItem.data();
195void MousePointer::setConfiningItem(QQuickItem *item)
197 if (item != m_confiningItem) {
198 m_confiningItem = item;
199 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