2012-10-19 03:18:20 +00:00
|
|
|
from .GraphicsWidget import GraphicsWidget
|
|
|
|
from .LabelItem import LabelItem
|
|
|
|
from ..Qt import QtGui, QtCore
|
|
|
|
from .. import functions as fn
|
2012-11-23 21:05:14 +00:00
|
|
|
from ..Point import Point
|
|
|
|
from .GraphicsWidgetAnchor import GraphicsWidgetAnchor
|
2012-10-19 03:18:20 +00:00
|
|
|
__all__ = ['LegendItem']
|
|
|
|
|
2012-11-23 21:05:14 +00:00
|
|
|
class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
|
2012-10-19 03:18:20 +00:00
|
|
|
"""
|
|
|
|
Displays a legend used for describing the contents of a plot.
|
2012-11-23 21:05:14 +00:00
|
|
|
LegendItems are most commonly created by calling PlotItem.addLegend().
|
2012-10-19 03:18:20 +00:00
|
|
|
|
|
|
|
Note that this item should not be added directly to a PlotItem. Instead,
|
|
|
|
Make it a direct descendant of the PlotItem::
|
|
|
|
|
|
|
|
legend.setParentItem(plotItem)
|
|
|
|
|
|
|
|
"""
|
2012-11-23 21:05:14 +00:00
|
|
|
def __init__(self, size=None, offset=None):
|
|
|
|
"""
|
|
|
|
========== ===============================================================
|
|
|
|
Arguments
|
|
|
|
size Specifies the fixed size (width, height) of the legend. If
|
|
|
|
this argument is omitted, the legend will autimatically resize
|
|
|
|
to fit its contents.
|
|
|
|
offset Specifies the offset position relative to the legend's parent.
|
|
|
|
Positive values offset from the left or top; negative values
|
|
|
|
offset from the right or bottom. If offset is None, the
|
|
|
|
legend must be anchored manually by calling anchor() or
|
|
|
|
positioned by calling setPos().
|
|
|
|
========== ===============================================================
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2012-10-19 03:18:20 +00:00
|
|
|
GraphicsWidget.__init__(self)
|
2012-11-23 21:05:14 +00:00
|
|
|
GraphicsWidgetAnchor.__init__(self)
|
2012-10-19 03:18:20 +00:00
|
|
|
self.setFlag(self.ItemIgnoresTransformations)
|
|
|
|
self.layout = QtGui.QGraphicsGridLayout()
|
|
|
|
self.setLayout(self.layout)
|
|
|
|
self.items = []
|
|
|
|
self.size = size
|
|
|
|
self.offset = offset
|
2012-11-23 21:05:14 +00:00
|
|
|
if size is not None:
|
|
|
|
self.setGeometry(QtCore.QRectF(0, 0, self.size[0], self.size[1]))
|
|
|
|
|
|
|
|
def setParentItem(self, p):
|
|
|
|
ret = GraphicsWidget.setParentItem(self, p)
|
|
|
|
if self.offset is not None:
|
|
|
|
offset = Point(self.offset)
|
|
|
|
anchorx = 1 if offset[0] <= 0 else 0
|
|
|
|
anchory = 1 if offset[1] <= 0 else 0
|
|
|
|
anchor = (anchorx, anchory)
|
|
|
|
self.anchor(itemPos=anchor, parentPos=anchor, offset=offset)
|
|
|
|
return ret
|
2012-10-19 03:18:20 +00:00
|
|
|
|
2012-11-23 21:05:14 +00:00
|
|
|
def addItem(self, item, name):
|
2012-10-19 03:18:20 +00:00
|
|
|
"""
|
|
|
|
Add a new entry to the legend.
|
2012-10-26 12:55:53 +00:00
|
|
|
|
2012-10-19 03:18:20 +00:00
|
|
|
=========== ========================================================
|
|
|
|
Arguments
|
|
|
|
item A PlotDataItem from which the line and point style
|
|
|
|
of the item will be determined
|
|
|
|
title The title to display for this item. Simple HTML allowed.
|
|
|
|
=========== ========================================================
|
|
|
|
"""
|
2012-11-23 21:05:14 +00:00
|
|
|
label = LabelItem(name)
|
2012-10-19 03:18:20 +00:00
|
|
|
sample = ItemSample(item)
|
|
|
|
row = len(self.items)
|
|
|
|
self.items.append((sample, label))
|
|
|
|
self.layout.addItem(sample, row, 0)
|
|
|
|
self.layout.addItem(label, row, 1)
|
2012-11-23 21:05:14 +00:00
|
|
|
self.updateSize()
|
|
|
|
|
|
|
|
def updateSize(self):
|
|
|
|
if self.size is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
height = 0
|
|
|
|
width = 0
|
2012-12-05 05:25:45 +00:00
|
|
|
#print("-------")
|
2012-11-23 21:05:14 +00:00
|
|
|
for sample, label in self.items:
|
|
|
|
height += max(sample.height(), label.height()) + 3
|
|
|
|
width = max(width, sample.width()+label.width())
|
2012-12-05 05:25:45 +00:00
|
|
|
#print(width, height)
|
|
|
|
#print width, height
|
2012-11-23 21:05:14 +00:00
|
|
|
self.setGeometry(0, 0, width+25, height)
|
2012-10-19 03:18:20 +00:00
|
|
|
|
|
|
|
def boundingRect(self):
|
2012-11-23 21:05:14 +00:00
|
|
|
return QtCore.QRectF(0, 0, self.width(), self.height())
|
2012-10-19 03:18:20 +00:00
|
|
|
|
|
|
|
def paint(self, p, *args):
|
|
|
|
p.setPen(fn.mkPen(255,255,255,100))
|
|
|
|
p.setBrush(fn.mkBrush(100,100,100,50))
|
|
|
|
p.drawRect(self.boundingRect())
|
|
|
|
|
|
|
|
|
|
|
|
class ItemSample(GraphicsWidget):
|
|
|
|
def __init__(self, item):
|
|
|
|
GraphicsWidget.__init__(self)
|
|
|
|
self.item = item
|
|
|
|
|
|
|
|
def boundingRect(self):
|
|
|
|
return QtCore.QRectF(0, 0, 20, 20)
|
|
|
|
|
|
|
|
def paint(self, p, *args):
|
2012-11-23 21:05:14 +00:00
|
|
|
opts = self.item.opts
|
|
|
|
|
|
|
|
if opts.get('fillLevel',None) is not None and opts.get('fillBrush',None) is not None:
|
|
|
|
p.setBrush(fn.mkBrush(opts['fillBrush']))
|
|
|
|
p.setPen(fn.mkPen(None))
|
|
|
|
p.drawPolygon(QtGui.QPolygonF([QtCore.QPointF(2,18), QtCore.QPointF(18,2), QtCore.QPointF(18,18)]))
|
|
|
|
|
|
|
|
p.setPen(fn.mkPen(opts['pen']))
|
2012-10-19 03:18:20 +00:00
|
|
|
p.drawLine(2, 18, 18, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-23 21:05:14 +00:00
|
|
|
|