Skip to content

Commit

Permalink
ENH: explicitly set locale to 'en_US' in QDoubleValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgav committed Jul 25, 2024
1 parent 02dbfa1 commit 7523a79
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 27 deletions.
7 changes: 3 additions & 4 deletions pyxrf/gui_module/dlg_edit_user_peak_parameters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging

from qtpy.QtGui import QDoubleValidator
from qtpy.QtWidgets import QDialog, QDialogButtonBox, QGridLayout, QLabel, QVBoxLayout

from .useful_widgets import LineEditExtended, LineEditReadOnly, set_tooltip
from .useful_widgets import DoubleValidator, LineEditExtended, LineEditReadOnly, set_tooltip

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -31,7 +30,7 @@ def __init__(self, parent=None):
self.le_fwhm = LineEditExtended()
set_tooltip(self.le_fwhm, "<b>FWHM</b> (in keV) of the user-defined peak.")

self._validator = QDoubleValidator()
self._validator = DoubleValidator()

vbox = QVBoxLayout()

Expand Down Expand Up @@ -99,7 +98,7 @@ def _validate_le(self, le_widget, text, condition):
if text is None:
text = le_widget.text()
valid = True
if self._validator.validate(text, 0)[0] != QDoubleValidator.Acceptable:
if self._validator.validate(text, 0)[0] != DoubleValidator.Acceptable:
valid = False
elif not condition(float(text)):
valid = False
Expand Down
7 changes: 3 additions & 4 deletions pyxrf/gui_module/dlg_find_elements.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import copy

from qtpy.QtGui import QDoubleValidator
from qtpy.QtWidgets import QDialog, QDialogButtonBox, QGridLayout, QGroupBox, QLabel, QPushButton, QVBoxLayout

from .useful_widgets import LineEditExtended, set_tooltip
from .useful_widgets import DoubleValidator, LineEditExtended, set_tooltip


class DialogFindElements(QDialog):
Expand All @@ -19,7 +18,7 @@ def __init__(self, parent=None):
# then simply save the changed parameter values.
self.find_elements_requested = False

self.validator = QDoubleValidator()
self.validator = DoubleValidator()

self.le_e_calib_a0 = LineEditExtended()
self.le_e_calib_a0.setValidator(self.validator)
Expand Down Expand Up @@ -218,7 +217,7 @@ def le_range_high_editing_finished(self):
self._read_le_value(self.le_range_high, self._dialog_data["energy_bound_high"])

def _validate_as_float(self, text):
return self.validator.validate(text, 0)[0] == QDoubleValidator.Acceptable
return self.validator.validate(text, 0)[0] == DoubleValidator.Acceptable

def _validate_text(self, line_edit, text):
line_edit.setValid(self._validate_as_float(text))
Expand Down
6 changes: 3 additions & 3 deletions pyxrf/gui_module/dlg_pileup_peak_parameters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging

from qtpy.QtCore import QRegExp
from qtpy.QtGui import QDoubleValidator, QRegExpValidator
from qtpy.QtGui import QRegExpValidator
from qtpy.QtWidgets import QDialog, QDialogButtonBox, QGridLayout, QLabel, QVBoxLayout

from .useful_widgets import LineEditExtended, LineEditReadOnly, set_tooltip
from .useful_widgets import DoubleValidator, LineEditExtended, LineEditReadOnly, set_tooltip

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -117,7 +117,7 @@ def _validate_eline(self, le_widget, text):
text = le_widget.text()

valid = True
if self._validator_eline.validate(text, 0)[0] != QDoubleValidator.Acceptable:
if self._validator_eline.validate(text, 0)[0] != DoubleValidator.Acceptable:
valid = False
else:
# Try to compute energy for the pileup peak
Expand Down
6 changes: 3 additions & 3 deletions pyxrf/gui_module/dlg_save_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os

from qtpy.QtCore import Qt
from qtpy.QtGui import QDoubleValidator
from qtpy.QtWidgets import (
QCheckBox,
QDialog,
Expand All @@ -15,6 +14,7 @@
)

from .useful_widgets import (
DoubleValidator,
DoubleValidatorStrict,
LineEditExtended,
LineEditReadOnly,
Expand Down Expand Up @@ -176,13 +176,13 @@ def pb_file_path_clicked(self):
self.file_path = file_path

def le_distance_to_sample_text_changed(self, text):
valid = self._le_distance_to_sample_validator.validate(text, 0)[0] == QDoubleValidator.Acceptable
valid = self._le_distance_to_sample_validator.validate(text, 0)[0] == DoubleValidator.Acceptable
self.le_distance_to_sample.setValid(valid)
self.pb_ok.setEnabled(valid)

def le_distance_to_sample_editing_finished(self):
text = self.le_distance_to_sample.text()
if self._le_distance_to_sample_validator.validate(text, 0)[0] == QDoubleValidator.Acceptable:
if self._le_distance_to_sample_validator.validate(text, 0)[0] == DoubleValidator.Acceptable:
self.__distance_to_sample = float(text)
self._show_distance_to_sample()
self._show_preview() # Show/hide warning on zero distance-to-sample value
Expand Down
5 changes: 3 additions & 2 deletions pyxrf/gui_module/tests/test_useful_widgets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import numpy.testing as npt
import pytest
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QDoubleValidator
from PyQt5.QtWidgets import QLineEdit

from pyxrf.gui_module.useful_widgets import (
DoubleValidator,
DoubleValidatorRelaxed,
IntValidatorRelaxed,
IntValidatorStrict,
Expand Down Expand Up @@ -126,7 +126,8 @@ def fn():
])
# fmt: on
def test_DoubleValidatorStrict(text, range, result):
validator = QDoubleValidator()
validator = DoubleValidator()

if range is not None:
validator.setRange(range[0], range[1], 5)
assert validator.validate(text, 0)[0] == result, "Validation failed"
Expand Down
15 changes: 13 additions & 2 deletions pyxrf/gui_module/useful_widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from qtpy.QtCore import Qt, Signal, Slot
from qtpy.QtCore import QLocale, Qt, Signal, Slot
from qtpy.QtGui import QColor, QDoubleValidator, QFontMetrics, QIntValidator, QPalette
from qtpy.QtWidgets import (
QCheckBox,
Expand Down Expand Up @@ -423,7 +423,18 @@ def validate(self, text, pos):
return result


class DoubleValidatorStrict(QDoubleValidator):
class DoubleValidator(QDoubleValidator):
"""
`DoubleValidator` is identical to QDoubleValidator except that it
is always using 'en_US' locale.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setLocale(QLocale("en_US"))


class DoubleValidatorStrict(DoubleValidator):
"""
`DoubleValidatorStrict` verifies additional condition: double number can not
contain commas, since it can't be converted to floating point number directly.
Expand Down
6 changes: 3 additions & 3 deletions pyxrf/gui_module/wnd_load_quant_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import textwrap

from qtpy.QtCore import Qt, Signal, Slot
from qtpy.QtGui import QBrush, QColor, QDoubleValidator
from qtpy.QtGui import QBrush, QColor
from qtpy.QtWidgets import (
QButtonGroup,
QCheckBox,
Expand All @@ -25,7 +25,7 @@
)

from .dlg_view_calib_standard import DialogViewCalibStandard
from .useful_widgets import LineEditExtended, SecondaryWindow, get_background_css, set_tooltip
from .useful_widgets import DoubleValidator, LineEditExtended, SecondaryWindow, get_background_css, set_tooltip

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -67,7 +67,7 @@ def initialize(self):

self._distance_to_sample = 0.0
self.le_distance_to_sample = LineEditExtended()
le_dist_validator = QDoubleValidator()
le_dist_validator = DoubleValidator()
le_dist_validator.setBottom(0)
self.le_distance_to_sample.setValidator(le_dist_validator)
self._set_distance_to_sample()
Expand Down
13 changes: 7 additions & 6 deletions pyxrf/gui_module/wnd_manage_emission_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np
from qtpy.QtCore import Qt, Signal, Slot
from qtpy.QtGui import QBrush, QColor, QDoubleValidator
from qtpy.QtGui import QBrush, QColor
from qtpy.QtWidgets import (
QCheckBox,
QHBoxLayout,
Expand All @@ -21,6 +21,7 @@
from .dlg_pileup_peak_parameters import DialogPileupPeakParameters
from .useful_widgets import (
CheckBoxNamed,
DoubleValidator,
ElementSelection,
LineEditExtended,
LineEditReadOnly,
Expand Down Expand Up @@ -146,7 +147,7 @@ def _setup_elines_table(self):
"""The table has only functionality necessary to demonstrate how it is going
to look. A lot more code is needed to actually make it run."""

self._validator_peak_height = QDoubleValidator()
self._validator_peak_height = DoubleValidator()
self._validator_peak_height.setBottom(0.01)

self.tbl_elines = QTableWidget()
Expand Down Expand Up @@ -190,7 +191,7 @@ def _setup_action_buttons(self):
self.pb_remove_rel.clicked.connect(self.pb_remove_rel_clicked)

self.le_remove_rel = LineEditExtended("")
self._validator_le_remove_rel = QDoubleValidator()
self._validator_le_remove_rel = DoubleValidator()
self._validator_le_remove_rel.setBottom(0.01) # Some small number
self._validator_le_remove_rel.setTop(100.0)
self.le_remove_rel.setText(self._format_threshold(self._remove_peak_threshold))
Expand Down Expand Up @@ -539,7 +540,7 @@ def tbl_elines_item_changed(self, item):
if n_col == 4:
text = item.text()
eline = self._table_contents[n_row]["eline"]
if self._validator_peak_height.validate(text, 0)[0] != QDoubleValidator.Acceptable:
if self._validator_peak_height.validate(text, 0)[0] != DoubleValidator.Acceptable:
val = self._table_contents[n_row]["peak_int"]
self._enable_events = False
item.setText(f"{val:.2f}")
Expand Down Expand Up @@ -598,7 +599,7 @@ def le_remove_rel_text_changed(self, text):

def le_remove_rel_editing_finished(self):
text = self.le_remove_rel.text()
if self._validator_le_remove_rel.validate(text, 0)[0] == QDoubleValidator.Acceptable:
if self._validator_le_remove_rel.validate(text, 0)[0] == DoubleValidator.Acceptable:
self._remove_peak_threshold = float(text)
else:
self.le_remove_rel.setText(self._format_threshold(self._remove_peak_threshold))
Expand All @@ -624,7 +625,7 @@ def _display_peak_intensity(self, eline):
def _update_le_remove_rel_state(self, text=None):
if text is None:
text = self.le_remove_rel.text()
state = self._validator_le_remove_rel.validate(text, 0)[0] == QDoubleValidator.Acceptable
state = self._validator_le_remove_rel.validate(text, 0)[0] == DoubleValidator.Acceptable
self.le_remove_rel.setValid(state)
self.pb_remove_rel.setEnabled(state)

Expand Down
6 changes: 6 additions & 0 deletions pyxrf/model/lineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,9 @@ def plot_selected_energy_range_original(self, *, e_low=None, e_high=None):
self.plot_energy_barh = PolyCollection.span_where(
x_v, ymin=y_min, ymax=y_max, where=ss, facecolor="white", edgecolor="yellow", alpha=1
)
# self.plot_energy_barh = self._ax.fill_between(
# x_v, y1=y_min, y2=y_max, where=ss, facecolor="white", edgecolor="yellow", alpha=1
# )
self._ax.add_collection(self.plot_energy_barh)

def plot_multi_exp_data(self):
Expand Down Expand Up @@ -1474,6 +1477,9 @@ def plot_selected_energy_range(self, *, axes, barh_existing, e_low=None, e_high=
barh_new = PolyCollection.span_where(
x_v, ymin=y_min, ymax=y_max, where=ss, facecolor="white", edgecolor="yellow", alpha=1
)
# barh_new = axes.fill_between(
# x_v, y1=y_min, y2=y_max, where=ss, facecolor="white", edgecolor="yellow", alpha=1
# )
axes.add_collection(barh_new)

return barh_new
Expand Down

0 comments on commit 7523a79

Please sign in to comment.