Added MouseEventSpy as base for an auto shutdown

This commit is contained in:
Anika Raemer 2019-04-03 17:44:46 +02:00
parent d251120275
commit 420a5ee428
4 changed files with 79 additions and 2 deletions

View file

@ -10,7 +10,8 @@ SOURCES += main.cpp \
controllers/NavigationController.cpp \
models/UiStateModel.cpp \
controllers/MusicController.cpp \
models/MusicModel.cpp
models/MusicModel.cpp \
MouseEventSpy.cpp
RESOURCES += qml.qrc \
lenapi.qrc
@ -43,6 +44,7 @@ HEADERS += \
controllers/NavigationController.h \
models/UiStateModel.h \
controllers/MusicController.h \
models/MusicModel.h
models/MusicModel.h \
MouseEventSpy.h
INCLUDEPATH+=/usr/local/include

45
LenaPi/MouseEventSpy.cpp Normal file
View 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);
}

27
LenaPi/MouseEventSpy.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef MOUSEEVENTSPY_H
#define MOUSEEVENTSPY_H
#include <QObject>
/**
* Catches all mouse event and starts timer -- basically behaves like a screensaver.
* Based on
* https://stackoverflow.com/questions/46173105/how-can-i-reset-a-timer-every-time-i-receive-a-touch-event-from-a-qml-page
*
* @todo implement timer and behavior on timeout.
*/
class MouseEventSpy : public QObject
{
Q_OBJECT
public:
explicit MouseEventSpy(QObject *parent = 0);
static MouseEventSpy* instance();
protected:
bool eventFilter(QObject* watched, QEvent* event);
signals:
void mouseEventDetected();
};
#endif // MOUSEEVENTSPY_H

View file

@ -1,6 +1,7 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "controllers/NavigationController.h"
#include "MouseEventSpy.h"
int main(int argc, char *argv[])
{
@ -28,6 +29,8 @@ int main(int argc, char *argv[])
navController.setContext(engine.rootContext());
navController.init("/home/ar/source/lenaMusic/");
// install MouseEventSpy used for auto shut down of RaspberryPi if not used for a predefined time.
MouseEventSpy::instance();
engine.load(QUrl("qrc:/main.qml"));