SmoothScroll.h 1.1 KB

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