nheko/src/ui/RippleOverlay.cpp

67 lines
1.5 KiB
C++
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-04-06 01:06:42 +02:00
#include <QPainter>
#include "Ripple.h"
#include "RippleOverlay.h"
RippleOverlay::RippleOverlay(QWidget *parent)
2017-08-20 12:47:22 +02:00
: OverlayWidget(parent)
, use_clip_(false)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
setAttribute(Qt::WA_TransparentForMouseEvents);
setAttribute(Qt::WA_NoSystemBackground);
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RippleOverlay::addRipple(Ripple *ripple)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
ripple->setOverlay(this);
ripples_.push_back(ripple);
ripple->start();
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RippleOverlay::addRipple(const QPoint &position, qreal radius)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
Ripple *ripple = new Ripple(position);
ripple->setRadiusEndValue(radius);
addRipple(ripple);
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RippleOverlay::removeRipple(Ripple *ripple)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
if (ripples_.removeOne(ripple))
delete ripple;
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RippleOverlay::paintEvent(QPaintEvent *event)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
Q_UNUSED(event)
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
if (use_clip_)
painter.setClipPath(clip_path_);
2017-04-06 01:06:42 +02:00
for (auto it = ripples_.constBegin(); it != ripples_.constEnd(); ++it)
2017-09-10 11:59:21 +02:00
paintRipple(&painter, *it);
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
RippleOverlay::paintRipple(QPainter *painter, Ripple *ripple)
2017-04-06 01:06:42 +02:00
{
2017-09-10 11:59:21 +02:00
const qreal radius = ripple->radius();
const QPointF center = ripple->center();
2017-04-06 01:06:42 +02:00
2017-09-10 11:59:21 +02:00
painter->setOpacity(ripple->opacity());
painter->setBrush(ripple->brush());
painter->drawEllipse(center, radius, radius);
2017-04-06 01:06:42 +02:00
}