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
80 InputDispatcherFilter::instance()->registerPointer(this);
81}
82
83MousePointer::~MousePointer()
84{
85 registerScreen(nullptr);
86 InputDispatcherFilter::instance()->unregisterPointer(this);
87}
88
89void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
90{
91 Q_ASSERT(parentItem() != nullptr);
92
93 if (m_confiningItem.isNull()) {
94 return;
95 }
96
97 QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
98
99 QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
100
101 if (newX < confiningRect.x()) {
102 newX = confiningRect.x();
103 } else if (newX > confiningRect.right()) {
104 newX = confiningRect.right();
105 }
106
107 if (newY < confiningRect.y()) {
108 newY = confiningRect.y();
109 } else if (newY > confiningRect.bottom()) {
110 newY = confiningRect.bottom();
111 }
112}
113
114int MousePointer::topBoundaryOffset() const
115{
116 return m_topBoundaryOffset;
117}
118
119void MousePointer::setTopBoundaryOffset(int topBoundaryOffset)
120{
121 if (m_topBoundaryOffset == topBoundaryOffset)
122 return;
123
124 m_topBoundaryOffset = topBoundaryOffset;
125 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
126}
127
128void MousePointer::itemChange(ItemChange change, const ItemChangeData &value)
129{
130 if (change == ItemSceneChange) {
131 registerWindow(value.window);
132 }
133}
134
135void MousePointer::registerWindow(QWindow *window)
136{
137 if (window == m_registeredWindow) {
138 return;
139 }
140
141 if (m_registeredWindow) {
142 m_registeredWindow->disconnect(this);
143 }
144
145 m_registeredWindow = window;
146
147 if (m_registeredWindow) {
148 connect(window, &QWindow::screenChanged, this, &MousePointer::registerScreen);
149 registerScreen(window->screen());
150 } else {
151 registerScreen(nullptr);
152 }
153}
154
155void MousePointer::registerScreen(QScreen *screen)
156{
157 if (m_registeredScreen == screen) {
158 return;
159 }
160
161 if (m_registeredScreen) {
162 auto previousCursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
163 if (previousCursor) {
164 previousCursor->unregisterMousePointer(this);
165 } else {
166 qCritical("QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
167 }
168 }
169
170 m_registeredScreen = screen;
171
172 if (m_registeredScreen) {
173 auto cursor = dynamic_cast<MirPlatformCursor*>(m_registeredScreen->handle()->cursor());
174 if (cursor) {
175 cursor->registerMousePointer(this);
176 } else {
177 qCritical("QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
178 }
179 }
180}
181
182void MousePointer::setCursorName(const QString &cursorName)
183{
184 if (cursorName != m_cursorName) {
185 m_cursorName = cursorName;
186 Q_EMIT cursorNameChanged(m_cursorName);
187 }
188}
189
190void MousePointer::setThemeName(const QString &themeName)
191{
192 if (m_themeName != themeName) {
193 m_themeName = themeName;
194 Q_EMIT themeNameChanged(m_themeName);
195 }
196}
197
198void MousePointer::moveTo(const QPoint &position)
199{
200 setPosition(position);
201 Q_EMIT mouseMoved();
202}
203
204void MousePointer::setCustomCursor(const QCursor &customCursor)
205{
206 CursorImageProvider::instance()->setCustomCursor(customCursor);
207}
208
209QQuickItem* MousePointer::confiningItem() const
210{
211 return m_confiningItem.data();
212}
213
214void MousePointer::setConfiningItem(QQuickItem *item)
215{
216 if (item != m_confiningItem) {
217 m_confiningItem = item;
218 Q_EMIT confiningItemChanged();
219 }
220}