From 0de0bf4c44e5600303c5fdf317bf498d120ac947 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Thu, 28 Sep 2017 09:05:08 -0700 Subject: [PATCH] Fix: very small ellipse/circle ROIs have bad click areas --- pyqtgraph/graphicsItems/ROI.py | 42 ++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/pyqtgraph/graphicsItems/ROI.py b/pyqtgraph/graphicsItems/ROI.py index 473506cf..850104c0 100644 --- a/pyqtgraph/graphicsItems/ROI.py +++ b/pyqtgraph/graphicsItems/ROI.py @@ -1725,10 +1725,18 @@ class EllipseROI(ROI): """ def __init__(self, pos, size, **args): #QtGui.QGraphicsRectItem.__init__(self, 0, 0, size[0], size[1]) + self.path = None ROI.__init__(self, pos, size, **args) + self.sigRegionChanged.connect(self._clearPath) + self._addHandles() + + def _addHandles(self): self.addRotateHandle([1.0, 0.5], [0.5, 0.5]) self.addScaleHandle([0.5*2.**-0.5 + 0.5, 0.5*2.**-0.5 + 0.5], [0.5, 0.5]) + def _clearPath(self): + self.path = None + def paint(self, p, opt, widget): r = self.boundingRect() p.setRenderHint(QtGui.QPainter.Antialiasing) @@ -1764,8 +1772,27 @@ class EllipseROI(ROI): return arr * mask def shape(self): - self.path = QtGui.QPainterPath() - self.path.addEllipse(self.boundingRect()) + if self.path is None: + path = QtGui.QPainterPath() + + # Note: Qt has a bug where very small ellipses (radius <0.001) do + # not correctly intersect with mouse position (upper-left and + # lower-right quadrants are not clickable). + #path.addEllipse(self.boundingRect()) + + # Workaround: manually draw the path. + br = self.boundingRect() + center = br.center() + r1 = br.width() / 2. + r2 = br.height() / 2. + theta = np.linspace(0, 2*np.pi, 24) + x = center.x() + r1 * np.cos(theta) + y = center.y() + r2 * np.sin(theta) + path.moveTo(x[0], y[0]) + for i in range(1, len(x)): + path.lineTo(x[i], y[i]) + self.path = path + return self.path @@ -1782,10 +1809,15 @@ class CircleROI(EllipseROI): ============== ============================================================= """ - def __init__(self, pos, size, **args): - ROI.__init__(self, pos, size, **args) + def __init__(self, pos, size=None, radius=None, **args): + if size is None: + if radius is None: + raise TypeError("Must provide either size or radius.") + size = (radius*2, radius*2) + EllipseROI.__init__(self, pos, size, **args) self.aspectLocked = True - #self.addTranslateHandle([0.5, 0.5]) + + def _addHandles(self): self.addScaleHandle([0.5*2.**-0.5 + 0.5, 0.5*2.**-0.5 + 0.5], [0.5, 0.5])