2
* Copyright 2015 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
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 Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authors: Zsombor Egri <zsombor.egri@canonical.com>
19
#include "gesturedetector.h"
21
#include <QtCore/QEvent>
22
#include <QtCore/QRectF>
23
#include <QtGui/QTouchEvent>
24
#include <QtQuick/QQuickItem>
25
#include <QtGui/QGuiApplication>
26
#include <QtGui/QStyleHints>
28
#define DETECTION_AREA_THICKNESS_GU 1.2
30
GestureDetector::GestureDetector(QObject *parent)
32
, m_owner(qobject_cast<QQuickItem*>(parent))
34
, m_bottomUpSwipeDetected(false)
38
GestureDetector::~GestureDetector()
40
Q_FOREACH(QObject *object, m_filteredItems) {
41
object->removeEventFilter(this);
43
m_filteredItems.clear();
46
void GestureDetector::setStatus(Status status)
48
if (status == m_status) {
52
Q_EMIT statusChanged(m_status);
55
bool GestureDetector::isDetecting()
57
return (m_status > Ready && m_status < Completed);
60
void GestureDetector::setItemFilter(QObject *item)
62
m_filteredItems.append(item);
63
item->installEventFilter(this);
66
void GestureDetector::removeItemFilter(QObject *item)
68
m_filteredItems.removeAll(item);
69
item->removeEventFilter(this);
72
bool GestureDetector::handleTouchEvent(QObject *target, QTouchEvent *event)
74
switch (event->type()) {
75
case QEvent::TouchBegin: {
77
QPointF itemPoint = m_owner->mapFromScene(event->touchPoints()[0].scenePos());
78
qreal thickness = UCUnits::instance().gu(DETECTION_AREA_THICKNESS_GU);
79
QRectF detectionArea(0.0, m_owner->height() - thickness, m_owner->width(), thickness);
80
if (detectionArea.contains(itemPoint)) {
81
m_startPoint = itemPoint;
83
if (target == parent()) {
90
case QEvent::TouchEnd:
92
m_startPoint = QPointF();
96
case QEvent::TouchCancel: {
97
m_startPoint = QPointF();
101
case QEvent::TouchUpdate: {
102
if (m_status == Started) {
103
QPointF itemPoint = m_owner->mapFromScene(event->touchPoints()[0].scenePos());
104
if (abs(m_startPoint.y() - itemPoint.y()) >= qApp->styleHints()->startDragDistance()) {
106
Q_EMIT bottomUpSwipeDetected();
111
default: return false;
115
bool GestureDetector::eventFilter(QObject *target, QEvent *event)
117
if (m_filteredItems.contains(target)) {
118
QEvent::Type type = event->type();
119
if (type == QEvent::TouchBegin
120
|| type == QEvent::TouchUpdate
121
|| type == QEvent::TouchEnd
122
|| type == QEvent::TouchCancel) {
123
QTouchEvent *touch = static_cast<QTouchEvent*>(event);
124
return handleTouchEvent(target, touch);
130
return QObject::eventFilter(target, event);