summaryrefslogtreecommitdiff
path: root/examples/plotInteractiveImageROI.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/plotInteractiveImageROI.py')
-rw-r--r--examples/plotInteractiveImageROI.py62
1 files changed, 51 insertions, 11 deletions
diff --git a/examples/plotInteractiveImageROI.py b/examples/plotInteractiveImageROI.py
index d45bdf5..a373966 100644
--- a/examples/plotInteractiveImageROI.py
+++ b/examples/plotInteractiveImageROI.py
@@ -1,8 +1,7 @@
#!/usr/bin/env python
-# coding: utf-8
# /*##########################################################################
#
-# Copyright (c) 2018 European Synchrotron Radiation Facility
+# Copyright (c) 2018-2021 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -38,15 +37,17 @@ from silx.gui import qt
from silx.gui.plot import Plot2D
from silx.gui.plot.tools.roi import RegionOfInterestManager
from silx.gui.plot.tools.roi import RegionOfInterestTableWidget
+from silx.gui.plot.tools.roi import RoiModeSelectorAction
from silx.gui.plot.items.roi import RectangleROI
+from silx.gui.plot.items import LineMixIn, SymbolMixIn
+from silx.gui.plot.actions import control as control_actions
def dummy_image():
"""Create a dummy image"""
x = numpy.linspace(-1.5, 1.5, 1024)
xv, yv = numpy.meshgrid(x, x)
- signal = numpy.exp(- (xv ** 2 / 0.15 ** 2
- + yv ** 2 / 0.25 ** 2))
+ signal = numpy.exp(-(xv**2 / 0.15**2 + yv**2 / 0.25**2))
# add noise
signal += 0.3 * numpy.random.random(size=signal.shape)
return signal
@@ -56,27 +57,40 @@ app = qt.QApplication([]) # Start QApplication
# Create the plot widget and add an image
plot = Plot2D()
-plot.getDefaultColormap().setName('viridis')
+plot.getDefaultColormap().setName("viridis")
+plot.setKeepDataAspectRatio(True)
plot.addImage(dummy_image())
+toolbar = qt.QToolBar()
+toolbar.addAction(control_actions.OpenGLAction(parent=toolbar, plot=plot))
+plot.addToolBar(toolbar)
+
# Create the object controlling the ROIs and set it up
roiManager = RegionOfInterestManager(plot)
-roiManager.setColor('pink') # Set the color of ROI
+roiManager.setColor("pink") # Set the color of ROI
# Set the name of each created region of interest
def updateAddedRegionOfInterest(roi):
"""Called for each added region of interest: set the name"""
- if roi.getLabel() == '':
- roi.setLabel('ROI %d' % len(roiManager.getRois()))
+ if roi.getName() == "":
+ roi.setName("ROI %d" % len(roiManager.getRois()))
+ if isinstance(roi, LineMixIn):
+ roi.setLineWidth(1)
+ roi.setLineStyle("--")
+ if isinstance(roi, SymbolMixIn):
+ roi.setSymbolSize(5)
+ roi.setSelectable(True)
+ roi.setEditable(True)
roiManager.sigRoiAdded.connect(updateAddedRegionOfInterest)
+
# Add a rectangular region of interest
roi = RectangleROI()
roi.setGeometry(origin=(50, 50), size=(200, 200))
-roi.setLabel('Initial ROI')
+roi.setName("Initial ROI")
roiManager.addRoi(roi)
# Create the table widget displaying
@@ -92,13 +106,38 @@ for roiClass in roiManager.getSupportedRoiClasses():
action = roiManager.getInteractionModeAction(roiClass)
roiToolbar.addAction(action)
+
+class AutoHideToolBar(qt.QToolBar):
+ """A toolbar which hide itself if no actions are visible"""
+
+ def actionEvent(self, event):
+ if event.type() == qt.QEvent.ActionChanged:
+ self._updateVisibility()
+ return qt.QToolBar.actionEvent(self, event)
+
+ def _updateVisibility(self):
+ visible = False
+ for action in self.actions():
+ if action.isVisible():
+ visible = True
+ break
+ self.setVisible(visible)
+
+
+roiToolbarEdit = AutoHideToolBar()
+modeSelectorAction = RoiModeSelectorAction()
+modeSelectorAction.setRoiManager(roiManager)
+roiToolbarEdit.addAction(modeSelectorAction)
+
# Add the region of interest table and the buttons to a dock widget
widget = qt.QWidget()
layout = qt.QVBoxLayout()
widget.setLayout(layout)
layout.addWidget(roiToolbar)
+layout.addWidget(roiToolbarEdit)
layout.addWidget(roiTable)
+
def roiDockVisibilityChanged(visible):
"""Handle change of visibility of the roi dock widget
@@ -107,13 +146,14 @@ def roiDockVisibilityChanged(visible):
if not visible:
roiManager.stop()
-dock = qt.QDockWidget('Image ROI')
+
+dock = qt.QDockWidget("Image ROI")
dock.setWidget(widget)
dock.visibilityChanged.connect(roiDockVisibilityChanged)
plot.addTabbedDockWidget(dock)
# Show the widget and start the application
plot.show()
-result = app.exec_()
+result = app.exec()
app.deleteLater()
sys.exit(result)