summary refs log tree commit diff
path: root/src/ui/RippleOverlay.cpp
blob: 00915deb844392720c3b5d142739915726dd8066 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QPainter>

#include "Ripple.h"
#include "RippleOverlay.h"

RippleOverlay::RippleOverlay(QWidget *parent)
  : OverlayWidget(parent)
  , use_clip_(false)
{
        setAttribute(Qt::WA_TransparentForMouseEvents);
        setAttribute(Qt::WA_NoSystemBackground);
}

void
RippleOverlay::addRipple(Ripple *ripple)
{
        ripple->setOverlay(this);
        ripples_.push_back(ripple);
        ripple->start();
}

void
RippleOverlay::addRipple(const QPoint &position, qreal radius)
{
        Ripple *ripple = new Ripple(position);
        ripple->setRadiusEndValue(radius);
        addRipple(ripple);
}

void
RippleOverlay::removeRipple(Ripple *ripple)
{
        if (ripples_.removeOne(ripple))
                delete ripple;
}

void
RippleOverlay::paintEvent(QPaintEvent *event)
{
        Q_UNUSED(event)

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(Qt::NoPen);

        if (use_clip_)
                painter.setClipPath(clip_path_);

        for (auto it = ripples_.constBegin(); it != ripples_.constEnd(); ++it)
                paintRipple(&painter, *it);
}

void
RippleOverlay::paintRipple(QPainter *painter, Ripple *ripple)
{
        const qreal radius   = ripple->radius();
        const QPointF center = ripple->center();

        painter->setOpacity(ripple->opacity());
        painter->setBrush(ripple->brush());
        painter->drawEllipse(center, radius, radius);
}