Added MouseEventSpy as base for an auto shutdown
This commit is contained in:
parent
d251120275
commit
420a5ee428
4 changed files with 79 additions and 2 deletions
45
LenaPi/MouseEventSpy.cpp
Normal file
45
LenaPi/MouseEventSpy.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue