Changeset View
Changeset View
Standalone View
Standalone View
libs/widgetutils/KisKineticScroller.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 Emmet O'Neill <emmetoneill.pdx@gmail.com> | ||||
3 | * Copyright (C) 2018 Eoin O'Neill <eoinoneill1991@gmail.com> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library 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 GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <KisKineticScroller.h> | ||||
22 | #include <QAbstractItemView> | ||||
23 | #include <ksharedconfig.h> | ||||
24 | #include <kconfiggroup.h> | ||||
25 | | ||||
26 | QScroller* KisKineticScroller::createPreconfiguredScroller(QAbstractScrollArea *scrollArea) { | ||||
27 | KConfigGroup config = KSharedConfig::openConfig()->group(""); | ||||
28 | int sensitivity = config.readEntry("KineticScrollingSensitivity", 75); | ||||
29 | bool enabled = config.readEntry("KineticScrollingEnabled", true); | ||||
30 | bool hideScrollBars = config.readEntry("KineticScrollingHideScrollbar", false); | ||||
31 | float resistanceCoefficient = config.readEntry("KineticScrollingResistanceCoefficient", 10.0f); | ||||
32 | float dragVelocitySmoothFactor = config.readEntry("KineticScrollingDragVelocitySmoothingFactor", 1.0f); | ||||
33 | float minimumVelocity = config.readEntry("KineticScrollingMinimumVelocity", 0.0f); | ||||
34 | float axisLockThresh = config.readEntry("KineticScrollingAxisLockThreshold", 1.0f); | ||||
35 | float maximumClickThroughVelocity = config.readEntry("KineticScrollingMaxClickThroughVelocity", 0.0f); | ||||
36 | float flickAccelerationFactor = config.readEntry("KineticScrollingFlickAccelerationFactor", 1.5f); | ||||
37 | float overshootDragResistanceFactor = config.readEntry("KineticScrollingOvershotDragResistanceFactor", 0.1f); | ||||
38 | float overshootDragDistanceFactor = config.readEntry("KineticScrollingOvershootDragDistanceFactor", 0.3f); | ||||
39 | float overshootScrollDistanceFactor = config.readEntry("KineticScrollingOvershootScrollDistanceFactor", 0.1f); | ||||
40 | float overshootScrollTime = config.readEntry("KineticScrollingOvershootScrollTime", 0.4f); | ||||
41 | QScroller::ScrollerGestureType gestureType = getConfiguredGestureType(); | ||||
42 | | ||||
43 | if (enabled && scrollArea) { | ||||
44 | if (hideScrollBars) { | ||||
45 | scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); | ||||
46 | scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); | ||||
47 | } | ||||
48 | | ||||
49 | QAbstractItemView *itemView = qobject_cast<QAbstractItemView *>(scrollArea); | ||||
50 | if (itemView) { | ||||
51 | itemView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); | ||||
52 | } | ||||
53 | | ||||
54 | QScroller *scroller = QScroller::scroller(scrollArea); | ||||
55 | scroller->grabGesture(scrollArea, gestureType); | ||||
56 | | ||||
57 | QScrollerProperties properties; | ||||
58 | | ||||
59 | // DragStartDistance seems to be based on meter per second; though it's | ||||
60 | // not explicitly documented, other QScroller values are in that metric. | ||||
61 | // To start kinetic scrolling, with minimal sensitity, we expect a drag | ||||
62 | // of 10 mm, with minimum sensitity any > 0 mm. | ||||
63 | const float mm = 0.001f; | ||||
64 | const float resistance = 1.0f - (sensitivity / 100.0f); | ||||
65 | const float mousePressEventDelay = config.readEntry("KineticScrollingMousePressDelay", 1.0f - 0.75f * resistance); | ||||
66 | | ||||
67 | properties.setScrollMetric(QScrollerProperties::DragStartDistance, resistance * resistanceCoefficient * mm); | ||||
68 | properties.setScrollMetric(QScrollerProperties::DragVelocitySmoothingFactor, dragVelocitySmoothFactor); | ||||
69 | properties.setScrollMetric(QScrollerProperties::MinimumVelocity, minimumVelocity); | ||||
70 | properties.setScrollMetric(QScrollerProperties::AxisLockThreshold, axisLockThresh); | ||||
71 | properties.setScrollMetric(QScrollerProperties::MaximumClickThroughVelocity, maximumClickThroughVelocity); | ||||
72 | properties.setScrollMetric(QScrollerProperties::MousePressEventDelay, mousePressEventDelay); | ||||
73 | properties.setScrollMetric(QScrollerProperties::AcceleratingFlickSpeedupFactor, flickAccelerationFactor); | ||||
74 | | ||||
75 | properties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOn); | ||||
76 | properties.setScrollMetric(QScrollerProperties::OvershootDragResistanceFactor, overshootDragResistanceFactor); | ||||
77 | properties.setScrollMetric(QScrollerProperties::OvershootDragDistanceFactor, overshootDragDistanceFactor); | ||||
78 | properties.setScrollMetric(QScrollerProperties::OvershootScrollDistanceFactor, overshootScrollDistanceFactor); | ||||
79 | properties.setScrollMetric(QScrollerProperties::OvershootScrollTime, overshootScrollTime); | ||||
80 | | ||||
81 | scroller->setScrollerProperties(properties); | ||||
82 | | ||||
83 | return scroller; | ||||
84 | } | ||||
85 | | ||||
86 | return nullptr; | ||||
87 | } | ||||
88 | | ||||
89 | QScroller::ScrollerGestureType KisKineticScroller::getConfiguredGestureType() { | ||||
90 | KConfigGroup config = KSharedConfig::openConfig()->group(""); | ||||
91 | int gesture = config.readEntry("KineticScrollingGesture", 0); | ||||
92 | | ||||
93 | switch (gesture) { | ||||
94 | case 0: { | ||||
95 | return QScroller::TouchGesture; | ||||
96 | } | ||||
97 | case 1: { | ||||
98 | return QScroller::LeftMouseButtonGesture; | ||||
99 | } | ||||
100 | case 2: { | ||||
101 | return QScroller::MiddleMouseButtonGesture; | ||||
102 | } | ||||
103 | case 3: { | ||||
104 | return QScroller::RightMouseButtonGesture; | ||||
105 | } | ||||
106 | default: | ||||
107 | return QScroller::MiddleMouseButtonGesture; | ||||
108 | } | ||||
109 | } | ||||
110 | | ||||
111 | void KisKineticScroller::updateCursor(QWidget *source, QScroller::State state) { | ||||
112 | if( state == QScroller::State::Pressed ) { | ||||
113 | source->setCursor(Qt::OpenHandCursor); | ||||
114 | } else if (state == QScroller::State::Dragging) { | ||||
115 | source->setCursor(Qt::ClosedHandCursor); | ||||
116 | } else { | ||||
117 | source->setCursor(Qt::ArrowCursor); | ||||
118 | } | ||||
119 | } |