from pyqtgraph.Qt import QtGui, QtCore import pyqtgraph.functions as fn from GraphicsWidget import GraphicsWidget __all__ = ['LabelItem'] class LabelItem(GraphicsWidget): """ GraphicsWidget displaying text. Used mainly as axis labels, titles, etc. Note: To display text inside a scaled view (ViewBox, PlotWidget, etc) use QGraphicsTextItem with the flag ItemIgnoresTransformations set. """ def __init__(self, text, parent=None, **args): GraphicsWidget.__init__(self, parent) self.item = QtGui.QGraphicsTextItem(self) self.opts = args if 'color' not in args: self.opts['color'] = 'CCC' else: if isinstance(args['color'], QtGui.QColor): self.opts['color'] = fn.colorStr(args['color'])[:6] self.sizeHint = {} self.setText(text) 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: color: string (example: 'CCFF00') size: string (example: '8pt') bold: boolean italic: boolean """ self.text = text opts = self.opts.copy() for k in args: opts[k] = args[k] optlist = [] if 'color' in opts: 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 = "%s" % ('; '.join(optlist), text) #print full self.item.setHtml(full) self.updateMin() def resizeEvent(self, ev): 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()) #print c1, c2, dif, self.item.pos() 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()) #print self.text, bounds.width(), bounds.height() #self.sizeHint = { #QtCore.Qt.MinimumSize: (bounds.width(), bounds.height()), #QtCore.Qt.PreferredSize: (bounds.width(), bounds.height()), #QtCore.Qt.MaximumSize: (bounds.width()*2, bounds.height()*2), #QtCore.Qt.MinimumDescent: (0, 0) ##?? what is this? #} #def sizeHint(self, hint, constraint): #return self.sizeHint[hint]