From 420a5ee428f90849f96814d65d01332b93cdb9de Mon Sep 17 00:00:00 2001 From: Anika Raemer Date: Wed, 3 Apr 2019 17:44:46 +0200 Subject: [PATCH] Added MouseEventSpy as base for an auto shutdown --- LenaPi/LenaPi.pro | 6 ++++-- LenaPi/MouseEventSpy.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ LenaPi/MouseEventSpy.h | 27 ++++++++++++++++++++++++ LenaPi/main.cpp | 3 +++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 LenaPi/MouseEventSpy.cpp create mode 100644 LenaPi/MouseEventSpy.h diff --git a/LenaPi/LenaPi.pro b/LenaPi/LenaPi.pro index 56e316b..390855c 100644 --- a/LenaPi/LenaPi.pro +++ b/LenaPi/LenaPi.pro @@ -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 diff --git a/LenaPi/MouseEventSpy.cpp b/LenaPi/MouseEventSpy.cpp new file mode 100644 index 0000000..332a6ba --- /dev/null +++ b/LenaPi/MouseEventSpy.cpp @@ -0,0 +1,45 @@ +#include "MouseEventSpy.h" + +#include +#include +#include + +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); +} diff --git a/LenaPi/MouseEventSpy.h b/LenaPi/MouseEventSpy.h new file mode 100644 index 0000000..67012fc --- /dev/null +++ b/LenaPi/MouseEventSpy.h @@ -0,0 +1,27 @@ +#ifndef MOUSEEVENTSPY_H +#define MOUSEEVENTSPY_H + +#include + +/** + * 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 diff --git a/LenaPi/main.cpp b/LenaPi/main.cpp index bd0022e..f8ce39a 100644 --- a/LenaPi/main.cpp +++ b/LenaPi/main.cpp @@ -1,6 +1,7 @@ #include #include #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"));