~ci-train-bot/unity8/unity8-ubuntu-zesty-2404

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2015-2016 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "WindowInputMonitor.h"

#include <QQuickWindow>

using namespace UnityUtil;

WindowInputMonitor::WindowInputMonitor(QQuickItem *parent)
    : WindowInputMonitor(new Timer, new ElapsedTimer, parent)
{
}

WindowInputMonitor::WindowInputMonitor(UnityUtil::AbstractTimer *timer,
        UnityUtil::AbstractElapsedTimer *elapsedTimer,
        QQuickItem *parent)
    : QQuickItem(parent)
    , m_windowBeingTouched(false)
    , m_windowLastTouchedTimer(elapsedTimer)
    , m_activationTimer(timer)
{
    m_windowLastTouchedTimer->start();

    connect(this, &QQuickItem::windowChanged,
            this, &WindowInputMonitor::setupFilterOnWindow);

    connect(m_activationTimer, &UnityUtil::AbstractTimer::timeout,
        this, &WindowInputMonitor::emitActivatedIfNoTouchesAround);
    m_activationTimer->setInterval(msecsWithoutTouches);
    m_activationTimer->setSingleShot(true);
}

WindowInputMonitor::~WindowInputMonitor()
{
    delete m_windowLastTouchedTimer;
    delete m_activationTimer;
}

bool WindowInputMonitor::eventFilter(QObject *watched, QEvent *event)
{
    Q_ASSERT(!m_filteredWindow.isNull());
    Q_ASSERT(watched == static_cast<QObject*>(m_filteredWindow.data()));
    Q_UNUSED(watched);

    update(event);

    // We're only monitoring, never filtering out events
    return false;
}

void WindowInputMonitor::update(QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);

        if (m_pressedHomeKey == 0 && m_homeKeys.contains(keyEvent->key()) && !keyEvent->isAutoRepeat()
                && !m_activationTimer->isRunning()
                && !m_windowBeingTouched
                && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
            m_pressedHomeKey = keyEvent->key();
            m_activationTimer->start();
        }

    } else if (event->type() == QEvent::KeyRelease) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);

        if (keyEvent->key() == m_pressedHomeKey) {
            m_pressedHomeKey = 0;
        }

    } else if (event->type() == QEvent::TouchBegin) {

        m_activationTimer->stop();
        m_windowBeingTouched = true;
        Q_EMIT touchBegun();

    } else if (event->type() == QEvent::TouchEnd) {

        m_windowBeingTouched = false;
        m_windowLastTouchedTimer->start();

        QTouchEvent * touchEv = static_cast<QTouchEvent *>(event);
        if (touchEv && !touchEv->touchPoints().isEmpty()) {
            const QPointF pos = touchEv->touchPoints().last().screenPos();
            Q_EMIT touchEnded(pos);
        }
    }
}

void WindowInputMonitor::setupFilterOnWindow(QQuickWindow *window)
{
    if (!m_filteredWindow.isNull()) {
        m_filteredWindow->removeEventFilter(this);
        m_filteredWindow.clear();
    }

    if (window) {
        window->installEventFilter(this);
        m_filteredWindow = window;
    }
}

void WindowInputMonitor::emitActivatedIfNoTouchesAround()
{
    if (m_pressedHomeKey == 0 && !m_windowBeingTouched &&
            (m_windowLastTouchedTimer->elapsed() > msecsWithoutTouches)) {
        Q_EMIT homeKeyActivated();
    }
}