Skip to content

Commit

Permalink
update type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmeigh committed Feb 5, 2024
1 parent d45d4f0 commit 84aa6b1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
11 changes: 11 additions & 0 deletions mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ def do_add_roi(self) -> None:
self.view.auto_range_image()
self.do_add_roi_to_table(roi_name)

def change_roi_colour(self, roi_name: str, new_colour: tuple) -> None:
"""
Change the colour of a given ROI in both the spectrum widget and the table.
@param roi_name: Name of the ROI to change color.
@param new_colour: The new color for the ROI.
"""
if roi_name in self.view.spectrum.roi_dict:
self.view.spectrum.roi_dict[roi_name].colour = new_colour
self.view.update_roi_color_in_table(roi_name, new_colour)

def add_rits_roi(self) -> None:
roi_name = ROI_RITS
self.model.set_new_roi(roi_name)
Expand Down
12 changes: 5 additions & 7 deletions mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ def data(self, index, role):
return Qt.Checked
return Qt.Unchecked

def recolour_row(self, row, new_color):
print(f"recolour_row called for row {row} with new_color {new_color}")

if 0 <= row < len(self._data) and len(new_color) == 4:
for column in range(self.columnCount()):
index = self.index(row, column)
self.dataChanged.emit(index, index, [Qt.BackgroundRole])
def update_color(self, row, new_color):
if 0 <= row < len(self._data):
self._data[row][1] = new_color
index = self.index(row, 1)
self.dataChanged.emit(index, index, [Qt.DisplayRole, Qt.BackgroundRole])

def setData(self, index, value, role):
"""
Expand Down
13 changes: 8 additions & 5 deletions mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from PyQt5.QtCore import pyqtSignal, Qt, QSignalBlocker
from PyQt5 import QtGui, QtWidgets
from pyqtgraph import ROI, GraphicsLayoutWidget, LinearRegionItem, PlotItem, mkPen
from PyQt5.QtGui import QPen
from mantidimaging.core.utility.close_enough_point import CloseEnoughPoint
from mantidimaging.core.utility.sensible_roi import SensibleROI
from mantidimaging.gui.widgets.mi_mini_image_view.view import MIMiniImageView
Expand All @@ -24,7 +25,7 @@ class SpectrumROI(ROI):
@param args: Arguments to pass to the ROI object
@param kwargs: Keyword arguments to pass to the ROI object
"""
sig_colour_change = pyqtSignal()
sig_colour_change = pyqtSignal(str, tuple)

def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -51,7 +52,7 @@ def contextMenuEvent(self, event) -> None:
if selected_color.isValid():
new_color = (selected_color.red(), selected_color.green(), selected_color.blue(), 255)
self.colour = new_color # Update the ROI color
self.sig_colour_change.emit() # Emit the signal
self.sig_colour_change.emit(self._name, new_color)



Expand All @@ -72,14 +73,15 @@ def colour(self) -> tuple[int, int, int, int]:
return self._colour

@colour.setter
def set_colour(self, colour: tuple[int, int, int, int]) -> None:
def colour(self, colour: tuple[int, int, int, int]) -> None:
self._colour = colour
self.setPen(fn.mkPen(colour))
self.setPen(self._colour)

@property
def selected_row(self) -> Optional[int]:
return self._selected_row


class SpectrumWidget(GraphicsLayoutWidget):
"""
The widget containing the spectrum plot and the image projection.
Expand All @@ -93,6 +95,7 @@ class SpectrumWidget(GraphicsLayoutWidget):

range_changed = pyqtSignal(object)
roi_changed = pyqtSignal()
roiColorChangeRequested = pyqtSignal(str, tuple)

def __init__(self) -> None:
super().__init__()
Expand Down Expand Up @@ -171,7 +174,6 @@ def set_roi_visibility_flags(self, name: str, visible: bool) -> None:
self.roi_dict[name].setAcceptedMouseButtons(Qt.NoButton)
self.roi_dict[name].sigRegionChanged.connect(self.roi_changed.emit)


def set_roi_alpha(self, name: str, alpha: float) -> None:
"""
Change the alpha value of an existing ROI
Expand All @@ -194,6 +196,7 @@ def add_roi(self, roi: SensibleROI, name: str) -> None:
"""
roi_object = SpectrumROI(name, roi, pos=(0, 0), rotatable=False, scaleSnap=True, translateSnap=True)
roi_object.colour = self.colour_generator()
roi_object.sig_colour_change.connect(lambda name, color: self.roiColorChangeRequested.emit(name, color))

self.roi_dict[name] = roi_object.roi
self.max_roi_size = roi_object.size()
Expand Down
25 changes: 25 additions & 0 deletions mantidimaging/gui/windows/spectrum_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, main_window: 'MainWindowView'):

self.spectrum.range_changed.connect(self.presenter.handle_range_slide_moved)
self.spectrum.roi_changed.connect(self.presenter.handle_roi_moved)
self.spectrum.roiColorChangeRequested.connect(self.presenter.change_roi_colour)

self._current_dataset_id = None
self.sampleStackSelector.stack_selected_uuid.connect(self.presenter.handle_sample_change)
Expand Down Expand Up @@ -246,6 +247,30 @@ def set_new_roi(self) -> None:
"""
self.presenter.do_add_roi()

def update_roi_color_in_table(self, roi_name: str, new_color: tuple):
"""
Finds ROI by name in table and updates colour.
@param roi_name: Name of the ROI to update.
@param new_color: The new color for the ROI in (R, G, B) format.
"""
row = self.find_row_for_roi(roi_name)
if row is not None:
self.roi_table_model.update_color(row, new_color)

def find_row_for_roi(self, roi_name: str) -> int:
"""
Returns row index for ROI name, or None if not found.
@param roi_name: Name ROI find.
@return: Row index ROI or None.
"""
for row in range(self.roi_table_model.rowCount()):
if self.roi_table_model.index(row, 0).data() == roi_name:
return row
return None


def set_roi_alpha(self, alpha: float, roi_name: str) -> None:
"""
Set the alpha value for the selected ROI and update the spectrum to reflect the change.
Expand Down

0 comments on commit 84aa6b1

Please sign in to comment.