1 // SPDX-FileCopyrightText: 2021 Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 
5 #include "Ripple.h"
6 #include "RippleOverlay.h"
7 
Ripple(const QPoint & center,QObject * parent)8 Ripple::Ripple(const QPoint &center, QObject *parent)
9   : QParallelAnimationGroup(parent)
10   , overlay_(nullptr)
11   , radius_anim_(animate("radius"))
12   , opacity_anim_(animate("opacity"))
13   , radius_(0)
14   , opacity_(0)
15   , center_(center)
16 {
17     init();
18 }
19 
Ripple(const QPoint & center,RippleOverlay * overlay,QObject * parent)20 Ripple::Ripple(const QPoint &center, RippleOverlay *overlay, QObject *parent)
21   : QParallelAnimationGroup(parent)
22   , overlay_(overlay)
23   , radius_anim_(animate("radius"))
24   , opacity_anim_(animate("opacity"))
25   , radius_(0)
26   , opacity_(0)
27   , center_(center)
28 {
29     init();
30 }
31 
32 void
setRadius(qreal radius)33 Ripple::setRadius(qreal radius)
34 {
35     Q_ASSERT(overlay_);
36 
37     if (radius_ == radius)
38         return;
39 
40     radius_ = radius;
41     overlay_->update();
42 }
43 
44 void
setOpacity(qreal opacity)45 Ripple::setOpacity(qreal opacity)
46 {
47     Q_ASSERT(overlay_);
48 
49     if (opacity_ == opacity)
50         return;
51 
52     opacity_ = opacity;
53     overlay_->update();
54 }
55 
56 void
setColor(const QColor & color)57 Ripple::setColor(const QColor &color)
58 {
59     if (brush_.color() == color)
60         return;
61 
62     brush_.setColor(color);
63 
64     if (overlay_)
65         overlay_->update();
66 }
67 
68 void
setBrush(const QBrush & brush)69 Ripple::setBrush(const QBrush &brush)
70 {
71     brush_ = brush;
72 
73     if (overlay_)
74         overlay_->update();
75 }
76 
77 void
destroy()78 Ripple::destroy()
79 {
80     Q_ASSERT(overlay_);
81 
82     overlay_->removeRipple(this);
83 }
84 
85 QPropertyAnimation *
animate(const QByteArray & property,const QEasingCurve & easing,int duration)86 Ripple::animate(const QByteArray &property, const QEasingCurve &easing, int duration)
87 {
88     QPropertyAnimation *animation = new QPropertyAnimation;
89     animation->setTargetObject(this);
90     animation->setPropertyName(property);
91     animation->setEasingCurve(easing);
92     animation->setDuration(duration);
93 
94     addAnimation(animation);
95 
96     return animation;
97 }
98 
99 void
init()100 Ripple::init()
101 {
102     setOpacityStartValue(0.5);
103     setOpacityEndValue(0);
104     setRadiusStartValue(0);
105     setRadiusEndValue(300);
106 
107     brush_.setColor(Qt::black);
108     brush_.setStyle(Qt::SolidPattern);
109 
110     connect(this, SIGNAL(finished()), this, SLOT(destroy()));
111 }
112