2013-12-22 07:08:39 +00:00
|
|
|
from ..Qt import QtGui
|
|
|
|
from .. import functions as fn
|
2014-01-23 15:34:26 +00:00
|
|
|
from .PlotDataItem import PlotDataItem
|
|
|
|
from .PlotCurveItem import PlotCurveItem
|
2012-11-29 03:56:31 +00:00
|
|
|
|
2013-12-22 07:08:39 +00:00
|
|
|
class FillBetweenItem(QtGui.QGraphicsPathItem):
|
2012-11-29 03:56:31 +00:00
|
|
|
"""
|
|
|
|
GraphicsItem filling the space between two PlotDataItems.
|
|
|
|
"""
|
2014-01-23 15:34:26 +00:00
|
|
|
def __init__(self, curve1=None, curve2=None, brush=None):
|
2013-12-22 07:08:39 +00:00
|
|
|
QtGui.QGraphicsPathItem.__init__(self)
|
2014-01-23 15:34:26 +00:00
|
|
|
self.curves = None
|
|
|
|
if curve1 is not None and curve2 is not None:
|
|
|
|
self.setCurves(curve1, curve2)
|
|
|
|
elif curve1 is not None or curve2 is not None:
|
|
|
|
raise Exception("Must specify two curves to fill between.")
|
|
|
|
|
2012-11-29 03:56:31 +00:00
|
|
|
if brush is not None:
|
2013-12-22 07:08:39 +00:00
|
|
|
self.setBrush(fn.mkBrush(brush))
|
2014-01-23 15:34:26 +00:00
|
|
|
self.updatePath()
|
|
|
|
|
|
|
|
def setCurves(self, curve1, curve2):
|
|
|
|
"""Set the curves to fill between.
|
|
|
|
|
2014-04-11 14:54:21 +00:00
|
|
|
Arguments must be instances of PlotDataItem or PlotCurveItem.
|
|
|
|
|
|
|
|
Added in version 0.9.9
|
|
|
|
"""
|
2014-01-23 15:34:26 +00:00
|
|
|
|
|
|
|
if self.curves is not None:
|
|
|
|
for c in self.curves:
|
|
|
|
try:
|
|
|
|
c.sigPlotChanged.disconnect(self.curveChanged)
|
2014-02-07 15:38:41 +00:00
|
|
|
except (TypeError, RuntimeError):
|
2014-01-23 15:34:26 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
curves = [curve1, curve2]
|
|
|
|
for c in curves:
|
|
|
|
if not isinstance(c, PlotDataItem) and not isinstance(c, PlotCurveItem):
|
|
|
|
raise TypeError("Curves must be PlotDataItem or PlotCurveItem.")
|
|
|
|
self.curves = curves
|
|
|
|
curve1.sigPlotChanged.connect(self.curveChanged)
|
|
|
|
curve2.sigPlotChanged.connect(self.curveChanged)
|
|
|
|
self.setZValue(min(curve1.zValue(), curve2.zValue())-1)
|
|
|
|
self.curveChanged()
|
|
|
|
|
|
|
|
def setBrush(self, *args, **kwds):
|
|
|
|
"""Change the fill brush. Acceps the same arguments as pg.mkBrush()"""
|
|
|
|
QtGui.QGraphicsPathItem.setBrush(self, fn.mkBrush(*args, **kwds))
|
|
|
|
|
|
|
|
def curveChanged(self):
|
2012-11-29 03:56:31 +00:00
|
|
|
self.updatePath()
|
|
|
|
|
|
|
|
def updatePath(self):
|
2014-01-23 15:34:26 +00:00
|
|
|
if self.curves is None:
|
|
|
|
self.setPath(QtGui.QPainterPath())
|
|
|
|
return
|
|
|
|
paths = []
|
|
|
|
for c in self.curves:
|
|
|
|
if isinstance(c, PlotDataItem):
|
|
|
|
paths.append(c.curve.getPath())
|
|
|
|
elif isinstance(c, PlotCurveItem):
|
|
|
|
paths.append(c.getPath())
|
|
|
|
|
2013-12-22 07:08:39 +00:00
|
|
|
path = QtGui.QPainterPath()
|
2014-01-23 15:34:26 +00:00
|
|
|
p1 = paths[0].toSubpathPolygons()
|
|
|
|
p2 = paths[1].toReversed().toSubpathPolygons()
|
|
|
|
if len(p1) == 0 or len(p2) == 0:
|
|
|
|
self.setPath(QtGui.QPainterPath())
|
|
|
|
return
|
|
|
|
|
|
|
|
path.addPolygon(p1[0] + p2[0])
|
2012-11-29 03:56:31 +00:00
|
|
|
self.setPath(path)
|