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) {
80 InputDispatcherFilter::instance()->registerPointer(
this);
83MousePointer::~MousePointer()
85 registerScreen(
nullptr);
86 InputDispatcherFilter::instance()->unregisterPointer(
this);
89void MousePointer::applyItemConfinement(qreal &newX, qreal &newY)
91 Q_ASSERT(parentItem() !=
nullptr);
93 if (m_confiningItem.isNull()) {
97 QRectF confiningItemGeometry(0, 0, m_confiningItem->width(), m_confiningItem->height());
99 QRectF confiningRect = m_confiningItem->mapRectToItem(parentItem(), confiningItemGeometry);
101 if (newX < confiningRect.x()) {
102 newX = confiningRect.x();
103 }
else if (newX > confiningRect.right()) {
104 newX = confiningRect.right();
107 if (newY < confiningRect.y()) {
108 newY = confiningRect.y();
109 }
else if (newY > confiningRect.bottom()) {
110 newY = confiningRect.bottom();
114int MousePointer::topBoundaryOffset()
const
116 return m_topBoundaryOffset;
119void MousePointer::setTopBoundaryOffset(
int topBoundaryOffset)
121 if (m_topBoundaryOffset == topBoundaryOffset)
124 m_topBoundaryOffset = topBoundaryOffset;
125 Q_EMIT topBoundaryOffsetChanged(topBoundaryOffset);
128void MousePointer::itemChange(ItemChange change,
const ItemChangeData &value)
130 if (change == ItemSceneChange) {
131 registerWindow(value.window);
135void MousePointer::registerWindow(QWindow *window)
137 if (window == m_registeredWindow) {
141 if (m_registeredWindow) {
142 m_registeredWindow->disconnect(
this);
145 m_registeredWindow = window;
147 if (m_registeredWindow) {
148 connect(window, &QWindow::screenChanged,
this, &MousePointer::registerScreen);
149 registerScreen(window->screen());
151 registerScreen(
nullptr);
155void MousePointer::registerScreen(QScreen *screen)
157 if (m_registeredScreen == screen) {
161 if (m_registeredScreen) {
162 auto previousCursor =
dynamic_cast<MirPlatformCursor*
>(m_registeredScreen->handle()->cursor());
163 if (previousCursor) {
164 previousCursor->unregisterMousePointer(
this);
166 qCritical(
"QPlatformCursor is not a MirPlatformCursor! Cursor module only works in a Mir server.");
170 m_registeredScreen = screen;
172 if (m_registeredScreen) {
173 auto cursor =
dynamic_cast<MirPlatformCursor*
>(m_registeredScreen->handle()->cursor());
175 cursor->registerMousePointer(
this);
177 qCritical(
"QPlaformCursor is not a MirPlatformCursor! Cursor module only works in Mir.");
182void MousePointer::setCursorName(
const QString &cursorName)
184 if (cursorName != m_cursorName) {
185 m_cursorName = cursorName;
186 Q_EMIT cursorNameChanged(m_cursorName);
190void MousePointer::setThemeName(
const QString &themeName)
192 if (m_themeName != themeName) {
193 m_themeName = themeName;
194 Q_EMIT themeNameChanged(m_themeName);
198void MousePointer::moveTo(
const QPoint &position)
200 setPosition(position);
204void MousePointer::setCustomCursor(
const QCursor &customCursor)
206 CursorImageProvider::instance()->setCustomCursor(customCursor);
209QQuickItem* MousePointer::confiningItem()
const
211 return m_confiningItem.data();
214void MousePointer::setConfiningItem(QQuickItem *item)
216 if (item != m_confiningItem) {
217 m_confiningItem = item;
218 Q_EMIT confiningItemChanged();