1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
from time import perf_counter
from .. import functions as fn
from ..Qt import QtWidgets
__all__ = ['ValueLabel']
class ValueLabel(QtWidgets.QLabel):
"""
QLabel specifically for displaying numerical values.
Extends QLabel adding some extra functionality:
- displaying units with si prefix
- built-in exponential averaging
"""
def __init__(self, parent=None, suffix='', siPrefix=False, averageTime=0, formatStr=None, suffixPower=1):
"""
============== ==================================================================================
**Arguments:**
suffix (str or None) The suffix to place after the value
siPrefix (bool) Whether to add an SI prefix to the units and display a scaled value
averageTime (float) The length of time in seconds to average values. If this value
is 0, then no averaging is performed. As this value increases
the display value will appear to change more slowly and smoothly.
formatStr (str) Optionally, provide a format string to use when displaying text. The text
will be generated by calling formatStr.format(value=, avgValue=, suffix=)
(see Python documentation on str.format)
This option is not compatible with siPrefix
suffixPower (int/float) The power to which the suffix is raised. For example, if suffix='m²',
the suffixPower should be 2. This ensures correct scaling when using SI prefixes.
Supports positive, negative and non-integral powers. Ignored if siPrefix is False.
Note: The power only affects the scaling, not the suffix itself. For example, with
suffix='m' and suffixPower=2, the displayed suffix will still be 'm'.
============== ==================================================================================
"""
QtWidgets.QLabel.__init__(self, parent)
self.values = []
self.averageTime = averageTime ## no averaging by default
self.suffix = suffix
self.siPrefix = siPrefix
if formatStr is None:
formatStr = '{avgValue:0.2g} {suffix}'
self.formatStr = formatStr
self.suffixPower = suffixPower
def setValue(self, value):
now = perf_counter()
self.values.append((now, value))
cutoff = now - self.averageTime
while len(self.values) > 0 and self.values[0][0] < cutoff:
self.values.pop(0)
self.update()
def setFormatStr(self, text):
self.formatStr = text
self.update()
def setAverageTime(self, t):
self.averageTime = t
def averageValue(self):
return sum(v[1] for v in self.values) / float(len(self.values))
def paintEvent(self, ev):
self.setText(self.generateText())
return super().paintEvent(ev)
def generateText(self):
if len(self.values) == 0:
return ''
avg = self.averageValue()
val = self.values[-1][1]
if self.siPrefix:
return fn.siFormat(avg, suffix=self.suffix, power=self.suffixPower)
else:
return self.formatStr.format(value=val, avgValue=avg, suffix=self.suffix)
|