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() - Added FillBetweenItem.setCurves()
- MultiPlotWidget now has setMinimumPlotHeight method and displays scroll bar - MultiPlotWidget now has setMinimumPlotHeight method and displays scroll bar
when plots do not fit inside the widget. when plots do not fit inside the widget.
- Added BarGraphItem.shape() to allow better mouse interaction
Bugfixes: Bugfixes:
- PlotCurveItem now has correct clicking behavior--clicks within a few px - 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: # Final example shows how to handle mouse clicks:
class BarGraph(pg.BarGraphItem): class BarGraph(pg.BarGraphItem):
def mouseClickEvent(self, event): 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) 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.clickEvents = []
self.dragButtons = [] self.dragButtons = []
self.prepItems = weakref.WeakKeyDictionary() ## set of items with prepareForPaintMethods
self.mouseGrabber = None self.mouseGrabber = None
self.dragItem = None self.dragItem = None
self.lastDrag = None self.lastDrag = None
self.hoverItems = weakref.WeakKeyDictionary() self.hoverItems = weakref.WeakKeyDictionary()
self.lastHoverEvent = None 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 = [QtGui.QAction("Export...", self)]
self.contextMenu[0].triggered.connect(self.showExportDialog) self.contextMenu[0].triggered.connect(self.showExportDialog)
@ -437,10 +433,10 @@ class GraphicsScene(QtGui.QGraphicsScene):
for item in items: for item in items:
if hoverable and not hasattr(item, 'hoverEvent'): if hoverable and not hasattr(item, 'hoverEvent'):
continue continue
shape = item.shape() shape = item.shape() # Note: default shape() returns boundingRect()
if shape is None: if shape is None:
continue continue
if item.mapToScene(shape).contains(point): if shape.contains(item.mapFromScene(point)):
items2.append(item) items2.append(item)
## Sort by descending Z-order (don't trust scene.itms() to do this either) ## 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, pens=None,
brushes=None, brushes=None,
) )
self._shape = None
self.picture = None
self.setOpts(**opts) self.setOpts(**opts)
def setOpts(self, **opts): def setOpts(self, **opts):
self.opts.update(opts) self.opts.update(opts)
self.picture = None self.picture = None
self._shape = None
self.update() self.update()
self.informViewBoundsChanged() self.informViewBoundsChanged()
def drawPicture(self): def drawPicture(self):
self.picture = QtGui.QPicture() self.picture = QtGui.QPicture()
self._shape = QtGui.QPainterPath()
p = QtGui.QPainter(self.picture) p = QtGui.QPainter(self.picture)
pen = self.opts['pen'] pen = self.opts['pen']
@ -122,6 +126,10 @@ class BarGraphItem(GraphicsObject):
if brushes is not None: if brushes is not None:
p.setBrush(fn.mkBrush(brushes[i])) p.setBrush(fn.mkBrush(brushes[i]))
if np.isscalar(x0):
x = x0
else:
x = x0[i]
if np.isscalar(y0): if np.isscalar(y0):
y = y0 y = y0
else: else:
@ -130,9 +138,15 @@ class BarGraphItem(GraphicsObject):
w = width w = width
else: else:
w = width[i] w = width[i]
if np.isscalar(height):
p.drawRect(QtCore.QRectF(x0[i], y, w, height[i])) h = height
else:
h = height[i]
rect = QtCore.QRectF(x, y, w, h)
p.drawRect(rect)
self._shape.addRect(rect)
p.end() p.end()
self.prepareGeometryChange() self.prepareGeometryChange()
@ -148,4 +162,7 @@ class BarGraphItem(GraphicsObject):
self.drawPicture() self.drawPicture()
return QtCore.QRectF(self.picture.boundingRect()) return QtCore.QRectF(self.picture.boundingRect())
def shape(self):
if self.picture is None:
self.drawPicture()
return self._shape