Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/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", false); | ||||
30 | bool hideScrollBars = config.readEntry("KineticScrollingHideScrollbar", false); | ||||
31 | QScroller::ScrollerGestureType gestureType = getConfiguredGestureType(); | ||||
32 | | ||||
33 | if(enabled && scrollArea){ | ||||
34 | if(hideScrollBars){ | ||||
35 | scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); | ||||
36 | scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); | ||||
37 | } | ||||
38 | | ||||
39 | QAbstractItemView *itemView = qobject_cast<QAbstractItemView *>(scrollArea); | ||||
40 | if(itemView){ | ||||
41 | itemView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); | ||||
42 | } | ||||
43 | | ||||
44 | QScroller *scroller = QScroller::scroller(scrollArea); | ||||
45 | scroller->grabGesture(scrollArea, gestureType); | ||||
46 | | ||||
47 | QScrollerProperties properties; | ||||
48 | | ||||
49 | // DragStartDistance seems to be based on meter per second; though it's | ||||
50 | // not explicitly documented, other QScroller values are in that metric. | ||||
51 | // To start kinetic scrolling, with minimal sensitity, we expect a drag | ||||
52 | // of 10 mm, with minimum sensitity any > 0 mm. | ||||
53 | const float mm = 0.001f; | ||||
54 | const float resistance = 1.0f - (sensitivity / 100.0f); | ||||
55 | | ||||
56 | properties.setScrollMetric(QScrollerProperties::DragStartDistance, resistance * 10.0f * mm); | ||||
57 | properties.setScrollMetric(QScrollerProperties::DragVelocitySmoothingFactor, 1.0f); | ||||
58 | properties.setScrollMetric(QScrollerProperties::MinimumVelocity, 0.0f); | ||||
59 | properties.setScrollMetric(QScrollerProperties::AxisLockThreshold, 1.0f); | ||||
60 | properties.setScrollMetric(QScrollerProperties::MaximumClickThroughVelocity, 0.0f); | ||||
61 | properties.setScrollMetric(QScrollerProperties::MousePressEventDelay, 1.0f - 0.75f * resistance); | ||||
62 | properties.setScrollMetric(QScrollerProperties::AcceleratingFlickSpeedupFactor, 1.5f); | ||||
63 | | ||||
64 | properties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, QScrollerProperties::OvershootAlwaysOn); | ||||
65 | properties.setScrollMetric(QScrollerProperties::OvershootDragResistanceFactor, 0.1); | ||||
66 | properties.setScrollMetric(QScrollerProperties::OvershootDragDistanceFactor, 0.3); | ||||
67 | properties.setScrollMetric(QScrollerProperties::OvershootScrollDistanceFactor, 0.1); | ||||
68 | properties.setScrollMetric(QScrollerProperties::OvershootScrollTime, 0.4); | ||||
69 | | ||||
70 | scroller->setScrollerProperties(properties); | ||||
71 | | ||||
72 | return scroller; | ||||
73 | } | ||||
74 | | ||||
75 | return nullptr; | ||||
76 | } | ||||
77 | | ||||
78 | QScroller::ScrollerGestureType KisKineticScroller::getConfiguredGestureType() { | ||||
79 | KConfigGroup config = KSharedConfig::openConfig()->group(""); | ||||
80 | int gesture = config.readEntry("KineticScrollingGesture", 0); | ||||
81 | | ||||
82 | switch (gesture) { | ||||
83 | case 0: { | ||||
84 | return QScroller::TouchGesture; | ||||
85 | } | ||||
86 | case 1: { | ||||
87 | return QScroller::LeftMouseButtonGesture; | ||||
88 | } | ||||
89 | case 2: { | ||||
90 | return QScroller::MiddleMouseButtonGesture; | ||||
91 | } | ||||
92 | case 3: { | ||||
93 | return QScroller::RightMouseButtonGesture; | ||||
94 | } | ||||
95 | default: | ||||
96 | return QScroller::MiddleMouseButtonGesture; | ||||
97 | } | ||||
98 | } | ||||
99 | | ||||
100 | void KisKineticScroller::updateCursor(QWidget *source, QScroller::State state) { | ||||
101 | if( state == QScroller::State::Pressed ) { | ||||
102 | source->setCursor(Qt::OpenHandCursor); | ||||
103 | } else if (state == QScroller::State::Dragging) { | ||||
104 | source->setCursor(Qt::ClosedHandCursor); | ||||
105 | } else { | ||||
106 | source->setCursor(Qt::ArrowCursor); | ||||
107 | } | ||||
108 | } |