]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistsmoothscroller.cpp
Fix includes
[dolphin.git] / src / kitemviews / private / kitemlistsmoothscroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistsmoothscroller.h"
21
22 #include <QEvent>
23 #include <QPropertyAnimation>
24 #include <QScrollBar>
25 #include <QWheelEvent>
26 #include <QStyle>
27
28 KItemListSmoothScroller::KItemListSmoothScroller(QScrollBar* scrollBar,
29 QObject* parent) :
30 QObject(parent),
31 m_scrollBarPressed(false),
32 m_smoothScrolling(true),
33 m_scrollBar(scrollBar),
34 m_animation(0)
35 {
36 m_animation = new QPropertyAnimation(this);
37 const int duration = m_scrollBar->style()->styleHint(QStyle::SH_Widget_Animate, nullptr, m_scrollBar) ? 100 : 1;
38 m_animation->setDuration(duration);
39 connect(m_animation, &QPropertyAnimation::stateChanged,
40 this, &KItemListSmoothScroller::slotAnimationStateChanged);
41
42 m_scrollBar->installEventFilter(this);
43 }
44
45 KItemListSmoothScroller::~KItemListSmoothScroller()
46 {
47 }
48
49 void KItemListSmoothScroller::setScrollBar(QScrollBar *scrollBar)
50 {
51 m_scrollBar = scrollBar;
52 }
53
54 QScrollBar* KItemListSmoothScroller::scrollBar() const
55 {
56 return m_scrollBar;
57 }
58
59 void KItemListSmoothScroller::setTargetObject(QObject* target)
60 {
61 m_animation->setTargetObject(target);
62 }
63
64 QObject* KItemListSmoothScroller::targetObject() const
65 {
66 return m_animation->targetObject();
67 }
68
69 void KItemListSmoothScroller::setPropertyName(const QByteArray& propertyName)
70 {
71 m_animation->setPropertyName(propertyName);
72 }
73
74 QByteArray KItemListSmoothScroller::propertyName() const
75 {
76 return m_animation->propertyName();
77 }
78
79 void KItemListSmoothScroller::scrollContentsBy(qreal distance)
80 {
81 QObject* target = targetObject();
82 if (!target) {
83 return;
84 }
85
86 const QByteArray name = propertyName();
87 const qreal currentOffset = target->property(name).toReal();
88 if (static_cast<int>(currentOffset) == m_scrollBar->value()) {
89 // The current offset is already synchronous to the scrollbar
90 return;
91 }
92
93 const bool animRunning = (m_animation->state() == QAbstractAnimation::Running);
94 if (animRunning) {
95 // Stopping a running animation means skipping the range from the current offset
96 // until the target offset. To prevent skipping of the range the difference
97 // is added to the new target offset.
98 const qreal oldEndOffset = m_animation->endValue().toReal();
99 distance += (currentOffset - oldEndOffset);
100 }
101
102 const qreal endOffset = currentOffset - distance;
103 if (m_smoothScrolling || animRunning) {
104 qreal startOffset = currentOffset;
105 if (animRunning) {
106 // If the animation was running and has been interrupted by assigning a new end-offset
107 // one frame must be added to the start-offset to keep the animation smooth. This also
108 // assures that animation proceeds even in cases where new end-offset are triggered
109 // within a very short timeslots.
110 startOffset += (endOffset - currentOffset) * 1000 / (m_animation->duration() * 60);
111 if (currentOffset < endOffset) {
112 startOffset = qMin(startOffset, endOffset);
113 } else {
114 startOffset = qMax(startOffset, endOffset);
115 }
116 }
117
118 m_animation->stop();
119 m_animation->setStartValue(startOffset);
120 m_animation->setEndValue(endOffset);
121 m_animation->setEasingCurve(animRunning ? QEasingCurve::OutQuad : QEasingCurve::InOutQuad);
122 m_animation->start();
123 target->setProperty(name, startOffset);
124 } else {
125 target->setProperty(name, endOffset);
126 }
127 }
128
129 void KItemListSmoothScroller::scrollTo(qreal position)
130 {
131 int newValue = position;
132 newValue = qBound(0, newValue, m_scrollBar->maximum());
133
134 if (newValue != m_scrollBar->value()) {
135 m_smoothScrolling = true;
136 m_scrollBar->setValue(newValue);
137 }
138 }
139
140 bool KItemListSmoothScroller::requestScrollBarUpdate(int newMaximum)
141 {
142 if (m_animation->state() == QAbstractAnimation::Running) {
143 if (newMaximum == m_scrollBar->maximum()) {
144 // The value has been changed by the animation, no update
145 // of the scrollbars is required as their target state will be
146 // reached with the end of the animation.
147 return false;
148 }
149
150 // The maximum has been changed which indicates that the content
151 // of the view has been changed. Stop the animation in any case and
152 // update the scrollbars immediately.
153 m_animation->stop();
154 }
155 return true;
156 }
157
158 bool KItemListSmoothScroller::eventFilter(QObject* obj, QEvent* event)
159 {
160 Q_ASSERT(obj == m_scrollBar);
161
162 switch (event->type()) {
163 case QEvent::MouseButtonPress:
164 m_scrollBarPressed = true;
165 m_smoothScrolling = true;
166 break;
167
168 case QEvent::MouseButtonRelease:
169 m_scrollBarPressed = false;
170 m_smoothScrolling = false;
171 break;
172
173 case QEvent::Wheel:
174 handleWheelEvent(static_cast<QWheelEvent*>(event));
175 break;
176
177 default:
178 break;
179 }
180
181 return QObject::eventFilter(obj, event);
182 }
183
184 void KItemListSmoothScroller::slotAnimationStateChanged(QAbstractAnimation::State newState,
185 QAbstractAnimation::State oldState)
186 {
187 Q_UNUSED(oldState);
188 if (newState == QAbstractAnimation::Stopped && m_smoothScrolling && !m_scrollBarPressed) {
189 m_smoothScrolling = false;
190 }
191 }
192
193 void KItemListSmoothScroller::handleWheelEvent(QWheelEvent* event)
194 {
195 const int numDegrees = event->delta() / 8;
196 const int numSteps = numDegrees / 15;
197
198 const bool previous = m_smoothScrolling;
199
200 m_smoothScrolling = true;
201 const int value = m_scrollBar->value();
202 const int pageStep = m_scrollBar->pageStep();
203 m_scrollBar->setValue(value - numSteps * pageStep);
204
205 m_smoothScrolling = previous;
206
207 event->accept();
208 }
209