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