add more inputs, bind to actual data

This commit is contained in:
Jakob Ketterl
2021-02-19 18:18:25 +01:00
parent 27c16c3720
commit 039b57d28b
10 changed files with 76 additions and 19 deletions

View File

@ -139,8 +139,8 @@ class TextAreaInput(Input):
class CheckboxInput(Input):
def __init__(self, id, label, checkboxText, infotext=None):
super().__init__(id, label, infotext=infotext)
def __init__(self, id, label, checkboxText, infotext=None, converter: Converter = None):
super().__init__(id, label, infotext=infotext, converter=converter)
self.checkboxText = checkboxText
def render_input(self, value):
@ -162,7 +162,7 @@ class CheckboxInput(Input):
return " ".join(["form-check", "form-control-sm"])
def parse(self, data):
return {self.id: self.id in data and data[self.id][0] == "on"}
return {self.id: self.converter.convert_from_form(self.id in data and data[self.id][0] == "on")}
class Option(object):

View File

@ -22,15 +22,21 @@ class NullConverter(Converter):
class OptionalConverter(Converter):
"""
Maps None to an empty string, and reverse
useful for optional fields
Transforms a special form value to None
The default is look for an empty string, but this can be used to adopt to other types.
If the default is not found, the actual value is passed to the sub_converter for further transformation.
useful for optional fields since None is not stored in the configuration
"""
def __init__(self, sub_converter: Converter = None, defaultFormValue=""):
self.sub_converter = NullConverter() if sub_converter is None else sub_converter
self.defaultFormValue = defaultFormValue
def convert_to_form(self, value):
return "" if value is None else value
return self.defaultFormValue if value is None else self.sub_converter.convert_to_form(value)
def convert_from_form(self, value):
return value if value else None
return None if value == self.defaultFormValue else self.sub_converter.convert_to_form(value)
class IntConverter(Converter):

13
owrx/form/soapy.py Normal file
View File

@ -0,0 +1,13 @@
from owrx.form import FloatInput
class SoapyGainInput(FloatInput):
def __init__(self, id, label, gain_stages):
super().__init__(id, label)
self.gain_stages = gain_stages
def render_input(self, value):
if not self.gain_stages:
return super().render_input(value)
# TODO implement input for multiple gain stages here
return "soapy gain stages here..."