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

Refactor SpectrumViewerWindow #2514

Merged
merged 8 commits into from
Feb 28, 2025
4 changes: 0 additions & 4 deletions mantidimaging/gui/windows/spectrum_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

LOG = getLogger(__name__)

ROI_ALL: Final = "all"
ROI_RITS: Final = "rits_roi"


Expand Down Expand Up @@ -91,12 +90,10 @@ class SpectrumViewerWindowModel:
tof_plot_range: tuple[float, float] | tuple[int, int] = (0, 0)
tof_mode: ToFUnitMode = ToFUnitMode.WAVELENGTH
tof_data: np.ndarray | None = None
tof_range_full: tuple[int, int] = (0, 0)

def __init__(self, presenter: SpectrumViewerWindowPresenter):
self.presenter = presenter
self._roi_id_counter = 0
self.special_roi_list = [ROI_ALL]

self.units = UnitConversion()

Expand Down Expand Up @@ -124,7 +121,6 @@ def set_stack(self, stack: ImageStack | None) -> None:
if stack is None:
return
self.tof_range = (0, stack.data.shape[0] - 1)
self.tof_range_full = self.tof_range
self.tof_data = self.get_stack_time_of_flight()

def set_normalise_stack(self, normalise_stack: ImageStack | None) -> None:
Expand Down
2 changes: 1 addition & 1 deletion mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def do_remove_roi(self, roi_name: str | None = None) -> None:
for name in list(self.get_roi_names()):
self.view.spectrum_widget.remove_roi(name)
self.view.spectrum_widget.roi_dict.clear()
self.view.table_view.roi_table_model.clear_table()
self.view.table_view.clear_table()
self.model.remove_all_roi()
else:
self.view.spectrum_widget.remove_roi(roi_name)
Expand Down
16 changes: 3 additions & 13 deletions mantidimaging/gui/windows/spectrum_viewer/test/presenter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from unittest import mock

import numpy as np

from PyQt5.QtCore import QAbstractTableModel
from PyQt5.QtWidgets import QPushButton, QActionGroup, QGroupBox, QAction, QCheckBox, QTabWidget, QWidget, QSpinBox
from PyQt5.QtWidgets import QPushButton, QActionGroup, QGroupBox, QAction, QCheckBox, QTabWidget
from parameterized import parameterized

from mantidimaging.core.data.dataset import Dataset
Expand All @@ -19,7 +17,7 @@
from mantidimaging.gui.windows.spectrum_viewer.model import ErrorMode, ToFUnitMode, ROI_RITS, SpecType
from mantidimaging.gui.windows.spectrum_viewer.spectrum_widget import SpectrumWidget, SpectrumPlotWidget, SpectrumROI
from mantidimaging.gui.widgets.spectrum_widgets.tof_properties import ExperimentSetupFormWidget
from mantidimaging.gui.windows.spectrum_viewer.view import ROITableWidget
from mantidimaging.gui.windows.spectrum_viewer.view import ROITableWidget, ROIPropertiesTableWidget
from mantidimaging.test_helpers import mock_versions, start_qapplication
from mantidimaging.test_helpers.unit_test_helper import generate_images

Expand All @@ -33,18 +31,10 @@ def setUp(self) -> None:
self.main_window = MainWindowView()
self.view = mock.create_autospec(SpectrumViewerWindowView, instance=True)
self.view.current_dataset_id = uuid.uuid4()
self.view.roi_properties_widget = mock.create_autospec(QWidget, instance=True)
self.view.roi_properties_widget.roiPropertiesSpinBoxes = {
"Top": mock.create_autospec(QSpinBox, instance=True),
"Bottom": mock.create_autospec(QSpinBox, instance=True),
"Left": mock.create_autospec(QSpinBox, instance=True),
"Right": mock.create_autospec(QSpinBox, instance=True)
}
self.view.roi_properties_widget = mock.create_autospec(ROIPropertiesTableWidget, instance=True)
self.view.table_view = mock.create_autospec(ROITableWidget, instance=True)
self.view.table_view.find_row_for_roi.return_value = 0
type(self.view.table_view).current_roi_name = mock.PropertyMock(return_value="roi")
self.view.table_view.roi_table_model = mock.create_autospec(QAbstractTableModel, instance=True)
self.view.table_view.roi_table_model.clear_table = mock.Mock()
mock_spectrum_roi_dict = mock.create_autospec(dict, instance=True)
self.view.spectrum_widget = mock.create_autospec(SpectrumWidget, roi_dict=mock_spectrum_roi_dict, instance=True)
self.view.spectrum_widget.spectrum_plot_widget = mock.create_autospec(SpectrumPlotWidget,
Expand Down
39 changes: 2 additions & 37 deletions mantidimaging/gui/windows/spectrum_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QCheckBox, QVBoxLayout, QFileDialog, QPushButton, QLabel, QAbstractItemView, QHeaderView, \
QTabWidget, QComboBox, QSpinBox, QTableWidget, QGroupBox, QActionGroup, QAction
QTabWidget, QComboBox, QSpinBox, QGroupBox, QActionGroup, QAction
from PyQt5.QtCore import QSignalBlocker, QModelIndex, pyqtSignal

from mantidimaging.core.utility import finder
Expand Down Expand Up @@ -153,12 +153,6 @@ def find_row_for_roi(self, roi_name: str) -> int | None:
return row
return None

def set_roi_name_by_row(self, row: int, name: str) -> None:
"""
Set the name of the ROI for a given row in the ROI table.
"""
self.roi_table_model.set_element(row, 0, name)

def update_roi_color(self, roi_name: str, new_color: tuple[int, int, int, int]) -> None:
"""
Finds ROI by name in table and updates it's colour (R, G, B) format.
Expand Down Expand Up @@ -208,22 +202,16 @@ class SpectrumViewerWindowView(BaseMainWindowView):
exportTabs: QTabWidget
normaliseErrorIcon: QLabel
shuttercountErrorIcon: QLabel
_current_dataset_id: UUID | None
normalise_error_issue: str = ""
shuttercount_error_issue: str = ""
image_output_mode_combobox: QComboBox
transmission_error_mode_combobox: QComboBox
bin_size_spinBox: QSpinBox
bin_step_spinBox: QSpinBox

roiPropertiesTableWidget: QTableWidget
roiPropertiesGroupBox: QGroupBox
roi_properties_widget: ROIPropertiesTableWidget

spectrum_widget: SpectrumWidget

number_roi_properties_procced: int = 0

experimentSetupGroupBox: QGroupBox
experimentSetupFormWidget: ExperimentSetupFormWidget

Expand All @@ -235,8 +223,6 @@ def __init__(self, main_window: MainWindowView):
icon_path = finder.ROOT_PATH + "/gui/ui/images/exclamation-triangle-red.png"
self.normalise_error_icon_pixmap = QPixmap(icon_path)

self.selected_row: int = 0

self.presenter = SpectrumViewerWindowPresenter(self, main_window)

self.spectrum_widget = SpectrumWidget(main_window)
Expand Down Expand Up @@ -267,7 +253,7 @@ def __init__(self, main_window: MainWindowView):
if self.presenter.model.tof_data is None:
self.tof_mode_select_group.setEnabled(False)

self._current_dataset_id = None
self.current_dataset_id: UUID | None = None
self.sampleStackSelector.stack_selected_uuid.connect(self.presenter.handle_sample_change)
self.sampleStackSelector.stack_selected_uuid.connect(self.presenter.handle_button_enabled)
self.normaliseStackSelector.stack_selected_uuid.connect(self.presenter.handle_normalise_stack_change)
Expand Down Expand Up @@ -356,14 +342,6 @@ def on_visibility_change(self) -> None:

self.set_roi_properties()

@property
def current_dataset_id(self) -> UUID | None:
return self._current_dataset_id

@current_dataset_id.setter
def current_dataset_id(self, uuid: UUID | None) -> None:
self._current_dataset_id = uuid

def _configure_dropdown(self, selector: DatasetSelectorWidgetView) -> None:
selector.presenter.show_stacks = True
selector.subscribe_to_main_window(self.main_window)
Expand Down Expand Up @@ -526,16 +504,6 @@ def remove_roi(self) -> None:
else:
self.set_roi_properties()

def clear_all_rois(self) -> None:
"""
Clear all ROIs from the table view
"""
self.table_view.roi_table_model.clear_table()
self.spectrum_widget.spectrum_data_dict = {}
self.spectrum_widget.spectrum.clearPlots()
self.removeBtn.setEnabled(False)
self.disable_roi_properties()

@property
def transmission_error_mode(self) -> str:
return self.transmission_error_mode_combobox.currentText()
Expand Down Expand Up @@ -580,9 +548,6 @@ def disable_roi_properties(self) -> None:
self.roi_properties_widget.set_roi_name("None selected")
self.roi_properties_widget.enable_widgets(False)

def get_checked_menu_option(self) -> QAction:
return self.tof_mode_select_group.checkedAction()

def setup_roi_properties_spinboxes(self) -> None:
assert self.spectrum_widget.image.image_data is not None
self.roi_properties_widget.set_roi_limits(self.spectrum_widget.image.image_data.shape)
Loading