Cleanup: add docstrings and setter methods to InfLineLabel, remove unused code

This commit is contained in:
Luke Campagnola 2016-02-22 00:23:36 -08:00
parent a72fec51b1
commit 7a0dfd768a
2 changed files with 46 additions and 23 deletions

View File

@ -18,14 +18,12 @@ pg.setConfigOptions(antialias=True)
p1 = win.addPlot(title="Plot Items example", y=np.random.normal(size=100, scale=10), pen=0.5) p1 = win.addPlot(title="Plot Items example", y=np.random.normal(size=100, scale=10), pen=0.5)
p1.setYRange(-40, 40) p1.setYRange(-40, 40)
inf1 = pg.InfiniteLine(movable=True, angle=90, text='x={value:0.2f}', inf1 = pg.InfiniteLine(movable=True, angle=90, label='x={value:0.2f}',
textOpts={'position':0.1, 'color': (200,200,100), 'fill': (200,200,200,50), 'movable': True}) labelOpts={'position':0.1, 'color': (200,200,100), 'fill': (200,200,200,50), 'movable': True})
inf2 = pg.InfiniteLine(movable=True, angle=0, pen=(0, 0, 200), bounds = [-20, 20], hoverPen=(0,200,0), text='y={value:0.2f}mm', inf2 = pg.InfiniteLine(movable=True, angle=0, pen=(0, 0, 200), bounds = [-20, 20], hoverPen=(0,200,0), label='y={value:0.2f}mm',
textOpts={'color': (200,0,0), 'movable': True, 'fill': (0, 0, 200, 100)}) labelOpts={'color': (200,0,0), 'movable': True, 'fill': (0, 0, 200, 100)})
inf3 = pg.InfiniteLine(movable=True, angle=45, pen='g', text='diagonal', textOpts={'rotateAxis': [1, 0], 'fill': (0, 200, 0, 100), 'movable': True}) inf3 = pg.InfiniteLine(movable=True, angle=45, pen='g', label='diagonal', labelOpts={'rotateAxis': [1, 0], 'fill': (0, 200, 0, 100), 'movable': True})
inf1.setPos([2,2]) inf1.setPos([2,2])
#inf1.setTextLocation(position=0.75)
#inf2.setTextLocation(shift=0.8)
p1.addItem(inf1) p1.addItem(inf1)
p1.addItem(inf2) p1.addItem(inf2)
p1.addItem(inf3) p1.addItem(inf3)

View File

@ -31,7 +31,7 @@ class InfiniteLine(GraphicsObject):
sigPositionChanged = QtCore.Signal(object) sigPositionChanged = QtCore.Signal(object)
def __init__(self, pos=None, angle=90, pen=None, movable=False, bounds=None, def __init__(self, pos=None, angle=90, pen=None, movable=False, bounds=None,
hoverPen=None, text=None, textOpts=None, name=None): hoverPen=None, label=None, labelOpts=None, name=None):
""" """
=============== ================================================================== =============== ==================================================================
**Arguments:** **Arguments:**
@ -47,10 +47,10 @@ class InfiniteLine(GraphicsObject):
Default pen is red. Default pen is red.
bounds Optional [min, max] bounding values. Bounds are only valid if the bounds Optional [min, max] bounding values. Bounds are only valid if the
line is vertical or horizontal. line is vertical or horizontal.
text Text to be displayed in a label attached to the line, or label Text to be displayed in a label attached to the line, or
None to show no label (default is None). May optionally None to show no label (default is None). May optionally
include formatting strings to display the line value. include formatting strings to display the line value.
textOpts A dict of keyword arguments to use when constructing the labelOpts A dict of keyword arguments to use when constructing the
text label. See :class:`InfLineLabel`. text label. See :class:`InfLineLabel`.
name Name of the item name Name of the item
=============== ================================================================== =============== ==================================================================
@ -68,15 +68,9 @@ class InfiniteLine(GraphicsObject):
self.p = [0, 0] self.p = [0, 0]
self.setAngle(angle) self.setAngle(angle)
if text is not None: if label is not None:
textOpts = {} if textOpts is None else textOpts labelOpts = {} if labelOpts is None else labelOpts
self.textItem = InfLineLabel(self, text=text, **textOpts) self.label = InfLineLabel(self, text=label, **labelOpts)
self.textItem.setParentItem(self)
self.anchorLeft = (1., 0.5)
self.anchorRight = (0., 0.5)
self.anchorUp = (0.5, 1.)
self.anchorDown = (0.5, 0.)
if pos is None: if pos is None:
pos = Point(0,0) pos = Point(0,0)
@ -167,10 +161,6 @@ class InfiniteLine(GraphicsObject):
newPos[1] = min(newPos[1], self.maxRange[1]) newPos[1] = min(newPos[1], self.maxRange[1])
if self.p != newPos: if self.p != newPos:
# Invalidate bounding rect and line
self._boundingRect = None
self._line = None
self.p = newPos self.p = newPos
self._invalidateCache() self._invalidateCache()
GraphicsObject.setPos(self, Point(self.p)) GraphicsObject.setPos(self, Point(self.p))
@ -308,6 +298,19 @@ class InfLineLabel(TextItem):
the line and within the view box. the line and within the view box.
* Automatically reformats text when the line value has changed. * Automatically reformats text when the line value has changed.
* Can optionally be dragged to change its location along the line. * Can optionally be dragged to change its location along the line.
* Optionally aligns to its parent line.
=============== ==================================================================
**Arguments:**
line The InfiniteLine to which this label will be attached.
text String to display in the label. May contain a {value} formatting
string to display the current value of the line.
movable Bool; if True, then the label can be dragged along the line.
position Relative position (0.0-1.0) within the view to position the label
along the line.
=============== ==================================================================
All extra keyword arguments are passed to TextItem.
""" """
def __init__(self, line, text="", movable=False, position=0.5, **kwds): def __init__(self, line, text="", movable=False, position=0.5, **kwds):
self.line = line self.line = line
@ -316,6 +319,7 @@ class InfLineLabel(TextItem):
self.format = text self.format = text
self.line.sigPositionChanged.connect(self.valueChanged) self.line.sigPositionChanged.connect(self.valueChanged)
TextItem.__init__(self, **kwds) TextItem.__init__(self, **kwds)
self.setParentItem(line)
self.valueChanged() self.valueChanged()
def valueChanged(self): def valueChanged(self):
@ -361,9 +365,30 @@ class InfLineLabel(TextItem):
self.updatePosition() self.updatePosition()
def setMovable(self, m): def setMovable(self, m):
"""Set whether this label is movable by dragging along the line.
"""
self.movable = m self.movable = m
self.setAcceptHoverEvents(m) self.setAcceptHoverEvents(m)
def setPosition(self, p):
"""Set the relative position (0.0-1.0) of this label within the view box
and along the line.
For horizontal (angle=0) and vertical (angle=90) lines, a value of 0.0
places the text at the bottom or left of the view, respectively.
"""
self.orthoPos = p
self.updatePosition()
def setFormat(self, text):
"""Set the text format string for this label.
May optionally contain "{value}" to include the lines current value
(the text will be reformatted whenever the line is moved).
"""
self.format = format
self.valueChanged()
def mouseDragEvent(self, ev): def mouseDragEvent(self, ev):
if self.movable and ev.button() == QtCore.Qt.LeftButton: if self.movable and ev.button() == QtCore.Qt.LeftButton:
if ev.isStart(): if ev.isStart():