-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnice_checkbox.py
545 lines (450 loc) · 17.3 KB
/
nice_checkbox.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import sys
openpype_dir = r"C:\Users\jakub.trllo\Desktop\pype\pype3"
sys.path.append(
r"{}\.venv\Lib\site-packages".format(openpype_dir)
)
from math import floor, sqrt, ceil
from Qt import QtWidgets, QtCore, QtGui
class NiceCheckbox(QtWidgets.QFrame):
stateChanged = QtCore.Signal(int)
def __init__(
self, checked=False, draw_icons=False, parent=None,
use_checkbox_height_hint=None
):
super(NiceCheckbox, self).__init__(parent)
self._checked = checked
if checked:
checkstate = QtCore.Qt.Checked
else:
checkstate = QtCore.Qt.Unchecked
self._checkstate = checkstate
self._is_tristate = False
self._draw_icons = draw_icons
self._animation_timer = QtCore.QTimer(self)
self._animation_timeout = 6
self._use_checkbox_height_hint = use_checkbox_height_hint
self._first_show = True
self._fixed_width_set = False
self._fixed_height_set = False
self._current_step = None
self._steps = 21
self._middle_step = 11
self.set_steps(self._steps)
self._pressed = False
self._under_mouse = False
self.checked_bg_color = QtGui.QColor(67, 181, 129)
self.unchecked_bg_color = QtGui.QColor(230, 230, 230)
self.unchecked_bg_color = QtGui.QColor(79, 79, 79)
self.checker_checked_color = QtGui.QColor(255, 255, 255)
self.checker_unchecked_color = self.checker_checked_color
self.border_color = QtGui.QColor(44, 44, 44)
self.border_color_hover = QtGui.QColor(119, 131, 126)
self.icon_scale_factor = sqrt(2) / 2
icon_path_stroker = QtGui.QPainterPathStroker()
icon_path_stroker.setCapStyle(QtCore.Qt.RoundCap)
icon_path_stroker.setJoinStyle(QtCore.Qt.RoundJoin)
self.icon_path_stroker = icon_path_stroker
self._animation_timer.timeout.connect(self._on_animation_timeout)
self._base_size = QtCore.QSize(90, 50)
def setTristate(self, tristate=True):
if self._is_tristate == tristate:
return
self._is_tristate = tristate
def set_draw_icons(self, draw_icons=None):
if draw_icons is None:
draw_icons = not self._draw_icons
if draw_icons == self._draw_icons:
return
self._draw_icons = draw_icons
self.repaint()
def showEvent(self, event):
super(NiceCheckbox, self).showEvent(event)
if self._first_show:
self._first_show = False
if (
self._use_checkbox_height_hint
or (
self._use_checkbox_height_hint is None
and not (self._fixed_width_set or self._fixed_height_set)
)
):
checkbox_height = self.style().pixelMetric(
QtWidgets.QStyle.PM_IndicatorHeight
)
checkbox_height += checkbox_height % 2
width = (2 * checkbox_height) - (checkbox_height / 5)
new_size = QtCore.QSize(width, checkbox_height)
self.setFixedSize(new_size)
def resizeEvent(self, event):
new_size = QtCore.QSize(self._base_size)
new_size.scale(event.size(), QtCore.Qt.KeepAspectRatio)
self.resize(new_size)
def setFixedHeight(self, *args, **kwargs):
self._fixed_height_set = True
super(NiceCheckbox, self).setFixedHeight(*args, **kwargs)
if not self._fixed_width_set:
width = (
self.height() / self._base_size.height()
) * self._base_size.width()
self.setFixedWidth(width)
def setFixedWidth(self, *args, **kwargs):
self._fixed_width_set = True
super(NiceCheckbox, self).setFixedWidth(*args, **kwargs)
if not self._fixed_height_set:
width = (
self.width() / self._base_size.width()
) * self._base_size.height()
self.setFixedHeight(width)
def setFixedSize(self, *args, **kwargs):
self._fixed_height_set = True
self._fixed_width_set = True
super(NiceCheckbox, self).setFixedSize(*args, **kwargs)
def steps(self):
return self._steps
def set_steps(self, steps):
if steps < 2:
steps = 2
# Make sure animation is stopped
if self._animation_timer.isActive():
self._animation_timer.stop()
# Set steps and set current step by current checkstate
self._steps = steps
diff = steps % 2
self._middle_step = (int(steps - diff) / 2) + diff
if self._checkstate == QtCore.Qt.Checked:
self._current_step = self._steps
elif self._checkstate == QtCore.Qt.Unchecked:
self._current_step = 0
else:
self._current_step = self._middle_step
def checkState(self):
return self._checkstate
def isChecked(self):
return self._checked
def setCheckState(self, state):
if self._checkstate == state:
return
self._checkstate = state
if state == QtCore.Qt.Checked:
self._checked = True
elif state == QtCore.Qt.Unchecked:
self._checked = False
self.stateChanged.emit(self.checkState())
if self._animation_timer.isActive():
self._animation_timer.stop()
if self.isEnabled():
# Start animation
self._animation_timer.start(self._animation_timeout)
else:
# Do not animate change if is disabled
if state == QtCore.Qt.Checked:
self._current_step = self._steps
elif state == QtCore.Qt.Unchecked:
self._current_step = 0
else:
self._current_step = self._middle_step
self.repaint()
def setChecked(self, checked):
if checked == self._checked:
return
if checked:
checkstate = QtCore.Qt.Checked
else:
checkstate = QtCore.Qt.Unchecked
self.setCheckState(checkstate)
def nextCheckState(self):
if self._checkstate == QtCore.Qt.Unchecked:
if self._is_tristate:
return QtCore.Qt.PartiallyChecked
return QtCore.Qt.Checked
if self._checkstate == QtCore.Qt.Checked:
return QtCore.Qt.Unchecked
if self._checked:
return QtCore.Qt.Unchecked
return QtCore.Qt.Checked
def sizeHint(self):
return self._base_size
def mousePressEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self._pressed = True
self.repaint()
super(NiceCheckbox, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
if self._pressed and not event.buttons() & QtCore.Qt.LeftButton:
self._pressed = False
if self.rect().contains(event.pos()):
self.setCheckState(self.nextCheckState())
super(NiceCheckbox, self).mouseReleaseEvent(event)
def mouseMoveEvent(self, event):
if self._pressed:
under_mouse = self.rect().contains(event.pos())
if under_mouse != self._under_mouse:
self._under_mouse = under_mouse
self.repaint()
super(NiceCheckbox, self).mouseMoveEvent(event)
def enterEvent(self, event):
self._under_mouse = True
if self.isEnabled():
self.repaint()
super(NiceCheckbox, self).enterEvent(event)
def leaveEvent(self, event):
self._under_mouse = False
if self.isEnabled():
self.repaint()
super(NiceCheckbox, self).leaveEvent(event)
def _on_animation_timeout(self):
if self._checkstate == QtCore.Qt.Checked:
self._current_step += 1
if self._current_step == self._steps:
self._animation_timer.stop()
elif self._checkstate == QtCore.Qt.Unchecked:
self._current_step -= 1
if self._current_step == 0:
self._animation_timer.stop()
else:
if self._current_step < self._middle_step:
self._current_step += 1
elif self._current_step > self._middle_step:
self._current_step -= 1
if self._current_step == self._middle_step:
self._animation_timer.stop()
self.repaint()
@staticmethod
def steped_color(color1, color2, offset_ratio):
red_dif = (
color1.red() - color2.red()
)
green_dif = (
color1.green() - color2.green()
)
blue_dif = (
color1.blue() - color2.blue()
)
red = int(color2.red() + (
red_dif * offset_ratio
))
green = int(color2.green() + (
green_dif * offset_ratio
))
blue = int(color2.blue() + (
blue_dif * offset_ratio
))
return QtGui.QColor(red, green, blue)
def paintEvent(self, event):
if self.width() < 1 or self.height() < 1:
return
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
frame_rect = QtCore.QRect(event.rect())
if self.isEnabled() and self._under_mouse:
pen_color = self.border_color_hover
else:
pen_color = self.border_color
# Draw inner background
if self._current_step == self._steps:
bg_color = self.checked_bg_color
checker_color = self.checker_checked_color
elif self._current_step == 0:
bg_color = self.unchecked_bg_color
checker_color = self.checker_unchecked_color
else:
offset_ratio = self._current_step / self._steps
# Animation bg
bg_color = self.steped_color(
self.checked_bg_color,
self.unchecked_bg_color,
offset_ratio
)
checker_color = self.steped_color(
self.checker_checked_color,
self.checker_unchecked_color,
offset_ratio
)
margins_ratio = 20
size_without_margins = int(
frame_rect.height() / margins_ratio * (margins_ratio - 2)
)
margin_size_c = ceil(frame_rect.height() - size_without_margins) / 2
checkbox_rect = QtCore.QRect(
frame_rect.x() + margin_size_c,
frame_rect.y() + margin_size_c,
frame_rect.width() - (margin_size_c * 2),
frame_rect.height() - (margin_size_c * 2)
)
if checkbox_rect.width() > checkbox_rect.height():
radius = floor(checkbox_rect.height() / 2)
else:
radius = floor(checkbox_rect.width() / 2)
painter.setPen(QtCore.Qt.transparent)
painter.setBrush(bg_color)
painter.drawRoundedRect(checkbox_rect, radius, radius)
# Draw checker
checker_size = size_without_margins - (margin_size_c * 2)
area_width = (
checkbox_rect.width()
- (margin_size_c * 2)
- checker_size
)
if self._current_step == 0:
x_offset = 0
else:
x_offset = (area_width / self._steps) * self._current_step
pos_x = checkbox_rect.x() + x_offset + margin_size_c
pos_y = checkbox_rect.y() + margin_size_c
checker_rect = QtCore.QRect(pos_x, pos_y, checker_size, checker_size)
under_mouse = self.isEnabled() and self._under_mouse
shadow_x = checker_rect.x()
shadow_y = checker_rect.y() + margin_size_c
shadow_size = min(
frame_rect.right() - shadow_x,
frame_rect.bottom() - shadow_y,
checker_size + (2 * margin_size_c)
)
shadow_rect = QtCore.QRect(
checker_rect.x(),
shadow_y,
shadow_size,
shadow_size
)
shadow_brush = QtGui.QRadialGradient(
shadow_rect.center(),
shadow_rect.height() / 2
)
shadow_brush.setColorAt(0.6, QtCore.Qt.black)
shadow_brush.setColorAt(1, QtCore.Qt.transparent)
painter.setPen(QtCore.Qt.transparent)
painter.setBrush(shadow_brush)
painter.drawEllipse(shadow_rect)
painter.setBrush(checker_color)
painter.drawEllipse(checker_rect)
smaller_checker_rect = checker_rect.adjusted(
margin_size_c, margin_size_c, -margin_size_c, -margin_size_c
)
gradient = QtGui.QLinearGradient(
smaller_checker_rect.bottomRight(),
smaller_checker_rect.topLeft()
)
gradient.setColorAt(0, checker_color)
if under_mouse:
dark_value = 120
else:
dark_value = 115
gradient.setColorAt(1, checker_color.darker(dark_value))
painter.setBrush(gradient)
painter.drawEllipse(smaller_checker_rect)
if self._draw_icons:
painter.setBrush(bg_color)
icon_path = self._get_icon_path(painter, checker_rect)
painter.drawPath(icon_path)
# Draw shadow overlay
if not self.isEnabled():
level = 33
alpha = 127
painter.setPen(QtCore.Qt.transparent)
painter.setBrush(QtGui.QColor(level, level, level, alpha))
painter.drawRoundedRect(checkbox_rect, radius, radius)
painter.end()
def _get_icon_path(self, painter, checker_rect):
self.icon_path_stroker.setWidth(checker_rect.height() / 5)
if self._current_step == self._steps:
return self._get_enabled_icon_path(painter, checker_rect)
if self._current_step == 0:
return self._get_disabled_icon_path(painter, checker_rect)
if self._current_step == self._middle_step:
return self._get_middle_circle_path(painter, checker_rect)
disabled_step = self._steps - self._current_step
enabled_step = self._steps - disabled_step
half_steps = self._steps + 1 - ((self._steps + 1) % 2)
if enabled_step > disabled_step:
return self._get_enabled_icon_path(
painter, checker_rect, enabled_step, half_steps
)
else:
return self._get_disabled_icon_path(
painter, checker_rect, disabled_step, half_steps
)
def _get_middle_circle_path(self, painter, checker_rect):
width = self.icon_path_stroker.width()
path = QtGui.QPainterPath()
path.addEllipse(checker_rect.center(), width, width)
return path
def _get_enabled_icon_path(
self, painter, checker_rect, step=None, half_steps=None
):
fifteenth = checker_rect.height() / 15
# Left point
p1 = QtCore.QPoint(
checker_rect.x() + (5 * fifteenth),
checker_rect.y() + (9 * fifteenth)
)
# Middle bottom point
p2 = QtCore.QPoint(
checker_rect.center().x(),
checker_rect.y() + (11 * fifteenth)
)
# Top right point
p3 = QtCore.QPoint(
checker_rect.x() + (10 * fifteenth),
checker_rect.y() + (5 * fifteenth)
)
if step is not None:
multiplier = (half_steps - step)
p1c = p1 - checker_rect.center()
p2c = p2 - checker_rect.center()
p3c = p3 - checker_rect.center()
p1o = QtCore.QPoint(
(p1c.x() / half_steps) * multiplier,
(p1c.y() / half_steps) * multiplier
)
p2o = QtCore.QPoint(
(p2c.x() / half_steps) * multiplier,
(p2c.y() / half_steps) * multiplier
)
p3o = QtCore.QPoint(
(p3c.x() / half_steps) * multiplier,
(p3c.y() / half_steps) * multiplier
)
p1 -= p1o
p2 -= p2o
p3 -= p3o
path = QtGui.QPainterPath(p1)
path.lineTo(p2)
path.lineTo(p3)
stroked_path = self.icon_path_stroker.createStroke(path)
return stroked_path
def _get_disabled_icon_path(
self, painter, checker_rect, step=None, half_steps=None
):
center_point = QtCore.QPointF(
checker_rect.width() / 2, checker_rect.height() / 2
)
offset = (
(center_point + QtCore.QPointF(0, 0)) / 2
).x() / 4 * 5
if step is not None:
diff = center_point.x() - offset
diff_offset = (diff / half_steps) * (half_steps - step)
offset += diff_offset
line1_p1 = QtCore.QPointF(
checker_rect.topLeft().x() + offset,
checker_rect.topLeft().y() + offset,
)
line1_p2 = QtCore.QPointF(
checker_rect.bottomRight().x() - offset,
checker_rect.bottomRight().y() - offset
)
line2_p1 = QtCore.QPointF(
checker_rect.bottomLeft().x() + offset,
checker_rect.bottomLeft().y() - offset
)
line2_p2 = QtCore.QPointF(
checker_rect.topRight().x() - offset,
checker_rect.topRight().y() + offset
)
path = QtGui.QPainterPath()
path.moveTo(line1_p1)
path.lineTo(line1_p2)
path.moveTo(line2_p1)
path.lineTo(line2_p2)
stroked_path = self.icon_path_stroker.createStroke(path)
return stroked_path