kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
SI value formatting
- Output string can be limmited now - if a value is outside a given range an "unprintable" string can be supplied - The new formatting is used in Marker input fieldpull/113/head
rodzic
5ac8e032b6
commit
c258043b3e
|
@ -24,14 +24,16 @@ from NanoVNASaver import SITools
|
|||
from NanoVNASaver import RFTools
|
||||
|
||||
FMT_FREQ = SITools.Format(space_str=" ")
|
||||
FMT_FREQ_INPUT = SITools.Format(
|
||||
max_nr_digits=10, printable_min=0, unprintable_under="- ")
|
||||
FMT_Q_FACTOR = SITools.Format(max_nr_digits=4, assume_infinity=False,
|
||||
min_offset=0, max_offset=0, allow_strip=True)
|
||||
FMT_GROUP_DELAY = SITools.Format(max_nr_digits=5)
|
||||
FMT_REACT = SITools.Format(max_nr_digits=5, space_str=" ", allow_strip=True)
|
||||
|
||||
|
||||
def formatFrequency(freq: float) -> str:
|
||||
return str(SITools.Value(freq, "Hz", FMT_FREQ))
|
||||
def format_frequency(freq: float, fmt=FMT_FREQ) -> str:
|
||||
return str(SITools.Value(freq, "Hz", fmt))
|
||||
|
||||
|
||||
def format_gain(val: float, invert: bool = False) -> str:
|
||||
|
@ -56,13 +58,13 @@ def format_resistance(val: float) -> str:
|
|||
return str(SITools.Value(val, "\N{OHM SIGN}", FMT_REACT))
|
||||
|
||||
|
||||
def format_capacity(val: float, allow_negative: bool=True) -> str:
|
||||
def format_capacity(val: float, allow_negative: bool = True) -> str:
|
||||
if not allow_negative and val < 0:
|
||||
return "- pF"
|
||||
return str(SITools.Value(val, "F", FMT_REACT))
|
||||
|
||||
|
||||
def format_inductance(val: float, allow_negative: bool=True) -> str:
|
||||
def format_inductance(val: float, allow_negative: bool = True) -> str:
|
||||
if not allow_negative and val < 0:
|
||||
return "- nH"
|
||||
return str(SITools.Value(val, "H", FMT_REACT))
|
||||
|
@ -88,6 +90,32 @@ def format_complex_imp(z: complex) -> str:
|
|||
return s + f"{abs(z.imag):.4g}\N{OHM SIGN}"
|
||||
|
||||
|
||||
class FrequencyInput(QtWidgets.QLineEdit):
|
||||
|
||||
def __init__(self, text=""):
|
||||
super().__init__(text)
|
||||
self.nextFrequency = -1
|
||||
self.previousFrequency = -1
|
||||
|
||||
def setText(self, text: str) -> None:
|
||||
super().setText(format_frequency(text, FMT_FREQ_INPUT))
|
||||
|
||||
def keyPressEvent(self, a0: QtGui.QKeyEvent) -> None:
|
||||
if a0.type() == QtCore.QEvent.KeyPress:
|
||||
if a0.key() == QtCore.Qt.Key_Up and self.nextFrequency != -1:
|
||||
a0.accept()
|
||||
self.setText(self.nextFrequency)
|
||||
self.textEdited.emit(self.text())
|
||||
return
|
||||
if a0.key() == QtCore.Qt.Key_Down and \
|
||||
self.previousFrequency != -1:
|
||||
a0.accept()
|
||||
self.setText(self.previousFrequency)
|
||||
self.textEdited.emit(self.text())
|
||||
return
|
||||
super().keyPressEvent(a0)
|
||||
|
||||
|
||||
class Marker(QtCore.QObject):
|
||||
name = "Marker"
|
||||
frequency = 0
|
||||
|
@ -101,33 +129,14 @@ class Marker(QtCore.QObject):
|
|||
|
||||
fieldSelection = []
|
||||
|
||||
class FrequencyInput(QtWidgets.QLineEdit):
|
||||
nextFrequency = -1
|
||||
previousFrequency = -1
|
||||
|
||||
def keyPressEvent(self, a0: QtGui.QKeyEvent) -> None:
|
||||
if a0.type() == QtCore.QEvent.KeyPress:
|
||||
if a0.key() == QtCore.Qt.Key_Up and self.nextFrequency != -1:
|
||||
a0.accept()
|
||||
self.setText(str(self.nextFrequency))
|
||||
self.textEdited.emit(self.text())
|
||||
return
|
||||
if a0.key() == QtCore.Qt.Key_Down and \
|
||||
self.previousFrequency != -1:
|
||||
a0.accept()
|
||||
self.setText(str(self.previousFrequency))
|
||||
self.textEdited.emit(self.text())
|
||||
return
|
||||
super().keyPressEvent(a0)
|
||||
|
||||
def __init__(self, name, initialColor, frequency=""):
|
||||
super().__init__()
|
||||
self.name = name
|
||||
|
||||
if frequency.isnumeric():
|
||||
self.frequency = int(frequency)
|
||||
self.frequencyInput = Marker.FrequencyInput(frequency)
|
||||
self.frequency = RFTools.RFTools.parseFrequency(frequency)
|
||||
|
||||
self.frequencyInput = FrequencyInput(
|
||||
format_frequency(self.frequency, FMT_FREQ_INPUT))
|
||||
self.frequencyInput.setAlignment(QtCore.Qt.AlignRight)
|
||||
self.frequencyInput.textEdited.connect(self.setFrequency)
|
||||
|
||||
|
@ -415,7 +424,7 @@ class Marker(QtCore.QObject):
|
|||
else:
|
||||
x_p_str = ind_p_str
|
||||
|
||||
self.frequency_label.setText(formatFrequency(s11.freq))
|
||||
self.frequency_label.setText(format_frequency(s11.freq))
|
||||
|
||||
self.impedance_label.setText(format_complex_imp(imp))
|
||||
self.series_r_label.setText(format_resistance(imp.real))
|
||||
|
|
|
@ -42,6 +42,10 @@ class Format(NamedTuple):
|
|||
max_offset: int = 8
|
||||
allow_strip: bool = False
|
||||
allways_signed: bool = False
|
||||
printable_min: float = -math.inf
|
||||
printable_max: float = math.inf
|
||||
unprintable_under: str = ""
|
||||
unprintable_over: str = ""
|
||||
parse_sloppy_unit: bool = False
|
||||
parse_sloppy_kilo: bool = False
|
||||
parse_clamp_min: float = -math.inf
|
||||
|
@ -58,6 +62,7 @@ class Value:
|
|||
assert 3 <= fmt.max_nr_digits <= 30
|
||||
assert -8 <= fmt.min_offset <= fmt.max_offset <= 8
|
||||
assert fmt.parse_clamp_min < fmt.parse_clamp_max
|
||||
assert fmt.printable_min < fmt.printable_max
|
||||
self._unit = unit
|
||||
self.fmt = fmt
|
||||
if isinstance(value, str):
|
||||
|
@ -76,6 +81,9 @@ class Value:
|
|||
abs(self._value) >= 10 ** ((fmt.max_offset + 1) * 3):
|
||||
return (("-" if self._value < 0 else "") +
|
||||
"\N{INFINITY}" + fmt.space_str + self._unit)
|
||||
if (self._value < fmt.printable_min or
|
||||
self._value > fmt.printable_max):
|
||||
return fmt.unprintable_str + self._unit
|
||||
|
||||
if self._value == 0:
|
||||
offset = 0
|
||||
|
|
Ładowanie…
Reference in New Issue