qt-material-widgets/source/lib/qtmaterialripple.cpp

119 lines
2.3 KiB
C++
Raw Normal View History

2023-01-31 10:08:03 +00:00
#include "lib/qtmaterialripple.h"
2017-09-29 06:53:21 +00:00
#include "lib/qtmaterialrippleoverlay.h"
/*!
* \class QtMaterialRipple
* \internal
*/
2023-01-31 10:08:03 +00:00
QtMaterialRipple::QtMaterialRipple(const QPoint& center, QObject* parent)
: QParallelAnimationGroup(parent)
, m_overlay(0)
, m_radiusAnimation(animate("radius"))
, m_opacityAnimation(animate("opacity"))
, m_radius(0)
, m_opacity(0)
, m_center(center)
2017-09-29 06:53:21 +00:00
{
2023-01-31 10:08:03 +00:00
init();
2017-09-29 06:53:21 +00:00
}
2023-01-31 10:08:03 +00:00
QtMaterialRipple::QtMaterialRipple(const QPoint& center,
QtMaterialRippleOverlay* overlay,
QObject* parent)
: QParallelAnimationGroup(parent)
, m_overlay(overlay)
, m_radiusAnimation(animate("radius"))
, m_opacityAnimation(animate("opacity"))
, m_radius(0)
, m_opacity(0)
, m_center(center)
2017-09-29 06:53:21 +00:00
{
2023-01-31 10:08:03 +00:00
init();
2017-09-29 06:53:21 +00:00
}
2023-01-31 10:08:03 +00:00
QtMaterialRipple::~QtMaterialRipple() {}
2017-09-29 06:53:21 +00:00
void QtMaterialRipple::setRadius(qreal radius)
{
2023-01-31 10:08:03 +00:00
Q_ASSERT(m_overlay);
2017-09-29 06:53:21 +00:00
2023-01-31 10:08:03 +00:00
if (m_radius == radius) {
return;
}
m_radius = radius;
m_overlay->update();
2017-09-29 06:53:21 +00:00
}
void QtMaterialRipple::setOpacity(qreal opacity)
{
2023-01-31 10:08:03 +00:00
Q_ASSERT(m_overlay);
2017-09-29 06:53:21 +00:00
2023-01-31 10:08:03 +00:00
if (m_opacity == opacity) {
return;
}
m_opacity = opacity;
m_overlay->update();
2017-09-29 06:53:21 +00:00
}
2023-01-31 10:08:03 +00:00
void QtMaterialRipple::setColor(const QColor& color)
2017-09-29 06:53:21 +00:00
{
2023-01-31 10:08:03 +00:00
if (m_brush.color() == color) {
return;
}
m_brush.setColor(color);
if (m_overlay) {
m_overlay->update();
}
2017-09-29 06:53:21 +00:00
}
2023-01-31 10:08:03 +00:00
void QtMaterialRipple::setBrush(const QBrush& brush)
2017-09-29 06:53:21 +00:00
{
2023-01-31 10:08:03 +00:00
m_brush = brush;
2017-09-29 06:53:21 +00:00
2023-01-31 10:08:03 +00:00
if (m_overlay) {
m_overlay->update();
}
2017-09-29 06:53:21 +00:00
}
void QtMaterialRipple::destroy()
{
2023-01-31 10:08:03 +00:00
Q_ASSERT(m_overlay);
2017-09-29 06:53:21 +00:00
2023-01-31 10:08:03 +00:00
m_overlay->removeRipple(this);
2017-09-29 06:53:21 +00:00
}
/*!
* \internal
*/
2023-01-31 10:08:03 +00:00
QPropertyAnimation* QtMaterialRipple::animate(const QByteArray& property,
const QEasingCurve& easing,
2017-09-29 06:53:21 +00:00
int duration)
{
2023-01-31 10:08:03 +00:00
QPropertyAnimation* animation = new QPropertyAnimation;
animation->setTargetObject(this);
animation->setPropertyName(property);
animation->setEasingCurve(easing);
animation->setDuration(duration);
addAnimation(animation);
return animation;
2017-09-29 06:53:21 +00:00
}
/*!
* \internal
*/
void QtMaterialRipple::init()
{
2023-01-31 10:08:03 +00:00
setOpacityStartValue(0.5);
setOpacityEndValue(0);
setRadiusStartValue(0);
setRadiusEndValue(300);
2017-09-29 06:53:21 +00:00
2023-01-31 10:08:03 +00:00
m_brush.setColor(Qt::black);
m_brush.setStyle(Qt::SolidPattern);
2017-09-29 06:53:21 +00:00
2023-01-31 10:08:03 +00:00
connect(this, SIGNAL(finished()), this, SLOT(destroy()));
2017-09-29 06:53:21 +00:00
}