def paintEvent(self,event):
painter=QPainter(self.viewport())
offset=QPointF(self.contentOffset())
er = event.rect()
viewportRect = self.viewport().rect()
editable = not self.isReadOnly()
block = self.firstVisibleBlock()
maximumWidth = self.document().documentLayout().documentSize().width()
painter.setBrushOrigin(offset)
maxX = offset.x() + max(float(viewportRect.width()), maximumWidth)- self.document().documentMargin()
er.setRight(min(er.right(), maxX))
painter.setClipRect(er)
context = self.getPaintContext()
while block.isValid():
r = self.blockBoundingRect(block).translated(offset)
layout = block.layout()
if not block.isVisible():
ry=offset.y()
ry= ry+r.height()
offset.setY(ry)
block = block.next()
continue
if (r.bottom() >= er.top() and r.top() <= er.bottom()):
blockFormat = block.blockFormat()
bg = blockFormat.background()
if (bg != Qt.NoBrush) :
contentsRect = r
contentsRect.setWidth(max(r.width(), maximumWidth))
painter.fillRect(contentsRect, bg)
selections=[]
blpos = block.position()
bllen = block.length()
for rg in context.selections:
selStart = rg.cursor.selectionStart() - blpos
selEnd = rg.cursor.selectionEnd() - blpos
if (selStart < bllen and selEnd > 0 and selEnd > selStart) :
o=QTextLayout.FormatRange()
o.start = selStart
o.length = selEnd - selStart
o.format = rg.format
selections.append(o)
elif (not rg.cursor.hasSelection() and rg.format.hasProperty(QTextFormat.FullWidthSelection) and block.contains(rg.cursor.position())) :
o= QTextLayout.FormatRange()
l = layout.lineForTextPosition(rg.cursor.position() - blpos)
o.start = l.textStart()
o.length = l.textLength()
if (o.start + o.length == bllen - 1):
o.length+=1
o.format = rg.format
selections.append(o)
drawCursor = ((editable or (self.textInteractionFlags() & Qt.TextSelectableByKeyboard)) and context.cursorPosition >= blpos and context.cursorPosition < blpos + bllen)
drawCursorAsBlock = drawCursor and self.overwriteMode()
if (drawCursorAsBlock) :
if (context.cursorPosition == blpos + bllen - 1) :
drawCursorAsBlock = False
else :
o=QTextLayout.FormatRange()
o.start = context.cursorPosition - blpos
o.length = 1
o.format.setForeground(self.palette().base())
o.format.setBackground(self.palette().text())
selections.append(o)
layout.draw(painter, offset, selections, QRectF(er))
if ((drawCursor and not drawCursorAsBlock) or (editable and context.cursorPosition < -1 and not layout.preeditAreaText().isEmpty())):
cpos = context.cursorPosition
if (cpos < -1):
cpos = layout.preeditAreaPosition() - (cpos + 2)
else:
cpos -= blpos
layout.drawCursor(painter, offset, cpos, self.cursorWidth())
ry = offset.y()
ry = ry + r.height()
offset.setY(ry)
if (offset.y() > viewportRect.height()):
break
block = block.next()
if (self.backgroundVisible() and not block.isValid() and offset.y() <= er.bottom() and (self.centerOnScroll() or self.verticalScrollBar().maximum() == self.verticalScrollBar().minimum())):
painter.fillRect(QRect(QPoint(int(er.left()), int(offset.y())), er.bottomRight()), self.palette().background())