ArrowItem has more flexibility in the shapes it can draw

This commit is contained in:
Luke Campagnola 2012-05-29 23:19:20 -04:00
parent f258c3d87c
commit 35357308b9

View File

@ -1,8 +1,9 @@
from pyqtgraph.Qt import QtGui, QtCore from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph.functions as fn import pyqtgraph.functions as fn
import numpy as np
__all__ = ['ArrowItem'] __all__ = ['ArrowItem']
class ArrowItem(QtGui.QGraphicsPolygonItem):
class ArrowItem(QtGui.QGraphicsPathItem):
""" """
For displaying scale-invariant arrows. For displaying scale-invariant arrows.
For arrows pointing to a location on a curve, see CurveArrow For arrows pointing to a location on a curve, see CurveArrow
@ -11,16 +12,16 @@ class ArrowItem(QtGui.QGraphicsPolygonItem):
def __init__(self, **opts): def __init__(self, **opts):
QtGui.QGraphicsPolygonItem.__init__(self, opts.get('parent', None)) QtGui.QGraphicsPathItem.__init__(self, opts.get('parent', None))
defOpts = { defOpts = {
'style': 'tri',
'pxMode': True, 'pxMode': True,
'size': 20,
'angle': -150, ## If the angle is 0, the arrow points left 'angle': -150, ## If the angle is 0, the arrow points left
'pos': (0,0), 'pos': (0,0),
'width': None, ## width is automatically size / 2. 'headLen': 20,
'tipAngle': 25, 'tipAngle': 25,
'baseAngle': 90, 'baseAngle': 0,
'tailLen': None,
'tailWidth': 3,
'pen': (200,200,200), 'pen': (200,200,200),
'brush': (50,50,200), 'brush': (50,50,200),
} }
@ -37,23 +38,9 @@ class ArrowItem(QtGui.QGraphicsPolygonItem):
def setStyle(self, **opts): def setStyle(self, **opts):
self.opts = opts self.opts = opts
if opts['style'] == 'tri': opt = {k:self.opts[k] for k in ['headLen', 'tipAngle', 'baseAngle', 'tailLen', 'tailWidth']}
if opts['width'] is None: self.path = fn.makeArrowPath(**opt)
width = opts['size'] / 2. self.setPath(self.path)
else:
width = opts['width']
points = [
QtCore.QPointF(0,0),
QtCore.QPointF(opts['size'],-width/2.),
QtCore.QPointF(opts['size'],width/2.),
]
poly = QtGui.QPolygonF(points)
else:
raise Exception("Unrecognized arrow style '%s'" % opts['style'])
self.setPolygon(poly)
if opts['pxMode']: if opts['pxMode']:
self.setFlags(self.flags() | self.ItemIgnoresTransformations) self.setFlags(self.flags() | self.ItemIgnoresTransformations)
@ -62,4 +49,4 @@ class ArrowItem(QtGui.QGraphicsPolygonItem):
def paint(self, p, *args): def paint(self, p, *args):
p.setRenderHint(QtGui.QPainter.Antialiasing) p.setRenderHint(QtGui.QPainter.Antialiasing)
QtGui.QGraphicsPolygonItem.paint(self, p, *args) QtGui.QGraphicsPathItem.paint(self, p, *args)