2013-12-22 07:08:39 +00:00
|
|
|
from ..Qt import QtGui, QtCore
|
|
|
|
from .. import functions as fn
|
2012-05-11 22:05:41 +00:00
|
|
|
from .GraphicsWidget import GraphicsWidget
|
2013-03-26 17:46:26 +00:00
|
|
|
from .GraphicsWidgetAnchor import GraphicsWidgetAnchor
|
2013-12-22 07:08:39 +00:00
|
|
|
from .. import getConfigOption
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['LabelItem']
|
|
|
|
|
2013-03-26 17:46:26 +00:00
|
|
|
class LabelItem(GraphicsWidget, GraphicsWidgetAnchor):
|
2012-03-02 02:55:32 +00:00
|
|
|
"""
|
|
|
|
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)
|
2013-03-26 17:46:26 +00:00
|
|
|
GraphicsWidgetAnchor.__init__(self)
|
2012-03-02 02:55:32 +00:00
|
|
|
self.item = QtGui.QGraphicsTextItem(self)
|
2012-04-03 05:01:33 +00:00
|
|
|
self.opts = {
|
2012-06-29 18:39:27 +00:00
|
|
|
'color': None,
|
2012-04-03 05:01:33 +00:00
|
|
|
'justify': 'center'
|
|
|
|
}
|
|
|
|
self.opts.update(args)
|
2012-06-29 18:39:27 +00:00
|
|
|
self._sizeHint = {}
|
2012-03-02 02:55:32 +00:00
|
|
|
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
|
2012-06-29 18:39:27 +00:00
|
|
|
opts = self.opts
|
2012-03-02 02:55:32 +00:00
|
|
|
for k in args:
|
|
|
|
opts[k] = args[k]
|
|
|
|
|
|
|
|
optlist = []
|
2012-06-29 18:39:27 +00:00
|
|
|
|
|
|
|
color = self.opts['color']
|
|
|
|
if color is None:
|
2013-12-22 07:08:39 +00:00
|
|
|
color = getConfigOption('foreground')
|
2012-06-29 18:39:27 +00:00
|
|
|
color = fn.mkColor(color)
|
|
|
|
optlist.append('color: #' + fn.colorStr(color)[:6])
|
2012-03-02 02:55:32 +00:00
|
|
|
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)
|
2012-06-29 18:39:27 +00:00
|
|
|
self.updateGeometry()
|
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-06-29 18:39:27 +00:00
|
|
|
self.item.setPos(0,0)
|
|
|
|
bounds = self.itemRect()
|
|
|
|
left = self.mapFromItem(self.item, QtCore.QPointF(0,0)) - self.mapFromItem(self.item, QtCore.QPointF(1,0))
|
|
|
|
rect = self.rect()
|
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
if self.opts['justify'] == 'left':
|
2012-06-29 18:39:27 +00:00
|
|
|
if left.x() != 0:
|
|
|
|
bounds.moveLeft(rect.left())
|
|
|
|
if left.y() < 0:
|
|
|
|
bounds.moveTop(rect.top())
|
|
|
|
elif left.y() > 0:
|
|
|
|
bounds.moveBottom(rect.bottom())
|
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
elif self.opts['justify'] == 'center':
|
2012-06-29 18:39:27 +00:00
|
|
|
bounds.moveCenter(rect.center())
|
|
|
|
#bounds = self.itemRect()
|
|
|
|
#self.item.setPos(self.width()/2. - bounds.width()/2., 0)
|
2012-04-03 05:01:33 +00:00
|
|
|
elif self.opts['justify'] == 'right':
|
2012-06-29 18:39:27 +00:00
|
|
|
if left.x() != 0:
|
|
|
|
bounds.moveRight(rect.right())
|
|
|
|
if left.y() < 0:
|
|
|
|
bounds.moveBottom(rect.bottom())
|
|
|
|
elif left.y() > 0:
|
|
|
|
bounds.moveTop(rect.top())
|
|
|
|
#bounds = self.itemRect()
|
|
|
|
#self.item.setPos(self.width() - bounds.width(), 0)
|
|
|
|
|
|
|
|
self.item.setPos(bounds.topLeft() - self.itemRect().topLeft())
|
|
|
|
self.updateMin()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
def setAngle(self, angle):
|
|
|
|
self.angle = angle
|
|
|
|
self.item.resetTransform()
|
|
|
|
self.item.rotate(angle)
|
|
|
|
self.updateMin()
|
|
|
|
|
2012-06-29 18:39:27 +00:00
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
def updateMin(self):
|
2012-06-29 18:39:27 +00:00
|
|
|
bounds = self.itemRect()
|
2012-03-02 02:55:32 +00:00
|
|
|
self.setMinimumWidth(bounds.width())
|
|
|
|
self.setMinimumHeight(bounds.height())
|
|
|
|
|
2012-06-29 18:39:27 +00:00
|
|
|
self._sizeHint = {
|
2012-04-03 05:01:33 +00:00
|
|
|
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-06-29 18:39:27 +00:00
|
|
|
self.updateGeometry()
|
2012-03-02 02:55:32 +00:00
|
|
|
|
2012-04-03 05:01:33 +00:00
|
|
|
def sizeHint(self, hint, constraint):
|
2012-06-29 18:39:27 +00:00
|
|
|
if hint not in self._sizeHint:
|
2012-04-03 05:01:33 +00:00
|
|
|
return QtCore.QSizeF(0, 0)
|
2012-06-29 18:39:27 +00:00
|
|
|
return QtCore.QSizeF(*self._sizeHint[hint])
|
|
|
|
|
|
|
|
def itemRect(self):
|
|
|
|
return self.item.mapRectToParent(self.item.boundingRect())
|
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())
|
2012-06-29 18:39:27 +00:00
|
|
|
#p.setPen(fn.mkPen('g'))
|
|
|
|
#p.drawRect(self.itemRect())
|
2012-05-08 22:14:12 +00:00
|
|
|
|