This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
toggle.py
352 lines (265 loc) · 11.4 KB
/
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from .core.ripple_overlay import *
class QMaterialToggleThumb(QWidget):
def __init__(self, parent: "QMaterialToggle"):
QWidget.__init__(self, parent)
self.m_toggle = parent
self.m_thumbColor = QColor()
self.m_shift = 0
self.m_offset = 0
effect = QGraphicsDropShadowEffect()
effect.setBlurRadius(6)
effect.setColor(QColor(0, 0, 0, 80))
effect.setOffset(QPointF(0, 1))
self.setGraphicsEffect(effect)
parent.installEventFilter(self)
def setShift(self, shift: float) -> None:
if self.m_shift == shift:
return
self.m_shift = shift
self.updateOffset()
def shift(self) -> float:
return self.m_shift
def offset(self) -> float:
return self.m_offset
def setThumbColor(self, color: QColor) -> None:
self.m_thumbColor = color
self.update()
def thumbColor(self) -> QColor:
return self.m_thumbColor
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
type: QEvent.Type = event.type()
if QEvent.Resize == type or QEvent.Move == type:
self.setGeometry(self.m_toggle.rect().adjusted(8, 8, -8, -8))
self.updateOffset()
return QWidget.eventFilter(self, obj, event)
def paintEvent(self, event: QPaintEvent) -> None:
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
brush = QBrush()
brush.setStyle(Qt.SolidPattern)
brush.setColor(self.m_thumbColor if self.m_toggle.isEnabled() else Qt.white)
painter.setBrush(brush)
painter.setPen(Qt.NoPen)
if Qt.Horizontal == self.m_toggle.orientation():
s: int = self.height() - 10
r: QRectF = QRectF(5 + self.m_offset, 5, s, s)
else:
s: int = self.width() - 10
r: QRectF = QRectF(5, 5 + self.m_offset, s, s)
painter.drawEllipse(r)
if not self.m_toggle.isEnabled():
brush.setColor(self.m_toggle.disabledColor())
painter.setBrush(brush)
painter.drawEllipse(r)
def updateOffset(self) -> None:
s = QSize(
self.size()
if Qt.Horizontal == self.m_toggle.orientation()
else self.size().transposed()
)
self.m_offset = self.m_shift * s.width() - s.height()
self.update()
_shift = Property(float, fset=setShift, fget=shift)
_thumbColor = Property(QColor, fset=setThumbColor, fget=thumbColor)
class QMaterialToggleTrack(QWidget):
def __init__(self, parent: "QMaterialToggle"):
QWidget.__init__(self, parent)
self.m_toggle = parent
self.m_trackColor = QColor()
parent.installEventFilter(self)
def setTrackColor(self, color: QColor) -> None:
self.m_trackColor = color
self.update()
def trackColor(self) -> QColor:
return self.m_trackColor
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
type: QEvent.Type = event.type()
if QEvent.Resize == type or QEvent.Move == type:
self.setGeometry(self.m_toggle.rect())
return QWidget.eventFilter(self, obj, event)
def paintEvent(self, event: QPaintEvent) -> None:
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
brush = QBrush()
if self.m_toggle.isEnabled():
brush.setColor(self.m_trackColor)
painter.setOpacity(0.8)
else:
brush.setColor(self.m_toggle.disabledColor())
painter.setOpacity(0.6)
brush.setStyle(Qt.SolidPattern)
painter.setBrush(brush)
painter.setPen(Qt.NoPen)
if Qt.Horizontal == self.m_toggle.orientation():
h: int = self.height() / 2
r = QRect(0, h / 2, self.width(), h)
painter.drawRoundedRect(r.adjusted(14, 4, -14, -4), h / 2 - 4, h / 2 - 4)
else:
w: int = self.width() / 2
r = QRect(w / 2, 0, w, self.height())
painter.drawRoundedRect(r.adjusted(4, 14, -4, -14), w / 2 - 4, w / 2 - 4)
_trackColor = Property(QColor, fset=setTrackColor, fget=trackColor)
class QMaterialToggleRippleOverlay(QMaterialRippleOverlay):
def __init__(
self,
thumb: QMaterialToggleThumb,
track: QMaterialToggleTrack,
parent: "QMaterialToggle",
):
QMaterialRippleOverlay.__init__(self, parent)
self.m_toggle = parent
self.m_thumb = thumb
self.m_track = track
parent.toggled.connect(self.addToggleRipple)
thumb.installEventFilter(self)
def addToggleRipple(self) -> None:
if not self.m_toggle.isEnabled():
return
if Qt.Horizontal == self.m_toggle.orientation():
t: int = self.m_toggle.height() / 2
w: int = self.m_thumb.height() / 2 + 10
else:
t: int = self.m_toggle.width() / 2
w: int = self.m_thumb.width() / 2 + 10
ripple = QMaterialRipple(QPoint(10 + t, 20 + t))
ripple.setColor(self.m_track.trackColor())
ripple.setRadiusEndValue(w)
ripple.setOpacityStartValue(0.8)
self.addRipple(ripple)
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
if QEvent.Paint == event.type():
self.setGeometry(self.overlayGeometry())
items: List[QMaterialRipple] = self.ripples()
color: QColor = self.m_track.trackColor()
for i in items:
i.setColor(color)
return QMaterialRippleOverlay.eventFilter(self, obj, event)
def overlayGeometry(self) -> QRect:
offset: float = self.m_thumb.offset()
if Qt.Horizontal == self.m_toggle.orientation():
return self.m_toggle.geometry().adjusted(-10 + offset, -20, 10 + offset, 20)
else:
return self.m_toggle.geometry().adjusted(-10, -20 + offset, 10, 20 + offset)
class QMaterialToggle(QAbstractButton):
def __init__(self, parent: QWidget = None):
QAbstractButton.__init__(self, parent)
self.m_track = QMaterialToggleTrack(self)
self.m_thumb = QMaterialToggleThumb(self)
self.m_rippleOverlay = QMaterialToggleRippleOverlay(
self.m_thumb, self.m_track, self
)
self.m_stateMachine = QStateMachine(self)
self.m_offState = QState()
self.m_onState = QState()
self.m_orientation = Qt.Horizontal
self.m_useThemeColors = True
self.setCheckable(True)
self.setChecked(False)
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
self.m_stateMachine.addState(self.m_offState)
self.m_stateMachine.addState(self.m_onState)
self.m_stateMachine.setInitialState(self.m_offState)
self.m_offState.assignProperty(self.m_thumb, "_shift", 0)
self.m_onState.assignProperty(self.m_thumb, "_shift", 1)
transition = QSignalTransition(self.m_toggled)
transition.setTargetState(self.m_onState)
self.m_offState.addTransition(transition)
animation = QPropertyAnimation(self)
animation.setPropertyName(b"_shift")
animation.setTargetObject(self.m_thumb)
animation.setDuration(200)
animation.setEasingCurve(QEasingCurve.OutQuad)
transition.addAnimation(animation)
animation = QPropertyAnimation(self)
animation.setPropertyName(b"_trackColor")
animation.setTargetObject(self.m_track)
animation.setDuration(150)
transition.addAnimation(animation)
animation = QPropertyAnimation(self)
animation.setPropertyName(b"_thumbColor")
animation.setTargetObject(self.m_thumb)
animation.setDuration(150)
transition.addAnimation(animation)
self.setupProperties()
self.m_stateMachine.start()
QCoreApplication.processEvents()
def setupProperties(self) -> None:
if self.isEnabled():
shift: float = self.m_thumb.shift()
if qFuzzyCompare(shift, 1):
self.m_thumb.setThumbColor(self.activeColor())
self.m_track.setTrackColor(self.activeColor().lighter(110))
elif qFuzzyCompare(1 + shift, 1):
self.m_thumb.setThumbColor(self.inactiveColor())
self.m_track.setTrackColor(self.trackColor())
self.m_offState.assignProperty(
self.m_track, "trackColor", self.trackColor().lighter(110)
)
self.m_onState.assignProperty(
self.m_track, "trackColor", self.activeColor().lighter(110)
)
self.m_offState.assignProperty(self.m_thumb, "thumbColor", self.inactiveColor())
self.m_onState.assignProperty(self.m_thumb, "thumbColor", self.activeColor())
self.update()
def setUseThemeColors(self, value: bool) -> None:
self.m_useThemeColors = value
self.setupProperties()
def useThemeColors(self) -> bool:
return self.m_useThemeColors
def setDisabledColor(self, color: QColor) -> None:
self.m_disabledColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.setupProperties()
def disabledColor(self) -> QColor:
if self.m_useThemeColors or not self.m_disabledColor.isValid():
return QMaterialStyle.instance().themeColor("disabled")
else:
return self.m_disabledColor
def setActiveColor(self, color: QColor) -> None:
self.m_activeColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.setupProperties()
def activeColor(self) -> QColor:
if self.m_useThemeColors or not self.m_activeColor.isValid():
return QMaterialStyle.instance().themeColor("primary1")
else:
return self.m_activeColor
def setInactiveColor(self, color: QColor) -> None:
self.m_inactiveColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.m_setupProperties()
def inactiveColor(self) -> QColor:
if self.m_useThemeColors or not self.m_inactiveColor.isValid():
return QMaterialStyle.instance().themeColor("canvas")
else:
return self.m_inactiveColor
def setTrackColor(self, color: QColor) -> None:
self.m_trackColor = color
MATERIAL_DISABLE_THEME_COLORS(self)
self.m_setupProperties()
def trackColor(self) -> QColor:
if self.m_useThemeColors or not self.m_trackColor.isValid():
return QMaterialStyle.instance().themeColor("accent3")
else:
return self.m_trackColor
def setOrientation(self, orientation: Qt.Orientation) -> None:
if self.m_orientation == orientation:
return
self.m_orientation = orientation
self.updateGeometry()
def orientation(self) -> Qt.Orientation:
return self.m_orientation
def sizeHint(self) -> QSize:
return QSize(64, 48) if Qt.Horizontal == self.m_orientation else QSize(48, 64)
def event(self, event: QEvent) -> bool:
if event.type() == QEvent.ParentChange:
widget = self.parentWidget()
if widget:
self.m_rippleOverlay.setParent(widget)
return QAbstractButton.event(self, event)
def paintEvent(self, event: QPaintEvent) -> None:
...
_disabledColor = Property(QColor, fset=setDisabledColor, fget=disabledColor)
_activeColor = Property(QColor, fset=setActiveColor, fget=activeColor)
_inactiveColor = Property(QColor, fset=setInactiveColor, fget=inactiveColor)
_trackColor = Property(QColor, fset=setTrackColor, fget=trackColor)