on feea5e1 help sign in
unity8/plugins/Utils/HomeKeyWatcher.cpp
105 lines 3.1 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
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; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "HomeKeyWatcher.h"
19#include <QQuickWindow>
21using namespace UnityUtil;
23HomeKeyWatcher::HomeKeyWatcher(QQuickItem *parent)
24 : HomeKeyWatcher(new Timer, new ElapsedTimer, parent)
28HomeKeyWatcher::HomeKeyWatcher(UnityUtil::AbstractTimer *timer,
29 UnityUtil::AbstractElapsedTimer *elapsedTimer,
30 QQuickItem *parent)
31 : QQuickItem(parent)
32 , m_windowBeingTouched(false)
33 , m_windowLastTouchedTimer(elapsedTimer)
34 , m_emitActivatedIfNoTouchesAroundTimer(timer)
36 m_windowLastTouchedTimer->start();
38 connect(this, &QQuickItem::windowChanged,
39 this, &HomeKeyWatcher::setupFilterOnWindow);
41 connect(m_emitActivatedIfNoTouchesAroundTimer, &UnityUtil::AbstractTimer::timeout,
42 this, &HomeKeyWatcher::emitActivatedIfNoTouchesAround);
43 m_emitActivatedIfNoTouchesAroundTimer->setInterval(msecsWithoutTouches);
46HomeKeyWatcher::~HomeKeyWatcher()
48 delete m_windowLastTouchedTimer;
49 delete m_emitActivatedIfNoTouchesAroundTimer;
52bool HomeKeyWatcher::eventFilter(QObject *watched, QEvent *event)
54 Q_ASSERT(!m_filteredWindow.isNull());
55 Q_ASSERT(watched == static_cast<QObject*>(m_filteredWindow.data()));
56 Q_UNUSED(watched);
58 update(event);
60 // We're only monitoring, never filtering out events
61 return false;
64void HomeKeyWatcher::update(QEvent *event)
66 if (event->type() == QEvent::KeyPress) {
67 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
69 if (keyEvent->key() == Qt::Key_Super_L && !keyEvent->isAutoRepeat()
70 && !m_windowBeingTouched
71 && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
72 m_emitActivatedIfNoTouchesAroundTimer->start();
73 }
75 } else if (event->type() == QEvent::TouchBegin) {
77 m_emitActivatedIfNoTouchesAroundTimer->stop();
78 m_windowBeingTouched = true;
80 } else if (event->type() == QEvent::TouchEnd) {
82 m_windowBeingTouched = false;
83 m_windowLastTouchedTimer->start();
84 }
87void HomeKeyWatcher::setupFilterOnWindow(QQuickWindow *window)
89 if (!m_filteredWindow.isNull()) {
90 m_filteredWindow->removeEventFilter(this);
91 m_filteredWindow.clear();
92 }
94 if (window) {
95 window->installEventFilter(this);
96 m_filteredWindow = window;
97 }
100void HomeKeyWatcher::emitActivatedIfNoTouchesAround()
102 if (!m_windowBeingTouched && (m_windowLastTouchedTimer->elapsed() > msecsWithoutTouches)) {
103 Q_EMIT activated();
104 }