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