-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtkSliderWidget.py
222 lines (197 loc) · 7.1 KB
/
tkSliderWidget.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
from tkinter import *
from tkinter.ttk import *
from typing import TypedDict, List, Callable, Optional, Union
class Bar(TypedDict):
Ids: List[int]
Pos: float
Value: float
num_t = Union[int, float]
class Slider(Frame):
LINE_COLOR = "#476b6b"
LINE_WIDTH = 3
BAR_COLOR_INNER = "#5c8a8a"
BAR_COLOR_OUTTER = "#c2d6d6"
BAR_RADIUS = 10
BAR_RADIUS_INNER = BAR_RADIUS - 5
DIGIT_PRECISION = ".1f" # for showing in the canvas
# relative step size in 0 to 1, set to 0 for no step size restiction
# may be override by the step_size argument in __init__
STEP_SIZE:float = 0.0
def __init__(
self,
master,
width: int = 400,
height: int = 80,
min_val: num_t = 0,
max_val: num_t = 1,
step_size: Optional[float] = None,
init_lis: Optional[list[num_t]] = None,
show_value = True,
removable = False,
addable = False,
):
if step_size == None:
# inherit from class variable
step_size = self.STEP_SIZE
assert step_size >= 0, "step size must be positive"
assert step_size <= max_val - min_val, "step size must be smaller than range"
assert min_val < max_val, "min value must be smaller than max value"
Frame.__init__(self, master, height=height, width=width)
self.master = master
if init_lis == None:
init_lis = [min_val]
self.init_lis = init_lis
self.max_val = max_val
self.min_val = min_val
self.step_size_frac = step_size / float(max_val - min_val) # step size fraction
self.show_value = show_value
self.H = height
self.W = width
self.canv_H = self.H
self.canv_W = self.W
if not show_value:
self.slider_y = self.canv_H / 2 # y pos of the slider
else:
self.slider_y = self.canv_H * 2 / 5
self.slider_x = Slider.BAR_RADIUS # x pos of the slider (left side)
self._val_change_callback = lambda lis: None
self.bars: List[Bar] = []
self.selected_idx = None # current selection bar index
for value in self.init_lis:
pos = (value - min_val) / (max_val - min_val)
ids = []
bar: Bar = {"Pos": pos, "Ids": ids, "Value": value}
self.bars.append(bar)
self.canv = Canvas(self, height=self.canv_H, width=self.canv_W)
self.canv.pack()
self.canv.bind("<Motion>", self._mouseMotion)
self.canv.bind("<B1-Motion>", self._moveBar)
if removable:
self.canv.bind("<3>", self._removeBar)
if addable:
self.canv.bind("<ButtonRelease-1>", self._addBar)
self.__addTrack(
self.slider_x, self.slider_y, self.canv_W - self.slider_x, self.slider_y
)
for bar in self.bars:
bar["Ids"] = self.__addBar(bar["Pos"])
def getValues(self) -> List[float]:
values = [bar["Value"] for bar in self.bars]
return sorted(values)
def setValueChangeCallback(self, callback: Callable[[List[float]], None]):
self._val_change_callback = callback
def _mouseMotion(self, event):
x = event.x
y = event.y
selection = self.__checkSelection(x, y)
if selection[0]:
self.canv.config(cursor="hand2")
self.selected_idx = selection[1]
else:
self.canv.config(cursor="")
self.selected_idx = None
def _moveBar(self, event):
x = event.x
y = event.y
if self.selected_idx == None:
return False
pos = self.__calcPos(x)
idx = self.selected_idx
if self.step_size_frac > 0:
curr_pos = self.bars[idx]["Pos"]
if abs(curr_pos - pos) < (self.step_size_frac * 0.75):
return
pos = round(pos / self.step_size_frac) * self.step_size_frac
self.__moveBar(idx, pos)
def _removeBar(self, event):
x = event.x
y = event.y
if self.selected_idx == None:
return False
idx = self.selected_idx
ids = self.bars[idx]["Ids"]
for id in ids:
self.canv.delete(id)
self.bars.pop(idx)
def _addBar(self, event):
x = event.x
y = event.y
if self.selected_idx == None:
pos = self.__calcPos(x)
ids = []
bar = {
"Pos": pos,
"Ids": ids,
"Value": self.__calcPos(x) * (self.max_val - self.min_val)
+ self.min_val,
}
self.bars.append(bar)
for i in self.bars:
ids = i["Ids"]
for id in ids:
self.canv.delete(id)
for bar in self.bars:
bar["Ids"] = self.__addBar(bar["Pos"])
def __addTrack(self, startx, starty, endx, endy):
id1 = self.canv.create_line(
startx, starty, endx, endy, fill=Slider.LINE_COLOR, width=Slider.LINE_WIDTH
)
return id
def __addBar(self, pos):
"""@ pos: position of the bar, ranged from (0,1)"""
if pos < 0 or pos > 1:
raise Exception("Pos error - Pos: " + str(pos))
R = Slider.BAR_RADIUS
r = Slider.BAR_RADIUS_INNER
L = self.canv_W - 2 * self.slider_x
y = self.slider_y
x = self.slider_x + pos * L
id_outer = self.canv.create_oval(
x - R,
y - R,
x + R,
y + R,
fill=Slider.BAR_COLOR_OUTTER,
width=2,
outline="",
)
id_inner = self.canv.create_oval(
x - r, y - r, x + r, y + r, fill=Slider.BAR_COLOR_INNER, outline=""
)
if self.show_value:
y_value = y + Slider.BAR_RADIUS + 8
value = pos * (self.max_val - self.min_val) + self.min_val
id_value = self.canv.create_text(
x, y_value, text=format(value, Slider.DIGIT_PRECISION)
)
return [id_outer, id_inner, id_value]
else:
return [id_outer, id_inner]
def __moveBar(self, idx, pos):
ids = self.bars[idx]["Ids"]
for id in ids:
self.canv.delete(id)
self.bars[idx]["Ids"] = self.__addBar(pos)
self.bars[idx]["Pos"] = pos
self.bars[idx]["Value"] = pos * (self.max_val - self.min_val) + self.min_val
self._val_change_callback(self.getValues())
def __calcPos(self, x):
"""calculate position from x coordinate"""
pos = (x - self.slider_x) / (self.canv_W - 2 * self.slider_x)
if pos < 0:
return 0
elif pos > 1:
return 1
else:
return pos
def __checkSelection(self, x, y):
"""
To check if the position is inside the bounding rectangle of a Bar
Return [True, bar_index] or [False, None]
"""
for idx in range(len(self.bars)):
id = self.bars[idx]["Ids"][0]
bbox = self.canv.bbox(id)
if bbox[0] < x and bbox[2] > x and bbox[1] < y and bbox[3] > y:
return [True, idx]
return [False, None]