qtmaterialrippleoverlay.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "lib/qtmaterialrippleoverlay.h"
  2. #include <QPainter>
  3. #include "lib/qtmaterialripple.h"
  4. /*!
  5. * \class QtMaterialRippleOverlay
  6. * \internal
  7. */
  8. QtMaterialRippleOverlay::QtMaterialRippleOverlay(QWidget *parent)
  9. : QtMaterialOverlayWidget(parent),
  10. m_useClip(false)
  11. {
  12. setAttribute(Qt::WA_TransparentForMouseEvents);
  13. setAttribute(Qt::WA_NoSystemBackground);
  14. }
  15. QtMaterialRippleOverlay::~QtMaterialRippleOverlay()
  16. {
  17. }
  18. void QtMaterialRippleOverlay::addRipple(QtMaterialRipple *ripple)
  19. {
  20. ripple->setOverlay(this);
  21. m_ripples.push_back(ripple);
  22. ripple->start();
  23. connect(this, SIGNAL(destroyed(QObject*)), ripple, SLOT(stop()));
  24. connect(this, SIGNAL(destroyed(QObject*)), ripple, SLOT(deleteLater()));
  25. }
  26. void QtMaterialRippleOverlay::addRipple(const QPoint &position, qreal radius)
  27. {
  28. QtMaterialRipple *ripple = new QtMaterialRipple(position);
  29. ripple->setRadiusEndValue(radius);
  30. addRipple(ripple);
  31. }
  32. void QtMaterialRippleOverlay::removeRipple(QtMaterialRipple *ripple)
  33. {
  34. if (m_ripples.removeOne(ripple)) {
  35. delete ripple;
  36. update();
  37. }
  38. }
  39. /*!
  40. * \reimp
  41. */
  42. void QtMaterialRippleOverlay::paintEvent(QPaintEvent *event)
  43. {
  44. Q_UNUSED(event)
  45. QPainter painter(this);
  46. painter.setRenderHint(QPainter::Antialiasing);
  47. painter.setPen(Qt::NoPen);
  48. if (m_useClip) {
  49. painter.setClipPath(m_clipPath);
  50. }
  51. QList<QtMaterialRipple *>::const_iterator i;
  52. for (i = m_ripples.begin(); i != m_ripples.end(); ++i) {
  53. paintRipple(&painter, *i);
  54. }
  55. }
  56. void QtMaterialRippleOverlay::paintRipple(QPainter *painter, QtMaterialRipple *ripple)
  57. {
  58. const qreal radius = ripple->radius();
  59. const QPointF center = ripple->center();
  60. painter->setOpacity(ripple->opacity());
  61. painter->setBrush(ripple->brush());
  62. painter->drawEllipse(center, radius, radius);
  63. }