-
Notifications
You must be signed in to change notification settings - Fork 286
/
py_toggle.py
88 lines (75 loc) · 2.76 KB
/
py_toggle.py
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
# ///////////////////////////////////////////////////////////////
#
# BY: WANDERSON M.PIMENTA
# PROJECT MADE WITH: Qt Designer and PySide6
# V: 1.0.0
#
# This project can be used freely for all uses, as long as they maintain the
# respective credits only in the Python scripts, any information in the visual
# interface (GUI) can be modified without any implication.
#
# There are limitations on Qt licenses if you want to use your products
# commercially, I recommend reading them on the official website:
# https://doc.qt.io/qtforpython/licenses.html
#
# ///////////////////////////////////////////////////////////////
# IMPORT QT CORE
# ///////////////////////////////////////////////////////////////
from qt_core import *
class PyToggle(QCheckBox):
def __init__(
self,
width = 50,
bg_color = "#777",
circle_color = "#DDD",
active_color = "#00BCFF",
animation_curve = QEasingCurve.OutBounce
):
QCheckBox.__init__(self)
self.setFixedSize(width, 28)
self.setCursor(Qt.PointingHandCursor)
# COLORS
self._bg_color = bg_color
self._circle_color = circle_color
self._active_color = active_color
self._position = 3
self.animation = QPropertyAnimation(self, b"position")
self.animation.setEasingCurve(animation_curve)
self.animation.setDuration(500)
self.stateChanged.connect(self.setup_animation)
@Property(float)
def position(self):
return self._position
@position.setter
def position(self, pos):
self._position = pos
self.update()
# START STOP ANIMATION
def setup_animation(self, value):
self.animation.stop()
if value:
self.animation.setEndValue(self.width() - 26)
else:
self.animation.setEndValue(4)
self.animation.start()
def hitButton(self, pos: QPoint):
return self.contentsRect().contains(pos)
def paintEvent(self, e):
p = QPainter(self)
p.setRenderHint(QPainter.Antialiasing)
p.setFont(QFont("Segoe UI", 9))
# SET PEN
p.setPen(Qt.NoPen)
# DRAW RECT
rect = QRect(0, 0, self.width(), self.height())
if not self.isChecked():
p.setBrush(QColor(self._bg_color))
p.drawRoundedRect(0,0,rect.width(), 28, 14, 14)
p.setBrush(QColor(self._circle_color))
p.drawEllipse(self._position, 3, 22, 22)
else:
p.setBrush(QColor(self._active_color))
p.drawRoundedRect(0,0,rect.width(), 28, 14, 14)
p.setBrush(QColor(self._circle_color))
p.drawEllipse(self._position, 3, 22, 22)
p.end()