Falkon Develop
Cross-platform Qt-based web browser
wheelhelper.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "wheelhelper.h"
19
20#include <QWheelEvent>
21
23= default;
24
26{
27 m_wheelDelta = 0;
28 m_directions.clear();
29}
30
31void WheelHelper::processEvent(QWheelEvent *event)
32{
33 int delta = event->angleDelta().x() ? event->angleDelta().x() : event->angleDelta().y();
34 bool directionY = delta == event->angleDelta().y();
35
36 // When scroll to both directions, prefer the major one
37 if (event->angleDelta().x() && event->angleDelta().y()) {
38 if (qAbs(event->angleDelta().y()) > qAbs(event->angleDelta().x())) {
39 delta = event->angleDelta().y();
40 directionY = true;
41 } else {
42 delta = event->angleDelta().x();
43 directionY = false;
44 }
45 }
46
47 // Reset when direction changes
48 if ((delta < 0 && m_wheelDelta > 0) || (delta > 0 && m_wheelDelta < 0)) {
49 m_wheelDelta = 0;
50 }
51
52 m_wheelDelta += delta;
53
54 // Angle delta 120 for common "one click"
55 // See: http://qt-project.org/doc/qt-5/qml-qtquick-wheelevent.html#angleDelta-prop
56 while (m_wheelDelta >= 120) {
57 m_wheelDelta -= 120;
58 if (directionY) {
59 m_directions.enqueue(WheelUp);
60 } else {
61 m_directions.enqueue(WheelLeft);
62 }
63 }
64 while (m_wheelDelta <= -120) {
65 m_wheelDelta += 120;
66 if (directionY) {
67 m_directions.enqueue(WheelDown);
68 } else {
69 m_directions.enqueue(WheelRight);
70 }
71 }
72}
73
75{
76 return m_directions.isEmpty() ? None : m_directions.dequeue();
77}
Direction takeDirection()
Definition: wheelhelper.cpp:74
void reset()
Definition: wheelhelper.cpp:25
void processEvent(QWheelEvent *event)
Definition: wheelhelper.cpp:31