2
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "InactivityTimer.h"
20
#include <QCoreApplication>
23
InactivityTimer::InactivityTimer(QObject* parent)
25
, m_timer(new QTimer(this))
28
m_timer->setSingleShot(true);
29
connect(m_timer, SIGNAL(timeout()), SLOT(timeout()));
32
void InactivityTimer::setInactivityTimeout(int inactivityTimeout)
34
Q_ASSERT(inactivityTimeout > 0);
36
m_timer->setInterval(inactivityTimeout);
39
void InactivityTimer::activate()
42
qApp->installEventFilter(this);
48
void InactivityTimer::deactivate()
50
qApp->removeEventFilter(this);
55
bool InactivityTimer::eventFilter(QObject* watched, QEvent* event)
57
const QEvent::Type type = event->type();
59
if ((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease)
60
|| (type >= QEvent::HoverEnter && type <= QEvent::HoverMove)
61
|| (type == QEvent::Wheel)) {
66
return QObject::eventFilter(watched, event);
69
void InactivityTimer::timeout()
71
// make sure we don't emit the signal a second time while it's still processed
72
if (!m_emitMutx.tryLock()) {
76
if (m_active && !m_timer->isActive()) {
77
emit inactivityDetected();