File indexing completed on 2024-05-12 04:58:29
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "wheelhelper.h" 0019 0020 #include <QWheelEvent> 0021 0022 WheelHelper::WheelHelper() 0023 = default; 0024 0025 void WheelHelper::reset() 0026 { 0027 m_wheelDelta = 0; 0028 m_directions.clear(); 0029 } 0030 0031 void WheelHelper::processEvent(QWheelEvent *event) 0032 { 0033 int delta = event->angleDelta().x() ? event->angleDelta().x() : event->angleDelta().y(); 0034 bool directionY = delta == event->angleDelta().y(); 0035 0036 // When scroll to both directions, prefer the major one 0037 if (event->angleDelta().x() && event->angleDelta().y()) { 0038 if (qAbs(event->angleDelta().y()) > qAbs(event->angleDelta().x())) { 0039 delta = event->angleDelta().y(); 0040 directionY = true; 0041 } else { 0042 delta = event->angleDelta().x(); 0043 directionY = false; 0044 } 0045 } 0046 0047 // Reset when direction changes 0048 if ((delta < 0 && m_wheelDelta > 0) || (delta > 0 && m_wheelDelta < 0)) { 0049 m_wheelDelta = 0; 0050 } 0051 0052 m_wheelDelta += delta; 0053 0054 // Angle delta 120 for common "one click" 0055 // See: http://qt-project.org/doc/qt-5/qml-qtquick-wheelevent.html#angleDelta-prop 0056 while (m_wheelDelta >= 120) { 0057 m_wheelDelta -= 120; 0058 if (directionY) { 0059 m_directions.enqueue(WheelUp); 0060 } else { 0061 m_directions.enqueue(WheelLeft); 0062 } 0063 } 0064 while (m_wheelDelta <= -120) { 0065 m_wheelDelta += 120; 0066 if (directionY) { 0067 m_directions.enqueue(WheelDown); 0068 } else { 0069 m_directions.enqueue(WheelRight); 0070 } 0071 } 0072 } 0073 0074 WheelHelper::Direction WheelHelper::takeDirection() 0075 { 0076 return m_directions.isEmpty() ? None : m_directions.dequeue(); 0077 }