-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSwitchButton.h
More file actions
76 lines (65 loc) · 1.78 KB
/
Copy pathSwitchButton.h
File metadata and controls
76 lines (65 loc) · 1.78 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
#pragma once
#ifndef SWITCHBUTTON_H
#define SWITCHBUTTON_H
#include "QAbstractButton"
#include "QPropertyAnimation"
/**
* This class inherits QAbstractButton.
* It is an utility class to create android-like animated switches
* inspiration:
* https://stackoverflow.com/questions/14780517/toggle-switch-in-qt
*/
class SwitchButton : public QAbstractButton
{
Q_OBJECT
Q_PROPERTY(int offset READ offset WRITE setOffset)
Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
Q_PROPERTY(bool _switch READ state WRITE setState)
public:
SwitchButton(QString s0, QString s1, QWidget* parent = 0);
//~SwitchButton();
QSize sizeHint() const override;
void setHeight(int h);
int offset() const
{
return _x;
}
void setOffset(int o)
{
_x = o;
update();
}
QBrush brush() const
{
return _brush;
}
void setBrush(const QBrush& brsh)
{
_brush = brsh;
}
bool state() const
{
return _switch;
}
void setState(bool toggle)
{
_switch = toggle;
animateSwitch();
update();
}
void animateSwitch();
signals:
void emitSetEnabled(bool toggle);
protected:
void paintEvent(QPaintEvent* event) override;
void mouseReleaseEvent(QMouseEvent*) override;
void enterEvent(QEvent* e) override;
bool _switch; /**< state fo the switch */
qreal _opacity; /**< opacity */
int _x, _y, _height, _margin; /**< position and dimensions */
QBrush _thumb, _track, _brush;
QPropertyAnimation* _anim = nullptr; /**< animation */
QString _s0; /**< text in state 0 */
QString _s1; /**< text in state 1 */
};
#endif // SWITCHBUTTON_H