id renamed to identifier in _make_channelframe()

label_cache arrays now initialised with empty strings

update_levels() now called when initialising ChannelFrame
This commit is contained in:
onyx-and-iris 2023-08-06 23:15:08 +01:00
parent 0ad40ab708
commit 6222ab1e62

View File

@ -151,17 +151,18 @@ class ChannelLabelFrame(ttk.LabelFrame):
def sync_labels(self): def sync_labels(self):
"""sync labelframes according to label text""" """sync labelframes according to label text"""
retval = self.getter("label") retval = self.getter("label")
self.parent.label_cache[self.id].insert(self.index, retval) if self.parent.label_cache[self.id][self.index] != retval:
if len(retval) > 10: self.parent.label_cache[self.id][self.index] = retval
retval = f"{retval[:8]}.." if len(retval) > 10:
if not retval: retval = f"{retval[:8]}.."
self.parent.columnconfigure(self.index, minsize=0) if not retval:
self.parent.parent.subject.remove(self) self.parent.columnconfigure(self.index, minsize=0)
self.grid_remove() self.parent.parent.subject.remove(self)
else: self.grid_remove()
self.parent.parent.subject.add(self) else:
self.grid() self.parent.parent.subject.add(self)
self.configure(text=retval) self.grid()
self.configure(text=retval)
def grid_configure(self): def grid_configure(self):
self.grid(padx=_configuration.channel_xpadding, sticky=(tk.N, tk.S)) self.grid(padx=_configuration.channel_xpadding, sticky=(tk.N, tk.S))
@ -228,15 +229,18 @@ class Bus(ChannelLabelFrame):
class ChannelFrame(ttk.Frame): class ChannelFrame(ttk.Frame):
label_cache = {"strip": list(), "bus": list()}
def init(self, parent, id): def init(self, parent, id):
super().__init__(parent) super().__init__(parent)
self.parent = parent self.parent = parent
self.id = id self.id = id
self.phys_in, self.virt_in = parent.kind.ins self.phys_in, self.virt_in = parent.kind.ins
self.phys_out, self.virt_out = parent.kind.outs self.phys_out, self.virt_out = parent.kind.outs
self.label_cache = {
"strip": [""] * (self.phys_in + self.virt_in),
"bus": [""] * (self.phys_out + self.virt_out),
}
self.parent.subject.add(self) self.parent.subject.add(self)
self.update_labels()
@property @property
def target(self): def target(self):
@ -258,13 +262,13 @@ class ChannelFrame(ttk.Frame):
if isinstance(frame, ttk.LabelFrame) if isinstance(frame, ttk.LabelFrame)
) )
def update_labels(self):
for labelframe in self.labelframes:
labelframe.on_update("labelframe")
def on_update(self, subject): def on_update(self, subject):
if subject == "pdirty": if subject == "pdirty":
target = getattr(self.target, self.id) self.update_labels()
num = getattr(self.parent.kind, f"num_{self.id}")
if self.label_cache[self.id] != [target[i].label for i in range(num)]:
for labelframe in self.labelframes:
labelframe.on_update("labelframe")
def grid_configure(self): def grid_configure(self):
[ [
@ -280,7 +284,7 @@ class ChannelFrame(ttk.Frame):
setattr(self.parent, f"{self.identifier}_frame", None) setattr(self.parent, f"{self.identifier}_frame", None)
def _make_channelframe(parent, id): def _make_channelframe(parent, identifier):
""" """
Creates a Channel Frame class of type strip or bus Creates a Channel Frame class of type strip or bus
""" """
@ -288,29 +292,33 @@ def _make_channelframe(parent, id):
phys_in, virt_in = parent.kind.ins phys_in, virt_in = parent.kind.ins
phys_out, virt_out = parent.kind.outs phys_out, virt_out = parent.kind.outs
def init_labels(self, id): def init_labels(self):
""" """
Grids each labelframe, grid_removes any without a label Grids each labelframe, grid_removes any without a label
""" """
for i, labelframe in enumerate( for i, labelframe in enumerate(
getattr(self, "strips" if id == "strip" else "buses") getattr(self, "strips" if identifier == "strip" else "buses")
): ):
labelframe.grid(row=0, column=i) labelframe.grid(row=0, column=i)
if not labelframe.target.label: label = labelframe.target.label
if not label:
self.columnconfigure(i, minsize=0) self.columnconfigure(i, minsize=0)
labelframe.grid_remove() labelframe.grid_remove()
self.label_cache[identifier][i] = label
def init_strip(self, *args, **kwargs): def init_strip(self, *args, **kwargs):
self.init(parent, id) self.init(parent, identifier)
self.strips = tuple(Strip(self, i, id) for i in range(phys_in + virt_in)) self.strips = tuple(
Strip(self, i, identifier) for i in range(phys_in + virt_in)
)
self.grid(row=0, column=0, sticky=(tk.W)) self.grid(row=0, column=0, sticky=(tk.W))
self.grid_configure() self.grid_configure()
init_labels(self, id) init_labels(self)
def init_bus(self, *args, **kwargs): def init_bus(self, *args, **kwargs):
self.init(parent, id) self.init(parent, identifier)
self.buses = tuple(Bus(self, i, id) for i in range(phys_out + virt_out)) self.buses = tuple(Bus(self, i, identifier) for i in range(phys_out + virt_out))
if _configuration.extended: if _configuration.extended:
if _configuration.extends_horizontal: if _configuration.extends_horizontal:
self.grid(row=0, column=2, sticky=(tk.W)) self.grid(row=0, column=2, sticky=(tk.W))
@ -319,11 +327,11 @@ def _make_channelframe(parent, id):
else: else:
self.grid(row=0, column=0) self.grid(row=0, column=0)
self.grid_configure() self.grid_configure()
init_labels(self, id) init_labels(self)
if id == "strip": if identifier == "strip":
CHANNELFRAME_cls = type( CHANNELFRAME_cls = type(
f"ChannelFrame{id.capitalize()}", f"ChannelFrame{identifier.capitalize()}",
(ChannelFrame,), (ChannelFrame,),
{ {
"__init__": init_strip, "__init__": init_strip,
@ -331,7 +339,7 @@ def _make_channelframe(parent, id):
) )
else: else:
CHANNELFRAME_cls = type( CHANNELFRAME_cls = type(
f"ChannelFrame{id.capitalize()}", f"ChannelFrame{identifier.capitalize()}",
(ChannelFrame,), (ChannelFrame,),
{ {
"__init__": init_bus, "__init__": init_bus,