aboutsummaryrefslogtreecommitdiff
path: root/src/autolockeventfilter.h
blob: 9c35c45757c918d0fbc7c0a4fe328247d8682f79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef AUTOLOCKEVENTFILTER_H
#define AUTOLOCKEVENTFILTER_H

#include <QDebug>
#include <QEvent>
#include <QTimer>

class AutoLockEventFilter : public QObject {
  Q_OBJECT

public:
  explicit AutoLockEventFilter(int timeoutmillis)
      : timeoutmillis(timeoutmillis) {
    autoLockTimer = new QTimer(this);
    connect(autoLockTimer, &QTimer::timeout, this,
            QOverload<>::of(&AutoLockEventFilter::lockApp));
    resetTimer();
  }

  ~AutoLockEventFilter() {
    autoLockTimer->stop();
    autoLockTimer->deleteLater();
  }

signals:
  void autoLockTimerTimeout();

private:
  QTimer *autoLockTimer = nullptr;
  int timeoutmillis;

public slots:
  void stopTimer() { autoLockTimer->stop(); }
  void resetTimer() { autoLockTimer->start(timeoutmillis); }
  void lockApp() { emit autoLockTimerTimeout(); }
  void setTimeoutmillis(int newTimeoutmillis) {
    timeoutmillis = newTimeoutmillis;
  }

protected:
  bool eventFilter(QObject *obj, QEvent *ev) {
    if (ev->type() == QEvent::KeyPress
            || ev->type() == QEvent::MouseMove
            || ev->type() == QEvent::Wheel
            || ev->type() == QEvent::TouchUpdate
            || ev->type() == QEvent::MouseButtonRelease
            || ev->type() == QEvent::MouseButtonPress
            || ev->type() == QEvent::MouseButtonDblClick
            || ev->type() == QEvent::Gesture
            || ev->type() == QEvent::FocusIn
            || ev->type() == QEvent::FocusOut
            || ev->type() == QEvent::Enter
            ) {
      resetTimer();
    }
    return QObject::eventFilter(obj, ev);
  }
};
#endif // AUTOLOCKEVENTFILTER_H