[OpenDocString] kdeconnect-kde (cpp)
pointerlocker.cpp
void AbstractPointerLocker::setWindow(QWindow *window)
{
    if (m_window == window) {
        return;
    }
    m_window = window;
    Q_EMIT windowChanged();
}
Sets the window object within the pointer locking mechanism. First it checks if the window is already set, if it is set, and emits the windowChanged signal. Then it emits the windowChanged signal.
PointerLockerQt::PointerLockerQt(QObject *parent)
    : AbstractPointerLocker(parent)
{
}
This implements the pointer locking mechanism for the internal pointer objects.
PointerLockerQt::~PointerLockerQt() = default;
This removes the pointer locking mechanism from the internal list of locks and sets the default value to the value of the lock method.
void PointerLockerQt::setLocked(bool lock)
{
    if (m_isLocked == lock) {
        return;
    }
    m_isLocked = lock;

    if (lock) {
        /* Cursor needs to be hidden such that Xwayland emulates warps. */
        QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
        m_originalPosition = QCursor::pos();
        m_window->installEventFilter(this);
        Q_EMIT lockedChanged(true);
        Q_EMIT lockEffectiveChanged(true);
    } else {
        m_window->removeEventFilter(this);
        QGuiApplication::restoreOverrideCursor();
        Q_EMIT lockedChanged(false);
        Q_EMIT lockEffectiveChanged(false);
    }
}
Sets the lock value on the Qt object. It sets the cursor to the blank cursor, sets the lock position to the position of the cursor, and installs the event filter to update the lockEffectiveChanged signal.
bool PointerLockerQt::isLocked() const
{
    return m_isLocked;
}
This implements the isLocked() function.
bool PointerLockerQt::eventFilter(QObject *watched, QEvent *event)
{
    if (watched != m_window || event->type() != QEvent::MouseMove || !isLocked()) {
        return false;
    }

    const auto newPos = QCursor::pos();
    const QPointF dist = newPos - m_originalPosition;
    Q_EMIT pointerMoved({dist.x(), dist.y()});
    QCursor::setPos(m_originalPosition);

    return true;
}
This filters out mouse moves. It returns true if the object is not the current window, or the event is not a mouse move event.