1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
from PyQt4 import QtCore, QtGui
from demoitem import DemoItem
class DemoTextItem(DemoItem):
STATIC_TEXT, DYNAMIC_TEXT = range(2)
def __init__(self, text, font, textColor, textWidth, scene=None,
parent=None, type=STATIC_TEXT, bgColor=QtGui.QColor()):
super(DemoTextItem, self).__init__(scene, parent)
self.type = type
self.text = text
self.font = font
self.textColor = textColor
self.bgColor = bgColor
self.textWidth = textWidth
self.noSubPixeling = True
def setText(self, text):
self.text = text
self.update()
def createImage(self, matrix):
if self.type == DemoTextItem.DYNAMIC_TEXT:
return None
sx = min(matrix.m11(), matrix.m22())
sy = max(matrix.m22(), sx)
textItem = QtGui.QGraphicsTextItem()
textItem.setHtml(self.text)
textItem.setTextWidth(self.textWidth)
textItem.setFont(self.font)
textItem.setDefaultTextColor(self.textColor)
textItem.document().setDocumentMargin(2)
w = textItem.boundingRect().width()
h = textItem.boundingRect().height()
image = QtGui.QImage(int(w * sx), int(h * sy),
QtGui.QImage.Format_ARGB32_Premultiplied)
image.fill(QtGui.QColor(0, 0, 0, 0).rgba())
painter = QtGui.QPainter(image)
painter.scale(sx, sy)
style = QtGui.QStyleOptionGraphicsItem()
textItem.paint(painter, style, None)
return image
def animationStarted(self, id=0):
self.noSubPixeling = False
def animationStopped(self, id=0):
self.noSubPixeling = True
def boundingRect(self):
if self.type == DemoTextItem.STATIC_TEXT:
return super(DemoTextItem, self).boundingRect()
# Sorry for using magic number.
return QtCore.QRectF(0, 0, 50, 20)
def paint(self, painter, option, widget):
if self.type == DemoTextItem.STATIC_TEXT:
super(DemoTextItem, self).paint(painter, option, widget)
return
painter.setPen(self.textColor)
painter.drawText(0, 0, self.text)
|