lena_pi/LenaPi/MouseEventSpy.h
2021-08-28 14:08:02 +02:00

60 lines
1.5 KiB
C++

#ifndef MOUSEEVENTSPY_H
#define MOUSEEVENTSPY_H
#include <QObject>
#include <QEvent>
/**
* @brief Catches all mouse events and reports them via signal.
*
* Based on
* https://stackoverflow.com/questions/46173105/how-can-i-reset-a-timer-every-time-i-receive-a-touch-event-from-a-qml-page
*/
class MouseEventSpy : public QObject
{
Q_OBJECT
protected:
explicit MouseEventSpy(QObject *parent = nullptr) : QObject(parent) { /* nothing */ }
signals:
void mouseEventDetected();
public:
/**
* @brief Create instance if necessary.
*
* If no instance has been created yet, create new instance and install it as event filter
* in QGuiApplication.
*
* @see MouseEventSpy::instance()
*/
static void init();
/**
* @brief Implements the singleton pattern.
* @return Instance of MouseEventSpy
*
* If no instance has been created yet, create new instance and install it as event filter
* in QGuiApplication.
*/
static MouseEventSpy* instance();
protected:
/**
* @brief Expand QObject::eventFilter to react to MouseEvents.
*
* Restart timer each time a mouse event is detected.
*/
bool eventFilter(QObject* watched, QEvent* event);
private:
/**
* @brief Checks whether the event type is a mouse event.
* @param t Event type
* @return Returns whether the event type is a mouse event.
*/
bool isMouseEvent(QEvent::Type t);
};
#endif // MOUSEEVENTSPY_H