Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent use of locale #230

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
29 changes: 18 additions & 11 deletions pydm/widgets/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 1 addition & 8 deletions pydm/widgets/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion pydm/widgets/line_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions pydm/widgets/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -392,6 +392,10 @@ def __init__(self, parent=None, init_channel=None):
self.lower_label.setText('<min>')
self.upper_label.setText('<max>')

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)
Expand All @@ -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):
"""
Expand Down Expand Up @@ -996,4 +1000,4 @@ def originAtZero(self, checked):
----------
checked : bool
"""
self.scale_indicator.set_origin_at_zero(checked)
self.scale_indicator.set_origin_at_zero(checked)
20 changes: 8 additions & 12 deletions pydm/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions pydm/widgets/spinbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down