SmoothScroll.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "SmoothScroll.h"
  2. #include <QApplication>
  3. #include <QDateTime>
  4. #include <QScrollBar>
  5. #include <QTimer>
  6. #include <QWheelEvent>
  7. #include <QtMath>
  8. SmoothScroll::SmoothScroll(QAbstractScrollArea *widget, Qt::Orientation orient)
  9. : QObject(widget),
  10. m_widget(widget),
  11. m_orient(orient),
  12. m_fps(60),
  13. m_duration(400),
  14. m_stepsTotal(0),
  15. m_stepRatio(1.5),
  16. m_acceleration(1),
  17. m_lastWheelEvent(nullptr),
  18. m_smoothMoveTimer(new QTimer(this)),
  19. m_smoothMode(SmoothMode::LINEAR)
  20. {
  21. connect(m_smoothMoveTimer.get(), &QTimer::timeout, this, &SmoothScroll::smoothMove);
  22. }
  23. void SmoothScroll::wheelEvent(QWheelEvent *event)
  24. {
  25. // only process the wheel events triggered by mouse
  26. // if ((m_smoothMode == SmoothMode::NO_SMOOTH) || (abs(event->angleDelta().y()) % 120 != 0)) {
  27. // m_widget->wheelEvent(event);
  28. // return;
  29. // }
  30. // push current time to queque
  31. qint64 now = QDateTime::currentDateTime().toMSecsSinceEpoch();
  32. m_scrollStamps.append(now);
  33. while (now - m_scrollStamps.at(0) > 500) {
  34. m_scrollStamps.pop_front();
  35. }
  36. // adjust the acceration ratio based on unprocessed events
  37. double accerationRatio = qMin(m_scrollStamps.count() / 15.0, 1.0);
  38. m_lastWheelEvent.reset(new QWheelEvent(event->pos(), event->globalPos(), event->pixelDelta(), event->angleDelta(),
  39. event->delta(), event->orientation(), event->buttons(), event->modifiers()));
  40. // get the number of steps
  41. m_stepsTotal = m_fps * m_duration / 1000.0;
  42. // get the moving distance corresponding to each event
  43. double delta = event->angleDelta().y() * m_stepRatio;
  44. if (!qFuzzyCompare(m_acceleration, 0)) {
  45. delta += delta * m_acceleration * accerationRatio;
  46. }
  47. // form a list of moving distances and steps, and insert it into the queue for processing.
  48. m_stepsLeftQueue.append(StepsLeftElement{ delta, m_stepsTotal });
  49. // overflow time of timer: 1000ms/frames
  50. m_smoothMoveTimer->start(int(1000 / m_fps));
  51. }
  52. void SmoothScroll::setSmoothMode(const SmoothMode &smoothMode)
  53. {
  54. m_smoothMode = smoothMode;
  55. }
  56. SmoothMode SmoothScroll::smoothMode() const
  57. {
  58. return m_smoothMode;
  59. }
  60. /// scroll smoothly when timer time out
  61. void SmoothScroll::smoothMove()
  62. {
  63. if (!m_lastWheelEvent) {
  64. return;
  65. }
  66. double totalDelta = 0;
  67. // Calculate the scrolling distance of all unprocessed events,
  68. // the timer will reduce the number of steps by 1 each time it overflows.
  69. for (auto &i : m_stepsLeftQueue) {
  70. totalDelta += subDelta(i.delta, i.stepLeft);
  71. i.stepLeft -= 1;
  72. }
  73. // If the event has been processed, move it out of the queue
  74. while (!m_stepsLeftQueue.isEmpty() && qFuzzyIsNull(m_stepsLeftQueue.at(0).stepLeft)) {
  75. m_stepsLeftQueue.pop_front();
  76. }
  77. QScrollBar *bar;
  78. QPoint p;
  79. // construct wheel event
  80. if (m_orient == Qt::Vertical) {
  81. p = QPoint(0, qRound(totalDelta));
  82. bar = m_widget->verticalScrollBar();
  83. } else {
  84. p = QPoint(qRound(totalDelta), 0);
  85. bar = m_widget->horizontalScrollBar();
  86. }
  87. QWheelEvent e(m_lastWheelEvent->pos(), m_lastWheelEvent->globalPos(), QPoint(), p, qRound(totalDelta), m_orient,
  88. m_lastWheelEvent->buttons(), Qt::NoModifier);
  89. // send wheel event to app
  90. QApplication::sendEvent(bar, &e);
  91. // stop scrolling if the queque is empty
  92. if (m_stepsLeftQueue.isEmpty()) {
  93. m_smoothMoveTimer->stop();
  94. }
  95. }
  96. /// get the interpolation for each step
  97. double SmoothScroll::subDelta(double delta, double stepsLeft)
  98. {
  99. double m = m_stepsTotal / 2;
  100. double x = fabs(m_stepsTotal - stepsLeft - m);
  101. double res = 0;
  102. if (m_smoothMode == SmoothMode::NO_SMOOTH) {
  103. res = 0;
  104. } else if (m_smoothMode == SmoothMode::CONSTANT) {
  105. res = delta / m_stepsTotal;
  106. } else if (m_smoothMode == SmoothMode::LINEAR) {
  107. res = 2 * delta / m_stepsTotal * (m - x) / m;
  108. } else if (m_smoothMode == SmoothMode::QUADRATI) {
  109. res = (1 - x * x / m / m) * delta * 3 / 4 / m;
  110. } else if (m_smoothMode == SmoothMode::COSINE) {
  111. res = (cos(x * M_PI / m) + 1) / (2 * m) * delta;
  112. }
  113. return res;
  114. }