added callback util

This commit is contained in:
2021-06-09 14:29:21 +02:00
parent 546d5312fb
commit 881631bdb0
2 changed files with 22 additions and 6 deletions

View File

@ -1,5 +1,6 @@
from .frame import Frame
from .logic import decide_src_plt_cfg
from .utils import cb, cb_dbg
from .cfgdlgs import ConfigDialog1D, ConfigDialog2D #TODO
@ -16,7 +17,7 @@ class Actor:
self.cache = cache = src.cache.get_view()
cfg.ti_cache_size.value = str(cache.size)
cfg.ti_cache_size.on_change("value", self.do_update_cache_size)
cfg.ti_cache_size.on_change("value", cb_dbg(self.do_update_cache_size))
if isinstance(cfg, ConfigDialog1D): #TODO
cfg.curves_latest.active = plt.line_latest.visible
@ -32,17 +33,16 @@ class Actor:
if isinstance(cfg, ConfigDialog2D): #TODO
cfg.sel_cmap.value = plt.palette
cfg.sel_cmap.on_change("value", lambda _attr, _old, new: plt.change_cmap(new))
cfg.sp_vmin.on_change("value", lambda _attr, _old, new: plt.change_vmin(new))
cfg.sp_vmax.on_change("value", lambda _attr, _old, new: plt.change_vmax(new))
cfg.sel_cmap.on_change("value", cb(plt.change_cmap))
cfg.sp_vmin.on_change("value", cb(plt.change_vmin))
cfg.sp_vmax.on_change("value", cb(plt.change_vmax))
def delete(self):
self.cache = None
def do_update_cache_size(self, attr, old, new):
print(f"CB Update cache size: attribute \"{attr}\" changed from \"{old}\" to \"{new}\"")
def do_update_cache_size(self, new):
self.cache.set_size(int(new))
self.cfg.ti_cache_size.value = str(self.cache.size)

View File

@ -1,6 +1,22 @@
from functools import partial
def cb(handler):
def wrapper(attr, old, new):
handler(new)
return wrapper
def cb_dbg(handler):
fn = handler.__name__
def wrapper(attr, old, new):
print(f"Callback {fn}: attribute \"{attr}\" changed from \"{old}\" to \"{new}\"")
handler(new)
return wrapper
def adjust_margin(obj, top=0, right=0, bottom=0, left=0):
delta = (top, right, bottom, left)
obj.margin = [m + d for m, d in zip(obj.margin, delta)]