2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #ifndef KITEMLISTSMOOTHSCROLLER_H
8 #define KITEMLISTSMOOTHSCROLLER_H
10 #include "dolphin_export.h"
12 #include <QAbstractAnimation>
15 class QPropertyAnimation
;
20 * @brief Helper class for KItemListContainer to have a smooth
21 * scrolling when adjusting the scrollbars.
23 class DOLPHIN_EXPORT KItemListSmoothScroller
: public QObject
28 explicit KItemListSmoothScroller(QScrollBar
*scrollBar
, QObject
*parent
= nullptr);
29 ~KItemListSmoothScroller() override
;
31 void setScrollBar(QScrollBar
*scrollBar
);
32 QScrollBar
*scrollBar() const;
34 void setTargetObject(QObject
*target
);
35 QObject
*targetObject() const;
37 void setPropertyName(const QByteArray
&propertyName
);
38 QByteArray
propertyName() const;
41 * Adjusts the position of the target by \p distance
42 * pixels. Is invoked in the context of QAbstractScrollArea::scrollContentsBy()
43 * where the scrollbars already have the new position but the content
44 * has not been scrolled yet.
46 void scrollContentsBy(qreal distance
);
49 * Does a smooth-scrolling to the position \p position
50 * on the target and also adjusts the corresponding scrollbar
51 * to the new position.
53 void scrollTo(qreal position
);
56 * Must be invoked before the scrollbar should get updated to have a new
57 * maximum. True is returned if the new maximum can be applied. If false
58 * is returned the maximum has already been reached and the value will
59 * be reached at the end of the animation.
61 // TODO: This interface is tricky to understand. Try to make this more
62 // generic/readable if the corresponding code in KItemListContainer got
64 bool requestScrollBarUpdate(int newMaximum
);
67 * Forwards wheel events to the scrollbar, ensuring smooth and proper scrolling
69 void handleWheelEvent(QWheelEvent
*event
);
73 * Emitted when the scrolling animation has finished
75 void scrollingStopped();
78 bool eventFilter(QObject
*obj
, QEvent
*event
) override
;
81 void slotAnimationStateChanged(QAbstractAnimation::State newState
, QAbstractAnimation::State oldState
);
83 * Calculates the duration of the smooth scrolling animation.
84 * If \p isSmoothScrollingEnabled is true, the duration will be calculated
85 * using the widget animation duration from the current style. Otherwise,
86 * the animation will be instantaneous.
88 void updateAnimationDuration(bool isSmoothScrollingEnabled
);
91 bool m_scrollBarPressed
;
92 bool m_smoothScrolling
;
93 QScrollBar
*m_scrollBar
;
94 QPropertyAnimation
*m_animation
;