]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistsmoothscroller.h
Build with QT_NO_KEYWORDS
[dolphin.git] / src / kitemviews / private / kitemlistsmoothscroller.h
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #ifndef KITEMLISTSMOOTHSCROLLER_H
8 #define KITEMLISTSMOOTHSCROLLER_H
9
10 #include "dolphin_export.h"
11
12 #include <QAbstractAnimation>
13 #include <QObject>
14
15 class QPropertyAnimation;
16 class QScrollBar;
17 class QWheelEvent;
18
19 /**
20 * @brief Helper class for KItemListContainer to have a smooth
21 * scrolling when adjusting the scrollbars.
22 */
23 class DOLPHIN_EXPORT KItemListSmoothScroller : public QObject
24 {
25 Q_OBJECT
26
27 public:
28 explicit KItemListSmoothScroller(QScrollBar* scrollBar,
29 QObject* parent = nullptr);
30 ~KItemListSmoothScroller() override;
31
32 void setScrollBar(QScrollBar* scrollBar);
33 QScrollBar* scrollBar() const;
34
35 void setTargetObject(QObject* target);
36 QObject* targetObject() const;
37
38 void setPropertyName(const QByteArray& propertyName);
39 QByteArray propertyName() const;
40
41 /**
42 * Adjusts the position of the target by \p distance
43 * pixels. Is invoked in the context of QAbstractScrollArea::scrollContentsBy()
44 * where the scrollbars already have the new position but the content
45 * has not been scrolled yet.
46 */
47 void scrollContentsBy(qreal distance);
48
49 /**
50 * Does a smooth-scrolling to the position \p position
51 * on the target and also adjusts the corresponding scrollbar
52 * to the new position.
53 */
54 void scrollTo(qreal position);
55
56 /**
57 * Must be invoked before the scrollbar should get updated to have a new
58 * maximum. True is returned if the new maximum can be applied. If false
59 * is returned the maximum has already been reached and the value will
60 * be reached at the end of the animation.
61 */
62 // TODO: This interface is tricky to understand. Try to make this more
63 // generic/readable if the corresponding code in KItemListContainer got
64 // stable.
65 bool requestScrollBarUpdate(int newMaximum);
66
67 /**
68 * Forwards wheel events to the scrollbar, ensuring smooth and proper scrolling
69 */
70 void handleWheelEvent(QWheelEvent* event);
71
72 protected:
73 bool eventFilter(QObject* obj, QEvent* event) override;
74
75 private Q_SLOTS:
76 void slotAnimationStateChanged(QAbstractAnimation::State newState,
77 QAbstractAnimation::State oldState);
78
79 private:
80 bool m_scrollBarPressed;
81 bool m_smoothScrolling;
82 QScrollBar* m_scrollBar;
83 QPropertyAnimation* m_animation;
84 };
85
86 #endif
87
88