Fix: very small ellipse/circle ROIs have bad click areas

This commit is contained in:
Luke Campagnola 2017-09-28 09:05:08 -07:00
parent 97b71a2b28
commit 0de0bf4c44

View File

@ -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])