2013-12-22 07:08:39 +00:00
|
|
|
from ..Qt import QtCore, QtGui
|
|
|
|
from ..Point import Point
|
2012-05-11 22:05:41 +00:00
|
|
|
from .UIGraphicsItem import *
|
2013-12-22 07:08:39 +00:00
|
|
|
from .. import functions as fn
|
2012-03-18 03:10:00 +00:00
|
|
|
|
|
|
|
class TextItem(UIGraphicsItem):
|
|
|
|
"""
|
|
|
|
GraphicsItem displaying unscaled text (the text will always appear normal even inside a scaled ViewBox).
|
|
|
|
"""
|
2012-12-22 21:51:25 +00:00
|
|
|
def __init__(self, text='', color=(200,200,200), html=None, anchor=(0,0), border=None, fill=None, angle=0):
|
2012-03-18 03:10:00 +00:00
|
|
|
"""
|
2014-02-05 20:04:33 +00:00
|
|
|
============== =================================================================================
|
2014-02-03 20:13:10 +00:00
|
|
|
**Arguments:**
|
2014-02-05 20:04:33 +00:00
|
|
|
*text* The text to display
|
|
|
|
*color* The color of the text (any format accepted by pg.mkColor)
|
|
|
|
*html* If specified, this overrides both *text* and *color*
|
|
|
|
*anchor* A QPointF or (x,y) sequence indicating what region of the text box will
|
|
|
|
be anchored to the item's position. A value of (0,0) sets the upper-left corner
|
|
|
|
of the text box to be at the position specified by setPos(), while a value of (1,1)
|
|
|
|
sets the lower-right corner.
|
|
|
|
*border* A pen to use when drawing the border
|
|
|
|
*fill* A brush to use when filling within the border
|
2014-03-01 02:07:43 +00:00
|
|
|
============== =================================================================================
|
2012-03-18 03:10:00 +00:00
|
|
|
"""
|
2012-12-22 21:51:25 +00:00
|
|
|
|
|
|
|
## not working yet
|
|
|
|
#*angle* Angle in degrees to rotate text (note that the rotation assigned in this item's
|
|
|
|
#transformation will be ignored)
|
|
|
|
|
2013-12-22 07:08:39 +00:00
|
|
|
self.anchor = Point(anchor)
|
2012-12-25 05:43:31 +00:00
|
|
|
#self.angle = 0
|
2012-03-18 03:10:00 +00:00
|
|
|
UIGraphicsItem.__init__(self)
|
|
|
|
self.textItem = QtGui.QGraphicsTextItem()
|
2012-12-25 05:43:31 +00:00
|
|
|
self.textItem.setParentItem(self)
|
2012-03-18 03:10:00 +00:00
|
|
|
self.lastTransform = None
|
|
|
|
self._bounds = QtCore.QRectF()
|
|
|
|
if html is None:
|
|
|
|
self.setText(text, color)
|
|
|
|
else:
|
|
|
|
self.setHtml(html)
|
2013-12-22 07:08:39 +00:00
|
|
|
self.fill = fn.mkBrush(fill)
|
|
|
|
self.border = fn.mkPen(border)
|
2012-12-25 05:43:31 +00:00
|
|
|
self.rotate(angle)
|
|
|
|
self.setFlag(self.ItemIgnoresTransformations) ## This is required to keep the text unscaled inside the viewport
|
2012-03-18 03:10:00 +00:00
|
|
|
|
|
|
|
def setText(self, text, color=(200,200,200)):
|
2013-12-22 07:08:39 +00:00
|
|
|
color = fn.mkColor(color)
|
2012-03-18 03:10:00 +00:00
|
|
|
self.textItem.setDefaultTextColor(color)
|
|
|
|
self.textItem.setPlainText(text)
|
2012-12-25 05:43:31 +00:00
|
|
|
self.updateText()
|
2012-03-18 03:10:00 +00:00
|
|
|
#html = '<span style="color: #%s; text-align: center;">%s</span>' % (color, text)
|
|
|
|
#self.setHtml(html)
|
|
|
|
|
|
|
|
def updateAnchor(self):
|
|
|
|
pass
|
|
|
|
#self.resetTransform()
|
|
|
|
#self.translate(0, 20)
|
|
|
|
|
|
|
|
def setPlainText(self, *args):
|
|
|
|
self.textItem.setPlainText(*args)
|
|
|
|
self.updateText()
|
|
|
|
|
|
|
|
def setHtml(self, *args):
|
|
|
|
self.textItem.setHtml(*args)
|
|
|
|
self.updateText()
|
|
|
|
|
|
|
|
def setTextWidth(self, *args):
|
|
|
|
self.textItem.setTextWidth(*args)
|
|
|
|
self.updateText()
|
|
|
|
|
|
|
|
def setFont(self, *args):
|
|
|
|
self.textItem.setFont(*args)
|
|
|
|
self.updateText()
|
|
|
|
|
2012-12-25 05:43:31 +00:00
|
|
|
#def setAngle(self, angle):
|
|
|
|
#self.angle = angle
|
|
|
|
#self.updateText()
|
|
|
|
|
|
|
|
|
2012-03-18 03:10:00 +00:00
|
|
|
def updateText(self):
|
|
|
|
|
2012-12-25 05:43:31 +00:00
|
|
|
## Needed to maintain font size when rendering to image with increased resolution
|
|
|
|
self.textItem.resetTransform()
|
|
|
|
#self.textItem.rotate(self.angle)
|
|
|
|
if self._exportOpts is not False and 'resolutionScale' in self._exportOpts:
|
|
|
|
s = self._exportOpts['resolutionScale']
|
|
|
|
self.textItem.scale(s, s)
|
|
|
|
|
|
|
|
#br = self.textItem.mapRectToParent(self.textItem.boundingRect())
|
|
|
|
self.textItem.setPos(0,0)
|
|
|
|
br = self.textItem.boundingRect()
|
2013-12-22 07:08:39 +00:00
|
|
|
apos = self.textItem.mapToParent(Point(br.width()*self.anchor.x(), br.height()*self.anchor.y()))
|
2012-12-25 05:43:31 +00:00
|
|
|
#print br, apos
|
|
|
|
self.textItem.setPos(-apos.x(), -apos.y())
|
|
|
|
|
|
|
|
#def textBoundingRect(self):
|
|
|
|
### return the bounds of the text box in device coordinates
|
|
|
|
#pos = self.mapToDevice(QtCore.QPointF(0,0))
|
|
|
|
#if pos is None:
|
|
|
|
#return None
|
|
|
|
#tbr = self.textItem.boundingRect()
|
|
|
|
#return QtCore.QRectF(pos.x() - tbr.width()*self.anchor.x(), pos.y() - tbr.height()*self.anchor.y(), tbr.width(), tbr.height())
|
2012-03-18 03:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def viewRangeChanged(self):
|
2012-12-25 05:43:31 +00:00
|
|
|
self.updateText()
|
2012-03-18 03:10:00 +00:00
|
|
|
|
|
|
|
def boundingRect(self):
|
2012-12-25 05:43:31 +00:00
|
|
|
return self.textItem.mapToParent(self.textItem.boundingRect()).boundingRect()
|
2012-03-18 03:10:00 +00:00
|
|
|
|
|
|
|
def paint(self, p, *args):
|
|
|
|
tr = p.transform()
|
|
|
|
if self.lastTransform is not None:
|
|
|
|
if tr != self.lastTransform:
|
|
|
|
self.viewRangeChanged()
|
|
|
|
self.lastTransform = tr
|
|
|
|
|
2012-12-28 21:23:28 +00:00
|
|
|
if self.border.style() != QtCore.Qt.NoPen or self.fill.style() != QtCore.Qt.NoBrush:
|
|
|
|
p.setPen(self.border)
|
|
|
|
p.setBrush(self.fill)
|
|
|
|
p.setRenderHint(p.Antialiasing, True)
|
|
|
|
p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect()))
|
2012-03-18 03:10:00 +00:00
|
|
|
|
|
|
|
|