]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistsmoothscroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kitemlistsmoothscroller.h"
22 #include <KGlobalSettings>
24 #include <QPropertyAnimation>
26 #include <QWheelEvent>
30 KItemListSmoothScroller::KItemListSmoothScroller(QScrollBar
* scrollBar
,
33 m_scrollBarPressed(false),
34 m_smoothScrolling(true),
35 m_scrollBar(scrollBar
),
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
)));
44 m_scrollBar
->installEventFilter(this);
47 KItemListSmoothScroller::~KItemListSmoothScroller()
51 void KItemListSmoothScroller::setScrollBar(QScrollBar
*scrollBar
)
53 m_scrollBar
= scrollBar
;
56 QScrollBar
* KItemListSmoothScroller::scrollBar() const
61 void KItemListSmoothScroller::setTargetObject(QObject
* target
)
63 m_animation
->setTargetObject(target
);
66 QObject
* KItemListSmoothScroller::targetObject() const
68 return m_animation
->targetObject();
71 void KItemListSmoothScroller::setPropertyName(const QByteArray
& propertyName
)
73 m_animation
->setPropertyName(propertyName
);
76 QByteArray
KItemListSmoothScroller::propertyName() const
78 return m_animation
->propertyName();
81 void KItemListSmoothScroller::scrollContentsBy(qreal distance
)
83 QObject
* target
= targetObject();
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
95 const bool animRunning
= (m_animation
->state() == QAbstractAnimation::Running
);
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
);
104 const qreal endOffset
= currentOffset
- distance
;
105 if (m_smoothScrolling
|| animRunning
) {
106 qreal startOffset
= currentOffset
;
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
);
116 startOffset
= qMax(startOffset
, endOffset
);
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
);
127 target
->setProperty(name
, endOffset
);
131 void KItemListSmoothScroller::scrollTo(qreal position
)
133 m_smoothScrolling
= true;
134 m_scrollBar
->setValue(position
);
137 bool KItemListSmoothScroller::requestScrollBarUpdate(int newMaximum
)
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.
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.
155 bool KItemListSmoothScroller::eventFilter(QObject
* obj
, QEvent
* event
)
157 Q_ASSERT(obj
== m_scrollBar
);
159 switch (event
->type()) {
160 case QEvent::MouseButtonPress
:
161 m_scrollBarPressed
= true;
162 m_smoothScrolling
= true;
165 case QEvent::MouseButtonRelease
:
166 m_scrollBarPressed
= false;
167 m_smoothScrolling
= false;
171 handleWheelEvent(static_cast<QWheelEvent
*>(event
));
178 return QObject::eventFilter(obj
, event
);
181 void KItemListSmoothScroller::slotAnimationStateChanged(QAbstractAnimation::State newState
,
182 QAbstractAnimation::State oldState
)
185 if (newState
== QAbstractAnimation::Stopped
&& m_smoothScrolling
&& !m_scrollBarPressed
) {
186 m_smoothScrolling
= false;
190 void KItemListSmoothScroller::handleWheelEvent(QWheelEvent
* event
)
192 const int numDegrees
= event
->delta() / 8;
193 const int numSteps
= numDegrees
/ 15;
195 const bool previous
= m_smoothScrolling
;
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
);
202 m_smoothScrolling
= previous
;
207 #include "kitemlistsmoothscroller.moc"