-
Notifications
You must be signed in to change notification settings - Fork 0
/
opbutton.h
126 lines (106 loc) · 3.01 KB
/
opbutton.h
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
#ifndef OPBUTTON_H
#define OPBUTTON_H
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <QPixmap>
#include <QBitmap>
#include <QPainter>
#include <QPushButton>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
class OpButton : public QPushButton
{
Q_OBJECT
Q_PROPERTY(float opacityLevel READ opacityLevel WRITE setOpacityLevel)
public:
//VARIABLES
int w = 0;
int h = 0;
QPropertyAnimation *animation;
QGraphicsOpacityEffect *opacity;
OpButton(QImage im, int width, int height)
{
w = width;
h = height;
setIcon(QIcon(QPixmap::fromImage(im)));
setIconSize(QSize(width,height));
setStyleSheet("OpButton{background:transparent;border:none;}");
setup();
}
OpButton(QString icon, int width, int height, QString color)
{
w = width;
h = height;
setIcon(QIcon(icon));
setIconSize(QSize(width,height));
setStyleSheet("OpButton{background:transparent;border:none;}");
setColor(color);
setup();
}
OpButton(QString icon, int width, int height)
{
w = width;
h = height;
setIcon(QIcon(icon));
setIconSize(QSize(width,height));
setStyleSheet("OpButton{background:transparent;border:none;}");
setup();
}
OpButton(QString txt, QString style)
{
setText(txt);
setStyleSheet("OpButton{background:transparent;border:none;"+style+"}");
setup();
}
public slots:
//METHODS
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(QSize(w*2,h*2));
QPixmap newPix(m.size());
newPix.fill( QColor(color) );
newPix.setMask( m.createMaskFromColor( Qt::transparent ) );
setIcon(QIcon(newPix));
}
};
#endif // OPBUTTON_H