-
Notifications
You must be signed in to change notification settings - Fork 0
/
binSelector.py
91 lines (59 loc) · 3.18 KB
/
binSelector.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
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont
from resolveBinTree import ResolveBinTree
class BinSelector(ttk.Combobox):
def __init__(self, master, selectedBinLabel, selectBinFunction, allowNoSelection = True, noneLabel = "None", **kw) -> None:
self.allowNoSelection = allowNoSelection
self.selectBinFunction = selectBinFunction
self.noneLabel = noneLabel
self.binPaths = self.generateBinPaths()
self.selectedBin, selectedBinLabel = self.findSelectedBinFromPath(selectedBinLabel)
self.selectedBinLabelVar = tk.StringVar(value = selectedBinLabel)
self.setSelectedBin(self.selectedBin, selectedBinLabel)
super().__init__(master, textvariable=self.selectedBinLabelVar, **kw)
self["values"] = self.binPaths
self.bind('<FocusIn>', self.onFocusIn)
self.bind('<<ComboboxSelected>>', self.onItemSelected)
self.bind('<ButtonPress>', self.onConfigure)
self["state"] = "readonly"
def getSelectedBin(self):
return self.selectedBin
def getSelectedBinPath(self):
return self.selectedBinLabel
def getMasterBin(self):
return ResolveBinTree.get()
def getDefaultBin(self):
return None if self.allowNoSelection else self.getMasterBin()
def findSelectedBinFromPath(self, selectedBinPath):
if self.allowNoSelection and selectedBinPath == self.noneLabel:
return None, self.noneLabel
masterBin = ResolveBinTree.get()
bin = masterBin.findBinFromPath(selectedBinPath, None if self.allowNoSelection else masterBin)
if bin == None:
print(f"[Bin Selector] Failed to find bin from path {selectedBinPath}")
selectedBinPath = masterBin.getPath() if masterBin else ""
return bin, selectedBinPath
def setSelectedBin(self, selectedBin, selectedBinLabel = None):
self.selectedBin = selectedBin
if selectedBin:
self.selectedBinLabel = selectedBinLabel
self.selectedBinLabelVar.set(selectedBinLabel)
def generateBinPaths(self):
labels = ResolveBinTree.get().getBinPathsRecursive()
if self.allowNoSelection:
labels.insert(0, self.noneLabel)
return labels
def onConfigure(self, event):
style = ttk.Style()
long = max(self.cget('values'), key=len)
font = tkfont.nametofont(str(self.cget('font')))
width = max(0,font.measure(long.strip() + '0') - self.winfo_width())
style.configure('TCombobox', postoffset=(0,0,width,0))
def onFocusIn(self, event):
self["values"] = self.binPaths = self.generateBinPaths()
def onItemSelected(self, event):
selectedBinPath = self.selectedBinLabelVar.get()
bin, selectedBinPath = self.findSelectedBinFromPath(selectedBinPath)
self.setSelectedBin(bin, selectedBinPath)
self.selectBinFunction(event)