diff --git a/pydm/widgets/base.py b/pydm/widgets/base.py index 1ee64002e..62f86a712 100644 --- a/pydm/widgets/base.py +++ b/pydm/widgets/base.py @@ -1,4 +1,5 @@ import functools +from locale import format_string as locale_format import numpy as np from ..PyQt.QtGui import QApplication, QColor, QCursor, QMenu from ..PyQt.QtCore import Qt, QEvent, pyqtSignal, pyqtSlot, pyqtProperty @@ -190,7 +191,6 @@ def __init__(self, init_channel=None): self._lower_ctrl_limit = None self.enum_strings = None - self.format_string = "{}" self.value = None self.channeltype = None @@ -210,7 +210,7 @@ def context_menu(self): Generates the custom context menu, and populates it with any external tools that have been loaded. PyDMWidget subclasses should override this method (after calling superclass implementation) to add the menu. - + Returns ------- QMenu @@ -676,21 +676,28 @@ def channel(self, value): def update_format_string(self): """ - Reconstruct the format string to be used when representing the + Indicate a PV property (value, unit, precision) or PyDMWidget + property () has changed. + """ + pass + + def get_formatted_string(self, value): + """Return the formatted string to be used when representing the output value. Returns ------- - format_string : str - The format string to be used including or not the precision - and unit + string : str + The formated string to be used as the output value """ - self.format_string = "{}" - if isinstance(self.value, (int, float)): - self.format_string = "{:." + str(self._prec) + "f}" + if isinstance(value, float): + string = locale_format('%.' + str(self._prec) + 'f', value) + else: + string = '{}'.format(value) + if self._show_units and self._unit != "": - self.format_string += " {}".format(self._unit) - return self.format_string + string += " {}".format(self._unit) + return string def restore_original_tooltip(self): if self._tooltip is None: diff --git a/pydm/widgets/label.py b/pydm/widgets/label.py index 8f2681b54..8db8c4b08 100644 --- a/pydm/widgets/label.py +++ b/pydm/widgets/label.py @@ -70,11 +70,4 @@ def value_changed(self, new_value): except IndexError: self.setText("**INVALID**") return - # If the value is a number (float or int), display it using a - # format string if necessary. - if isinstance(new_value, (int, float)): - self.setText(self.format_string.format(new_value)) - return - # If you made it this far, just turn whatever the heck the value - # is into a string and display it. - self.setText(str(new_value)) + self.setText(self.get_formatted_string(new_value)) diff --git a/pydm/widgets/line_edit.py b/pydm/widgets/line_edit.py index 6871c7fe8..4166c7865 100644 --- a/pydm/widgets/line_edit.py +++ b/pydm/widgets/line_edit.py @@ -241,7 +241,7 @@ def set_display(self): if self._display_format_type == DisplayFormat.Default: if isinstance(new_value, (int, float)): - self._display = str(self.format_string.format(new_value)) + self._display = self.get_formatted_string(new_value) self.setText(self._display) return diff --git a/pydm/widgets/scale.py b/pydm/widgets/scale.py index 7a9280ee0..06429aa24 100644 --- a/pydm/widgets/scale.py +++ b/pydm/widgets/scale.py @@ -345,7 +345,7 @@ def set_num_divisions(self, divisions): if isinstance(divisions, int) and divisions > 0 and self._num_divisions != divisions: self._num_divisions = divisions self.repaint() - + def get_scale_height(self): return self._scale_height @@ -392,6 +392,10 @@ def __init__(self, parent=None, init_channel=None): self.lower_label.setText('') self.upper_label.setText('') + self.value_label.setAlignment(Qt.AlignCenter) + self.lower_label.setAlignment(Qt.AlignLeft) + self.upper_label.setAlignment(Qt.AlignRight) + self._value_position = Qt.TopEdge self.value_label.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) @@ -403,7 +407,7 @@ def update_labels(self): """ self.lower_label.setText(str(self.scale_indicator._lower_limit)) self.upper_label.setText(str(self.scale_indicator._upper_limit)) - self.value_label.setText(self.format_string.format(self.scale_indicator._value)) + self.value_label.setText(self.get_formatted_string(self.scale_indicator._value)) def value_changed(self, new_value): """ @@ -996,4 +1000,4 @@ def originAtZero(self, checked): ---------- checked : bool """ - self.scale_indicator.set_origin_at_zero(checked) \ No newline at end of file + self.scale_indicator.set_origin_at_zero(checked) diff --git a/pydm/widgets/slider.py b/pydm/widgets/slider.py index 7ff2f67d8..4faaf22e6 100644 --- a/pydm/widgets/slider.py +++ b/pydm/widgets/slider.py @@ -140,15 +140,15 @@ def update_labels(self): if self.minimum is None: self.low_lim_label.setText("") else: - self.low_lim_label.setText(self.format_string.format(self.minimum)) + self.low_lim_label.setText(self.get_formatted_string(self.minimum)) if self.maximum is None: self.high_lim_label.setText("") else: - self.high_lim_label.setText(self.format_string.format(self.maximum)) + self.high_lim_label.setText(self.get_formatted_string(self.maximum)) if self.value is None: self.value_label.setText("") else: - self.value_label.setText(self.format_string.format(self.value)) + self.value_label.setText(self.get_formatted_string(self.value)) def reset_slider_limits(self): """ @@ -218,7 +218,7 @@ def value_changed(self, new_val): """ PyDMWritableWidget.value_changed(self, new_val) if hasattr(self, "value_label"): - self.value_label.setText(self.format_string.format(self.value)) + self.value_label.setText(self.get_formatted_string(self.value)) if not self._slider.isSliderDown(): self.set_slider_to_closest_value(self.value) @@ -263,15 +263,11 @@ def ctrl_limit_changed(self, which, new_limit): def update_format_string(self): """ - Reconstruct the format string to be used when representing the - output value. - - Returns - ------- - format_string : str - The format string to be used including or not the precision - and unit + Indicate a PV property (value, unit, precision) or PyDMWidget + property (precision, showUnits, step_size, showStepExponent) + has changed. """ + fs = PyDMWritableWidget.update_format_string(self) self.update_labels() return fs diff --git a/pydm/widgets/spinbox.py b/pydm/widgets/spinbox.py index da5446045..6f89149fd 100644 --- a/pydm/widgets/spinbox.py +++ b/pydm/widgets/spinbox.py @@ -71,14 +71,9 @@ def update_step_size(self): def update_format_string(self): """ - Reconstruct the format string to be used when representing the - output value. - - Returns - ------- - format_string : str - The format string to be used including or not the precision - and unit + Indicate a PV property (value, unit, precision) or PyDMWidget + property (precision, showUnits, step_size, showStepExponent) + has changed. """ if self._show_units: units = " {}".format(self._unit)