17#include "WindowInputMonitor.h"
19#include <QQuickWindow>
21using namespace LomiriUtil;
23WindowInputMonitor::WindowInputMonitor(QQuickItem *parent)
32 , m_windowBeingTouched(false)
33 , m_windowLastTouchedTimer(elapsedTimer)
34 , m_activationTimer(timer)
36 m_windowLastTouchedTimer->start();
38 connect(
this, &QQuickItem::windowChanged,
39 this, &WindowInputMonitor::setupFilterOnWindow);
41 connect(m_activationTimer, &LomiriUtil::AbstractTimer::timeout,
42 this, &WindowInputMonitor::emitActivatedIfNoTouchesAround);
43 m_activationTimer->setInterval(msecsWithoutTouches);
44 m_activationTimer->setSingleShot(
true);
47WindowInputMonitor::~WindowInputMonitor()
49 delete m_windowLastTouchedTimer;
50 delete m_activationTimer;
53bool WindowInputMonitor::eventFilter(QObject *watched, QEvent *event)
55 Q_ASSERT(!m_filteredWindow.isNull());
56 Q_ASSERT(watched ==
static_cast<QObject*
>(m_filteredWindow.data()));
65void WindowInputMonitor::update(QEvent *event)
67 if (event->type() == QEvent::KeyPress) {
68 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
70 if (m_pressedHomeKey == 0 && m_homeKeys.contains(keyEvent->key()) && !keyEvent->isAutoRepeat()
71 && !m_activationTimer->isRunning()
72 && !m_windowBeingTouched
73 && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
74 m_pressedHomeKey = keyEvent->key();
75 m_activationTimer->start();
76 }
else if (m_pressedHomeKey != 0 && !m_homeKeys.contains(keyEvent->key())) {
78 m_activationTimer->stop();
81 }
else if (event->type() == QEvent::KeyRelease) {
82 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
84 if (keyEvent->key() == m_pressedHomeKey) {
88 }
else if (event->type() == QEvent::TouchBegin) {
90 m_activationTimer->stop();
91 m_windowBeingTouched =
true;
94 }
else if (event->type() == QEvent::TouchEnd) {
96 m_windowBeingTouched =
false;
97 m_windowLastTouchedTimer->start();
99 QTouchEvent * touchEv =
static_cast<QTouchEvent *
>(event);
100 if (touchEv && !touchEv->touchPoints().isEmpty()) {
101 const QPointF pos = touchEv->touchPoints().last().screenPos();
107void WindowInputMonitor::setupFilterOnWindow(QQuickWindow *window)
109 if (!m_filteredWindow.isNull()) {
110 m_filteredWindow->removeEventFilter(
this);
111 m_filteredWindow.clear();
115 window->installEventFilter(
this);
116 m_filteredWindow = window;
120void WindowInputMonitor::emitActivatedIfNoTouchesAround()
122 if (m_pressedHomeKey == 0 && !m_windowBeingTouched &&
123 (m_windowLastTouchedTimer->elapsed() > msecsWithoutTouches)) {