From 5f4369fbb39beda4ee57b7bef2a47beb7535a95f Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Tue, 9 Nov 2021 16:24:15 +0000 Subject: [PATCH] FloatAdj uses inheritance not composition. --- gui/demos/adjuster.py | 5 +++-- gui/widgets/adjuster.py | 36 ++++++++++++++++-------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/gui/demos/adjuster.py b/gui/demos/adjuster.py index f50e030..1d718a6 100644 --- a/gui/demos/adjuster.py +++ b/gui/demos/adjuster.py @@ -39,9 +39,10 @@ class BaseScreen(Screen): a = Adjuster(wri, row, self.lbl3.mcol + 2, fgcolor=YELLOW, callback=self.adj3_callback) Label(wri, row, a.mcol + 2, "Log") + # Demo of FloatAdj class self.fa = FloatAdj(wri, a.mrow + 5, col, color=BLUE, map_func=lambda x: (x - 0.5) * 20, - fstr="{:6.2f}", text="class") + value=0.5, fstr="{:6.2f}", text="class") CloseButton(wri) # Quit the application def adj1_callback(self, adj): @@ -57,7 +58,7 @@ class BaseScreen(Screen): self.lbl3.value(f"{v:4.2f}") #def after_open(self): # Demo of programmatic change - #self.fa.value(0.5) + #self.fa.value(0.8) def test(): print("Demo of Adjuster control.") diff --git a/gui/widgets/adjuster.py b/gui/widgets/adjuster.py index aaea3f1..2368938 100644 --- a/gui/widgets/adjuster.py +++ b/gui/widgets/adjuster.py @@ -52,37 +52,33 @@ class Adjuster(LinearIO): display.line(int(self.xorigin), int(self.yorigin), x_end, y_end, color) # This class combines an Adjuster with one or two labels. Numerous layout -# options exist: users may wish to write their own versions of this example. -# The map_func enables instances to have their own mapping of Adjuster value +# options exist: users may wish to write a version of this example with +# different visual characteristics. +# The map_func enables each instance to have its own mapping of Adjuster value # to perform offset, scaling, log mapping etc. # The object's value is that of the Adjuster, in range 0.0-1.0. The scaled # value is retrieved with .mapped_value() -class FloatAdj: +class FloatAdj(Adjuster): def __init__(self, wri, row, col, *, lbl_width=60, value=0.0, color=None, fstr="{:4.2f}", map_func=lambda v: v, text="", callback=dolittle, args=[]): - self.fstr = fstr self.map_func = map_func - self.callback = callback + self.facb = callback self.args = args + self.fstr = fstr self.lbl = Label(wri, row, col, lbl_width, bdcolor=color) - self.adj = Adjuster(wri, row, self.lbl.mcol + 2, value=value, - fgcolor=color, callback=self.cb) - l = Label(wri, row, self.adj.mcol + 2, text) if text else self.adj - # Facilitate relative positioning. - self.mcol = l.mcol - self.mrow = self.adj.mrow + super().__init__(wri, row, self.lbl.mcol + 2, value=value, + fgcolor=color, callback=self.cb) + if text: + self.legend = Label(wri, row, self.mcol + 2, text) + self.mcol = self.legend.mcol # For relative positioning - def cb(self, adj): - self.lbl.value(self.fstr.format(self.mapped_value(adj))) - self.callback(self, *self.args) + def cb(self, adj): # Runs on init and when value changes + self.lbl.value(self.fstr.format(self.mapped_value())) + self.facb(self, *self.args) # Run user callback - # Behave like a Widget. - def value(self, v=None): - return self.adj.value(v) - - def mapped_value(self, adj=None): # Special handling for initial callback - return self.map_func(self.value() if adj is None else adj.value()) + def mapped_value(self): + return self.map_func(self.value())