openwebrx-clone/owrx/form/input/wsjt.py

94 lines
3.3 KiB
Python
Raw Permalink Normal View History

2021-04-29 13:17:21 +00:00
from owrx.form.input import Input
from owrx.form.input.converter import JsonConverter
2021-02-08 19:30:12 +00:00
from owrx.wsjt import Q65Mode, Q65Interval
2021-02-15 21:14:56 +00:00
from owrx.modes import Modes, WsjtMode
2021-02-15 19:19:43 +00:00
import html
2021-02-08 19:30:12 +00:00
class Q65ModeMatrix(Input):
def checkbox_id(self, mode, interval):
return "{0}-{1}-{2}".format(self.id, mode.value, interval.value)
def render_checkbox(self, mode: Q65Mode, interval: Q65Interval, value, errors):
2021-02-08 19:30:12 +00:00
return """
<div class="{classes}">
<input class="form-check-input" type="checkbox" id="{id}" name="{id}" {checked} {disabled}>
<label class="form-check-label" for="{id}">
{checkboxText}
</label>
</div>
""".format(
classes=self.input_classes(errors),
2021-02-08 19:30:12 +00:00
id=self.checkbox_id(mode, interval),
checked="checked" if "{}{}".format(mode.name, interval.value) in value else "",
checkboxText="Mode {} interval {}s".format(mode.name, interval.value),
2021-02-22 22:49:28 +00:00
disabled="" if interval.is_available(mode) and not self.disabled else "disabled",
2021-02-08 19:30:12 +00:00
)
2021-03-25 14:02:59 +00:00
def render_input_group(self, value, errors):
2021-02-08 19:30:12 +00:00
return """
<div class="matrix q65-matrix">
{checkboxes}
2021-03-25 14:02:59 +00:00
{errors}
2021-02-08 19:30:12 +00:00
</div>
""".format(
2021-03-25 14:02:59 +00:00
checkboxes=self.render_input(value, errors),
errors=self.render_errors(errors),
)
def render_input(self, value, errors):
return "".join(
self.render_checkbox(mode, interval, value, errors) for interval in Q65Interval for mode in Q65Mode
2021-02-08 19:30:12 +00:00
)
def input_classes(self, error):
classes = ["form-check", "form-control-sm"]
if error:
classes.append("is-invalid")
return " ".join(classes)
2021-02-08 19:30:12 +00:00
def parse(self, data):
def in_response(mode, interval):
boxid = self.checkbox_id(mode, interval)
return boxid in data and data[boxid][0] == "on"
return {
self.id: [
"{}{}".format(mode.name, interval.value)
for interval in Q65Interval
for mode in Q65Mode
if in_response(mode, interval)
],
}
2021-02-15 19:19:43 +00:00
class WsjtDecodingDepthsInput(Input):
2021-02-15 21:22:07 +00:00
def defaultConverter(self):
return JsonConverter()
def render_input(self, value, errors):
2021-02-15 21:14:56 +00:00
def render_mode(m):
return """
<option value={mode}>{name}</option>
""".format(
mode=m.modulation,
name=m.name,
)
2021-02-15 19:19:43 +00:00
return """
2021-02-22 22:49:28 +00:00
<input type="hidden" class="{classes}" id="{id}" name="{id}" value="{value}" {disabled}>
2021-02-15 21:14:56 +00:00
<div class="inputs" style="display:none;">
<select class="form-control form-control-sm">{options}</select>
<input class="form-control form-control-sm" type="number" step="1">
</div>
2021-02-15 19:19:43 +00:00
""".format(
id=self.id,
classes=self.input_classes(errors),
2021-02-15 21:22:07 +00:00
value=html.escape(value),
2021-02-15 21:14:56 +00:00
options="".join(render_mode(m) for m in Modes.getAvailableModes() if isinstance(m, WsjtMode)),
2021-02-22 22:49:28 +00:00
disabled="disabled" if self.disabled else ""
2021-02-15 19:19:43 +00:00
)
def input_classes(self, error):
return super().input_classes(error) + " wsjt-decoding-depths"