Skip to content

Commit

Permalink
Merge pull request #3081 from vallsv/polish-roi-look-and-feel
Browse files Browse the repository at this point in the history
Polish roi look and feel
  • Loading branch information
t20100 authored May 29, 2020
2 parents 32570ae + e0522f8 commit 9f36b76
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
23 changes: 20 additions & 3 deletions silx/gui/plot/items/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,16 +1810,20 @@ def _updatedStyle(self, event, style):
self.__shape.setColor(style.getColor())
self.__shape.setLineStyle(style.getLineStyle())
self.__shape.setLineWidth(style.getLineWidth())
if self._handleClose is not None:
color = self._computeHandleColor(style.getColor())
self._handleClose.setColor(color)

def __createShape(self, interaction=False):
kind = "polygon" if not interaction else "polylines"
shape = items.Shape(kind)
shape.setPoints([[0, 0], [0, 0]])
shape.setFill(False)
shape.setOverlay(True)
shape.setLineStyle(self.getLineStyle())
shape.setLineWidth(self.getLineWidth())
shape.setColor(rgba(self.getColor()))
style = self.getCurrentStyle()
shape.setLineStyle(style.getLineStyle())
shape.setLineWidth(style.getLineWidth())
shape.setColor(rgba(style.getColor()))
return shape

def setFirstShapePoints(self, points):
Expand All @@ -1836,12 +1840,19 @@ def creationStarted(self):
color = self._computeHandleColor(rgba(self.getColor()))
self._handleClose.setColor(color)

# Hide the center while creating the first shape
self._handleCenter.setSymbol("")

# In interaction replace the polygon by a line, to display something unclosed
self.removeItem(self.__shape)
self.__shape = self.__createShape(interaction=True)
self.__shape.setPoints(self._points)
self.addItem(self.__shape)

def isBeingCreated(self):
"""Returns true if the ROI is in creation step"""
return self._handleClose is not None

def creationFinalized(self):
""""Called when the ROI creation interaction was finalized.
"""
Expand All @@ -1851,6 +1862,10 @@ def creationFinalized(self):
self.__shape = self.__createShape()
self.__shape.setPoints(self._points)
self.addItem(self.__shape)
# Hide the center while creating the first shape
self._handleCenter.setSymbol("+")
for handle in self._handlePoints:
handle.setSymbol("s")

def _updateText(self, text):
self._handleLabel.setText(text)
Expand Down Expand Up @@ -1879,6 +1894,8 @@ def setPoints(self, points):
if len(self._handlePoints) < len(points):
handle = self.addHandle()
self._handlePoints.append(handle)
if self.isBeingCreated():
handle.setSymbol("")
else:
handle = self._handlePoints.pop(-1)
self.removeHandle(handle)
Expand Down
12 changes: 7 additions & 5 deletions silx/gui/plot/tools/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def _handleInteraction(self, event):
if event['event'] == 'mouseClicked' and event['button'] == 'left':
points = numpy.array([(event['x'], event['y'])],
dtype=numpy.float64)
roi = self.createRoi(roiClass, points=points)
# Not an interactive creation
roi = self._createInteractiveRoi(roiClass, points=points)
roi.creationFinalized()
self.sigInteractiveRoiFinalized.emit(roi)
else: # other shapes
if (event['event'] in ('drawingProgress', 'drawingFinished') and
Expand All @@ -337,8 +339,7 @@ def _handleInteraction(self, event):
if self._drawnROI is None: # Create new ROI
# NOTE: Set something before createRoi, so isDrawing is True
self._drawnROI = object()
self._drawnROI = self.createRoi(roiClass, points=points)
self._drawnROI.creationStarted()
self._drawnROI = self._createInteractiveRoi(roiClass, points=points)
else:
self._drawnROI.setFirstShapePoints(points)

Expand Down Expand Up @@ -453,8 +454,8 @@ def _regionOfInterestChanged(self, event=None):
"""Handle ROI object changed"""
self.sigRoiChanged.emit()

def createRoi(self, roiClass, points, label=None, index=None):
"""Create a new ROI and add it to list of ROIs.
def _createInteractiveRoi(self, roiClass, points, label=None, index=None):
"""Create a new ROI with interactive creation.
:param class roiClass: The class of the ROI to create
:param numpy.ndarray points: The first shape used to create the ROI
Expand All @@ -469,6 +470,7 @@ def createRoi(self, roiClass, points, label=None, index=None):
roi = roiClass(parent=None)
if label is not None:
roi.setName(str(label))
roi.creationStarted()
roi.setFirstShapePoints(points)

self.addRoi(roi, index)
Expand Down
20 changes: 15 additions & 5 deletions silx/gui/plot/tools/test/testROI.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ def test(self):
manager.sigRoiChanged.connect(changedListener)

# Add a point
manager.createRoi(roiClass, points[0])
r = roiClass()
r.setFirstShapePoints(points[0])
manager.addRoi(r)
self.qapp.processEvents()
self.assertTrue(len(manager.getRois()), 1)
self.assertEqual(changedListener.callCount(), 1)
Expand All @@ -317,9 +319,13 @@ def test(self):
self.assertEqual(changedListener.callCount(), 2)

# Add two point
manager.createRoi(roiClass, points[0])
r = roiClass()
r.setFirstShapePoints(points[0])
manager.addRoi(r)
self.qapp.processEvents()
manager.createRoi(roiClass, points[1])
r = roiClass()
r.setFirstShapePoints(points[1])
manager.addRoi(r)
self.qapp.processEvents()
self.assertTrue(len(manager.getRois()), 2)
self.assertEqual(changedListener.callCount(), 4)
Expand All @@ -333,9 +339,13 @@ def test(self):
changedListener.clear()

# Add two point
manager.createRoi(roiClass, points[0])
r = roiClass()
r.setFirstShapePoints(points[0])
manager.addRoi(r)
self.qapp.processEvents()
manager.createRoi(roiClass, points[1])
r = roiClass()
r.setFirstShapePoints(points[1])
manager.addRoi(r)
self.qapp.processEvents()
self.assertTrue(len(manager.getRois()), 2)
self.assertEqual(changedListener.callCount(), 2)
Expand Down

0 comments on commit 9f36b76

Please sign in to comment.