17 #include "WindowInputMonitor.h" 19 #include <QQuickWindow> 23 WindowInputMonitor::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, &UnityUtil::AbstractTimer::timeout,
42 this, &WindowInputMonitor::emitActivatedIfNoTouchesAround);
43 m_activationTimer->setInterval(msecsWithoutTouches);
44 m_activationTimer->setSingleShot(
true);
47 WindowInputMonitor::~WindowInputMonitor()
49 delete m_windowLastTouchedTimer;
50 delete m_activationTimer;
53 bool WindowInputMonitor::eventFilter(QObject *watched, QEvent *event)
55 Q_ASSERT(!m_filteredWindow.isNull());
56 Q_ASSERT(watched == static_cast<QObject*>(m_filteredWindow.data()));
65 void 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();
78 }
else if (event->type() == QEvent::KeyRelease) {
79 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
81 if (keyEvent->key() == m_pressedHomeKey) {
85 }
else if (event->type() == QEvent::TouchBegin) {
87 m_activationTimer->stop();
88 m_windowBeingTouched =
true;
91 }
else if (event->type() == QEvent::TouchEnd) {
93 m_windowBeingTouched =
false;
94 m_windowLastTouchedTimer->start();
96 QTouchEvent * touchEv =
static_cast<QTouchEvent *
>(event);
97 if (touchEv && !touchEv->touchPoints().isEmpty()) {
98 const QPointF pos = touchEv->touchPoints().last().screenPos();
99 Q_EMIT touchEnded(pos);
104 void WindowInputMonitor::setupFilterOnWindow(QQuickWindow *window)
106 if (!m_filteredWindow.isNull()) {
107 m_filteredWindow->removeEventFilter(
this);
108 m_filteredWindow.clear();
112 window->installEventFilter(
this);
113 m_filteredWindow = window;
117 void WindowInputMonitor::emitActivatedIfNoTouchesAround()
119 if (m_pressedHomeKey == 0 && !m_windowBeingTouched &&
120 (m_windowLastTouchedTimer->elapsed() > msecsWithoutTouches)) {
121 Q_EMIT homeKeyActivated();