2012-03-02 02:55:32 +00:00
|
|
|
from pyqtgraph.Qt import QtGui, QtCore
|
|
|
|
import weakref
|
2012-05-11 22:05:41 +00:00
|
|
|
from .GraphicsObject import GraphicsObject
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
__all__ = ['UIGraphicsItem']
|
|
|
|
class UIGraphicsItem(GraphicsObject):
|
2012-05-08 22:14:12 +00:00
|
|
|
"""
|
|
|
|
Base class for graphics items with boundaries relative to a GraphicsView or ViewBox.
|
2012-03-02 02:55:32 +00:00
|
|
|
The purpose of this class is to allow the creation of GraphicsItems which live inside
|
|
|
|
a scalable view, but whose boundaries will always stay fixed relative to the view's boundaries.
|
|
|
|
For example: GridItem, InfiniteLine
|
|
|
|
|
|
|
|
The view can be specified on initialization or it can be automatically detected when the item is painted.
|
|
|
|
|
|
|
|
NOTE: Only the item's boundingRect is affected; the item is not transformed in any way. Use viewRangeChanged
|
|
|
|
to respond to changes in the view.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#sigViewChanged = QtCore.Signal(object) ## emitted whenever the viewport coords have changed
|
|
|
|
|
|
|
|
def __init__(self, bounds=None, parent=None):
|
|
|
|
"""
|
2012-05-08 22:14:12 +00:00
|
|
|
============== =============================================================================
|
|
|
|
**Arguments:**
|
|
|
|
bounds QRectF with coordinates relative to view box. The default is QRectF(0,0,1,1),
|
|
|
|
which means the item will have the same bounds as the view.
|
|
|
|
============== =============================================================================
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
|
|
|
GraphicsObject.__init__(self, parent)
|
|
|
|
self.setFlag(self.ItemSendsScenePositionChanges)
|
|
|
|
|
|
|
|
if bounds is None:
|
|
|
|
self._bounds = QtCore.QRectF(0, 0, 1, 1)
|
|
|
|
else:
|
|
|
|
self._bounds = bounds
|
|
|
|
|
|
|
|
self._boundingRect = None
|
2012-05-15 02:05:53 +00:00
|
|
|
self._updateView()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
def paint(self, *args):
|
|
|
|
## check for a new view object every time we paint.
|
|
|
|
#self.updateView()
|
|
|
|
pass
|
|
|
|
|
|
|
|
def itemChange(self, change, value):
|
|
|
|
ret = GraphicsObject.itemChange(self, change, value)
|
2012-05-15 02:05:53 +00:00
|
|
|
#if change == self.ItemParentHasChanged or change == self.ItemSceneHasChanged: ## handled by GraphicsItem now.
|
|
|
|
##print "caught parent/scene change:", self.parentItem(), self.scene()
|
|
|
|
#self.updateView()
|
|
|
|
if change == self.ItemScenePositionHasChanged:
|
2012-03-02 02:55:32 +00:00
|
|
|
self.setNewBounds()
|
|
|
|
return ret
|
|
|
|
|
2012-05-15 02:05:53 +00:00
|
|
|
#def updateView(self):
|
|
|
|
### called to see whether this item has a new view to connect to
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-05-15 02:05:53 +00:00
|
|
|
### check for this item's current viewbox or view widget
|
|
|
|
#view = self.getViewBox()
|
|
|
|
#if view is None:
|
|
|
|
##print " no view"
|
|
|
|
#return
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-05-15 02:05:53 +00:00
|
|
|
#if self._connectedView is not None and view is self._connectedView():
|
|
|
|
##print " already have view", view
|
|
|
|
#return
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-05-15 02:05:53 +00:00
|
|
|
### disconnect from previous view
|
|
|
|
#if self._connectedView is not None:
|
|
|
|
#cv = self._connectedView()
|
|
|
|
#if cv is not None:
|
|
|
|
##print "disconnect:", self
|
|
|
|
#cv.sigRangeChanged.disconnect(self.viewRangeChanged)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-05-15 02:05:53 +00:00
|
|
|
### connect to new view
|
|
|
|
##print "connect:", self
|
|
|
|
#view.sigRangeChanged.connect(self.viewRangeChanged)
|
|
|
|
#self._connectedView = weakref.ref(view)
|
|
|
|
#self.setNewBounds()
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def boundingRect(self):
|
|
|
|
if self._boundingRect is None:
|
|
|
|
br = self.viewRect()
|
|
|
|
if br is None:
|
|
|
|
return QtCore.QRectF()
|
|
|
|
else:
|
|
|
|
self._boundingRect = br
|
|
|
|
return QtCore.QRectF(self._boundingRect)
|
|
|
|
|
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
|
|
|
"""Called by ViewBox for determining the auto-range bounds.
|
|
|
|
By default, UIGraphicsItems are excluded from autoRange."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def viewRangeChanged(self):
|
|
|
|
"""Called when the view widget/viewbox is resized/rescaled"""
|
|
|
|
self.setNewBounds()
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def setNewBounds(self):
|
|
|
|
"""Update the item's bounding rect to match the viewport"""
|
|
|
|
self._boundingRect = None ## invalidate bounding rect, regenerate later if needed.
|
|
|
|
self.prepareGeometryChange()
|
|
|
|
|
|
|
|
|
|
|
|
def setPos(self, *args):
|
|
|
|
GraphicsObject.setPos(self, *args)
|
|
|
|
self.setNewBounds()
|
|
|
|
|
|
|
|
def mouseShape(self):
|
|
|
|
"""Return the shape of this item after expanding by 2 pixels"""
|
|
|
|
shape = self.shape()
|
|
|
|
ds = self.mapToDevice(shape)
|
|
|
|
stroker = QtGui.QPainterPathStroker()
|
|
|
|
stroker.setWidh(2)
|
|
|
|
ds2 = stroker.createStroke(ds).united(ds)
|
|
|
|
return self.mapFromDevice(ds2)
|
|
|
|
|
|
|
|
|
2012-05-08 22:14:12 +00:00
|
|
|
|