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

Change ROI Colour #2015

Merged
merged 11 commits into from
Feb 9, 2024
1 change: 1 addition & 0 deletions docs/release_notes/next/feature-1992-ROI-colour
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#1992 : Spectrum Viewer: Change ROI Colour Through Right Click Dialog Menu
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
10 changes: 10 additions & 0 deletions mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def data(self, index, role):
return Qt.Checked
return Qt.Unchecked

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):
"""
Set data in table
Expand Down Expand Up @@ -164,6 +170,8 @@ def rename_row(self, row: int, new_name: str) -> None:
self._data[row][0] = new_name
self.layoutChanged.emit()



def flags(self, index):
"""
Handle selection of table rows to disable selection of ROI colour column
Expand All @@ -179,6 +187,8 @@ def flags(self, index):
else:
return Qt.ItemIsEnabled



def roi_names(self) -> list:
"""
Return list of ROI names
Expand Down
25 changes: 23 additions & 2 deletions mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from typing import TYPE_CHECKING, Optional

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,6 +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(str, tuple)

def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -38,6 +40,22 @@ def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs):
self.addScaleHandle([0, 1], [1, 0])
self._selected_row = None

def contextMenuEvent(self, event) -> None:
menu = QtWidgets.QMenu()
change_color_action = menu.addAction("ROI Change Color")
selected_action = menu.exec_(event.screenPos())
if selected_action == change_color_action:
color_dialog = QtWidgets.QColorDialog()
current_color = QtGui.QColor(*self._colour)
selected_color = color_dialog.getColor(current_color)

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(self._name, new_color)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets split this into is own method. So that asking for the colour is separated from triggering it.

Also, move the menu into the existing menu.




@property
def name(self) -> str:
return self._name
Expand Down Expand Up @@ -77,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 @@ -162,7 +181,8 @@ def set_roi_alpha(self, name: str, alpha: float) -> None:
@param name: The name of the ROI.
@param alpha: The new alpha value of the ROI.
"""
self.roi_dict[name].colour = self.roi_dict[name].colour[:3] + (alpha, )

self.roi_dict[name].colour = self.roi_dict[name].colour[:3] + (alpha,)
self.roi_dict[name].setPen(self.roi_dict[name].colour)
self.roi_dict[name].hoverPen = mkPen(self.roi_dict[name].colour, width=3)
self.set_roi_visibility_flags(name, bool(alpha))
Expand All @@ -176,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
Loading