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 #ifndef KITEMLISTSMOOTHSCROLLER_H
21 #define KITEMLISTSMOOTHSCROLLER_H
23 #include "dolphin_export.h"
25 #include <QAbstractAnimation>
28 class QPropertyAnimation
;
33 * @brief Helper class for KItemListContainer to have a smooth
34 * scrolling when adjusting the scrollbars.
36 class DOLPHIN_EXPORT KItemListSmoothScroller
: public QObject
41 explicit KItemListSmoothScroller(QScrollBar
* scrollBar
,
43 virtual ~KItemListSmoothScroller();
45 void setScrollBar(QScrollBar
* scrollBar
);
46 QScrollBar
* scrollBar() const;
48 void setTargetObject(QObject
* target
);
49 QObject
* targetObject() const;
51 void setPropertyName(const QByteArray
& propertyName
);
52 QByteArray
propertyName() const;
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.
60 void scrollContentsBy(qreal distance
);
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.
67 void scrollTo(qreal position
);
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.
75 // TODO: This interface is tricky to understand. Try to make this more
76 // generic/readable if the corresponding code in KItemListContainer got
78 bool requestScrollBarUpdate(int newMaximum
);
81 * Forwards wheel events to the scrollbar, ensuring smooth and proper scrolling
83 void handleWheelEvent(QWheelEvent
* event
);
86 bool eventFilter(QObject
* obj
, QEvent
* event
) Q_DECL_OVERRIDE
;
89 void slotAnimationStateChanged(QAbstractAnimation::State newState
,
90 QAbstractAnimation::State oldState
);
93 bool m_scrollBarPressed
;
94 bool m_smoothScrolling
;
95 QScrollBar
* m_scrollBar
;
96 QPropertyAnimation
* m_animation
;