File indexing completed on 2024-05-12 05:47:31

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KITEMLISTSMOOTHSCROLLER_H
0008 #define KITEMLISTSMOOTHSCROLLER_H
0009 
0010 #include "dolphin_export.h"
0011 
0012 #include <QAbstractAnimation>
0013 #include <QObject>
0014 
0015 class QPropertyAnimation;
0016 class QScrollBar;
0017 class QWheelEvent;
0018 
0019 /**
0020  * @brief Helper class for KItemListContainer to have a smooth
0021  *        scrolling when adjusting the scrollbars.
0022  */
0023 class DOLPHIN_EXPORT KItemListSmoothScroller : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     explicit KItemListSmoothScroller(QScrollBar *scrollBar, QObject *parent = nullptr);
0029     ~KItemListSmoothScroller() override;
0030 
0031     void setScrollBar(QScrollBar *scrollBar);
0032     QScrollBar *scrollBar() const;
0033 
0034     void setTargetObject(QObject *target);
0035     QObject *targetObject() const;
0036 
0037     void setPropertyName(const QByteArray &propertyName);
0038     QByteArray propertyName() const;
0039 
0040     /**
0041      * Adjusts the position of the target by \p distance
0042      * pixels. Is invoked in the context of QAbstractScrollArea::scrollContentsBy()
0043      * where the scrollbars already have the new position but the content
0044      * has not been scrolled yet.
0045      */
0046     void scrollContentsBy(qreal distance);
0047 
0048     /**
0049      * Does a smooth-scrolling to the position \p position
0050      * on the target and also adjusts the corresponding scrollbar
0051      * to the new position.
0052      */
0053     void scrollTo(qreal position);
0054 
0055     /**
0056      * Must be invoked before the scrollbar should get updated to have a new
0057      * maximum. True is returned if the new maximum can be applied. If false
0058      * is returned the maximum has already been reached and the value will
0059      * be reached at the end of the animation.
0060      */
0061     // TODO: This interface is tricky to understand. Try to make this more
0062     // generic/readable if the corresponding code in KItemListContainer got
0063     // stable.
0064     bool requestScrollBarUpdate(int newMaximum);
0065 
0066     /**
0067      * Forwards wheel events to the scrollbar, ensuring smooth and proper scrolling
0068      */
0069     void handleWheelEvent(QWheelEvent *event);
0070 
0071 Q_SIGNALS:
0072     /**
0073      * Emitted when the scrolling animation has finished
0074      */
0075     void scrollingStopped();
0076 
0077 protected:
0078     bool eventFilter(QObject *obj, QEvent *event) override;
0079 
0080 private Q_SLOTS:
0081     void slotAnimationStateChanged(QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
0082 
0083 private:
0084     bool m_scrollBarPressed;
0085     bool m_smoothScrolling;
0086     QScrollBar *m_scrollBar;
0087     QPropertyAnimation *m_animation;
0088 };
0089 
0090 #endif