1 /****************************************************************************
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtBrowser project.
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 2 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.GPLv2 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU General Public License version 2 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3.0 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 3.0 requirements will be
31 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ****************************************************************************/
38 #include "touchtracker.h"
44 using namespace utils;
46 TouchTracker::TouchTracker(QQuickItem *parent)
48 , m_blockEvents(false)
55 m_currentPoint.ts = 0;
58 QQuickItem *TouchTracker::target() const
63 void TouchTracker::setTarget(QQuickItem * target)
69 int TouchTracker::xVelocity() const
71 qreal pos = qAbs(m_startPoint.x() - m_currentPoint.x());
72 qreal time = qAbs(m_startPoint.ts - m_currentPoint.ts);
73 return pos / time * 1000;
76 int TouchTracker::yVelocity() const
78 qreal pos = qAbs(m_startPoint.y() - m_currentPoint.y());
79 qreal time = qAbs(m_startPoint.ts - m_currentPoint.ts);
80 return pos / time * 1000;
84 qreal TouchTracker::touchX() const
86 return m_currentPoint.x();
89 qreal TouchTracker::touchY() const
91 return m_currentPoint.y();
94 bool TouchTracker::blockEvents() const
99 void TouchTracker::setBlockEvents(bool shouldBlock)
101 if (m_blockEvents == shouldBlock)
103 m_blockEvents = shouldBlock;
104 emit blockEventsChanged();
107 bool TouchTracker::eventFilter(QObject *obj, QEvent *event)
109 if (obj != m_delegate)
110 return QQuickItem::eventFilter(obj, event);
112 if (event->type() == QEvent::Wheel)
113 return m_blockEvents;
115 if (!isTouchEvent(event))
116 return QQuickItem::eventFilter(obj, event);
118 const QTouchEvent *touch = static_cast<QTouchEvent*>(event);
119 const QList<QTouchEvent::TouchPoint> &points = touch->touchPoints();
120 m_previousY = m_currentPoint.y();
121 m_currentPoint.pos = m_target->mapToScene(points.at(0).pos());
122 m_currentPoint.ts = QDateTime::currentMSecsSinceEpoch();
123 int currentDiff = m_previousY - m_currentPoint.y();
125 if ((currentDiff > 0 && m_diff < 0) || (currentDiff < 0 && m_diff > 0))
126 emit scrollDirectionChanged();
128 m_diff = currentDiff;
131 emit velocityChanged();
133 if (event->type() == QEvent::TouchEnd)
136 return m_blockEvents;
139 void TouchTracker::touchEvent(QTouchEvent * event)
143 QQuickItem::touchEvent(event);
148 event->setAccepted(false);
150 const QList<QTouchEvent::TouchPoint> &points = event->touchPoints();
151 m_currentPoint.pos = m_target->mapToScene(points.at(0).pos());
152 m_currentPoint.ts = QDateTime::currentMSecsSinceEpoch();
154 if (event->type() == QEvent::TouchBegin) {
155 m_startPoint = m_currentPoint;
161 // We have to find the delegate to be able to filter
162 // events from the WebEngineView.
163 // This is a hack and should preferably be made easier
164 // with the API in some way.
165 QQuickItem *child = m_target->childAt(m_currentPoint.x(), m_currentPoint.y());
166 if (child && m_delegate != child) {
167 child->installEventFilter(this);