ScrollArea.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "ScrollArea.h"
  2. #include <QWheelEvent>
  3. ScrollArea::ScrollArea(QWidget *parent, Qt::Orientation orient) : QScrollArea(parent)
  4. {
  5. m_smoothScroll = new SmoothScroll(this, orient);
  6. }
  7. void ScrollArea::setSmoothMode(const SmoothMode &mode)
  8. {
  9. m_smoothScroll->setSmoothMode(mode);
  10. }
  11. void ScrollArea::setViewportMargins(int left, int top, int right, int bottom)
  12. {
  13. QScrollArea::setViewportMargins(left, top, right, bottom);
  14. }
  15. void ScrollArea::wheelEvent(QWheelEvent *event)
  16. {
  17. // only process the wheel events triggered by mouse
  18. if ((m_smoothScroll->smoothMode() == SmoothMode::NO_SMOOTH) || (abs(event->angleDelta().y()) % 120 != 0)) {
  19. QScrollArea::wheelEvent(event);
  20. } else {
  21. m_smoothScroll->wheelEvent(event);
  22. }
  23. event->setAccepted(true);
  24. }
  25. SmoothScrollBar::SmoothScrollBar(QWidget *parent) : QScrollBar(parent), m_duration(500)
  26. {
  27. m_ani = new QPropertyAnimation(this);
  28. m_ani->setTargetObject(this);
  29. m_ani->setPropertyName("value");
  30. m_ani->setEasingCurve(QEasingCurve::OutCubic);
  31. m_ani->setDuration(m_duration);
  32. m_value = this->value();
  33. }
  34. void SmoothScrollBar::setValue(int value)
  35. {
  36. if (value == this->value()) {
  37. return;
  38. }
  39. // stop running animation
  40. m_ani->stop();
  41. // adjust the duration
  42. int dv = abs(value - this->value());
  43. if (dv < 50) {
  44. m_ani->setDuration(int(m_duration * dv / 70));
  45. } else {
  46. m_ani->setDuration(m_duration);
  47. };
  48. m_ani->setStartValue(this->value());
  49. m_ani->setEndValue(value);
  50. m_ani->start();
  51. }
  52. /// scroll the specified distance
  53. void SmoothScrollBar::scrollValue(int value)
  54. {
  55. m_value = value;
  56. m_value = qMax(this->minimum(), m_value);
  57. m_value = qMin(this->maximum(), m_value);
  58. this->setValue(m_value);
  59. }
  60. /// scroll to the specified position
  61. void SmoothScrollBar::scrollTo(int value)
  62. {
  63. m_value = value;
  64. m_value = qMax(this->minimum(), m_value);
  65. m_value = qMin(this->maximum(), m_value);
  66. this->setValue(m_value);
  67. }
  68. void SmoothScrollBar::resetValue(int value)
  69. {
  70. m_value = value;
  71. }
  72. /**
  73. * @brief set scroll animation
  74. * @param durationL: scroll duration
  75. * @param easing: animation type
  76. */
  77. void SmoothScrollBar::setScrollAnimation(int duration, QEasingCurve easing)
  78. {
  79. m_duration = duration;
  80. m_ani->setDuration(duration);
  81. m_ani->setEasingCurve(easing);
  82. }
  83. void SmoothScrollBar::mousePressEvent(QMouseEvent *event)
  84. {
  85. m_ani->stop();
  86. QScrollBar::mousePressEvent(event);
  87. m_value = this->value();
  88. }
  89. void SmoothScrollBar::mouseReleaseEvent(QMouseEvent *event)
  90. {
  91. m_ani->stop();
  92. QScrollBar::mouseReleaseEvent(event);
  93. m_value = this->value();
  94. }
  95. void SmoothScrollBar::mouseMoveEvent(QMouseEvent *event)
  96. {
  97. m_ani->stop();
  98. QScrollBar::mouseMoveEvent(event);
  99. m_value = this->value();
  100. }
  101. SmoothScrollArea::SmoothScrollArea(QWidget *parent) : QScrollArea(parent)
  102. {
  103. m_hScrollBar = new SmoothScrollBar(this);
  104. m_vScrollBar = new SmoothScrollBar(this);
  105. m_hScrollBar->setOrientation(Qt::Horizontal);
  106. m_vScrollBar->setOrientation(Qt::Vertical);
  107. setVerticalScrollBar(m_vScrollBar);
  108. setHorizontalScrollBar(m_hScrollBar);
  109. }
  110. /**
  111. * @brief set scroll animation
  112. * @param orientL: scroll orientation
  113. * @param duration: scroll duration
  114. * @param easing: animation type
  115. */
  116. void SmoothScrollArea::setScrollAnimation(Qt::Orientation orient, int duration, QEasingCurve easing)
  117. {
  118. SmoothScrollBar *bar = (orient == Qt::Horizontal ? m_hScrollBar : m_vScrollBar);
  119. bar->setScrollAnimation(duration, easing);
  120. }
  121. void SmoothScrollArea::setViewportMargins(int left, int top, int right, int bottom)
  122. {
  123. QScrollArea::setViewportMargins(left, top, right, bottom);
  124. }
  125. void SmoothScrollArea::wheelEvent(QWheelEvent *event)
  126. {
  127. if (event->modifiers() == Qt::NoModifier) {
  128. m_vScrollBar->scrollValue(-event->angleDelta().y());
  129. } else {
  130. m_hScrollBar->scrollValue(-event->angleDelta().x());
  131. }
  132. event->setAccepted(true);
  133. }