|
1 |
|
2 #ifndef SCROLLHELPER_H_ |
|
3 #define SCROLLHELPER_H_ |
|
4 |
|
5 #include <QPropertyAnimation> |
|
6 #include <QRectF> |
|
7 #include <QPoint> |
|
8 #include <QTime> |
|
9 #include <QTimer> |
|
10 #include "uitimer.h" |
|
11 |
|
12 namespace GVA { |
|
13 class ScrollHelper: public QObject |
|
14 { |
|
15 Q_OBJECT |
|
16 |
|
17 public: |
|
18 enum ScrollMode { |
|
19 ReadyMode = 0, |
|
20 PanScrollMode, |
|
21 KineticScrollMode |
|
22 }; |
|
23 |
|
24 enum ScrollState { |
|
25 IdleState = 0, // no scrolling |
|
26 ActiveState, // scroll poins added |
|
27 WaitState // no new scroll points |
|
28 }; |
|
29 |
|
30 enum ScrollProperty { |
|
31 Geometry, |
|
32 Position, |
|
33 Velocity |
|
34 }; |
|
35 |
|
36 ScrollHelper(QObject* scrolledWidget); |
|
37 ~ScrollHelper(); |
|
38 void scroll(QPointF& delta); |
|
39 |
|
40 bool isScrolling(); |
|
41 void panFromOvershoot(); |
|
42 void setFlickDurationLimits(int minDuration, int midDuration, int maxDuration); |
|
43 void setFlickSpeedLimits(qreal minSpeed, qreal midSpeed, qreal maxSpeed); |
|
44 void setDeceleration(qreal decel); |
|
45 qreal getDeceleration(); |
|
46 void kineticScroll(QPointF& speed); |
|
47 QSizeF viewportSize(); |
|
48 void setViewportSize(const QSizeF& size); |
|
49 void setFlickLimits(qreal minFlick, qreal midFlick, qreal maxFlick); |
|
50 void stopScrollNoSignal(); |
|
51 |
|
52 public slots: |
|
53 void stopScroll(); |
|
54 void scrollTimerCallback(); |
|
55 void scrollAnimationStateChanged(QAbstractAnimation::State newState, |
|
56 QAbstractAnimation::State); |
|
57 signals: |
|
58 void scrollFinished(); |
|
59 |
|
60 private: |
|
61 void reset(); |
|
62 void doScroll(QPointF& delta); |
|
63 QPointF getScrollPos(); |
|
64 QPointF getMaxScrollPos(); |
|
65 void setScrollPos(QPointF& pos); |
|
66 bool clampScrollPosition(QPointF& scrollPos); |
|
67 void startScrollAnimation(QPointF& scrollPos, QPointF& targetPos, |
|
68 int duration, QEasingCurve::Type& easingCurveType); |
|
69 int calcScrollDuration(const QPointF& speed, const QPointF& delta); |
|
70 int calcScrollDuration(qreal motionFactor); |
|
71 QPointF calcTargetScrollPosAndDuration(const QPointF& speed, |
|
72 const QPointF& scrollPos, int& duration); |
|
73 qreal calcMotionFactor(const qreal speed); |
|
74 QPointF calcDeceleration(const QPointF& initSpeed, const QPointF& distance, long time); |
|
75 QPointF speedForNextInterval(const QPointF& initSpeed, long timeInterval, const QPointF& decel); |
|
76 private: |
|
77 ScrollMode m_scrollMode; |
|
78 ScrollState m_scrollState; |
|
79 QPointF m_scrollDelta; |
|
80 QPointF m_curScrollPos; |
|
81 QPropertyAnimation* m_scrollAnimation; |
|
82 UiTimer* m_scrollTimer; |
|
83 QObject* m_scrolledWidget; |
|
84 int m_minFlickDuration; |
|
85 int m_maxFlickDuration; |
|
86 int m_midFlickDuration; |
|
87 qreal m_minFlickSpeed; |
|
88 qreal m_maxFlickSpeed; |
|
89 qreal m_midFlickSpeed; |
|
90 qreal m_decel; |
|
91 QPointF m_decelVec; |
|
92 QSizeF m_viewportSize; |
|
93 qreal m_maxFlickInViewportUnits; |
|
94 qreal m_minFlickInViewportUnits; |
|
95 qreal m_midFlickInViewportUnits; |
|
96 bool m_lockToY; |
|
97 bool m_lockToX; |
|
98 QPointF m_targetScrollPos; |
|
99 QPointF m_startScrollPos; |
|
100 QPointF m_kineticSpeed; |
|
101 long m_scrollTotalDuration; |
|
102 long m_scrollDuration; |
|
103 QEasingCurve::Type m_easingCurveType; |
|
104 QEasingCurve* m_easingCurve; |
|
105 QEasingCurve* m_easingCurveOvershoot; |
|
106 QEasingCurve* m_curEasingCurve; |
|
107 }; |
|
108 } |
|
109 #endif /* SCROLLHELPER_H_ */ |