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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QPainter>
#include <QPainterPath>
#include "FloatingButton.h"
FloatingButton::FloatingButton(const QIcon &icon, QWidget *parent)
: RaisedButton(parent)
{
setFixedSize(DIAMETER, DIAMETER);
setGeometry(buttonGeometry());
if (parentWidget())
parentWidget()->installEventFilter(this);
setFixedRippleRadius(50);
setIcon(icon);
raise();
}
QRect
FloatingButton::buttonGeometry() const
{
QWidget *parent = parentWidget();
if (!parent)
return QRect();
return QRect(parent->width() - (OFFSET_X + DIAMETER),
parent->height() - (OFFSET_Y + DIAMETER),
DIAMETER,
DIAMETER);
}
bool
FloatingButton::event(QEvent *event)
{
if (!parent())
return RaisedButton::event(event);
switch (event->type()) {
case QEvent::ParentChange: {
parent()->installEventFilter(this);
setGeometry(buttonGeometry());
break;
}
case QEvent::ParentAboutToChange: {
parent()->installEventFilter(this);
break;
}
default:
break;
}
return RaisedButton::event(event);
}
bool
FloatingButton::eventFilter(QObject *obj, QEvent *event)
{
const QEvent::Type type = event->type();
if (QEvent::Move == type || QEvent::Resize == type)
setGeometry(buttonGeometry());
return RaisedButton::eventFilter(obj, event);
}
void
FloatingButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QRect square = QRect(0, 0, DIAMETER, DIAMETER);
square.moveCenter(rect().center());
QPainter p(this);
p.setRenderHints(QPainter::Antialiasing);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(backgroundColor());
p.setBrush(brush);
p.setPen(Qt::NoPen);
p.drawEllipse(square);
QRect iconGeometry(0, 0, ICON_SIZE, ICON_SIZE);
iconGeometry.moveCenter(square.center());
QPixmap pixmap = icon().pixmap(QSize(ICON_SIZE, ICON_SIZE));
QPainter icon(&pixmap);
icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
icon.fillRect(pixmap.rect(), foregroundColor());
p.drawPixmap(iconGeometry, pixmap);
}
|