Added BarGraphItem.shape() to allow better mouse interaction

This commit is contained in:
Luke Campagnola 2014-02-09 10:38:29 -05:00
parent dc1af8946e
commit 5488f9ec84
4 changed files with 25 additions and 11 deletions

View File

@ -31,6 +31,7 @@ pyqtgraph-0.9.9 [unreleased]
- Added FillBetweenItem.setCurves()
- MultiPlotWidget now has setMinimumPlotHeight method and displays scroll bar
when plots do not fit inside the widget.
- Added BarGraphItem.shape() to allow better mouse interaction
Bugfixes:
- PlotCurveItem now has correct clicking behavior--clicks within a few px

View File

@ -28,7 +28,7 @@ win.addItem(bg3)
# Final example shows how to handle mouse clicks:
class BarGraph(pg.BarGraphItem):
def mouseClickEvent(self, event):
print "clicked"
print("clicked")
bg = BarGraph(x=x, y=y1*0.3+2, height=0.4+y1*0.2, width=0.8)

View File

@ -92,15 +92,11 @@ class GraphicsScene(QtGui.QGraphicsScene):
self.clickEvents = []
self.dragButtons = []
self.prepItems = weakref.WeakKeyDictionary() ## set of items with prepareForPaintMethods
self.mouseGrabber = None
self.dragItem = None
self.lastDrag = None
self.hoverItems = weakref.WeakKeyDictionary()
self.lastHoverEvent = None
#self.searchRect = QtGui.QGraphicsRectItem()
#self.searchRect.setPen(fn.mkPen(200,0,0))
#self.addItem(self.searchRect)
self.contextMenu = [QtGui.QAction("Export...", self)]
self.contextMenu[0].triggered.connect(self.showExportDialog)
@ -437,10 +433,10 @@ class GraphicsScene(QtGui.QGraphicsScene):
for item in items:
if hoverable and not hasattr(item, 'hoverEvent'):
continue
shape = item.shape()
shape = item.shape() # Note: default shape() returns boundingRect()
if shape is None:
continue
if item.mapToScene(shape).contains(point):
if shape.contains(item.mapFromScene(point)):
items2.append(item)
## Sort by descending Z-order (don't trust scene.itms() to do this either)

View File

@ -47,16 +47,20 @@ class BarGraphItem(GraphicsObject):
pens=None,
brushes=None,
)
self._shape = None
self.picture = None
self.setOpts(**opts)
def setOpts(self, **opts):
self.opts.update(opts)
self.picture = None
self._shape = None
self.update()
self.informViewBoundsChanged()
def drawPicture(self):
self.picture = QtGui.QPicture()
self._shape = QtGui.QPainterPath()
p = QtGui.QPainter(self.picture)
pen = self.opts['pen']
@ -122,6 +126,10 @@ class BarGraphItem(GraphicsObject):
if brushes is not None:
p.setBrush(fn.mkBrush(brushes[i]))
if np.isscalar(x0):
x = x0
else:
x = x0[i]
if np.isscalar(y0):
y = y0
else:
@ -130,9 +138,15 @@ class BarGraphItem(GraphicsObject):
w = width
else:
w = width[i]
if np.isscalar(height):
h = height
else:
h = height[i]
p.drawRect(QtCore.QRectF(x0[i], y, w, height[i]))
rect = QtCore.QRectF(x, y, w, h)
p.drawRect(rect)
self._shape.addRect(rect)
p.end()
self.prepareGeometryChange()
@ -148,4 +162,7 @@ class BarGraphItem(GraphicsObject):
self.drawPicture()
return QtCore.QRectF(self.picture.boundingRect())
def shape(self):
if self.picture is None:
self.drawPicture()
return self._shape