Lomiri
Loading...
Searching...
No Matches
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#include "InputDispatcherFilter.h"
20
21#include <QQuickWindow>
22
23// Lomiri API
24#include <lomiri/shell/application/MirPlatformCursor.h>
25
26MousePointer::MousePointer(QQuickItem *parent)
27 : MirMousePointerInterface(parent)
28 , m_cursorName(QStringLiteral("left_ptr"))
29 , m_themeName(QStringLiteral("default"))
30{
31 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedLeftBoundary,
32 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
33 if (window() && window()->screen() == screen) {
34 Q_EMIT pushedLeftBoundary(amount, buttons);
35 }
36 });
37 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedRightBoundary,
38 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
39 if (window() && window()->screen() == screen) {
40 Q_EMIT pushedRightBoundary(amount, buttons);
41 }
42 });
43 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedTopBoundary,
44 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
45 if (window() && window()->screen() == screen) {
46 Q_EMIT pushedTopBoundary(amount, buttons);
47 }
48 });
49 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedTopLeftCorner,
50 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
51 if (window() && window()->screen() == screen) {
52 Q_EMIT pushedTopLeftCorner(amount, buttons);
53 }
54 });
55 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedTopRightCorner,
56 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
57 if (window() && window()->screen() == screen) {
58 Q_EMIT pushedTopRightCorner(amount, buttons);
59 }
60 });
61 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedBottomLeftCorner,
62 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
63 if (window() && window()->screen() == screen) {
64 Q_EMIT pushedBottomLeftCorner(amount, buttons);
65 }
66 });
67 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushedBottomRightCorner,
68 this, [this](QScreen* screen, qreal amount, Qt::MouseButtons buttons) {
69 if (window() && window()->screen() == screen) {
70 Q_EMIT pushedBottomRightCorner(amount, buttons);
71 }
72 });
73 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushStopped,
74 this, [this](QScreen* screen) {
75 if (window() && window()->screen() == screen) {
76 Q_EMIT pushStopped();
77 }
78 });
79 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::screenEntered,
80 this, [this](QScreen* screen) {
81 if (window() && window()->screen() == screen) {
82 Q_EMIT screenEntered();
83 }
84 });
85
86 InputDispatcherFilter::instance()->registerPointer(this);
87}
88
89MousePointer::~MousePointer()
90{
91 registerScreen(nullptr);
92 InputDispatcherFilter::instance()->unregisterPointer(this);
93}
94
95void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
96{
97 Q_ASSERT(parentItem() != nullptr);
98
99 if (m_confiningItem.isNull()) {
100 return;
101 }
102
103 QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
104
105 QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
106
107 if (newX < confiningRect.x()) {
108 newX = confiningRect.x();
109 } else if (newX > confiningRect.right()) {
110 newX = confiningRect.right();
111 }
112
113 if (newY < confiningRect.y()) {
114 newY = confiningRect.y();
115 } else if (newY > confiningRect.bottom()) {
116 newY = confiningRect.bottom();
117 }
118}
119
120int MousePointer::topBoundaryOffset() const
121{
122 return m_topBoundaryOffset;
123}
124
125void MousePointer::setTopBoundaryOffset(int topBoundaryOffset)
126{
127 if (m_topBoundaryOffset == topBoundaryOffset)
128 return;
129
130 m_topBoundaryOffset = topBoundaryOffset;
131 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
132}
133
134void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
135{
136 if (change == ItemSceneChange) {
137 registerWindow(value.window);
138 }
139}
140
141void MousePointer::registerWindow(QWindow *window)
142{
143 if (window == m_registeredWindow) {
144 return;
145 }
146
147 if (m_registeredWindow) {
148 m_registeredWindow->disconnect(this);
149 }
150
151 m_registeredWindow = window;
152
153 if (m_registeredWindow) {
154 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
155 registerScreen(window->screen());
156 } else {
157 registerScreen(nullptr);
158 }
159}
160
161void MousePointer::registerScreen(QScreen *screen)
162{
163 if (m_registeredScreen == screen) {
164 return;
165 }
166
167 if (m_registeredScreen) {
168 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
169 if (previousCursor) {
170 previousCursor->unregisterMousePointer(this);
171 } else {
172 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
173 }
174 }
175
176 m_registeredScreen = screen;
177
178 if (m_registeredScreen) {
179 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
180 if (cursor) {
181 cursor->registerMousePointer(this);
182 } else {
183 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
184 }
185 }
186}
187
188void MousePointer::setCursorName(const QString &cursorName)
189{
190 if (cursorName != m_cursorName) {
191 m_cursorName = cursorName;
192 Q_EMIT cursorNameChanged(m_cursorName);
193 }
194}
195
196void MousePointer::setThemeName(const QString &themeName)
197{
198 if (m_themeName != themeName) {
199 m_themeName = themeName;
200 Q_EMIT themeNameChanged(m_themeName);
201 }
202}
203
204void MousePointer::moveTo(const QPoint &position)
205{
206 setPosition(position);
207 Q_EMIT mouseMoved();
208}
209
210void MousePointer::setCustomCursor(const QCursor &customCursor)
211{
212 CursorImageProvider::instance()->setCustomCursor(customCursor);
213}
214
215QQuickItem* MousePointer::confiningItem() const
216{
217 return m_confiningItem.data();
218}
219
220void MousePointer::setConfiningItem(QQuickItem *item)
221{
222 if (item != m_confiningItem) {
223 m_confiningItem = item;
224 Q_EMIT confiningItemChanged();
225 }
226}