qtmaterialripple.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "qtmaterialripple.h"
  2. #include "lib/qtmaterialrippleoverlay.h"
  3. /*!
  4. * \class QtMaterialRipple
  5. * \internal
  6. */
  7. QtMaterialRipple::QtMaterialRipple(const QPoint &center, QObject *parent)
  8. : QParallelAnimationGroup(parent),
  9. m_overlay(0),
  10. m_radiusAnimation(animate("radius")),
  11. m_opacityAnimation(animate("opacity")),
  12. m_radius(0),
  13. m_opacity(0),
  14. m_center(center)
  15. {
  16. init();
  17. }
  18. QtMaterialRipple::QtMaterialRipple(const QPoint &center,
  19. QtMaterialRippleOverlay *overlay,
  20. QObject *parent)
  21. : QParallelAnimationGroup(parent),
  22. m_overlay(overlay),
  23. m_radiusAnimation(animate("radius")),
  24. m_opacityAnimation(animate("opacity")),
  25. m_radius(0),
  26. m_opacity(0),
  27. m_center(center)
  28. {
  29. init();
  30. }
  31. QtMaterialRipple::~QtMaterialRipple()
  32. {
  33. }
  34. void QtMaterialRipple::setRadius(qreal radius)
  35. {
  36. Q_ASSERT(m_overlay);
  37. if (m_radius == radius) {
  38. return;
  39. }
  40. m_radius = radius;
  41. m_overlay->update();
  42. }
  43. void QtMaterialRipple::setOpacity(qreal opacity)
  44. {
  45. Q_ASSERT(m_overlay);
  46. if (m_opacity == opacity) {
  47. return;
  48. }
  49. m_opacity = opacity;
  50. m_overlay->update();
  51. }
  52. void QtMaterialRipple::setColor(const QColor &color)
  53. {
  54. if (m_brush.color() == color) {
  55. return;
  56. }
  57. m_brush.setColor(color);
  58. if (m_overlay) {
  59. m_overlay->update();
  60. }
  61. }
  62. void QtMaterialRipple::setBrush(const QBrush &brush)
  63. {
  64. m_brush = brush;
  65. if (m_overlay) {
  66. m_overlay->update();
  67. }
  68. }
  69. void QtMaterialRipple::destroy()
  70. {
  71. Q_ASSERT(m_overlay);
  72. m_overlay->removeRipple(this);
  73. }
  74. /*!
  75. * \internal
  76. */
  77. QPropertyAnimation *QtMaterialRipple::animate(const QByteArray &property,
  78. const QEasingCurve &easing,
  79. int duration)
  80. {
  81. QPropertyAnimation *animation = new QPropertyAnimation;
  82. animation->setTargetObject(this);
  83. animation->setPropertyName(property);
  84. animation->setEasingCurve(easing);
  85. animation->setDuration(duration);
  86. addAnimation(animation);
  87. return animation;
  88. }
  89. /*!
  90. * \internal
  91. */
  92. void QtMaterialRipple::init()
  93. {
  94. setOpacityStartValue(0.5);
  95. setOpacityEndValue(0);
  96. setRadiusStartValue(0);
  97. setRadiusEndValue(300);
  98. m_brush.setColor(Qt::black);
  99. m_brush.setStyle(Qt::SolidPattern);
  100. connect(this, SIGNAL(finished()), this, SLOT(destroy()));
  101. }