summaryrefslogtreecommitdiff
path: root/src/extras/Private/qquickmousethief.cpp
blob: 6904b6629fa03fa57a47b164db05d471c9484d64 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Extras module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qquickmousethief_p.h"

#include <QtCore/QMetaObject>
#include <QtQuick/QQuickWindow>

QQuickMouseThief::QQuickMouseThief(QObject *parent) :
    QObject(parent),
    mItem(0),
    mReceivedPressEvent(false),
    mAcceptCurrentEvent(false)
{
}

bool QQuickMouseThief::receivedPressEvent() const
{
    return mReceivedPressEvent;
}

void QQuickMouseThief::setReceivedPressEvent(bool receivedPressEvent)
{
    if (receivedPressEvent != mReceivedPressEvent) {
        mReceivedPressEvent = receivedPressEvent;
        emit receivedPressEventChanged();
    }
}

void QQuickMouseThief::grabMouse(QQuickItem *item)
{
    if (item) {
        mItem = item;

        // Handle the case where someone makes the PieMenu an orphan. E.g.:
        // property PieMenu pieMenu: PieMenu {}
        // They have to set the parent explicitly if they want that to work.
        // This can also be null if the menu is visible when constructed.
        if (mItem->window()) {
            // We install an event filter so that we can track release events.
            // This is because the MouseArea that we steal the mouse from stores the
            // pressed flag (before item is visible) and we don't, so we don't know
            // that it's a release.
            mItem->grabMouse();
            mItem->window()->installEventFilter(this);
        } else {
            // The menu was visible when constructed (visible: true), so install the
            // event filter later on.
            connect(mItem, SIGNAL(windowChanged(QQuickWindow*)),
                this, SLOT(itemWindowChanged(QQuickWindow*)));
        }
    }
}

void QQuickMouseThief::ungrabMouse()
{
    if (mItem) {
        // This can be null if someone does the following:
        // PieMenu { Component.onCompleted: { visible = true; visible = false; }
        if (mItem->window()) {
            if (mItem->window()->mouseGrabberItem() == mItem) {
                mItem->ungrabMouse();
            }
            mItem->window()->removeEventFilter(this);
        }
        mItem = 0;
    }
}

void QQuickMouseThief::acceptCurrentEvent()
{
    mAcceptCurrentEvent = true;
}

void QQuickMouseThief::itemWindowChanged(QQuickWindow *window)
{
    // The window will be null when the application is closing.
    if (window) {
        // This can be null if someone does the following:
        // PieMenu { Component.onCompleted: { visible = true; visible = false; }
        if (mItem) {
            mItem->grabMouse();
            window->installEventFilter(this);
        }
    }
}

bool QQuickMouseThief::eventFilter(QObject *, QEvent *event)
{
    if (!mItem)
        return false;

    // The PieMenu QML code dictates which events are accepted by
    // setting this property when it responds to our signals.
    mAcceptCurrentEvent = false;

    if (event->type() == QEvent::MouseButtonRelease) {
        const QPointF mouseWindowPos = static_cast<QMouseEvent *>(event)->windowPos();
        emitReleased(mouseWindowPos);
        // Even if the release should close the menu, we should emit
        // clicked to be consistent since we have to do it ourselves.
        bool releaseAccepted = mAcceptCurrentEvent;
        mAcceptCurrentEvent = false;

        emitClicked(mouseWindowPos);
        if (!mAcceptCurrentEvent) {
            // We might not have accepted click, but we may have accepted release.
            mAcceptCurrentEvent = releaseAccepted;
        }
    } else if (event->type() == QEvent::MouseButtonPress) {
        emitPressed(static_cast<QMouseEvent *>(event)->windowPos());
    } else if (event->type() == QEvent::TouchEnd) {
        // The finger(s) were lifted off the screen. We don't get this as a release event since
        // we're doing our own custom handling, so we handle it here.
        QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
        const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints();
        // We only care about one finger. If there's more than one, ignore them.
        if (!points.isEmpty()) {
            const QPointF pos = points.first().pos();
            emitReleased(pos);
            // Even if the release should close the menu, we should emit
            // clicked to be consistent since we have to do it ourselves.
            bool releaseAccepted = mAcceptCurrentEvent;
            mAcceptCurrentEvent = false;

            emitClicked(pos);
            if (!mAcceptCurrentEvent) {
                // We might not have accepted click, but we may have accepted release.
                mAcceptCurrentEvent = releaseAccepted;
            }
        }
    } else if (event->type() == QEvent::TouchBegin) {
        QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
        const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints();
        if (!points.isEmpty()) {
            emitPressed(points.first().pos());
        }
    } else if (event->type() == QEvent::TouchUpdate) {
        QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event);
        const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints();
        if (!points.isEmpty()) {
            const QPointF mappedPos = mItem->mapFromScene(points.first().pos());
            emit touchUpdate(mappedPos.x(), mappedPos.y());
        }
    }
    return mAcceptCurrentEvent;
}

void QQuickMouseThief::emitPressed(const QPointF &pos)
{
    setReceivedPressEvent(true);

    // PieMenu can't detect presses outside its own MouseArea,
    // so we need to provide these as well.
    QPointF mappedPos = mItem->mapFromScene(pos);
    emit pressed(mappedPos.x(), mappedPos.y());
}

void QQuickMouseThief::emitReleased(const QPointF &pos)
{
    QPointF mappedPos = mItem->mapFromScene(pos);
    emit released(mappedPos.x(), mappedPos.y());
}

void QQuickMouseThief::emitClicked(const QPointF &pos)
{
    // mItem can be destroyed if the slots connected to released() caused it to be hidden.
    // That's fine for us, since PieMenu doesn't need to handle anything if released()
    // is emitted when the triggerMode is PieMenu.TriggerOnRelease.
    if (mItem) {
        QPointF mappedPos = mItem->mapFromScene(pos);
        // Since we are creating the release events, we must also handle clicks.
        emit clicked(mappedPos.x(), mappedPos.y());
    }
}