Skip to content

Commit

Permalink
New SpinBoxObserver type to store the last value of each QSpinBox
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Borreguero <[email protected]>
  • Loading branch information
jmborr committed Dec 13, 2023
1 parent c7dd85f commit 317b01d
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 46 deletions.
74 changes: 74 additions & 0 deletions RefRed/gui_handling/observer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# standard imports
from dataclasses import dataclass

# third-party imports
from qtpy.QtWidgets import QSpinBox

# package imports


class SpinBoxObserver:
r"""Stores the last value for each of the registered QSpinBox objects.
Attributes
----------
NAN : int
Represents a value that the QSpinBox object cannot take
QUANTUM : int
The minimum allowed change in value for any of the registered QSpinBox objects
"""
NAN: int = -1
QUANTUM: int = 1

@dataclass
class RegistryEntry:
r"""Helper class to encapsulate entries in the registry of SpinBoxObserver._registry"""
spin_box: QSpinBox
last_value: int

def __init__(self):
self._registry = {} # database holding the last value for each of the registered QSpinBox objects

def entry_key(self, spin_box: QSpinBox) -> int:
r"""Generates a unique key for the given spin_box object using its memory address"""
return id(spin_box)

def get_entry(self, spin_box: QSpinBox) -> RegistryEntry:
r"""Retrieves the registry entry for the given ``spin_box``.
If ``spin_box`` is not registered, it is first registered with default ``last_value=SpinBoxObserver.NAN``
"""
key = self.entry_key(spin_box)
if key not in self._registry:
self.register(spin_box)
return self._registry[key]

def register(self, spin_box, last_value=NAN):
r"""Registers a new spin box in the internal registry with an optional initial last_value.
Parameters
----------
spin_box
The spin_box object to register.
last_value: The optional initial last_value for the spin_box. Default is the NAN constant.
"""
key = self.entry_key(spin_box)
self._registry[key] = self.RegistryEntry(spin_box=spin_box, last_value=last_value)

def quantum_change(self, spin_box: QSpinBox) -> bool:
r"""Determines if the current value of ``spin_box`` changed by exactly the QUANTUM amount from the last value.
Additionally, it updates the last value stored in the registry with the current value
Parameters
----------
spin_box
The spin box object to check for quantum change.
Returns
-------
``True`` if the current value differs from the last value by exactly the QUANTUM amount, ``False`` otherwise.
"""
entry = self.get_entry(spin_box)
last_value, new_value = entry.last_value, spin_box.value()
entry.last_value = new_value
return abs(new_value - last_value) == self.QUANTUM
16 changes: 8 additions & 8 deletions RefRed/interfaces/plot2d_dialog_refl_interface.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@
<sender>plot2dPeakFromSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>manual_input_peak1()</slot>
<slot>plot2d_peak_from_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1282</x>
Expand All @@ -1309,7 +1309,7 @@
<sender>plot2dPeakToSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>manual_input_peak2()</slot>
<slot>plot2d_peak_to_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1282</x>
Expand All @@ -1325,7 +1325,7 @@
<sender>plot2dBackFromValue</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>manual_input_back1()</slot>
<slot>plot2d_back_from_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1282</x>
Expand All @@ -1341,7 +1341,7 @@
<sender>plot2dBackToValue</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>manual_input_back2()</slot>
<slot>plot2d_back_to_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1282</x>
Expand All @@ -1362,12 +1362,12 @@
<slot>manual_input_of_tof_field()</slot>
<slot>manual_auto_tof_clicked()</slot>
<slot>manual_input_peak1()</slot>
<slot>plot2d_peak_from_spinbox_value_changed()</slot>
<slot>manual_input_peak2()</slot>
<slot>manual_input_peak1_2()</slot>
<slot>manual_input_peak2_2()</slot>
<slot>plot2d_peak_to_spinbox_value_changed()</slot>
<slot>manual_input_back1()</slot>
<slot>manual_input_back1_2()</slot>
<slot>plot2d_back_from_spinbox_value_changed()</slot>
<slot>manual_input_back2()</slot>
<slot>manual_input_back2_2()</slot>
<slot>plot2d_back_to_spinbox_value_changed()</slot>
</slots>
</ui>
12 changes: 8 additions & 4 deletions RefRed/interfaces/plot_dialog_refl_interface.ui
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@
<sender>plotPeakFromSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>plot_peak_from_spinbox_signal()</slot>
<slot>plot_peak_from_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>613</x>
Expand All @@ -671,7 +671,7 @@
<sender>plotPeakToSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>plot_peak_to_spinbox_signal()</slot>
<slot>plot_peak_to_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>613</x>
Expand All @@ -687,7 +687,7 @@
<sender>plotBackFromSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>plot_back_from_spinbox_signal()</slot>
<slot>plot_back_from_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>613</x>
Expand All @@ -703,7 +703,7 @@
<sender>plotBackToSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>Dialog</receiver>
<slot>plot_back_to_spinbox_signal()</slot>
<slot>plot_back_to_spinbox_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>613</x>
Expand All @@ -722,9 +722,13 @@
<slot>update_back1_spinbox()</slot>
<slot>update_back2_spinbox()</slot>
<slot>plot_peak_from_spinbox_signal()</slot>
<slot>plot_peak_from_spinbox_value_changed()</slot>
<slot>plot_peak_to_spinbox_signal()</slot>
<slot>plot_peak_to_spinbox_value_changed()</slot>
<slot>plot_back_from_spinbox_signal()</slot>
<slot>plot_back_from_spinbox_value_changed()</slot>
<slot>plot_back_to_spinbox_signal()</slot>
<slot>plot_back_to_spinbox_value_changed()</slot>
<slot>plot_back_flag_clicked()</slot>
</slots>
</ui>
60 changes: 32 additions & 28 deletions RefRed/interfaces/refred_main_interface.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6006,22 +6006,6 @@ p, li { white-space: pre-wrap; }
</hint>
</hints>
</connection>
<connection>
<sender>peakToValue</sender>
<signal>editingFinished()</signal>
<receiver>MainWindow</receiver>
<slot>data_peak_spinbox_validation()</slot>
<hints>
<hint type="sourcelabel">
<x>1566</x>
<y>553</y>
</hint>
<hint type="destinationlabel">
<x>778</x>
<y>551</y>
</hint>
</hints>
</connection>
<connection>
<sender>normBackgroundFlag</sender>
<signal>clicked()</signal>
Expand Down Expand Up @@ -6327,14 +6311,14 @@ p, li { white-space: pre-wrap; }
</hints>
</connection>
<connection>
<sender>peakFromValue</sender>
<signal>valueChanged(int)</signal>
<sender>peakToValue</sender>
<signal>editingFinished()</signal>
<receiver>MainWindow</receiver>
<slot>data_peak_spinbox_validation()</slot>
<hints>
<hint type="sourcelabel">
<x>1635</x>
<y>466</y>
<y>502</y>
</hint>
<hint type="destinationlabel">
<x>873</x>
Expand All @@ -6343,14 +6327,14 @@ p, li { white-space: pre-wrap; }
</hints>
</connection>
<connection>
<sender>peakToValue</sender>
<sender>backFromValue</sender>
<signal>valueChanged(int)</signal>
<receiver>MainWindow</receiver>
<slot>data_peak_spinbox_validation()</slot>
<slot>back_from_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1635</x>
<y>502</y>
<y>546</y>
</hint>
<hint type="destinationlabel">
<x>873</x>
Expand All @@ -6359,14 +6343,14 @@ p, li { white-space: pre-wrap; }
</hints>
</connection>
<connection>
<sender>backFromValue</sender>
<sender>backToValue</sender>
<signal>valueChanged(int)</signal>
<receiver>MainWindow</receiver>
<slot>data_back_spinbox_validation()</slot>
<slot>back_to_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1635</x>
<y>546</y>
<y>582</y>
</hint>
<hint type="destinationlabel">
<x>873</x>
Expand All @@ -6375,14 +6359,30 @@ p, li { white-space: pre-wrap; }
</hints>
</connection>
<connection>
<sender>backToValue</sender>
<sender>peakFromValue</sender>
<signal>valueChanged(int)</signal>
<receiver>MainWindow</receiver>
<slot>data_back_spinbox_validation()</slot>
<slot>peak_from_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1635</x>
<y>582</y>
<y>466</y>
</hint>
<hint type="destinationlabel">
<x>873</x>
<y>699</y>
</hint>
</hints>
</connection>
<connection>
<sender>peakToValue</sender>
<signal>valueChanged(int)</signal>
<receiver>MainWindow</receiver>
<slot>peak_to_value_changed()</slot>
<hints>
<hint type="sourcelabel">
<x>1635</x>
<y>502</y>
</hint>
<hint type="destinationlabel">
<x>873</x>
Expand All @@ -6395,8 +6395,12 @@ p, li { white-space: pre-wrap; }
<signal>signal1()</signal>
<slot>norm_back_spinbox_validation()</slot>
<slot>data_back_spinbox_validation()</slot>
<slot>back_from_value_changed()</slot>
<slot>back_to_value_changed()</slot>
<slot>norm_peak_spinbox_validation()</slot>
<slot>data_peak_spinbox_validation()</slot>
<slot>peak_from_value_changed()</slot>
<slot>peak_to_value_changed()</slot>
<slot>table_reduction_cell_changed()</slot>
<slot>data_norm_tab_changed()</slot>
<slot>load_configuration()</slot>
Expand Down
19 changes: 19 additions & 0 deletions RefRed/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FirstAngleRangeGuiHandler,
)
from RefRed.gui_handling.refred_interface_handler import RefRedInterfaceHandler
from RefRed.gui_handling.observer import SpinBoxObserver
from RefRed.initialization.gui import Gui as InitializeGui
from RefRed.initialization.gui_connections import GuiConnections as MakeGuiConnections
from RefRed.interfaces import load_ui
Expand Down Expand Up @@ -133,6 +134,8 @@ def __init__(self, argv=[], parent=None):
log_file = os.path.expanduser("~") + '/.refred.log'
logging.basicConfig(filename=log_file, level=logging.DEBUG)

self.spinbox_observer = SpinBoxObserver() # backup the last value for each spinbox in this widget

# home button of plots
def home_clicked_yi_plot(self):
HomePlotButtonClicked(parent=self, plot_type='yi')
Expand Down Expand Up @@ -260,6 +263,14 @@ def widget_modified(self, *args, **kwargs):
def data_back_spinbox_validation(self, *args, **kwargs):
DataBackSpinbox(parent=self)

def back_from_value_changed(self, *args, **kwargs):
if self.spinbox_observer.quantum_change(self.ui.backFromValue):
self.data_back_spinbox_validation(*args, **kwargs)

def back_to_value_changed(self, *args, **kwargs):
if self.spinbox_observer.quantum_change(self.ui.backToValue):
self.data_back_spinbox_validation(*args, **kwargs)

@config_file_has_been_modified
def data_back_checkbox(self, *args, **kwargs):
DataBackSpinbox(parent=self)
Expand All @@ -268,6 +279,14 @@ def data_back_checkbox(self, *args, **kwargs):
def data_peak_spinbox_validation(self, *args, **kwargs):
DataPeakSpinbox(parent=self)

def peak_from_value_changed(self, *args, **kwargs):
if self.spinbox_observer.quantum_change(self.ui.peakFromValue):
self.data_peak_spinbox_validation(*args, **kwargs)

def peak_to_value_changed(self, *args, **kwargs):
if self.spinbox_observer.quantum_change(self.ui.peakToValue):
self.data_peak_spinbox_validation(*args, **kwargs)

@config_file_has_been_modified
def norm_back_spinbox_validation(self, *args, **kwargs):
NormBackSpinbox(parent=self)
Expand Down
Loading

0 comments on commit 317b01d

Please sign in to comment.