From 810db2b3ff138aa7f0b4b96e595249ede19b57ec Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Mon, 22 Jan 2024 15:39:34 +0000 Subject: [PATCH 01/10] updated default values in Remove all stripes --- .../spectrum_viewer/spectrum_widget.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index ab94108f895..1407df8977a 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -5,6 +5,7 @@ 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 mantidimaging.core.utility.close_enough_point import CloseEnoughPoint @@ -38,6 +39,28 @@ def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs): self.addScaleHandle([0, 1], [1, 0]) self._selected_row = None + # Set context menu policy and connect signal to event + self.setAcceptHoverEvents(True) + def contextMenuEvent(self, event) -> None: + # Create the color dialog and select color + color_dialog = QtWidgets.QColorDialog() + selected_color = color_dialog.getColor(QtGui.QColor(*self._colour), title="Select ROI Color") + + # Check color is valid and set the new ROI color + if selected_color.isValid(): + new_color = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) + self.set_roi_color(new_color) + + # Accept the event to show been handled + event.accept() + + # Call the method in the parent to change_roi_colour + def set_roi_color(self, color: QtGui.QColor) -> None: + if isinstance(color, QtGui.QColor): + self._colour = color + self.parent().change_roi_colour(self._name, color) + + @property def name(self) -> str: return self._name @@ -138,7 +161,7 @@ def change_roi_colour(self, name: str, colour: tuple[int, int, int, int]) -> Non @param colour: The new colour of the ROI. """ self.roi_dict[name].colour = colour - self.roi_dict[name].setPen(self.roi_dict[name].colour) + self.roi_dict[name].setPen(mkPen[name].colour) def set_roi_visibility_flags(self, name: str, visible: bool) -> None: """ From b2514e0a0d29fb0c96732b466a6112b63ab02416 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Mon, 22 Jan 2024 15:54:40 +0000 Subject: [PATCH 02/10] update release notes --- docs/release_notes/next/feature-1992-ROI-colour | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/release_notes/next/feature-1992-ROI-colour diff --git a/docs/release_notes/next/feature-1992-ROI-colour b/docs/release_notes/next/feature-1992-ROI-colour new file mode 100644 index 00000000000..3de82d867b5 --- /dev/null +++ b/docs/release_notes/next/feature-1992-ROI-colour @@ -0,0 +1 @@ +#1992 : Spectrum Viewer: Change ROI Colour Through Right Click Dialog Menu From 972a4f904afb159153e3235a533d3b06493a9a1c Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Thu, 25 Jan 2024 15:16:11 +0000 Subject: [PATCH 03/10] add right click and roi change color but no change in table --- .../spectrum_viewer/spectrum_widget.py | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 1407df8977a..b2280344f73 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -7,7 +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 @@ -39,26 +39,18 @@ def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs): self.addScaleHandle([0, 1], [1, 0]) self._selected_row = None - # Set context menu policy and connect signal to event - self.setAcceptHoverEvents(True) def contextMenuEvent(self, event) -> None: - # Create the color dialog and select color - color_dialog = QtWidgets.QColorDialog() - selected_color = color_dialog.getColor(QtGui.QColor(*self._colour), title="Select ROI Color") - - # Check color is valid and set the new ROI color - if selected_color.isValid(): - new_color = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) - self.set_roi_color(new_color) + 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) - # Accept the event to show been handled - event.accept() + if selected_color.isValid(): + self.colour = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) - # Call the method in the parent to change_roi_colour - def set_roi_color(self, color: QtGui.QColor) -> None: - if isinstance(color, QtGui.QColor): - self._colour = color - self.parent().change_roi_colour(self._name, color) @property @@ -86,7 +78,6 @@ def colour(self, colour: tuple[int, int, int, int]) -> None: def selected_row(self) -> Optional[int]: return self._selected_row - class SpectrumWidget(GraphicsLayoutWidget): """ The widget containing the spectrum plot and the image projection. @@ -160,8 +151,10 @@ def change_roi_colour(self, name: str, colour: tuple[int, int, int, int]) -> Non @param name: The name of the ROI. @param colour: The new colour of the ROI. """ - self.roi_dict[name].colour = colour - self.roi_dict[name].setPen(mkPen[name].colour) + if name in self.roi_dict: + roi = self.roi_dict[name] + roi.colour = colour # Assuming colour setter in SpectrumROI is correctly implemented + roi.setPen(QPen(QtGui.QColor(*colour))) def set_roi_visibility_flags(self, name: str, visible: bool) -> None: """ @@ -178,6 +171,7 @@ 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 @@ -185,10 +179,11 @@ 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].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)) + if name in self.roi_dict: + roi = self.roi_dict[name] + new_color = roi.colour[:3] + (int(alpha * 255),) + roi.colour = new_color + roi.setPen(QPen(QtGui.QColor(*new_color))) def add_roi(self, roi: SensibleROI, name: str) -> None: """ From d45d4f002aa80feb848326c0e121964fe2945925 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Mon, 29 Jan 2024 17:05:06 +0000 Subject: [PATCH 04/10] add right click and roi change color but no change in table --- .../spectrum_viewer/roi_table_model.py | 12 +++++++++ .../spectrum_viewer/spectrum_widget.py | 26 +++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py b/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py index abbb2dd5dc3..26daff205ad 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py +++ b/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py @@ -61,6 +61,14 @@ 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 setData(self, index, value, role): """ Set data in table @@ -164,6 +172,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 @@ -179,6 +189,8 @@ def flags(self, index): else: return Qt.ItemIsEnabled + + def roi_names(self) -> list: """ Return list of ROI names diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index b2280344f73..51e7bd780ec 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -7,7 +7,6 @@ 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 @@ -25,6 +24,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() def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs): super().__init__(*args, **kwargs) @@ -49,7 +49,9 @@ def contextMenuEvent(self, event) -> None: selected_color = color_dialog.getColor(current_color) if selected_color.isValid(): - self.colour = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) + 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 @@ -70,9 +72,9 @@ def colour(self) -> tuple[int, int, int, int]: return self._colour @colour.setter - def colour(self, colour: tuple[int, int, int, int]) -> None: + def set_colour(self, colour: tuple[int, int, int, int]) -> None: self._colour = colour - self.setPen(self._colour) + self.setPen(fn.mkPen(colour)) @property def selected_row(self) -> Optional[int]: @@ -151,10 +153,8 @@ def change_roi_colour(self, name: str, colour: tuple[int, int, int, int]) -> Non @param name: The name of the ROI. @param colour: The new colour of the ROI. """ - if name in self.roi_dict: - roi = self.roi_dict[name] - roi.colour = colour # Assuming colour setter in SpectrumROI is correctly implemented - roi.setPen(QPen(QtGui.QColor(*colour))) + self.roi_dict[name].colour = colour + self.roi_dict[name].setPen(self.roi_dict[name].colour) def set_roi_visibility_flags(self, name: str, visible: bool) -> None: """ @@ -179,11 +179,11 @@ 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. """ - if name in self.roi_dict: - roi = self.roi_dict[name] - new_color = roi.colour[:3] + (int(alpha * 255),) - roi.colour = new_color - roi.setPen(QPen(QtGui.QColor(*new_color))) + + 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)) def add_roi(self, roi: SensibleROI, name: str) -> None: """ From 84aa6b16a36b21ca9da3ac6e271f7181fac8282d Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Mon, 5 Feb 2024 01:14:11 +0000 Subject: [PATCH 05/10] update type annotations --- .../gui/windows/spectrum_viewer/presenter.py | 11 ++++++++ .../spectrum_viewer/roi_table_model.py | 12 ++++----- .../spectrum_viewer/spectrum_widget.py | 13 ++++++---- .../gui/windows/spectrum_viewer/view.py | 25 +++++++++++++++++++ 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/presenter.py b/mantidimaging/gui/windows/spectrum_viewer/presenter.py index f7018fed1e1..e253fb32527 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/presenter.py +++ b/mantidimaging/gui/windows/spectrum_viewer/presenter.py @@ -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) diff --git a/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py b/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py index 26daff205ad..7b2d4469312 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py +++ b/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py @@ -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): """ diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 51e7bd780ec..89071b69671 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -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 @@ -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) @@ -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) @@ -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. @@ -93,6 +95,7 @@ class SpectrumWidget(GraphicsLayoutWidget): range_changed = pyqtSignal(object) roi_changed = pyqtSignal() + roiColorChangeRequested = pyqtSignal(str, tuple) def __init__(self) -> None: super().__init__() @@ -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 @@ -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() diff --git a/mantidimaging/gui/windows/spectrum_viewer/view.py b/mantidimaging/gui/windows/spectrum_viewer/view.py index 3a599cd3acb..8ad90eaee7b 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/view.py +++ b/mantidimaging/gui/windows/spectrum_viewer/view.py @@ -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) @@ -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. From 01883d2b8a3862dd99a5971e8abf05906478596b Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Mon, 5 Feb 2024 13:00:29 +0000 Subject: [PATCH 06/10] update type annotations --- .../gui/windows/spectrum_viewer/roi_table_model.py | 4 ---- .../gui/windows/spectrum_viewer/spectrum_widget.py | 6 ++---- mantidimaging/gui/windows/spectrum_viewer/view.py | 5 +---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py b/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py index 7b2d4469312..c4bc65e2ff8 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py +++ b/mantidimaging/gui/windows/spectrum_viewer/roi_table_model.py @@ -170,8 +170,6 @@ 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 @@ -187,8 +185,6 @@ def flags(self, index): else: return Qt.ItemIsEnabled - - def roi_names(self) -> list: """ Return list of ROI names diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 89071b69671..7dcd7382841 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -7,7 +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 @@ -54,8 +54,6 @@ def contextMenuEvent(self, event) -> None: self.colour = new_color # Update the ROI color self.sig_colour_change.emit(self._name, new_color) - - @property def name(self) -> str: return self._name @@ -182,7 +180,7 @@ def set_roi_alpha(self, name: str, alpha: float) -> None: @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)) diff --git a/mantidimaging/gui/windows/spectrum_viewer/view.py b/mantidimaging/gui/windows/spectrum_viewer/view.py index 8ad90eaee7b..a1e38770c29 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/view.py +++ b/mantidimaging/gui/windows/spectrum_viewer/view.py @@ -258,7 +258,7 @@ def update_roi_color_in_table(self, roi_name: str, new_color: tuple): if row is not None: self.roi_table_model.update_color(row, new_color) - def find_row_for_roi(self, roi_name: str) -> int: + def find_row_for_roi(self, roi_name: str) -> Optional[int]: """ Returns row index for ROI name, or None if not found. @@ -270,7 +270,6 @@ def find_row_for_roi(self, roi_name: str) -> int: 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. @@ -298,8 +297,6 @@ def add_roi_table_row(self, name: str, colour: tuple[int, int, int]): @param name: The name of the ROI @param colour: The colour of the ROI """ - circle_label = QLabel() - circle_label.setStyleSheet(f"background-color: {colour}; border-radius: 5px;") self.roi_table_model.appendNewRow(name, colour, True) self.selected_row = self.roi_table_model.rowCount() - 1 self.tableView.selectRow(self.selected_row) From e28ecabc70de6a3eeb9125af3340156db3e88c53 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Tue, 6 Feb 2024 12:21:08 +0000 Subject: [PATCH 07/10] sepatre into two functions --- .../spectrum_viewer/spectrum_widget.py | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 7dcd7382841..fec42a633fa 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -5,7 +5,8 @@ from typing import TYPE_CHECKING, Optional from PyQt5.QtCore import pyqtSignal, Qt, QSignalBlocker -from PyQt5 import QtGui, QtWidgets +from PyQt5.QtGui import QColor +from PyQt5.QtWidgets import QColorDialog, QAction, QMenu from pyqtgraph import ROI, GraphicsLayoutWidget, LinearRegionItem, PlotItem, mkPen from mantidimaging.core.utility.close_enough_point import CloseEnoughPoint @@ -40,19 +41,24 @@ 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) + self.setAcceptedMouseButtons(Qt.RightButton) + self.setAcceptHoverEvents(True) + + def contextMenuEvent(self, event): + menu = QMenu() + change_color_action = QAction("Change Color", self) + change_color_action.triggered.connect(self.onChangeColor) + menu.addAction(change_color_action) + menu.exec_(event.screenPos()) + + def onChangeColor(self): + current_color = QColor(*self._colour) + selected_color = QColorDialog.getColor(current_color) + + if selected_color.isValid(): + new_color = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) + self._colour = new_color + self.sig_colour_change.emit(self._name, new_color) @property def name(self) -> str: From eb8de8cd589c95af59b41ac44b412fe20c31a20c Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Tue, 6 Feb 2024 17:34:12 +0000 Subject: [PATCH 08/10] get rid if context menu --- .../windows/spectrum_viewer/spectrum_widget.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index fec42a633fa..0627af7269d 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -5,8 +5,8 @@ from typing import TYPE_CHECKING, Optional from PyQt5.QtCore import pyqtSignal, Qt, QSignalBlocker -from PyQt5.QtGui import QColor -from PyQt5.QtWidgets import QColorDialog, QAction, QMenu +from PyQt5.QtGui import QColor, QCursor +from PyQt5.QtWidgets import QColorDialog, QAction from pyqtgraph import ROI, GraphicsLayoutWidget, LinearRegionItem, PlotItem, mkPen from mantidimaging.core.utility.close_enough_point import CloseEnoughPoint @@ -41,20 +41,15 @@ def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs): self.addScaleHandle([0, 1], [1, 0]) self._selected_row = None - self.setAcceptedMouseButtons(Qt.RightButton) - self.setAcceptHoverEvents(True) - - def contextMenuEvent(self, event): - menu = QMenu() - change_color_action = QAction("Change Color", self) + menu = self.getMenu() + change_color_action = QAction("Change ROI Colour", self) change_color_action.triggered.connect(self.onChangeColor) menu.addAction(change_color_action) - menu.exec_(event.screenPos()) + menu.exec_(QCursor.pos()) def onChangeColor(self): current_color = QColor(*self._colour) selected_color = QColorDialog.getColor(current_color) - if selected_color.isValid(): new_color = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) self._colour = new_color From 0038917c4c3b8f668d43189a010927a50f00b5bd Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Fri, 9 Feb 2024 13:51:08 +0000 Subject: [PATCH 09/10] Added QMenu --- .../gui/windows/spectrum_viewer/spectrum_widget.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 0627af7269d..9f28a4c3fe4 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -5,8 +5,8 @@ from typing import TYPE_CHECKING, Optional from PyQt5.QtCore import pyqtSignal, Qt, QSignalBlocker -from PyQt5.QtGui import QColor, QCursor -from PyQt5.QtWidgets import QColorDialog, QAction +from PyQt5.QtGui import QColor +from PyQt5.QtWidgets import QColorDialog, QAction, QMenu from pyqtgraph import ROI, GraphicsLayoutWidget, LinearRegionItem, PlotItem, mkPen from mantidimaging.core.utility.close_enough_point import CloseEnoughPoint @@ -41,11 +41,10 @@ def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs): self.addScaleHandle([0, 1], [1, 0]) self._selected_row = None - menu = self.getMenu() + self.menu = QMenu() change_color_action = QAction("Change ROI Colour", self) change_color_action.triggered.connect(self.onChangeColor) - menu.addAction(change_color_action) - menu.exec_(QCursor.pos()) + self.menu.addAction(change_color_action) def onChangeColor(self): current_color = QColor(*self._colour) @@ -55,6 +54,9 @@ def onChangeColor(self): self._colour = new_color self.sig_colour_change.emit(self._name, new_color) + def contextMenuEnabled(self): + return True + @property def name(self) -> str: return self._name From a9a009533c88e005d1436282edee808302d2b831 Mon Sep 17 00:00:00 2001 From: ashmeigh Date: Fri, 9 Feb 2024 14:49:06 +0000 Subject: [PATCH 10/10] Added QMenu --- mantidimaging/gui/windows/spectrum_viewer/presenter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/presenter.py b/mantidimaging/gui/windows/spectrum_viewer/presenter.py index 8a96525f98b..1e67adfdabc 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/presenter.py +++ b/mantidimaging/gui/windows/spectrum_viewer/presenter.py @@ -232,8 +232,8 @@ def change_roi_colour(self, roi_name: str, new_colour: tuple) -> None: @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 + if roi_name in self.view.spectrum_widget.roi_dict: + self.view.spectrum_widget.roi_dict[roi_name].colour = new_colour self.view.update_roi_color_in_table(roi_name, new_colour) def add_rits_roi(self) -> None: