diff options
Diffstat (limited to 'grc/src/platforms')
-rw-r--r-- | grc/src/platforms/base/Param.py | 10 | ||||
-rw-r--r-- | grc/src/platforms/gui/Param.py | 45 | ||||
-rw-r--r-- | grc/src/platforms/python/Param.py | 49 |
3 files changed, 65 insertions, 39 deletions
diff --git a/grc/src/platforms/base/Param.py b/grc/src/platforms/base/Param.py index 6f512cb87..ebc9977fc 100644 --- a/grc/src/platforms/base/Param.py +++ b/grc/src/platforms/base/Param.py @@ -228,6 +228,16 @@ class Param(Element): def get_type(self): return self.get_parent().resolve_dependencies(self._type) def is_enum(self): return bool(self.get_options()) + def __repr__(self): + """ + Get the repr (nice string format) for this param. + Just return the value (special case enum). + Derived classes can handle complex formatting. + @return the string representation + """ + if self.is_enum(): return self.get_option(self.get_value()).get_name() + return self.get_value() + def get_input_class(self): """ Get the graphical gtk class to represent this parameter. diff --git a/grc/src/platforms/gui/Param.py b/grc/src/platforms/gui/Param.py index 7bc0d354f..2afe18c57 100644 --- a/grc/src/platforms/gui/Param.py +++ b/grc/src/platforms/gui/Param.py @@ -85,48 +85,15 @@ class Param(Element): def get_markup(self): """ - Create a markup to display the Param as a label on the SignalBlock. - If the data type is an Enum type, use the cname of the Enum's current choice. - Otherwise, use parsed the data type and use its string representation. - If the data type is not valid, use a red foreground color. + Create a markup to display the param as a label on the block. + If the param is valid, use the param's repr representation. + Otherwise, create a markup for error. @return pango markup string """ - ########################################################################### - # display logic for numbers - ########################################################################### - def float_to_str(var): - if var-int(var) == 0: return '%d'%int(var) - if var*10-int(var*10) == 0: return '%.1f'%var - if var*100-int(var*100) == 0: return '%.2f'%var - if var*1000-int(var*1000) == 0: return '%.3f'%var - else: return '%.3g'%var - def to_str(var): - if isinstance(var, str): return var - elif isinstance(var, complex): - if var == 0: return '0' #value is zero - elif var.imag == 0: return '%s'%float_to_str(var.real) #value is real - elif var.real == 0: return '%sj'%float_to_str(var.imag) #value is imaginary - elif var.imag < 0: return '%s-%sj'%(float_to_str(var.real), float_to_str(abs(var.imag))) - else: return '%s+%sj'%(float_to_str(var.real), float_to_str(var.imag)) - elif isinstance(var, float): return float_to_str(var) - elif isinstance(var, int): return '%d'%var - else: return str(var) - ########################################################################### if self.is_valid(): - data = self.evaluate() - t = self.get_type() - if self.is_enum(): - dt_str = self.get_option(self.get_value()).get_name() - elif isinstance(data, (list, tuple, set)): #vector types - if len(data) > 8: dt_str = self.get_value() #large vectors use code - else: dt_str = ', '.join(map(to_str, data)) #small vectors use eval - else: dt_str = to_str(data) #other types - #truncate - max_len = max(27 - len(self.get_name()), 3) - if len(dt_str) > max_len: - dt_str = dt_str[:max_len/2 -3] + '...' + dt_str[-max_len/2:] - return '<b>%s:</b> %s'%(Utils.xml_encode(self.get_name()), Utils.xml_encode(dt_str)) - else: return '<span foreground="red"><b>%s:</b> error</span>'%Utils.xml_encode(self.get_name()) + return '<b>%s:</b> %s'%(Utils.xml_encode(self.get_name()), Utils.xml_encode(repr(self))) + else: + return '<span foreground="red"><b>%s:</b> error</span>'%Utils.xml_encode(self.get_name()) def get_layout(self): """ diff --git a/grc/src/platforms/python/Param.py b/grc/src/platforms/python/Param.py index 42406788f..f830342a8 100644 --- a/grc/src/platforms/python/Param.py +++ b/grc/src/platforms/python/Param.py @@ -25,6 +25,7 @@ import os import pygtk pygtk.require('2.0') import gtk +from gnuradio import eng_notation class FileParam(EntryParam): """Provide an entry box for filename and a button to browse for a file.""" @@ -88,6 +89,54 @@ class Param(_Param): 'grid_pos', 'import', ] + def __repr__(self): + """ + Get the repr (nice string format) for this param. + @return the string representation + """ + if self.is_enum(): return _Param.__repr__(self) + ################################################## + # display logic for numbers + ################################################## + def to_str(num): + if isinstance(num, str): return num + elif isinstance(num, complex): + if num == 0: return '0' #value is zero + elif num.imag == 0: return '%s'%eng_notation.num_to_str(num.real) #value is real + elif num.real == 0: return '%sj'%eng_notation.num_to_str(num.imag) #value is imaginary + elif num.imag < 0: return '%s-%sj'%(eng_notation.num_to_str(num.real), eng_notation.num_to_str(abs(num.imag))) + else: return '%s+%sj'%(eng_notation.num_to_str(num.real), eng_notation.num_to_str(num.imag)) + elif isinstance(num, (float, int)): return eng_notation.num_to_str(num) + else: return str(var) + ################################################## + # split up formatting by type + ################################################## + truncate = 0 #default center truncate + max_len = max(27 - len(self.get_name()), 3) + e = self.evaluate() + t = self.get_type() + if t in ('int', 'real', 'complex'): dt_str = to_str(e) + elif isinstance(e, (list, tuple, set, numpy.ndarray)): #vector types + if len(e) > 8: + dt_str = self.get_value() #large vectors use code + truncate = 1 + else: dt_str = ', '.join(map(to_str, e)) #small vectors use eval + elif t in ('file_open', 'file_save'): + dt_str = self.get_value() + truncate = -1 + else: dt_str = to_str(e) #other types + ################################################## + # truncate + ################################################## + if len(dt_str) > max_len: + if truncate < 0: #front truncate + dt_str = '...' + dt_str[3-max_len:] + elif truncate == 0: #center truncate + dt_str = dt_str[:max_len/2 -3] + '...' + dt_str[-max_len/2:] + elif truncate > 0: #rear truncate + dt_str = dt_str[:max_len-3] + '...' + return dt_str + def get_input_class(self): if self.get_type() in ('file_open', 'file_save'): return FileParam return _Param.get_input_class(self) |