SmoothScroll.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef SMOOTHSCROLL_H
  2. #define SMOOTHSCROLL_H
  3. #include <QQueue>
  4. #include <QScrollArea>
  5. enum SmoothMode
  6. {
  7. NO_SMOOTH = 0,
  8. CONSTANT = 1,
  9. LINEAR = 2,
  10. QUADRATI = 3,
  11. COSINE = 4
  12. };
  13. class SmoothScroll : public QObject
  14. {
  15. Q_OBJECT
  16. public:
  17. struct StepsLeftElement
  18. {
  19. double delta;
  20. double stepLeft;
  21. };
  22. explicit SmoothScroll(QAbstractScrollArea *widget, Qt::Orientation orient = Qt::Vertical);
  23. void setSmoothMode(const SmoothMode &smoothMode);
  24. SmoothMode smoothMode() const;
  25. void wheelEvent(QWheelEvent *event);
  26. private slots:
  27. void smoothMove();
  28. private:
  29. double subDelta(double delta, double stepsLeft);
  30. private:
  31. QAbstractScrollArea *m_widget;
  32. Qt::Orientation m_orient;
  33. int m_fps;
  34. int m_duration;
  35. double m_stepsTotal;
  36. double m_stepRatio;
  37. double m_acceleration;
  38. QScopedPointer<QWheelEvent> m_lastWheelEvent;
  39. QQueue<qint64> m_scrollStamps;
  40. QQueue<StepsLeftElement> m_stepsLeftQueue;
  41. QScopedPointer<QTimer> m_smoothMoveTimer;
  42. SmoothMode m_smoothMode;
  43. };
  44. #endif // SMOOTHSCROLL_H