-
Notifications
You must be signed in to change notification settings - Fork 11
/
iconfliter.cc
101 lines (84 loc) · 2.26 KB
/
iconfliter.cc
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
101
#include "iconfliter.hpp"
#include <QtWidgets>
auto iconFromPaths(const QStringList &paths) -> QIcon
{
QIcon icon;
for (const auto &path : std::as_const(paths)) {
icon.addFile(path);
}
return icon;
}
class IconFliter::IconFliterPrivate
{
public:
explicit IconFliterPrivate(IconFliter *q)
: q_ptr(q)
{}
IconFliter *q_ptr;
QPointer<QAbstractButton> buttonPtr;
QIcon normalIcon;
QIcon hoverIcon;
QIcon pressedIcon;
};
IconFliter::IconFliter(QAbstractButton *parent)
: QObject{parent}
, d_ptr(new IconFliterPrivate(this))
{
d_ptr->buttonPtr = parent;
if (!d_ptr->buttonPtr.isNull()) {
d_ptr->buttonPtr->installEventFilter(this);
}
buildConnect();
}
IconFliter::~IconFliter() = default;
void IconFliter::setNormalIcon(const QIcon &icon)
{
d_ptr->normalIcon = icon;
if (!d_ptr->buttonPtr.isNull()) {
d_ptr->buttonPtr->setIcon(d_ptr->normalIcon);
}
}
void IconFliter::setNormalIcon(const QStringList &icons)
{
setNormalIcon(iconFromPaths(icons));
}
void IconFliter::setHoverIcon(const QIcon &icon)
{
d_ptr->hoverIcon = icon;
}
void IconFliter::setHoverIcon(const QStringList &icons)
{
setHoverIcon(iconFromPaths(icons));
}
void IconFliter::setPressedIcon(const QIcon &icon)
{
d_ptr->pressedIcon = icon;
}
void IconFliter::setPressedIcon(const QStringList &icons)
{
setPressedIcon(iconFromPaths(icons));
}
void IconFliter::onToggle(bool checked)
{
if (!d_ptr->buttonPtr.isNull()) {
d_ptr->buttonPtr->setIcon(checked ? d_ptr->pressedIcon : d_ptr->normalIcon);
}
}
auto IconFliter::eventFilter(QObject *watched, QEvent *event) -> bool
{
if (!d_ptr->buttonPtr.isNull() && watched == d_ptr->buttonPtr.data()) {
switch (event->type()) {
case QEvent::Enter: d_ptr->buttonPtr->setIcon(d_ptr->hoverIcon); break;
case QEvent::Leave: onToggle(d_ptr->buttonPtr->isChecked()); break;
case QEvent::MouseButtonPress: d_ptr->buttonPtr->setIcon(d_ptr->pressedIcon); break;
default: break;
}
}
return QObject::eventFilter(watched, event);
}
void IconFliter::buildConnect()
{
if (!d_ptr->buttonPtr.isNull()) {
connect(d_ptr->buttonPtr.data(), &QAbstractButton::toggled, this, &IconFliter::onToggle);
}
}