-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhoverbutton.h
More file actions
144 lines (120 loc) · 3.34 KB
/
hoverbutton.h
File metadata and controls
144 lines (120 loc) · 3.34 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef HOVERBUTTON_H
#define HOVERBUTTON_H
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <QPixmap>
#include <QBitmap>
#include <QPainter>
#include <QPushButton>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QMouseEvent>
class HoverButton : public QPushButton {
Q_OBJECT
Q_PROPERTY(float opacityLevel READ opacityLevel WRITE setOpacityLevel)
public:
int w;
int h;
QPropertyAnimation *animation;
QGraphicsOpacityEffect *opacity;
HoverButton() {
setAttribute(Qt::WA_Hover);
setMouseTracking(true);
setStyleSheet("HoverButton{background:transparent;border:none;}");
setup();
}
HoverButton(QString text) {
setAttribute(Qt::WA_Hover);
setText(text);
setMouseTracking(true);
setStyleSheet("HoverButton{background:transparent;border:none;}");
setup();
}
HoverButton(QIcon ico) {
setAttribute(Qt::WA_Hover);
setMouseTracking(true);
setIcon(ico);
setStyleSheet("HoverButton{background:transparent;border:none;}");
setup();
}
HoverButton(QString icon, int width, int height, QString color) {
setAttribute(Qt::WA_Hover);
setMouseTracking(true);
w = width;
h = height;
setIcon(QIcon(icon));
setIconSize(QSize(width,height));
setStyleSheet("HoverButton{background:transparent;border:none;}");
setColor(color);
setup();
}
HoverButton(QString icon, int width, int height) {
setAttribute(Qt::WA_Hover);
setMouseTracking(true);
w = width;
h = height;
setIcon(QIcon(icon));
setIconSize(QSize(width,height));
setStyleSheet("HoverButton{background:transparent;border:none;}");
setup();
}
public slots:
void changeIcon(QString icon) {
this->setIcon(QIcon(icon));
}
void setOpacityLevel (float level) {
this->opacity->setOpacity((qreal)level);
}
float opacityLevel() {
return 0;
}
void setup() {
opacity = new QGraphicsOpacityEffect();
opacity->setOpacity(0.1);
opacity->setOpacity(1);
setGraphicsEffect(opacity);
connect(this,SIGNAL(pressed()),this,SLOT(active()));
connect(this,SIGNAL(released()),this,SLOT(inactive()));
}
void active() {
animation = new QPropertyAnimation(this, "opacityLevel");
animation->setDuration(100);
animation->setStartValue(1);
animation->setEndValue(0.5);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
void inactive() {
animation = new QPropertyAnimation(this, "opacityLevel");
animation->setDuration(100);
animation->setStartValue(0.5);
animation->setEndValue(1);
animation->start(QAbstractAnimation::DeleteWhenStopped);
connect(animation,SIGNAL(finished()),this,SLOT(reset()));
}
void reset() {
this->opacity = new QGraphicsOpacityEffect();
this->opacity->setOpacity(1);
this->setGraphicsEffect(opacity);
}
void setColor(QString color) {
QPixmap m = icon().pixmap(icon().actualSize(QSize(128, 128)));
QPixmap newPix(m.size());
newPix.fill( QColor(color) );
newPix.setMask( m.createMaskFromColor( Qt::transparent ) );
setIcon(QIcon(newPix));
}
void enterEvent(QEvent*) {
setStyleSheet("HoverButton{background:#e7e7e8;border:none;}");
}
void leaveEvent(QEvent*) {
setStyleSheet("HoverButton{background:transparent;border:none;}");
}
signals:
void rightClicked();
protected:
void mouseReleaseEvent(QMouseEvent *e) {
if (e->button() == Qt::RightButton) emit rightClicked();
else if (e->button() == Qt::LeftButton) emit clicked();
}
};
#endif // HOVERBUTTON_H