From 5488f9ec849f7b7779afdfcc118870d78600fead Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Sun, 9 Feb 2014 10:38:29 -0500 Subject: [PATCH] Added BarGraphItem.shape() to allow better mouse interaction --- CHANGELOG | 1 + examples/BarGraphItem.py | 2 +- pyqtgraph/GraphicsScene/GraphicsScene.py | 8 ++------ pyqtgraph/graphicsItems/BarGraphItem.py | 25 ++++++++++++++++++++---- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index ec558564..40bf39a2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/examples/BarGraphItem.py b/examples/BarGraphItem.py index c93a1966..6caa8862 100644 --- a/examples/BarGraphItem.py +++ b/examples/BarGraphItem.py @@ -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) diff --git a/pyqtgraph/GraphicsScene/GraphicsScene.py b/pyqtgraph/GraphicsScene/GraphicsScene.py index d61a1fa4..a57cca34 100644 --- a/pyqtgraph/GraphicsScene/GraphicsScene.py +++ b/pyqtgraph/GraphicsScene/GraphicsScene.py @@ -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) diff --git a/pyqtgraph/graphicsItems/BarGraphItem.py b/pyqtgraph/graphicsItems/BarGraphItem.py index 9f9dbcde..a1d5d029 100644 --- a/pyqtgraph/graphicsItems/BarGraphItem.py +++ b/pyqtgraph/graphicsItems/BarGraphItem.py @@ -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] - - p.drawRect(QtCore.QRectF(x0[i], y, w, height[i])) - + if np.isscalar(height): + h = height + else: + h = 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()) - \ No newline at end of file + def shape(self): + if self.picture is None: + self.drawPicture() + return self._shape