17#include "MousePointer.h"
18#include "CursorImageProvider.h"
19#include "InputDispatcherFilter.h"
21#include <QQuickWindow>
24#include <lomiri/shell/application/MirPlatformCursor.h>
26MousePointer::MousePointer(QQuickItem *parent)
27 : MirMousePointerInterface(parent)
28 , m_cursorName(QStringLiteral(
"left_ptr"))
29 , m_themeName(QStringLiteral(
"default"))
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);
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);
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);
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);
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);
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);
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);
73 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::pushStopped,
74 this, [
this](QScreen* screen) {
75 if (window() && window()->screen() == screen) {
79 connect(InputDispatcherFilter::instance(), &InputDispatcherFilter::screenEntered,
80 this, [
this](QScreen* screen) {
81 if (window() && window()->screen() == screen) {
82 Q_EMIT screenEntered();
86 InputDispatcherFilter::instance()->registerPointer(
this);
89MousePointer::~MousePointer()
91 registerScreen(
nullptr);
92 InputDispatcherFilter::instance()->unregisterPointer(
this);
95void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
97 Q_ASSERT(parentItem() !=
nullptr);
99 if (m_confiningItem.isNull()) {
103 QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
105 QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
107 if (newX < confiningRect.x()) {
108 newX = confiningRect.x();
109 }
else if (newX > confiningRect.right()) {
110 newX = confiningRect.right();
113 if (newY < confiningRect.y()) {
114 newY = confiningRect.y();
115 }
else if (newY > confiningRect.bottom()) {
116 newY = confiningRect.bottom();
120int MousePointer::topBoundaryOffset()
const
122 return m_topBoundaryOffset;
125void MousePointer::setTopBoundaryOffset(
int topBoundaryOffset)
127 if (m_topBoundaryOffset == topBoundaryOffset)
130 m_topBoundaryOffset = topBoundaryOffset;
131 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
134void MousePointer::itemChange(ItemChange change,
const ItemChangeData &value)
136 if (change == ItemSceneChange) {
137 registerWindow(value.window);
141void MousePointer::registerWindow(QWindow *window)
143 if (window == m_registeredWindow) {
147 if (m_registeredWindow) {
148 m_registeredWindow->disconnect(
this);
151 m_registeredWindow = window;
153 if (m_registeredWindow) {
154 connect(window, &QWindow::screenChanged,
this, &MousePointer::registerScreen);
155 registerScreen(window->screen());
157 registerScreen(
nullptr);
161void MousePointer::registerScreen(QScreen *screen)
163 if (m_registeredScreen == screen) {
167 if (m_registeredScreen) {
168 auto previousCursor =
dynamic_cast<MirPlatformCursor*
>(m_registeredScreen->handle()->cursor());
169 if (previousCursor) {
170 previousCursor->unregisterMousePointer(
this);
172 qCritical(
"QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
176 m_registeredScreen = screen;
178 if (m_registeredScreen) {
179 auto cursor =
dynamic_cast<MirPlatformCursor*
>(m_registeredScreen->handle()->cursor());
181 cursor->registerMousePointer(
this);
183 qCritical(
"QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
188void MousePointer::setCursorName(
const QString &cursorName)
190 if (cursorName != m_cursorName) {
191 m_cursorName = cursorName;
192 Q_EMIT cursorNameChanged(m_cursorName);
196void MousePointer::setThemeName(
const QString &themeName)
198 if (m_themeName != themeName) {
199 m_themeName = themeName;
200 Q_EMIT themeNameChanged(m_themeName);
204void MousePointer::moveTo(
const QPoint &position)
206 setPosition(position);
210void MousePointer::setCustomCursor(
const QCursor &customCursor)
212 CursorImageProvider::instance()->setCustomCursor(customCursor);
215QQuickItem* MousePointer::confiningItem()
const
217 return m_confiningItem.data();
220void MousePointer::setConfiningItem(QQuickItem *item)
222 if (item != m_confiningItem) {
223 m_confiningItem = item;
224 Q_EMIT confiningItemChanged();