2013-12-22 07:08:39 +00:00
|
|
|
from ..Qt import QtGui, QtCore
|
|
|
|
from ..Point import Point
|
2016-02-03 11:52:01 +00:00
|
|
|
from .GraphicsObject import GraphicsObject
|
2016-01-15 15:10:24 +00:00
|
|
|
from .TextItem import TextItem
|
2016-02-03 11:52:01 +00:00
|
|
|
from .ViewBox import ViewBox
|
2013-12-22 07:08:39 +00:00
|
|
|
from .. import functions as fn
|
2012-03-02 02:55:32 +00:00
|
|
|
import numpy as np
|
|
|
|
import weakref
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['InfiniteLine']
|
2016-01-15 15:10:24 +00:00
|
|
|
|
|
|
|
|
2016-02-03 11:52:01 +00:00
|
|
|
class InfiniteLine(GraphicsObject):
|
2016-01-15 15:10:24 +00:00
|
|
|
"""
|
2016-02-03 11:52:01 +00:00
|
|
|
**Bases:** :class:`GraphicsObject <pyqtgraph.GraphicsObject>`
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
Displays a line of infinite length.
|
|
|
|
This line may be dragged to indicate a position in data coordinates.
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-04-25 17:12:40 +00:00
|
|
|
=============================== ===================================================
|
2014-02-12 20:25:31 +00:00
|
|
|
**Signals:**
|
2012-04-25 17:12:40 +00:00
|
|
|
sigDragged(self)
|
|
|
|
sigPositionChangeFinished(self)
|
|
|
|
sigPositionChanged(self)
|
|
|
|
=============================== ===================================================
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
sigDragged = QtCore.Signal(object)
|
|
|
|
sigPositionChangeFinished = QtCore.Signal(object)
|
|
|
|
sigPositionChanged = QtCore.Signal(object)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
|
|
|
def __init__(self, pos=None, angle=90, pen=None, movable=False, bounds=None,
|
|
|
|
hoverPen=None, label=False, textColor=None, textFill=None,
|
2016-02-16 07:14:53 +00:00
|
|
|
textPosition=[0.05, 0.5], textFormat="{:.3f}", draggableLabel=False,
|
2016-02-03 11:52:01 +00:00
|
|
|
suffix=None, name='InfiniteLine'):
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
2014-02-12 20:25:31 +00:00
|
|
|
=============== ==================================================================
|
2014-02-03 20:13:10 +00:00
|
|
|
**Arguments:**
|
2014-02-05 20:04:33 +00:00
|
|
|
pos Position of the line. This can be a QPointF or a single value for
|
|
|
|
vertical/horizontal lines.
|
|
|
|
angle Angle of line in degrees. 0 is horizontal, 90 is vertical.
|
|
|
|
pen Pen to use when drawing line. Can be any arguments that are valid
|
|
|
|
for :func:`mkPen <pyqtgraph.mkPen>`. Default pen is transparent
|
|
|
|
yellow.
|
|
|
|
movable If True, the line can be dragged to a new position by the user.
|
2016-01-15 15:10:24 +00:00
|
|
|
hoverPen Pen to use when drawing line when hovering over it. Can be any
|
|
|
|
arguments that are valid for :func:`mkPen <pyqtgraph.mkPen>`.
|
|
|
|
Default pen is red.
|
2014-02-05 20:04:33 +00:00
|
|
|
bounds Optional [min, max] bounding values. Bounds are only valid if the
|
|
|
|
line is vertical or horizontal.
|
2016-01-15 15:10:24 +00:00
|
|
|
label if True, a label is displayed next to the line to indicate its
|
|
|
|
location in data coordinates
|
|
|
|
textColor color of the label. Can be any argument fn.mkColor can understand.
|
|
|
|
textFill A brush to use when filling within the border of the text.
|
2016-02-16 05:48:59 +00:00
|
|
|
textPosition list of float (0-1) that defines when the precise location of the
|
|
|
|
label. The first float governs the location of the label in the
|
|
|
|
direction of the line, whereas the second one governs the shift
|
|
|
|
of the label from one side of the line to the other in the
|
|
|
|
orthogonal direction.
|
2016-01-15 15:10:24 +00:00
|
|
|
textFormat Any new python 3 str.format() format.
|
2016-02-16 07:14:53 +00:00
|
|
|
draggableLabel Bool. If True, the user can relocate the label during the dragging.
|
|
|
|
If set to True, the first entry of textPosition is no longer
|
|
|
|
useful.
|
2016-02-03 11:52:01 +00:00
|
|
|
suffix If not None, corresponds to the unit to show next to the label
|
|
|
|
name name of the item
|
2014-02-12 20:25:31 +00:00
|
|
|
=============== ==================================================================
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-03 11:52:01 +00:00
|
|
|
GraphicsObject.__init__(self)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
if bounds is None: ## allowed value boundaries for orthogonal lines
|
|
|
|
self.maxRange = [None, None]
|
|
|
|
else:
|
|
|
|
self.maxRange = bounds
|
|
|
|
self.moving = False
|
2016-02-03 11:52:01 +00:00
|
|
|
self.setMovable(movable)
|
2012-04-04 13:29:35 +00:00
|
|
|
self.mouseHovering = False
|
2016-02-03 11:52:01 +00:00
|
|
|
self.p = [0, 0]
|
|
|
|
self.setAngle(angle)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
|
|
|
if textColor is None:
|
2016-02-03 11:52:01 +00:00
|
|
|
textColor = (200, 200, 100)
|
2016-01-15 15:10:24 +00:00
|
|
|
self.textColor = textColor
|
2016-02-03 11:52:01 +00:00
|
|
|
self.textFill = textFill
|
2016-02-16 05:48:59 +00:00
|
|
|
self.textPosition = textPosition
|
2016-02-03 11:52:01 +00:00
|
|
|
self.suffix = suffix
|
|
|
|
|
2016-02-19 08:41:42 +00:00
|
|
|
|
|
|
|
self.textItem = InfLineLabel(self, fill=textFill)
|
|
|
|
self.textItem.setParentItem(self)
|
|
|
|
self.setDraggableLabel(draggableLabel)
|
|
|
|
self.showLabel(label)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
|
|
|
self.anchorLeft = (1., 0.5)
|
|
|
|
self.anchorRight = (0., 0.5)
|
|
|
|
self.anchorUp = (0.5, 1.)
|
|
|
|
self.anchorDown = (0.5, 0.)
|
2016-02-03 11:52:01 +00:00
|
|
|
|
|
|
|
if pos is None:
|
|
|
|
pos = Point(0,0)
|
|
|
|
self.setPos(pos)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
if pen is None:
|
|
|
|
pen = (200, 200, 100)
|
|
|
|
self.setPen(pen)
|
2016-01-15 15:10:24 +00:00
|
|
|
if hoverPen is None:
|
|
|
|
self.setHoverPen(color=(255,0,0), width=self.pen.width())
|
|
|
|
else:
|
|
|
|
self.setHoverPen(hoverPen)
|
2012-03-02 02:55:32 +00:00
|
|
|
self.currentPen = self.pen
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-03 11:52:01 +00:00
|
|
|
self.format = textFormat
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-03 11:52:01 +00:00
|
|
|
self._name = name
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-04 02:28:59 +00:00
|
|
|
# Cache complex value for drawing speed-up (#PR267)
|
|
|
|
self._line = None
|
|
|
|
self._boundingRect = None
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def setMovable(self, m):
|
2012-04-25 17:12:40 +00:00
|
|
|
"""Set whether the line is movable by the user."""
|
2012-03-02 02:55:32 +00:00
|
|
|
self.movable = m
|
|
|
|
self.setAcceptHoverEvents(m)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def setBounds(self, bounds):
|
|
|
|
"""Set the (minimum, maximum) allowable values when dragging."""
|
|
|
|
self.maxRange = bounds
|
|
|
|
self.setValue(self.value())
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2014-02-28 21:16:13 +00:00
|
|
|
def setPen(self, *args, **kwargs):
|
2016-01-15 15:10:24 +00:00
|
|
|
"""Set the pen for drawing the line. Allowable arguments are any that are valid
|
2012-04-25 17:12:40 +00:00
|
|
|
for :func:`mkPen <pyqtgraph.mkPen>`."""
|
2014-02-28 21:16:13 +00:00
|
|
|
self.pen = fn.mkPen(*args, **kwargs)
|
2014-05-10 18:19:27 +00:00
|
|
|
if not self.mouseHovering:
|
|
|
|
self.currentPen = self.pen
|
|
|
|
self.update()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2014-05-10 18:19:27 +00:00
|
|
|
def setHoverPen(self, *args, **kwargs):
|
2016-01-15 15:10:24 +00:00
|
|
|
"""Set the pen for drawing the line while the mouse hovers over it.
|
|
|
|
Allowable arguments are any that are valid
|
2014-05-10 18:19:27 +00:00
|
|
|
for :func:`mkPen <pyqtgraph.mkPen>`.
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2014-05-10 18:19:27 +00:00
|
|
|
If the line is not movable, then hovering is also disabled.
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2014-05-10 18:19:27 +00:00
|
|
|
Added in version 0.9.9."""
|
|
|
|
self.hoverPen = fn.mkPen(*args, **kwargs)
|
|
|
|
if self.mouseHovering:
|
|
|
|
self.currentPen = self.hoverPen
|
|
|
|
self.update()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def setAngle(self, angle):
|
|
|
|
"""
|
|
|
|
Takes angle argument in degrees.
|
|
|
|
0 is horizontal; 90 is vertical.
|
2016-01-15 15:10:24 +00:00
|
|
|
|
|
|
|
Note that the use of value() and setValue() changes if the line is
|
2012-03-02 02:55:32 +00:00
|
|
|
not vertical or horizontal.
|
|
|
|
"""
|
|
|
|
self.angle = ((angle+45) % 180) - 45 ## -45 <= angle < 135
|
2016-02-03 11:52:01 +00:00
|
|
|
self.resetTransform()
|
|
|
|
self.rotate(self.angle)
|
2012-03-02 02:55:32 +00:00
|
|
|
self.update()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def setPos(self, pos):
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
if type(pos) in [list, tuple]:
|
|
|
|
newPos = pos
|
|
|
|
elif isinstance(pos, QtCore.QPointF):
|
|
|
|
newPos = [pos.x(), pos.y()]
|
|
|
|
else:
|
|
|
|
if self.angle == 90:
|
|
|
|
newPos = [pos, 0]
|
|
|
|
elif self.angle == 0:
|
|
|
|
newPos = [0, pos]
|
|
|
|
else:
|
|
|
|
raise Exception("Must specify 2D coordinate for non-orthogonal lines.")
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
## check bounds (only works for orthogonal lines)
|
|
|
|
if self.angle == 90:
|
2016-01-15 15:10:24 +00:00
|
|
|
if self.maxRange[0] is not None:
|
2012-03-02 02:55:32 +00:00
|
|
|
newPos[0] = max(newPos[0], self.maxRange[0])
|
|
|
|
if self.maxRange[1] is not None:
|
|
|
|
newPos[0] = min(newPos[0], self.maxRange[1])
|
|
|
|
elif self.angle == 0:
|
|
|
|
if self.maxRange[0] is not None:
|
|
|
|
newPos[1] = max(newPos[1], self.maxRange[0])
|
|
|
|
if self.maxRange[1] is not None:
|
|
|
|
newPos[1] = min(newPos[1], self.maxRange[1])
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
if self.p != newPos:
|
|
|
|
self.p = newPos
|
2016-02-04 02:28:59 +00:00
|
|
|
self._invalidateCache()
|
2016-02-17 06:03:13 +00:00
|
|
|
GraphicsObject.setPos(self, Point(self.p))
|
2016-02-19 08:41:42 +00:00
|
|
|
self.sigPositionChanged.emit(self)
|
2016-02-03 11:52:01 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def getXPos(self):
|
|
|
|
return self.p[0]
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def getYPos(self):
|
|
|
|
return self.p[1]
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def getPos(self):
|
|
|
|
return self.p
|
|
|
|
|
|
|
|
def value(self):
|
2016-01-15 15:10:24 +00:00
|
|
|
"""Return the value of the line. Will be a single number for horizontal and
|
2012-04-25 17:12:40 +00:00
|
|
|
vertical lines, and a list of [x,y] values for diagonal lines."""
|
2012-03-02 02:55:32 +00:00
|
|
|
if self.angle%180 == 0:
|
|
|
|
return self.getYPos()
|
|
|
|
elif self.angle%180 == 90:
|
|
|
|
return self.getXPos()
|
|
|
|
else:
|
|
|
|
return self.getPos()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def setValue(self, v):
|
2016-01-15 15:10:24 +00:00
|
|
|
"""Set the position of the line. If line is horizontal or vertical, v can be
|
|
|
|
a single value. Otherwise, a 2D coordinate must be specified (list, tuple and
|
2012-04-25 17:12:40 +00:00
|
|
|
QPointF are all acceptable)."""
|
2012-03-02 02:55:32 +00:00
|
|
|
self.setPos(v)
|
|
|
|
|
|
|
|
## broken in 4.7
|
|
|
|
#def itemChange(self, change, val):
|
|
|
|
#if change in [self.ItemScenePositionHasChanged, self.ItemSceneHasChanged]:
|
|
|
|
#self.updateLine()
|
|
|
|
#print "update", change
|
|
|
|
#print self.getBoundingParents()
|
|
|
|
#else:
|
|
|
|
#print "ignore", change
|
|
|
|
#return GraphicsObject.itemChange(self, change, val)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-04 02:28:59 +00:00
|
|
|
def _invalidateCache(self):
|
|
|
|
self._line = None
|
|
|
|
self._boundingRect = None
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def boundingRect(self):
|
2016-02-04 02:28:59 +00:00
|
|
|
if self._boundingRect is None:
|
|
|
|
#br = UIGraphicsItem.boundingRect(self)
|
|
|
|
br = self.viewRect()
|
|
|
|
## add a 4-pixel radius around the line for mouse interaction.
|
|
|
|
|
|
|
|
px = self.pixelLength(direction=Point(1,0), ortho=True) ## get pixel length orthogonal to the line
|
|
|
|
if px is None:
|
|
|
|
px = 0
|
|
|
|
w = (max(4, self.pen.width()/2, self.hoverPen.width()/2)+1) * px
|
|
|
|
br.setBottom(-w)
|
|
|
|
br.setTop(w)
|
|
|
|
br = br.normalized()
|
|
|
|
self._boundingRect = br
|
|
|
|
self._line = QtCore.QLineF(br.right(), 0.0, br.left(), 0.0)
|
|
|
|
return self._boundingRect
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def paint(self, p, *args):
|
|
|
|
p.setPen(self.currentPen)
|
2016-02-04 02:28:59 +00:00
|
|
|
p.drawLine(self._line)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-05-08 21:56:55 +00:00
|
|
|
def dataBounds(self, axis, frac=1.0, orthoRange=None):
|
2012-03-02 02:55:32 +00:00
|
|
|
if axis == 0:
|
|
|
|
return None ## x axis should never be auto-scaled
|
|
|
|
else:
|
|
|
|
return (0,0)
|
|
|
|
|
|
|
|
def mouseDragEvent(self, ev):
|
|
|
|
if self.movable and ev.button() == QtCore.Qt.LeftButton:
|
|
|
|
if ev.isStart():
|
|
|
|
self.moving = True
|
2016-02-03 11:52:01 +00:00
|
|
|
self.cursorOffset = self.pos() - self.mapToParent(ev.buttonDownPos())
|
|
|
|
self.startPosition = self.pos()
|
2012-03-02 02:55:32 +00:00
|
|
|
ev.accept()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
if not self.moving:
|
|
|
|
return
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-03 11:52:01 +00:00
|
|
|
self.setPos(self.cursorOffset + self.mapToParent(ev.pos()))
|
2012-03-02 02:55:32 +00:00
|
|
|
self.sigDragged.emit(self)
|
|
|
|
if ev.isFinish():
|
|
|
|
self.moving = False
|
|
|
|
self.sigPositionChangeFinished.emit(self)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def mouseClickEvent(self, ev):
|
|
|
|
if self.moving and ev.button() == QtCore.Qt.RightButton:
|
|
|
|
ev.accept()
|
|
|
|
self.setPos(self.startPosition)
|
|
|
|
self.moving = False
|
|
|
|
self.sigDragged.emit(self)
|
|
|
|
self.sigPositionChangeFinished.emit(self)
|
|
|
|
|
|
|
|
def hoverEvent(self, ev):
|
2012-04-03 05:01:33 +00:00
|
|
|
if (not ev.isExit()) and self.movable and ev.acceptDrags(QtCore.Qt.LeftButton):
|
2012-04-04 13:29:35 +00:00
|
|
|
self.setMouseHover(True)
|
|
|
|
else:
|
|
|
|
self.setMouseHover(False)
|
|
|
|
|
|
|
|
def setMouseHover(self, hover):
|
2014-05-10 18:19:27 +00:00
|
|
|
## Inform the item that the mouse is (not) hovering over it
|
2012-04-04 13:29:35 +00:00
|
|
|
if self.mouseHovering == hover:
|
|
|
|
return
|
|
|
|
self.mouseHovering = hover
|
|
|
|
if hover:
|
2014-05-10 18:19:27 +00:00
|
|
|
self.currentPen = self.hoverPen
|
2012-03-02 02:55:32 +00:00
|
|
|
else:
|
|
|
|
self.currentPen = self.pen
|
|
|
|
self.update()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-03 11:52:01 +00:00
|
|
|
def viewTransformChanged(self):
|
|
|
|
"""
|
|
|
|
Called whenever the transformation matrix of the view has changed.
|
|
|
|
(eg, the view range has changed or the view was resized)
|
|
|
|
"""
|
2016-02-04 02:28:59 +00:00
|
|
|
self._invalidateCache()
|
2016-02-19 08:41:42 +00:00
|
|
|
self.textItem.updatePosition()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
|
|
|
def showLabel(self, state):
|
|
|
|
"""
|
|
|
|
Display or not the label indicating the location of the line in data
|
|
|
|
coordinates.
|
|
|
|
|
2016-02-05 10:54:00 +00:00
|
|
|
============== ======================================================
|
2016-01-15 15:10:24 +00:00
|
|
|
**Arguments:**
|
|
|
|
state If True, the label is shown. Otherwise, it is hidden.
|
2016-02-05 10:54:00 +00:00
|
|
|
============== ======================================================
|
2016-01-15 15:10:24 +00:00
|
|
|
"""
|
2016-02-19 08:41:42 +00:00
|
|
|
self.textItem.setVisible(state)
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-16 05:48:59 +00:00
|
|
|
def setTextLocation(self, position=0.05, shift=0.5):
|
2016-01-15 15:10:24 +00:00
|
|
|
"""
|
2016-02-16 05:48:59 +00:00
|
|
|
Set the parameters that defines the location of the label on the axis.
|
|
|
|
The position *parameter* governs the location of the label in the
|
|
|
|
direction of the line, whereas the *shift* governs the shift of the
|
|
|
|
label from one side of the line to the other in the orthogonal
|
|
|
|
direction.
|
2016-02-05 12:57:51 +00:00
|
|
|
|
|
|
|
============== ======================================================
|
|
|
|
**Arguments:**
|
2016-02-16 05:48:59 +00:00
|
|
|
position float (range of value = [0-1])
|
2016-02-05 12:57:51 +00:00
|
|
|
shift float (range of value = [0-1]).
|
|
|
|
============== ======================================================
|
2016-01-15 15:10:24 +00:00
|
|
|
"""
|
2016-02-19 08:41:42 +00:00
|
|
|
self.textItem.orthoPos = position
|
|
|
|
self.textItem.shiftPos = shift
|
|
|
|
self.textItem.updatePosition()
|
2016-01-15 15:10:24 +00:00
|
|
|
|
2016-02-16 07:14:53 +00:00
|
|
|
def setDraggableLabel(self, state):
|
|
|
|
"""
|
|
|
|
Set the state of the label regarding its behaviour during the dragging
|
|
|
|
of the line. If True, then the location of the label change during the
|
|
|
|
dragging of the line.
|
|
|
|
"""
|
2016-02-19 08:41:42 +00:00
|
|
|
self.textItem.setMovable(state)
|
2016-02-16 07:14:53 +00:00
|
|
|
|
2016-01-15 15:10:24 +00:00
|
|
|
def setName(self, name):
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
2016-02-19 08:41:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InfLineLabel(TextItem):
|
|
|
|
# a text label that attaches itself to an InfiniteLine
|
|
|
|
def __init__(self, line, **kwds):
|
|
|
|
self.line = line
|
|
|
|
self.movable = False
|
|
|
|
self.dragAxis = None # 0=x, 1=y
|
|
|
|
self.orthoPos = 0.5 # text will always be placed on the line at a position relative to view bounds
|
|
|
|
self.format = "{value}"
|
|
|
|
self.line.sigPositionChanged.connect(self.valueChanged)
|
|
|
|
TextItem.__init__(self, **kwds)
|
|
|
|
self.valueChanged()
|
|
|
|
|
|
|
|
def valueChanged(self):
|
|
|
|
if not self.isVisible():
|
|
|
|
return
|
|
|
|
value = self.line.value()
|
|
|
|
self.setText(self.format.format(value=value))
|
|
|
|
self.updatePosition()
|
|
|
|
|
|
|
|
def updatePosition(self):
|
|
|
|
view = self.getViewBox()
|
|
|
|
if not self.isVisible() or not isinstance(view, ViewBox):
|
|
|
|
# not in a viewbox, skip update
|
|
|
|
return
|
|
|
|
|
|
|
|
# 1. determine view extents along line axis
|
|
|
|
tr = view.childGroup.itemTransform(self.line)[0]
|
|
|
|
vr = tr.mapRect(view.viewRect())
|
|
|
|
pt1 = Point(vr.left(), 0)
|
|
|
|
pt2 = Point(vr.right(), 0)
|
|
|
|
|
|
|
|
# 2. pick relative point between extents and set position
|
|
|
|
pt = pt2 * self.orthoPos + pt1 * (1-self.orthoPos)
|
|
|
|
self.setPos(pt)
|
|
|
|
|
|
|
|
def setVisible(self, v):
|
|
|
|
TextItem.setVisible(self, v)
|
|
|
|
if v:
|
|
|
|
self.updateText()
|
|
|
|
self.updatePosition()
|
|
|
|
|
|
|
|
def setMovable(self, m):
|
|
|
|
self.movable = m
|
|
|
|
self.setAcceptHoverEvents(m)
|
|
|
|
|
|
|
|
def mouseDragEvent(self, ev):
|
|
|
|
if self.movable and ev.button() == QtCore.Qt.LeftButton:
|
|
|
|
if ev.isStart():
|
|
|
|
self._moving = True
|
|
|
|
self._cursorOffset = self._posToRel(ev.buttonDownPos())
|
|
|
|
self._startPosition = self.orthoPos
|
|
|
|
ev.accept()
|
|
|
|
|
|
|
|
if not self._moving:
|
|
|
|
return
|
|
|
|
|
|
|
|
rel = self._posToRel(ev.pos())
|
|
|
|
self.orthoPos = self._startPosition + rel - self._cursorOffset
|
|
|
|
self.updatePosition()
|
|
|
|
if ev.isFinish():
|
|
|
|
self._moving = False
|
|
|
|
|
|
|
|
def mouseClickEvent(self, ev):
|
|
|
|
if self.moving and ev.button() == QtCore.Qt.RightButton:
|
|
|
|
ev.accept()
|
|
|
|
self.orthoPos = self._startPosition
|
|
|
|
self.moving = False
|
|
|
|
|
|
|
|
def hoverEvent(self, ev):
|
|
|
|
if not ev.isExit() and self.movable:
|
|
|
|
ev.acceptDrags(QtCore.Qt.LeftButton)
|
|
|
|
|
|
|
|
def _posToRel(self, pos):
|
|
|
|
# convert local position to relative position along line between view bounds
|
|
|
|
view = self.getViewBox()
|
|
|
|
tr = view.childGroup.itemTransform(self.line)[0]
|
|
|
|
vr = tr.mapRect(view.viewRect())
|
|
|
|
pos = self.mapToParent(pos)
|
|
|
|
return (pos.x() - vr.left()) / vr.width()
|
|
|
|
|
|
|
|
|