]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistsmoothscroller.cpp
KItemViews: Internal directory restructuration
[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 <KGlobalSettings>
23 #include <QEvent>
24 #include <QPropertyAnimation>
25 #include <QScrollBar>
26 #include <QWheelEvent>
27
28 #include <KDebug>
29
30 KItemListSmoothScroller::KItemListSmoothScroller(QScrollBar* scrollBar,
31 QObject* parent) :
32 QObject(parent),
33 m_scrollBarPressed(false),
34 m_smoothScrolling(true),
35 m_scrollBar(scrollBar),
36 m_animation(0)
37 {
38 m_animation = new QPropertyAnimation(this);
39 const int duration = (KGlobalSettings::graphicEffectsLevel() == KGlobalSettings::NoEffects) ? 1 : 100;
40 m_animation->setDuration(duration);
41 connect(m_animation, SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)),
42 this, SLOT(slotAnimationStateChanged(QAbstractAnimation::State,QAbstractAnimation::State)));
43
44 m_scrollBar->installEventFilter(this);
45 }
46
47 KItemListSmoothScroller::~KItemListSmoothScroller()
48 {
49 }
50
51 void KItemListSmoothScroller::setScrollBar(QScrollBar *scrollBar)
52 {
53 m_scrollBar = scrollBar;
54 }
55
56 QScrollBar* KItemListSmoothScroller::scrollBar() const
57 {
58 return m_scrollBar;
59 }
60
61 void KItemListSmoothScroller::setTargetObject(QObject* target)
62 {
63 m_animation->setTargetObject(target);
64 }
65
66 QObject* KItemListSmoothScroller::targetObject() const
67 {
68 return m_animation->targetObject();
69 }
70
71 void KItemListSmoothScroller::setPropertyName(const QByteArray& propertyName)
72 {
73 m_animation->setPropertyName(propertyName);
74 }
75
76 QByteArray KItemListSmoothScroller::propertyName() const
77 {
78 return m_animation->propertyName();
79 }
80
81 void KItemListSmoothScroller::scrollContentsBy(qreal distance)
82 {
83 QObject* target = targetObject();
84 if (!target) {
85 return;
86 }
87
88 const QByteArray name = propertyName();
89 const qreal currentOffset = target->property(name).toReal();
90 if (static_cast<int>(currentOffset) == m_scrollBar->value()) {
91 // The current offset is already synchronous to the scrollbar
92 return;
93 }
94
95 const bool animRunning = (m_animation->state() == QAbstractAnimation::Running);
96 if (animRunning) {
97 // Stopping a running animation means skipping the range from the current offset
98 // until the target offset. To prevent skipping of the range the difference
99 // is added to the new target offset.
100 const qreal oldEndOffset = m_animation->endValue().toReal();
101 distance += (currentOffset - oldEndOffset);
102 }
103
104 const qreal endOffset = currentOffset - distance;
105 if (m_smoothScrolling || animRunning) {
106 qreal startOffset = currentOffset;
107 if (animRunning) {
108 // If the animation was running and has been interrupted by assigning a new end-offset
109 // one frame must be added to the start-offset to keep the animation smooth. This also
110 // assures that animation proceeds even in cases where new end-offset are triggered
111 // within a very short timeslots.
112 startOffset += (endOffset - currentOffset) * 1000 / (m_animation->duration() * 60);
113 if (currentOffset < endOffset) {
114 startOffset = qMin(startOffset, endOffset);
115 } else {
116 startOffset = qMax(startOffset, endOffset);
117 }
118 }
119
120 m_animation->stop();
121 m_animation->setStartValue(startOffset);
122 m_animation->setEndValue(endOffset);
123 m_animation->setEasingCurve(animRunning ? QEasingCurve::OutQuad : QEasingCurve::InOutQuad);
124 m_animation->start();
125 target->setProperty(name, startOffset);
126 } else {
127 target->setProperty(name, endOffset);
128 }
129 }
130
131 void KItemListSmoothScroller::scrollTo(qreal position)
132 {
133 m_smoothScrolling = true;
134 m_scrollBar->setValue(position);
135 }
136
137 bool KItemListSmoothScroller::requestScrollBarUpdate(int newMaximum)
138 {
139 if (m_animation->state() == QAbstractAnimation::Running) {
140 if (newMaximum == m_scrollBar->maximum()) {
141 // The value has been changed by the animation, no update
142 // of the scrollbars is required as their target state will be
143 // reached with the end of the animation.
144 return false;
145 }
146
147 // The maximum has been changed which indicates that the content
148 // of the view has been changed. Stop the animation in any case and
149 // update the scrollbars immediately.
150 m_animation->stop();
151 }
152 return true;
153 }
154
155 bool KItemListSmoothScroller::eventFilter(QObject* obj, QEvent* event)
156 {
157 Q_ASSERT(obj == m_scrollBar);
158
159 switch (event->type()) {
160 case QEvent::MouseButtonPress:
161 m_scrollBarPressed = true;
162 m_smoothScrolling = true;
163 break;
164
165 case QEvent::MouseButtonRelease:
166 m_scrollBarPressed = false;
167 m_smoothScrolling = false;
168 break;
169
170 case QEvent::Wheel:
171 handleWheelEvent(static_cast<QWheelEvent*>(event));
172 break;
173
174 default:
175 break;
176 }
177
178 return QObject::eventFilter(obj, event);
179 }
180
181 void KItemListSmoothScroller::slotAnimationStateChanged(QAbstractAnimation::State newState,
182 QAbstractAnimation::State oldState)
183 {
184 Q_UNUSED(oldState);
185 if (newState == QAbstractAnimation::Stopped && m_smoothScrolling && !m_scrollBarPressed) {
186 m_smoothScrolling = false;
187 }
188 }
189
190 void KItemListSmoothScroller::handleWheelEvent(QWheelEvent* event)
191 {
192 const int numDegrees = event->delta() / 8;
193 const int numSteps = numDegrees / 15;
194
195 const bool previous = m_smoothScrolling;
196
197 m_smoothScrolling = true;
198 const int value = m_scrollBar->value();
199 const int pageStep = m_scrollBar->pageStep();
200 m_scrollBar->setValue(value - numSteps * pageStep);
201
202 m_smoothScrolling = previous;
203
204 event->accept();
205 }
206
207 #include "kitemlistsmoothscroller.moc"