2012-03-02 02:55:32 +00:00
|
|
|
from pyqtgraph.Qt import QtGui, QtCore
|
|
|
|
import pyqtgraph.functions as fn
|
2012-05-11 22:05:41 +00:00
|
|
|
from .GraphicsWidget import GraphicsWidget
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['LabelItem']
|
|
|
|
|
|
|
|
class LabelItem(GraphicsWidget):
|
|
|
|
"""
|
|
|
|
GraphicsWidget displaying text.
|
|
|
|
Used mainly as axis labels, titles, etc.
|
|
|
|
|
2012-03-21 03:38:04 +00:00
|
|
|
Note: To display text inside a scaled view (ViewBox, PlotWidget, etc) use TextItem
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
def __init__(self, text=' ', parent=None, angle=0, **args):
|
2012-03-02 02:55:32 +00:00
|
|
|
GraphicsWidget.__init__(self, parent)
|
|
|
|
self.item = QtGui.QGraphicsTextItem(self)
|
2012-04-03 05:01:33 +00:00
|
|
|
self.opts = {
|
|
|
|
'color': 'CCC',
|
|
|
|
'justify': 'center'
|
|
|
|
}
|
|
|
|
self.opts.update(args)
|
2012-03-02 02:55:32 +00:00
|
|
|
self.sizeHint = {}
|
|
|
|
self.setText(text)
|
2012-03-21 03:38:04 +00:00
|
|
|
self.setAngle(angle)
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setAttr(self, attr, value):
|
|
|
|
"""Set default text properties. See setText() for accepted parameters."""
|
|
|
|
self.opts[attr] = value
|
|
|
|
|
|
|
|
def setText(self, text, **args):
|
|
|
|
"""Set the text and text properties in the label. Accepts optional arguments for auto-generating
|
|
|
|
a CSS style string:
|
2012-05-08 22:14:12 +00:00
|
|
|
|
|
|
|
==================== ==============================
|
|
|
|
**Style Arguments:**
|
|
|
|
color (str) example: 'CCFF00'
|
|
|
|
size (str) example: '8pt'
|
|
|
|
bold (bool)
|
|
|
|
italic (bool)
|
|
|
|
==================== ==============================
|
|
|
|
"""
|
2012-03-02 02:55:32 +00:00
|
|
|
self.text = text
|
|
|
|
opts = self.opts.copy()
|
|
|
|
for k in args:
|
|
|
|
opts[k] = args[k]
|
|
|
|
|
|
|
|
optlist = []
|
|
|
|
if 'color' in opts:
|
2012-04-03 05:01:33 +00:00
|
|
|
if isinstance(opts['color'], QtGui.QColor):
|
|
|
|
opts['color'] = fn.colorStr(opts['color'])[:6]
|
2012-03-02 02:55:32 +00:00
|
|
|
optlist.append('color: #' + opts['color'])
|
|
|
|
if 'size' in opts:
|
|
|
|
optlist.append('font-size: ' + opts['size'])
|
|
|
|
if 'bold' in opts and opts['bold'] in [True, False]:
|
|
|
|
optlist.append('font-weight: ' + {True:'bold', False:'normal'}[opts['bold']])
|
|
|
|
if 'italic' in opts and opts['italic'] in [True, False]:
|
|
|
|
optlist.append('font-style: ' + {True:'italic', False:'normal'}[opts['italic']])
|
|
|
|
full = "<span style='%s'>%s</span>" % ('; '.join(optlist), text)
|
|
|
|
#print full
|
|
|
|
self.item.setHtml(full)
|
|
|
|
self.updateMin()
|
2012-04-03 05:01:33 +00:00
|
|
|
self.resizeEvent(None)
|
|
|
|
self.update()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
def resizeEvent(self, ev):
|
2012-04-03 05:01:33 +00:00
|
|
|
#c1 = self.boundingRect().center()
|
|
|
|
#c2 = self.item.mapToParent(self.item.boundingRect().center()) # + self.item.pos()
|
|
|
|
#dif = c1 - c2
|
|
|
|
#self.item.moveBy(dif.x(), dif.y())
|
2012-03-02 02:55:32 +00:00
|
|
|
#print c1, c2, dif, self.item.pos()
|
2012-04-03 05:01:33 +00:00
|
|
|
if self.opts['justify'] == 'left':
|
|
|
|
self.item.setPos(0,0)
|
|
|
|
elif self.opts['justify'] == 'center':
|
|
|
|
bounds = self.item.mapRectToParent(self.item.boundingRect())
|
|
|
|
self.item.setPos(self.width()/2. - bounds.width()/2., 0)
|
|
|
|
elif self.opts['justify'] == 'right':
|
|
|
|
bounds = self.item.mapRectToParent(self.item.boundingRect())
|
|
|
|
self.item.setPos(self.width() - bounds.width(), 0)
|
|
|
|
#if self.width() > 0:
|
|
|
|
#self.item.setTextWidth(self.width())
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
def setAngle(self, angle):
|
|
|
|
self.angle = angle
|
|
|
|
self.item.resetTransform()
|
|
|
|
self.item.rotate(angle)
|
|
|
|
self.updateMin()
|
|
|
|
|
|
|
|
def updateMin(self):
|
|
|
|
bounds = self.item.mapRectToParent(self.item.boundingRect())
|
|
|
|
self.setMinimumWidth(bounds.width())
|
|
|
|
self.setMinimumHeight(bounds.height())
|
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
self.sizeHint = {
|
|
|
|
QtCore.Qt.MinimumSize: (bounds.width(), bounds.height()),
|
|
|
|
QtCore.Qt.PreferredSize: (bounds.width(), bounds.height()),
|
|
|
|
QtCore.Qt.MaximumSize: (-1, -1), #bounds.width()*2, bounds.height()*2),
|
|
|
|
QtCore.Qt.MinimumDescent: (0, 0) ##?? what is this?
|
|
|
|
}
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
self.update()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
def sizeHint(self, hint, constraint):
|
|
|
|
if hint not in self.sizeHint:
|
|
|
|
return QtCore.QSizeF(0, 0)
|
|
|
|
return QtCore.QSizeF(*self.sizeHint[hint])
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
#def paint(self, p, *args):
|
|
|
|
#p.setPen(fn.mkPen('r'))
|
|
|
|
#p.drawRect(self.rect())
|
|
|
|
#p.drawRect(self.item.boundingRect())
|
2012-05-08 22:14:12 +00:00
|
|
|
|