File indexing completed on 2024-11-24 04:50:40

0001 // SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "mousetracker.h"
0005 
0006 #include <QEvent>
0007 #include <QMouseEvent>
0008 
0009 MouseTracker *MouseTracker::instance()
0010 {
0011     static MouseTracker *trackerInstance = new MouseTracker;
0012     return trackerInstance;
0013 }
0014 
0015 MouseTracker::MouseTracker(QObject *parent)
0016     : QObject{parent}
0017 {
0018 }
0019 
0020 QPointF MouseTracker::mousePosition() const
0021 {
0022     return m_lastMousePos;
0023 }
0024 
0025 // This method is necessary for 'installEventFilter'
0026 bool MouseTracker::eventFilter(QObject *watched, QEvent *event)
0027 {
0028     Q_ASSERT(event);
0029     const auto type = event->type();
0030 
0031     switch (type) {
0032     case QEvent::MouseMove: {
0033         const auto mouseEvent = static_cast<QMouseEvent *>(event);
0034         m_lastMousePos = mouseEvent->scenePosition();
0035         Q_EMIT mousePositionChanged(m_lastMousePos);
0036         break;
0037     }
0038     case QEvent::MouseButtonRelease: {
0039         const auto mouseEvent = static_cast<QMouseEvent *>(event);
0040         const auto buttonClicked = mouseEvent->button();
0041         Q_EMIT mouseButtonReleased(buttonClicked);
0042         break;
0043     }
0044     default:
0045         break;
0046     }
0047 
0048     return QObject::eventFilter(watched, event);
0049 }
0050 
0051 #include "moc_mousetracker.cpp"