Adjuster can now take min-delta and max-delta args.

pull/55/head
Peter Hinch 2024-12-13 13:52:35 +00:00
rodzic d319f72c40
commit 7cb2277363
2 zmienionych plików z 68 dodań i 19 usunięć

Wyświetl plik

@ -67,7 +67,7 @@ target and a C device driver (unless you can acquire a suitable binary).
# Project status
Oct 2024: Oct 2024: Refresh locking can now be handled by device driver.
Oct 2024: Refresh locking can now be handled by device driver.
Sept 2024: Refresh control is now via a `Lock`. See [Realtime applications](./README.md#9-realtime-applications).
This is a breaking change for applications which use refresh control.
Sept 2024: Dropdown and Listbox widgets support dynamically variable lists of elements.
@ -2652,6 +2652,8 @@ Optional keyword only arguments:
* `callback=dolittle` Callback function runs when the user moves the knob or
the value is changed programmatically.
* `args=[]` A list/tuple of arguments for above callback.
* `min_delta=0.01` Amount value changes for one click in fine mode.
* `max_delta=0.1` Amount value changes for one click in normal mode.
Methods:
* `greyed_out` Optional Boolean argument `val=None`. If `None` returns the
@ -2672,12 +2674,12 @@ linked label.
### A numeric entry device
The file [widgets/adjuster.py](./gui/widgets/adjuster.py) includes an example
class which combines an `Adjuster` with one or two `Label` instances. The
`Adjuster` changes the displayed value in the `Label` to its left. Its use is
illustrated in [demos/adjuster.py](./gui/demos/adjuster.py). The class can be
class `FloatAdj` which combines an `Adjuster` with one or two `Label` instances.
The `Adjuster` changes the displayed value in the `Label` to its left. Its use
is illustrated in [demos/adjuster.py](./gui/demos/adjuster.py). The class can be
used as a template for a user class, which may have a different layout on
screen. It supports arbitrary mapping and number formatting on a per-instance
basis. See code comments for further details.
basis.
###### [Contents](./README.md#0-contents)

Wyświetl plik

@ -1,7 +1,7 @@
# adjuster.py Tiny control knob (rotary potentiometer) widget
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2021 Peter Hinch
# Copyright (c) 2021-2024 Peter Hinch
from gui.core.ugui import LinearIO, display
from gui.widgets.label import Label
@ -11,17 +11,42 @@ TWOPI = 2 * math.pi
# Null function
dolittle = lambda *_: None
# *********** CONTROL KNOB CLASS ***********
# *********** ADJUSTER CLASS ***********
class Adjuster(LinearIO):
def __init__(self, writer, row, col, *,
value=0.0, fgcolor=None, bgcolor=None, color=None,
prcolor=None, callback=dolittle, args=[]):
def __init__(
self,
writer,
row,
col,
*,
value=0.0,
fgcolor=None,
bgcolor=None,
color=None,
prcolor=None,
callback=dolittle,
args=[],
min_delta=0.01,
max_delta=0.1,
):
height = writer.height # Match a user-linked Label
super().__init__(writer, row, col, height, height,
fgcolor, bgcolor, False, value,
True, prcolor)
super().__init__(
writer,
row,
col,
height,
height,
fgcolor,
bgcolor,
False,
value,
True,
prcolor,
min_delta,
max_delta,
)
super()._set_callbacks(callback, args)
radius = height / 2
self.arc = 1.5 * math.pi # Usable angle of control
@ -51,6 +76,7 @@ class Adjuster(LinearIO):
y_end = int(self.yorigin - length * math.cos(angle))
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 a version of this example with
# different visual characteristics.
@ -59,10 +85,23 @@ class Adjuster(LinearIO):
# 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(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=[]):
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=[],
min_delta=0.01,
max_delta=0.1,
):
self.map_func = map_func
self.facb = callback
@ -70,8 +109,16 @@ class FloatAdj(Adjuster):
self.fstr = fstr
self.lbl = Label(wri, row, col, lbl_width, bdcolor=color)
super().__init__(wri, row, self.lbl.mcol + 2, value=value,
fgcolor=color, callback=self.cb)
super().__init__(
wri,
row,
self.lbl.mcol + 2,
value=value,
fgcolor=color,
callback=self.cb,
min_delta=min_delta,
max_delta=max_delta,
)
if text:
self.legend = Label(wri, row, self.mcol + 2, text)
self.mcol = self.legend.mcol # For relative positioning