From e5ed3446e0b6364e76114bd186c265ced7701421 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Tue, 30 Jan 2018 10:46:55 -0200 Subject: [PATCH 1/9] Changed line_edit set_display behviour to replace the decimal separator `.` to the locale one when the value being shown is a float --- pydm/widgets/line_edit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydm/widgets/line_edit.py b/pydm/widgets/line_edit.py index 6871c7fe8..e7b3f6ec2 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 = str(self.format_string.format(new_value)).replace('.', locale.localeconv()['decimal_point']) self.setText(self._display) return From d33ebf5e520a56d15b21359714a4e9e121b01a25 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Tue, 30 Jan 2018 10:47:22 -0200 Subject: [PATCH 2/9] Changed PyDMLabel set_display behaviour to replace the decimal separator `.` by the locale one when the value being shown is a float --- pydm/widgets/label.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pydm/widgets/label.py b/pydm/widgets/label.py index 8f2681b54..2e767e07b 100644 --- a/pydm/widgets/label.py +++ b/pydm/widgets/label.py @@ -1,3 +1,4 @@ +import locale from .base import PyDMWidget from ..PyQt.QtGui import QLabel, QApplication from ..PyQt.QtCore import Qt, pyqtProperty, Q_ENUMS @@ -73,7 +74,7 @@ def value_changed(self, new_value): # 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)) + self.setText(self.format_string.format(new_value).replace('.', locale.localeconv()['decimal_point'])) return # If you made it this far, just turn whatever the heck the value # is into a string and display it. From 871e1a6fcd65191d42718308aee8361682179b0c Mon Sep 17 00:00:00 2001 From: Guilherme Date: Wed, 31 Jan 2018 09:40:18 -0200 Subject: [PATCH 3/9] Altered PyDMWidget update_format_string method to use the 'n' specifier when the value is an int or float --- pydm/widgets/base.py | 5 +++-- pydm/widgets/label.py | 2 +- pydm/widgets/line_edit.py | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pydm/widgets/base.py b/pydm/widgets/base.py index 300e443a2..379a05b48 100644 --- a/pydm/widgets/base.py +++ b/pydm/widgets/base.py @@ -196,7 +196,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 @@ -673,7 +673,8 @@ def update_format_string(self): """ self.format_string = "{}" if isinstance(self.value, (int, float)): - self.format_string = "{:." + str(self._prec) + "f}" + number_len = str(len(str(int(self.value))) + self._prec) + self.format_string = "{:." + number_len + "n}" if self._show_units and self._unit != "": self.format_string += " {}".format(self._unit) return self.format_string diff --git a/pydm/widgets/label.py b/pydm/widgets/label.py index 2e767e07b..2d32609da 100644 --- a/pydm/widgets/label.py +++ b/pydm/widgets/label.py @@ -74,7 +74,7 @@ def value_changed(self, new_value): # 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).replace('.', locale.localeconv()['decimal_point'])) + 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. diff --git a/pydm/widgets/line_edit.py b/pydm/widgets/line_edit.py index e7b3f6ec2..3a2db82d6 100644 --- a/pydm/widgets/line_edit.py +++ b/pydm/widgets/line_edit.py @@ -241,7 +241,8 @@ 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)).replace('.', locale.localeconv()['decimal_point']) + print(self.format_string) + self._display = str(self.format_string.format(new_value)) self.setText(self._display) return From 436462849106e8dd375c69ae18ee75f2d55b9aab Mon Sep 17 00:00:00 2001 From: Guilherme Date: Wed, 31 Jan 2018 09:56:36 -0200 Subject: [PATCH 4/9] Changed PyDMWidget updated_format_string to handle negative values --- pydm/widgets/base.py | 2 +- pydm/widgets/line_edit.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pydm/widgets/base.py b/pydm/widgets/base.py index 379a05b48..ba1159d35 100644 --- a/pydm/widgets/base.py +++ b/pydm/widgets/base.py @@ -673,7 +673,7 @@ def update_format_string(self): """ self.format_string = "{}" if isinstance(self.value, (int, float)): - number_len = str(len(str(int(self.value))) + self._prec) + number_len = str(len(str(int(abs(self.value)))) + self._prec) self.format_string = "{:." + number_len + "n}" if self._show_units and self._unit != "": self.format_string += " {}".format(self._unit) diff --git a/pydm/widgets/line_edit.py b/pydm/widgets/line_edit.py index 3a2db82d6..6871c7fe8 100644 --- a/pydm/widgets/line_edit.py +++ b/pydm/widgets/line_edit.py @@ -241,7 +241,6 @@ def set_display(self): if self._display_format_type == DisplayFormat.Default: if isinstance(new_value, (int, float)): - print(self.format_string) self._display = str(self.format_string.format(new_value)) self.setText(self._display) return From f10cc90c4d805d2cb86b7c1a17067be03e510f2b Mon Sep 17 00:00:00 2001 From: Guilherme Date: Wed, 31 Jan 2018 10:00:02 -0200 Subject: [PATCH 5/9] Removing unused locale library from label.py --- pydm/widgets/label.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pydm/widgets/label.py b/pydm/widgets/label.py index 2d32609da..8f2681b54 100644 --- a/pydm/widgets/label.py +++ b/pydm/widgets/label.py @@ -1,4 +1,3 @@ -import locale from .base import PyDMWidget from ..PyQt.QtGui import QLabel, QApplication from ..PyQt.QtCore import Qt, pyqtProperty, Q_ENUMS From 634795344926682f51d4a25d90817311054aec98 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Thu, 8 Mar 2018 10:50:50 -0300 Subject: [PATCH 6/9] Implemented get_formatted_string --- pydm/widgets/base.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pydm/widgets/base.py b/pydm/widgets/base.py index ba1159d35..52619fd7f 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 @@ -176,7 +177,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 @@ -662,22 +662,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 - """ - self.format_string = "{}" - if isinstance(self.value, (int, float)): - number_len = str(len(str(int(abs(self.value)))) + self._prec) - self.format_string = "{:." + number_len + "n}" + string : str + The formated string to be used as the output value + """ + 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: From 7b8d36150d884aa9909edb953f03c8c43092cbf5 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Thu, 8 Mar 2018 10:51:21 -0300 Subject: [PATCH 7/9] Change format_string.format by get_formatted_string method --- pydm/widgets/label.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) 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)) From f83b12ca3ebf689527fbd882a097fbf248a8cbe1 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Thu, 8 Mar 2018 10:51:54 -0300 Subject: [PATCH 8/9] Changed format_string by the get_formatted_string method in line_edit --- pydm/widgets/line_edit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 385bac9ee9b7a668d9fde764adea43e4b16a4d8a Mon Sep 17 00:00:00 2001 From: Guilherme Date: Thu, 8 Mar 2018 10:53:53 -0300 Subject: [PATCH 9/9] Changed format_string to get_formatted_string method in the scale and silder widgets --- pydm/widgets/scale.py | 7 +++---- pydm/widgets/slider.py | 20 ++++++++------------ pydm/widgets/spinbox.py | 11 +++-------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/pydm/widgets/scale.py b/pydm/widgets/scale.py index 75b4b54da..0b1aa1219 100644 --- a/pydm/widgets/scale.py +++ b/pydm/widgets/scale.py @@ -145,7 +145,7 @@ def setNumDivisions(self, divisions): if isinstance(divisions, int) and divisions > 0 and self._num_divisions != divisions: self._num_divisions = divisions self.repaint() - + class PyDMScaleIndicator(QFrame, PyDMWidget): def __init__(self, parent=None, init_channel=None): @@ -162,7 +162,7 @@ def __init__(self, parent=None, init_channel=None): self.value_label.setText('') 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) @@ -172,7 +172,7 @@ def __init__(self, parent=None, init_channel=None): def updateAll(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): super(PyDMScaleIndicator, self).value_changed(new_value) @@ -282,4 +282,3 @@ def numDivisions(self): @numDivisions.setter def numDivisions(self, divisions): self.scale_indicator.setNumDivisions(divisions) - 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)