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
, &QPropertyAnimation::stateChanged
,
42 this, &KItemListSmoothScroller::slotAnimationStateChanged
);
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 int newValue
= position
;
134 newValue
= qBound(0, newValue
, m_scrollBar
->maximum());
136 if (newValue
!= m_scrollBar
->value()) {
137 m_smoothScrolling
= true;
138 m_scrollBar
->setValue(newValue
);
142 bool KItemListSmoothScroller::requestScrollBarUpdate(int newMaximum
)
144 if (m_animation
->state() == QAbstractAnimation::Running
) {
145 if (newMaximum
== m_scrollBar
->maximum()) {
146 // The value has been changed by the animation, no update
147 // of the scrollbars is required as their target state will be
148 // reached with the end of the animation.
152 // The maximum has been changed which indicates that the content
153 // of the view has been changed. Stop the animation in any case and
154 // update the scrollbars immediately.
160 bool KItemListSmoothScroller::eventFilter(QObject
* obj
, QEvent
* event
)
162 Q_ASSERT(obj
== m_scrollBar
);
164 switch (event
->type()) {
165 case QEvent::MouseButtonPress
:
166 m_scrollBarPressed
= true;
167 m_smoothScrolling
= true;
170 case QEvent::MouseButtonRelease
:
171 m_scrollBarPressed
= false;
172 m_smoothScrolling
= false;
176 handleWheelEvent(static_cast<QWheelEvent
*>(event
));
183 return QObject::eventFilter(obj
, event
);
186 void KItemListSmoothScroller::slotAnimationStateChanged(QAbstractAnimation::State newState
,
187 QAbstractAnimation::State oldState
)
190 if (newState
== QAbstractAnimation::Stopped
&& m_smoothScrolling
&& !m_scrollBarPressed
) {
191 m_smoothScrolling
= false;
195 void KItemListSmoothScroller::handleWheelEvent(QWheelEvent
* event
)
197 const int numDegrees
= event
->delta() / 8;
198 const int numSteps
= numDegrees
/ 15;
200 const bool previous
= m_smoothScrolling
;
202 m_smoothScrolling
= true;
203 const int value
= m_scrollBar
->value();
204 const int pageStep
= m_scrollBar
->pageStep();
205 m_scrollBar
->setValue(value
- numSteps
* pageStep
);
207 m_smoothScrolling
= previous
;
212 #include "kitemlistsmoothscroller.moc"