Connvert paintEvent of QPlainTextEdit to Pyqt

   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())

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值