MCPcopy Create free account
hub / github.com/DIMFLIX/Spectrum-Security / ToggleSwitch

Class ToggleSwitch

ui/widgets/toggleswitch.py:65–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63
64
65class ToggleSwitch(QWidget):
66
67 toggled = Signal()
68
69 def __init__(self, text="", onColor=QColor(189, 147, 249), offColor=QColor(136, 136, 136), handleColor=QColor(255, 255, 255),on=False, width=25, radius=23):
70 super().__init__()
71
72 self.text = text
73
74 self.on = on
75
76 self.opacity = QGraphicsOpacityEffect(self)
77 self.opacity.setOpacity(1)
78 self.setGraphicsEffect(self.opacity)
79
80 self.onColor = onColor
81 self.offColor = offColor
82 self.handleAlpha = False
83 self.handleColor = handleColor
84 self.width = width
85 self.radius = radius
86
87
88 self.setMinimumSize(self.width + (self.radius*2) + (len(self.text)*10), self.radius+2)
89
90 self.anim = AnimationHandler(self, 0, self.width, lambda x: 1 - ((1 - x)**3))
91 if self.on: self.anim.value = 1
92
93 def isToggled(self):
94 return not self.on
95
96 def desaturate(self, color):
97 cc = getattr(self, color)
98 h = cc.hue()
99 if h < 0: h = 0
100 s = cc.saturation()//4
101 if s > 255: s = 255
102 c = QColor.fromHsv(h, s, cc.value())
103 setattr(self, color, c)
104
105 def saturate(self, color):
106 cc = getattr(self, color)
107 h = cc.hue()
108 if h < 0: h = 0
109 s = cc.saturation()*4
110 if s > 255: s = 255
111 c = QColor.fromHsv(h, s, cc.value())
112 setattr(self, color, c)
113
114 def update(self, *args, **kwargs):
115 self.anim.update()
116 super().update(*args, **kwargs)
117
118 def mousePressEvent(self, event):
119 if self.isEnabled():
120 if self.on:
121 self.on = False
122 self.anim.start(reverse=True)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected