]> rtime.felk.cvut.cz Git - coffee/qtwebbrowser.git/blob - src/app/touchtracker.cpp
Use qml extension plugin
[coffee/qtwebbrowser.git] / src / app / touchtracker.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtBrowser project.
7 **
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.
16 **
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.
24 **
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.
32 **
33 **
34 ** $QT_END_LICENSE$
35 **
36 ****************************************************************************/
37
38 #include "touchtracker.h"
39
40 #include <QDateTime>
41
42 #include "engine.h"
43
44 using namespace utils;
45
46 TouchTracker::TouchTracker(QQuickItem *parent)
47     : QQuickItem(parent)
48     , m_blockEvents(false)
49     , m_diff(0)
50     , m_previousY(0)
51     , m_target(0)
52     , m_delegate(0)
53 {
54     m_startPoint.ts = 0;
55     m_currentPoint.ts = 0;
56 }
57
58 QQuickItem *TouchTracker::target() const
59 {
60     return m_target;
61 }
62
63 void TouchTracker::setTarget(QQuickItem * target)
64 {
65     m_target = target;
66     emit targetChanged();
67 }
68
69 int TouchTracker::xVelocity() const
70 {
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;
74 }
75
76 int TouchTracker::yVelocity() const
77 {
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;
81 }
82
83
84 qreal TouchTracker::touchX() const
85 {
86     return m_currentPoint.x();
87 }
88
89 qreal TouchTracker::touchY() const
90 {
91     return m_currentPoint.y();
92 }
93
94 bool TouchTracker::blockEvents() const
95 {
96     return m_blockEvents;
97 }
98
99 void TouchTracker::setBlockEvents(bool shouldBlock)
100 {
101     if (m_blockEvents == shouldBlock)
102         return;
103     m_blockEvents = shouldBlock;
104     emit blockEventsChanged();
105 }
106
107 bool TouchTracker::eventFilter(QObject *obj, QEvent *event)
108 {
109     if (obj != m_delegate)
110         return QQuickItem::eventFilter(obj, event);
111
112     if (event->type() == QEvent::Wheel)
113         return m_blockEvents;
114
115     if (!isTouchEvent(event))
116         return QQuickItem::eventFilter(obj, event);
117
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();
124
125     if ((currentDiff > 0 && m_diff < 0) || (currentDiff < 0 && m_diff > 0))
126         emit scrollDirectionChanged();
127
128     m_diff = currentDiff;
129
130     emit touchChanged();
131     emit velocityChanged();
132
133     if (event->type() ==  QEvent::TouchEnd)
134         emit touchEnd();
135
136     return m_blockEvents;
137 }
138
139 void TouchTracker::touchEvent(QTouchEvent * event)
140 {
141     if (!m_target) {
142         if (!m_blockEvents)
143             QQuickItem::touchEvent(event);
144
145         return;
146     }
147
148     event->setAccepted(false);
149
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();
153
154     if (event->type() ==  QEvent::TouchBegin) {
155         m_startPoint = m_currentPoint;
156         emit touchBegin();
157     }
158
159     emit touchChanged();
160
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);
168         m_delegate = child;
169     }
170 }