-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
114 lines (91 loc) · 3.07 KB
/
example.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
import sys
import logging
from hfilesize import FileSize
import humanize
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QLabel
from qt_range_slider import QtRangeSlider
def _size(size):
print(f"size: {size}")
if size < 0:
print("size is negative, resetting to 0")
size = 0
return humanize.naturalsize(FileSize(size))
# pylint: disable=too-many-arguments
def _render_only_slider(layout, min_value=0, max_value=10):
inner_layout = QHBoxLayout()
slider = QtRangeSlider(layout.parent(), min_value, max_value)
slider.setMinimumWidth(500)
inner_layout.addWidget(slider)
layout.addLayout(inner_layout)
def _render_slider_with_labels(
layout,
min_value=0,
max_value=10,
left_thumb_value=0,
right_thumb_value=None,
size_value=False,
):
inner_layout = QHBoxLayout()
label_min = QLabel(_size(left_thumb_value) if size_value else str(left_thumb_value))
label_min.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
label_min.setFixedWidth(70)
_right_thumb_value = right_thumb_value if right_thumb_value else max_value
label_max = QLabel(
_size(_right_thumb_value) if size_value else str(_right_thumb_value)
)
label_max.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
label_max.setFixedWidth(70)
slider = QtRangeSlider(
layout.parent(), min_value, max_value, left_thumb_value, right_thumb_value
)
slider.setMinimumWidth(500)
inner_layout.addWidget(label_min)
inner_layout.addWidget(slider)
inner_layout.addWidget(label_max)
layout.addLayout(inner_layout)
# connect signal-slots
if not size_value:
slider.left_thumb_value_changed.connect(label_min.setNum)
slider.right_thumb_value_changed.connect(label_max.setNum)
return slider
slider.left_thumb_value_changed.connect(lambda x: label_min.setText(_size(x)))
slider.right_thumb_value_changed.connect(lambda x: label_max.setText(_size(x)))
return slider
def main():
app = QApplication(sys.argv)
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
main_window = QWidget()
main_window.setMinimumSize(640, 480)
layout = QVBoxLayout(main_window)
min_value = 0
max_value = 10
_render_only_slider(layout, min_value, max_value)
_render_slider_with_labels(layout, min_value, max_value)
_render_slider_with_labels(
layout, min_value, max_value, min_value + 1, max_value - 1
)
min_value = 0
max_value = 10 * 1024 * 1024 * 1024
_render_slider_with_labels(
layout,
min_value,
max_value,
max_value // 3,
max_value * 3 // 4,
size_value=True,
)
slider = _render_slider_with_labels(
layout,
min_value,
max_value,
max_value // 3,
max_value * 3 // 4,
size_value=True,
)
slider.setMinimumWidth(500)
slider.set_ticks_count(10)
main_window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()