-
Notifications
You must be signed in to change notification settings - Fork 2
/
pushbutton.cc
91 lines (74 loc) · 1.97 KB
/
pushbutton.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
#include "pushbutton.hpp"
#include <QEvent>
namespace GUI {
class PushButton::PushButtonPrivate
{
public:
explicit PushButtonPrivate(PushButton *q)
: q_ptr(q)
{}
PushButton *q_ptr;
QIcon icon;
QIcon hoverIcon;
QIcon activeIcon;
};
PushButton::PushButton(QWidget *parent)
: QPushButton(parent)
, d_ptr(new PushButtonPrivate(this))
{
buildConnect();
installEventFilter(this);
}
PushButton::~PushButton() {}
void PushButton::setNormalIcon(const QIcon &icon)
{
d_ptr->icon = icon;
setIcon(icon);
}
void PushButton::setHoverIcon(const QIcon &icon)
{
d_ptr->hoverIcon = icon;
}
void PushButton::setActiveIcon(const QIcon &icon)
{
d_ptr->activeIcon = icon;
}
void PushButton::onToggled(bool checked)
{
setIcon(checked ? d_ptr->activeIcon : d_ptr->icon);
}
bool PushButton::eventFilter(QObject *watched, QEvent *event)
{
if (watched == this) {
switch (event->type()) {
case QEvent::Enter: setIcon(d_ptr->hoverIcon); break;
case QEvent::MouseButtonPress: setIcon(d_ptr->activeIcon); break;
case QEvent::Leave: setIcon(d_ptr->icon); break;
default: break;
}
}
return QPushButton::eventFilter(watched, event);
}
void PushButton::buildConnect()
{
connect(this, &PushButton::toggled, this, &PushButton::onToggled);
}
auto createPushButton(const QStringList &normalIconPaths,
const QStringList &hoverIconPaths,
const QStringList &activeIconPaths,
QWidget *parent) -> PushButton *
{
auto fromPaths = [&](const QStringList &Paths) -> QIcon {
QIcon icon;
for (const auto &path : std::as_const(Paths)) {
icon.addFile(path);
}
return icon;
};
auto *btn = new PushButton(parent);
btn->setNormalIcon(fromPaths(normalIconPaths));
btn->setHoverIcon(fromPaths(hoverIconPaths));
btn->setActiveIcon(fromPaths(activeIconPaths));
return btn;
}
} // namespace GUI