Skip to content

Commit

Permalink
Zoomable spectrum plot in the Spectrum Viewer (#2133)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc committed May 9, 2024
2 parents ccc3273 + 843a0d1 commit a9fbdbb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#2133: The Spectrum plot in the Spectrum Viewer can be zoomed via a click and drag zoom-box
8 changes: 6 additions & 2 deletions mantidimaging/gui/widgets/mi_mini_image_view/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class MIMiniImageView(GraphicsLayout, BadDataOverlay, AutoColorMenu):
bright_levels: None | list[int] = None
levels: list[float]

def __init__(self, name: str = "MIMiniImageView", parent: QWidget | None = None, recon_mode: bool = False):
def __init__(self,
name: str = "MIMiniImageView",
parent: QWidget | None = None,
recon_mode: bool = False,
view_box_type: type[ViewBox] = ViewBox):
super().__init__()

self.name = name.title()
self.im = ImageItem()
self.vb = ViewBox(invertY=True, lockAspect=True, name=name)
self.vb = view_box_type(invertY=True, lockAspect=True, name=name)
self.vb.addItem(self.im)
self.hist = HistogramLUTItem(self.im)
graveyard.append(self.vb)
Expand Down
51 changes: 47 additions & 4 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.QtGui import QColor
from PyQt5.QtWidgets import QColorDialog, QAction, QMenu, QSplitter, QWidget, QVBoxLayout

from pyqtgraph import ROI, GraphicsLayoutWidget, LinearRegionItem, PlotItem, mkPen, ViewBox

from mantidimaging.core.utility.close_enough_point import CloseEnoughPoint
Expand Down Expand Up @@ -257,6 +258,49 @@ def rename_roi(self, old_name: str, new_name: str) -> None:
self.roi_dict[new_name].rename_roi(new_name)


class CustomViewBox(ViewBox):

def __init__(self, *args, **kwds):
#kwds['enableMenu'] = False
ViewBox.__init__(self, *args, **kwds)
self.setMouseMode(self.PanMode)

def keyPressEvent(self, event):
if event.key() == Qt.Key_Control:
self.setMouseMode(self.RectMode)
for child in self.allChildren():
if isinstance(child, LinearRegionItem):
child.setMovable(False)
elif isinstance(child, SpectrumROI):
child.translatable = False

def keyReleaseEvent(self, event):
if event.key() == Qt.Key_Control:
self.rbScaleBox.hide()
self.setMouseMode(self.PanMode)
for child in self.allChildren():
if isinstance(child, LinearRegionItem):
child.setMovable(True)
elif isinstance(child, SpectrumROI):
child.translatable = True

## reimplement right-click to zoom out
def mouseClickEvent(self, ev):
if ev.button() == Qt.MouseButton.RightButton and not self.menuEnabled():
self.autoRange()
else:
ViewBox.mouseClickEvent(self, ev)

## reimplement mouseDragEvent to disable continuous axis zoom
def mouseDragEvent(self, ev, axis=None):
if axis is not None and ev.button() == Qt.MouseButton.RightButton:
ev.ignore()
elif ev.button() == Qt.MouseButton.LeftButton:
ViewBox.mouseDragEvent(self, ev)
else:
ViewBox.mouseDragEvent(self, ev, axis=axis)


class SpectrumPlotWidget(GraphicsLayoutWidget):

spectrum: PlotItem
Expand All @@ -268,8 +312,8 @@ class SpectrumPlotWidget(GraphicsLayoutWidget):
def __init__(self) -> None:
super().__init__()

self.vb = ViewBox()
self.spectrum = self.addPlot(viewbox=self.vb)
self.spectrum_viewbox = CustomViewBox(enableMenu=True)
self.spectrum = self.addPlot(viewBox=self.spectrum_viewbox)
self.nextRow()
self._tof_range_label = self.addLabel()
self.nextRow()
Expand Down Expand Up @@ -309,7 +353,6 @@ class SpectrumProjectionWidget(GraphicsLayoutWidget):

def __init__(self) -> None:
super().__init__()

self.image = MIMiniImageView(name="Projection")
self.image = MIMiniImageView(name="Projection", view_box_type=CustomViewBox)
self.addItem(self.image, 0, 0)
self.ci.layout.setRowStretchFactor(0, 3)
2 changes: 1 addition & 1 deletion mantidimaging/gui/windows/spectrum_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(self, main_window: MainWindowView):
self.spectrum_widget.roi_changed.connect(self.presenter.handle_roi_moved)
self.spectrum_widget.roiColorChangeRequested.connect(self.presenter.change_roi_colour)

self.spectrum_right_click_menu = self.spectrum_widget.spectrum_plot_widget.spectrum.vb.menu
self.spectrum_right_click_menu = self.spectrum.spectrum_viewbox.menu
self.units_menu = self.spectrum_right_click_menu.addMenu("Units")
self.tof_mode_select_group = QActionGroup(self)

Expand Down

0 comments on commit a9fbdbb

Please sign in to comment.