Skip to content

Commit

Permalink
Merge pull request #108 from neutrons/introduce_back2
Browse files Browse the repository at this point in the history
Introduce a second background in the boundaries widgets
  • Loading branch information
jmborr authored Dec 13, 2023
2 parents f34c7cd + 317b01d commit 16adf64
Show file tree
Hide file tree
Showing 15 changed files with 2,478 additions and 1,329 deletions.
8 changes: 4 additions & 4 deletions RefRed/gui_handling/data_norm_spinboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class DataPeakSpinbox(object):

def __init__(self, parent=None):
self.parent = parent
peak1 = self.parent.ui.dataPeakFromValue.value()
peak2 = self.parent.ui.dataPeakToValue.value()
peak1 = self.parent.ui.peakFromValue.value()
peak2 = self.parent.ui.peakToValue.value()
DataSpinbox(parent=parent, entry_type='peak', value_min=peak1, value_max=peak2)


Expand All @@ -98,8 +98,8 @@ class DataBackSpinbox(object):

def __init__(self, parent=None):
self.parent = parent
back1 = self.parent.ui.dataBackFromValue.value()
back2 = self.parent.ui.dataBackToValue.value()
back1 = self.parent.ui.backFromValue.value()
back2 = self.parent.ui.backToValue.value()
back_flag = self.parent.ui.dataBackgroundFlag.isChecked()
DataSpinbox(parent=parent, entry_type='back', value_min=back1, value_max=back2, flag=back_flag)

Expand Down
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
8 changes: 4 additions & 4 deletions RefRed/gui_handling/update_plot_widget_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def data_tab_widgets(self, status=False):
parent.ui.dataLowResFromValue.setEnabled(status)
parent.ui.dataLowResToLabel.setEnabled(status)
parent.ui.dataLowResToValue.setEnabled(status)
parent.ui.dataBackToValue.setEnabled(status)
parent.ui.dataBackFromValue.setEnabled(status)
parent.ui.dataPeakToValue.setEnabled(status)
parent.ui.dataPeakFromValue.setEnabled(status)
parent.ui.backToValue.setEnabled(status)
parent.ui.backFromValue.setEnabled(status)
parent.ui.peakToValue.setEnabled(status)
parent.ui.peakFromValue.setEnabled(status)
parent.ui.dataBackgroundFlag.setEnabled(status)

self.parent.ui.dataTOFmanualLabel.setEnabled(status)
Expand Down
22 changes: 14 additions & 8 deletions RefRed/initialization/gui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from qtpy import QtGui, QtCore, QtWidgets

import socket

from RefRed import WINDOW_TITLE
Expand Down Expand Up @@ -210,14 +211,19 @@ def init_error_label_widgets(self):
parent = self.parent
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.red)
parent.ui.data_peak1_error.setVisible(False)
parent.ui.data_peak1_error.setPalette(palette)
parent.ui.data_peak2_error.setVisible(False)
parent.ui.data_peak2_error.setPalette(palette)
parent.ui.data_back1_error.setVisible(False)
parent.ui.data_back1_error.setPalette(palette)
parent.ui.data_back2_error.setVisible(False)
parent.ui.data_back2_error.setPalette(palette)

for label_text in (
"peakFromError",
"peakToError",
"backFromError",
"backToError",
"back2FromError",
"back2ToError",
):
label: QtWidgets.QLabel = getattr(parent.ui, label_text)
label.setVisible(False)
label.setPalette(palette)

parent.ui.norm_peak1_error.setVisible(False)
parent.ui.norm_peak1_error.setPalette(palette)
parent.ui.norm_peak2_error.setVisible(False)
Expand Down
Loading

1 comment on commit 16adf64

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitLab pipeline for refred-dev has been submitted for this commit: "https://code.ornl.gov/sns-hfir-scse/deployments/conda-legacy-deploy/-/pipelines/496773"

Please sign in to comment.