forked from czajowaty/ad_resources_dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QLabelWithMouseEvents.hpp
61 lines (54 loc) · 1.38 KB
/
QLabelWithMouseEvents.hpp
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
#ifndef QLABELWITHMOUSEEVENTS_HPP
#define QLABELWITHMOUSEEVENTS_HPP
#include <QLabel>
class QLabelWithMouseEvents : public QLabel
{
public:
QLabelWithMouseEvents(QWidget* parent) : QLabel(parent)
{
standardColor_ = palette().color(QPalette::Background).rgb();
setAutoFillBackground(true);
}
bool isPressed() const
{ return pressed_; }
void setNotPressed()
{
pressed_ = false;
setBackgroundColor(standardColor_);
}
protected:
void enterEvent(QEvent* event) override
{
QLabel::enterEvent(event);
if (isPressed())
{ return; }
setBackgroundColor(hoverColor_);
}
void leaveEvent(QEvent* event) override
{
QLabel::leaveEvent(event);
if (isPressed())
{ return; }
setBackgroundColor(standardColor_);
}
void mousePressEvent(QMouseEvent* event) override
{
QLabel::mousePressEvent(event);
if (isPressed())
{ return; }
pressed_ = true;
setBackgroundColor(pressedColor_);
}
private:
void setBackgroundColor(QRgb color)
{
auto p = palette();
p.setColor(QPalette::Background, color);
setPalette(p);
}
QRgb standardColor_;
QRgb hoverColor_{qRgb(0xff, 0xff, 0x00)};
QRgb pressedColor_{qRgb(0x7f, 0xff, 0x00)};
bool pressed_{false};
};
#endif // QLABELWITHMOUSEEVENTS_HPP