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"
23 #include <QPropertyAnimation>
25 #include <QWheelEvent>
28 KItemListSmoothScroller::KItemListSmoothScroller(QScrollBar
* scrollBar
,
31 m_scrollBarPressed(false),
32 m_smoothScrolling(true),
33 m_scrollBar(scrollBar
),
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
);
42 m_scrollBar
->installEventFilter(this);
45 KItemListSmoothScroller::~KItemListSmoothScroller()
49 void KItemListSmoothScroller::setScrollBar(QScrollBar
*scrollBar
)
51 m_scrollBar
= scrollBar
;
54 QScrollBar
* KItemListSmoothScroller::scrollBar() const
59 void KItemListSmoothScroller::setTargetObject(QObject
* target
)
61 m_animation
->setTargetObject(target
);
64 QObject
* KItemListSmoothScroller::targetObject() const
66 return m_animation
->targetObject();
69 void KItemListSmoothScroller::setPropertyName(const QByteArray
& propertyName
)
71 m_animation
->setPropertyName(propertyName
);
74 QByteArray
KItemListSmoothScroller::propertyName() const
76 return m_animation
->propertyName();
79 void KItemListSmoothScroller::scrollContentsBy(qreal distance
)
81 QObject
* target
= targetObject();
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
93 const bool animRunning
= (m_animation
->state() == QAbstractAnimation::Running
);
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
);
102 const qreal endOffset
= currentOffset
- distance
;
103 if (m_smoothScrolling
|| animRunning
) {
104 qreal startOffset
= currentOffset
;
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
);
114 startOffset
= qMax(startOffset
, endOffset
);
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
);
125 target
->setProperty(name
, endOffset
);
129 void KItemListSmoothScroller::scrollTo(qreal position
)
131 int newValue
= position
;
132 newValue
= qBound(0, newValue
, m_scrollBar
->maximum());
134 if (newValue
!= m_scrollBar
->value()) {
135 m_smoothScrolling
= true;
136 m_scrollBar
->setValue(newValue
);
140 bool KItemListSmoothScroller::requestScrollBarUpdate(int newMaximum
)
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.
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.
158 bool KItemListSmoothScroller::eventFilter(QObject
* obj
, QEvent
* event
)
160 Q_ASSERT(obj
== m_scrollBar
);
162 switch (event
->type()) {
163 case QEvent::MouseButtonPress
:
164 m_scrollBarPressed
= true;
165 m_smoothScrolling
= true;
168 case QEvent::MouseButtonRelease
:
169 m_scrollBarPressed
= false;
170 m_smoothScrolling
= false;
174 handleWheelEvent(static_cast<QWheelEvent
*>(event
));
175 return true; // eat event so that QScrollBar does not scroll one step more by itself
181 return QObject::eventFilter(obj
, event
);
184 void KItemListSmoothScroller::slotAnimationStateChanged(QAbstractAnimation::State newState
,
185 QAbstractAnimation::State oldState
)
188 if (newState
== QAbstractAnimation::Stopped
&& m_smoothScrolling
&& !m_scrollBarPressed
) {
189 m_smoothScrolling
= false;
193 void KItemListSmoothScroller::handleWheelEvent(QWheelEvent
* event
)
195 const bool previous
= m_smoothScrolling
;
197 m_smoothScrolling
= true;
199 if (!event
->pixelDelta().isNull()) {
200 numPixels
= event
->pixelDelta().y();
202 const int numDegrees
= event
->angleDelta().y() / 8;
203 const int numSteps
= numDegrees
/ 15;
204 numPixels
= numSteps
* m_scrollBar
->pageStep() / 4;
206 int value
= m_scrollBar
->value();
207 if (event
->modifiers().testFlag(Qt::ShiftModifier
)) {
208 const int scrollingDirection
= numPixels
> 0 ? 1 : -1;
209 value
-= m_scrollBar
->pageStep() * scrollingDirection
;
213 m_scrollBar
->setValue(value
);
215 m_smoothScrolling
= previous
;