Restart timer on mouse event.
Todo: call shut down script on timeout
This commit is contained in:
parent
420a5ee428
commit
ce8ea8f582
2 changed files with 66 additions and 13 deletions
|
|
@ -1,25 +1,29 @@
|
||||||
#include "MouseEventSpy.h"
|
#include "MouseEventSpy.h"
|
||||||
|
|
||||||
#include <QEvent>
|
|
||||||
#include <QGuiApplication>
|
#include <QGuiApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize timer.
|
||||||
|
* @param parent Parent object
|
||||||
|
*/
|
||||||
MouseEventSpy::MouseEventSpy(QObject *parent) : QObject(parent)
|
MouseEventSpy::MouseEventSpy(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
qDebug() << "created Instance";
|
initTimer(10000);
|
||||||
|
mTimer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the SINGLETON PATTERN
|
* @brief Implements the singleton pattern
|
||||||
|
*
|
||||||
|
* If no instance has been created yet, create new instance and install it as event filter
|
||||||
|
* in QGuiApplication.
|
||||||
*/
|
*/
|
||||||
MouseEventSpy* MouseEventSpy::instance()
|
MouseEventSpy* MouseEventSpy::instance()
|
||||||
{
|
{
|
||||||
static MouseEventSpy* inst;
|
static MouseEventSpy* inst;
|
||||||
if (inst == nullptr)
|
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();
|
inst = new MouseEventSpy();
|
||||||
QGuiApplication* app = qGuiApp;
|
QGuiApplication* app = qGuiApp;
|
||||||
app->installEventFilter(inst);
|
app->installEventFilter(inst);
|
||||||
|
|
@ -28,18 +32,57 @@ MouseEventSpy* MouseEventSpy::instance()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the method is necessary for 'installEventFilter'
|
* @brief Expand QObject::eventFilter to react to MouseEvents.
|
||||||
|
*
|
||||||
|
* Restart timer each time a mouse event is detected.
|
||||||
*/
|
*/
|
||||||
bool MouseEventSpy::eventFilter(QObject* watched, QEvent* event)
|
bool MouseEventSpy::eventFilter(QObject* watched, QEvent* event)
|
||||||
{
|
{
|
||||||
QEvent::Type t = event->type();
|
QEvent::Type t = event->type();
|
||||||
if ((t == QEvent::MouseButtonDblClick
|
if (isMouseEvent(t)
|
||||||
|| t == QEvent::MouseButtonPress
|
|
||||||
|| t == QEvent::MouseButtonRelease
|
|
||||||
|| t == QEvent::MouseMove)
|
|
||||||
&& event->spontaneous() // Take only mouse events from outside of Qt
|
&& event->spontaneous() // Take only mouse events from outside of Qt
|
||||||
)
|
)
|
||||||
|
{
|
||||||
emit mouseEventDetected();
|
emit mouseEventDetected();
|
||||||
qDebug("MouseEvent detected.");
|
}
|
||||||
|
|
||||||
|
if(mTimer.isActive()){
|
||||||
|
mTimer.stop();
|
||||||
|
}
|
||||||
|
mTimer.start();
|
||||||
|
|
||||||
return QObject::eventFilter(watched, event);
|
return QObject::eventFilter(watched, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize and connect timer.
|
||||||
|
* @param interval Timeout interval in millisecond.
|
||||||
|
*/
|
||||||
|
void MouseEventSpy::initTimer(int interval)
|
||||||
|
{
|
||||||
|
connect(&mTimer, &QTimer::timeout, this, &MouseEventSpy::onTimeout);
|
||||||
|
mTimer.setInterval(interval);
|
||||||
|
mTimer.setSingleShot(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 MouseEventSpy::isMouseEvent(QEvent::Type t)
|
||||||
|
{
|
||||||
|
return (t == QEvent::MouseButtonDblClick
|
||||||
|
|| t == QEvent::MouseButtonPress
|
||||||
|
|| t == QEvent::MouseButtonRelease
|
||||||
|
|| t == QEvent::MouseMove);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Behavior on timeout: shut down RaspberryPi.
|
||||||
|
* @todo Call shutdown script.
|
||||||
|
*/
|
||||||
|
void MouseEventSpy::onTimeout()
|
||||||
|
{
|
||||||
|
qDebug() << "shutting down.";
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
#define MOUSEEVENTSPY_H
|
#define MOUSEEVENTSPY_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QTimer>
|
||||||
|
#include <QEvent>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Catches all mouse event and starts timer -- basically behaves like a screensaver.
|
* Catches all mouse event and starts timer -- basically behaves like a screensaver.
|
||||||
|
|
@ -20,6 +22,14 @@ public:
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject* watched, QEvent* event);
|
bool eventFilter(QObject* watched, QEvent* event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void initTimer(int interval);
|
||||||
|
bool isMouseEvent(QEvent::Type t);
|
||||||
|
QTimer mTimer;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void onTimeout();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void mouseEventDetected();
|
void mouseEventDetected();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue