diff --git a/mantidimaging/gui/windows/spectrum_viewer/model.py b/mantidimaging/gui/windows/spectrum_viewer/model.py index 50b0ab4c085..0bd036df350 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/model.py +++ b/mantidimaging/gui/windows/spectrum_viewer/model.py @@ -128,7 +128,11 @@ def set_stack(self, stack: ImageStack | None) -> None: 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() - self.set_new_roi(ROI_ALL) + height, width = self.get_image_shape() + self._roi_ranges[ROI_ALL] = SensibleROI.from_list([0, 0, width, height]) + + def set_normalise_stack(self, normalise_stack: ImageStack | None) -> None: + self._normalise_stack = normalise_stack def set_new_roi(self, name: str) -> None: """ @@ -137,13 +141,7 @@ def set_new_roi(self, name: str) -> None: @param name: The name of the new ROI """ height, width = self.get_image_shape() - self.set_roi(name, SensibleROI.from_list([0, 0, width, height])) - - def set_normalise_stack(self, normalise_stack: ImageStack | None) -> None: - self._normalise_stack = normalise_stack - - def set_roi(self, roi_name: str, roi: SensibleROI) -> None: - self._roi_ranges[roi_name] = roi + self._roi_ranges[name] = SensibleROI.from_list([0, 0, width, height]) def get_averaged_image(self) -> np.ndarray | None: """ diff --git a/mantidimaging/gui/windows/spectrum_viewer/presenter.py b/mantidimaging/gui/windows/spectrum_viewer/presenter.py index 891d6c65d08..86dae384737 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/presenter.py +++ b/mantidimaging/gui/windows/spectrum_viewer/presenter.py @@ -202,7 +202,7 @@ def handle_roi_moved(self, force_new_spectrums: bool = False) -> None: """ for name in self.view.spectrum_widget.roi_dict: current_roi = self.view.spectrum_widget.get_roi(name) - self.model.set_roi(name, current_roi) + self.view.spectrum_widget.set_roi(name, current_roi) if force_new_spectrums: spectrum = self.model.get_spectrum( current_roi, @@ -429,7 +429,7 @@ def change_selected_menu_option(self, opt: str) -> None: def do_adjust_roi(self) -> None: new_roi = self.convert_spinbox_roi_to_SensibleROI(self.view.roiPropertiesSpinBoxes) - self.model.set_roi(self.view.current_roi_name, new_roi) + self.view.spectrum_widget.set_roi(self.view.current_roi_name, new_roi) self.view.spectrum_widget.adjust_roi(new_roi, self.view.current_roi_name) def handle_storing_current_roi_name_on_tab_change(self) -> None: diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 4e4c93bb30c..833c91dedd9 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -244,6 +244,16 @@ def get_roi(self, roi_name: str) -> SensibleROI: else: raise KeyError(f"ROI with name {roi_name} does not exist in self.roi_dict or and is not 'all'") + def set_roi(self, roi_name: str, new_roi: SensibleROI) -> None: + if roi_name not in self.roi_dict: + raise KeyError(f"ROI '{roi_name}' does not exist.") + roi = self.roi_dict[roi_name] + + roi.blockSignals(True) + roi.setPos((new_roi.left, new_roi.top)) + roi.setSize((new_roi.width, new_roi.height)) + roi.blockSignals(False) + def remove_roi(self, roi_name: str) -> None: """ Remove a given ROI by name unless it is 'roi' or 'all'. diff --git a/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py b/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py index 5597cff8b29..33533ec719d 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py +++ b/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py @@ -228,7 +228,7 @@ def test_save_rits_roi_dat(self): norm = ImageStack(np.full([10, 11, 12], 2)) stack.data[:, :, :5] *= 2 self.model.set_new_roi("rits_roi") - self.model.set_roi("rits_roi", SensibleROI.from_list([0, 0, 10, 11])) + self.model._roi_ranges["rits_roi"] = SensibleROI.from_list([0, 0, 10, 11]) self.model.set_normalise_stack(norm) self.model._roi_ranges["ROI_RITS"] = SensibleROI.from_list([0, 0, 10, 11]) @@ -475,7 +475,7 @@ def test_save_single_rits_spectrum(self, mock_save_rits_roi): norm = ImageStack(np.full([10, 11, 12], 2)) stack.data[:, :, :5] *= 2 self.model.set_new_roi("rits_roi") - self.model.set_roi("rits_roi", SensibleROI.from_list([0, 0, 5, 5])) + self.model._roi_ranges["rits_roi"] = SensibleROI.from_list([0, 0, 5, 5]) self.model.set_normalise_stack(norm) self.model._roi_ranges["ROI_RITS"] = SensibleROI.from_list([0, 0, 5, 5]) @@ -490,7 +490,7 @@ def test_save_rits_correct_transmision(self, mock_save_rits_roi): for i in range(10): stack.data[:, :, i] *= i self.model.set_new_roi("rits_roi") - self.model.set_roi("rits_roi", SensibleROI.from_list([1, 0, 6, 4])) + self.model._roi_ranges["rits_roi"] = SensibleROI.from_list([1, 0, 6, 4]) self.model.set_normalise_stack(norm) mock_path = mock.create_autospec(Path, instance=True) diff --git a/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py b/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py index 6c2f3806c93..1fa4354f6c3 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py +++ b/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py @@ -152,6 +152,14 @@ def test_WHEN_roi_removed_THEN_roi_name_removed_from_list_of_roi_names(self): self.spectrum_widget.remove_roi("new_roi") self.assertListEqual(list(self.spectrum_widget.roi_dict.keys()), ["all", "roi"]) + def test_set_roi_updates_position_and_size(self): + self.spectrum_widget.roi_dict["test_roi"] = SpectrumROI("test_roi", SensibleROI(10, 20, 30, 40)) + new_roi = SensibleROI(50, 60, 120, 140) + self.spectrum_widget.set_roi("test_roi", new_roi) + roi = self.spectrum_widget.roi_dict["test_roi"] + self.assertEqual(roi.pos(), Point(50, 60)) + self.assertEqual(roi.size(), Point(70, 80)) + def test_WHEN_remove_roi_called_with_default_roi_THEN_raise_runtime_error(self): self.spectrum_widget.roi_dict = {"all": mock.Mock(), "roi": mock.Mock()} with self.assertRaises(RuntimeError):