lena_pi/LenaPi/MouseEventSpy.cpp
2019-04-03 17:44:46 +02:00

45 lines
1.2 KiB
C++

#include "MouseEventSpy.h"
#include <QEvent>
#include <QGuiApplication>
#include <QDebug>
MouseEventSpy::MouseEventSpy(QObject *parent) : QObject(parent)
{
qDebug() << "created Instance";
}
/**
* Implements the SINGLETON PATTERN
*/
MouseEventSpy* MouseEventSpy::instance()
{
static MouseEventSpy* inst;
if (inst == nullptr)
{
// If no instance has been created yet, create a new and install it as event filter.
// Uppon first use of the instance, it will automatically
// install itself in the QGuiApplication
inst = new MouseEventSpy();
QGuiApplication* app = qGuiApp;
app->installEventFilter(inst);
}
return inst;
}
/**
* This is the method is necessary for 'installEventFilter'
*/
bool MouseEventSpy::eventFilter(QObject* watched, QEvent* event)
{
QEvent::Type t = event->type();
if ((t == QEvent::MouseButtonDblClick
|| t == QEvent::MouseButtonPress
|| t == QEvent::MouseButtonRelease
|| t == QEvent::MouseMove)
&& event->spontaneous() // Take only mouse events from outside of Qt
)
emit mouseEventDetected();
qDebug("MouseEvent detected.");
return QObject::eventFilter(watched, event);
}