(self, paintEventRect)
Draw indentation markers
Draw indentation markers
Draw indentation markers
def _drawIndentMarkersAndEdge(self, paintEventRect):
        """Draw indentation markers
        """
        painter = QPainter(self.viewport())

        def drawWhiteSpace(block, column, char):
            leftCursorRect = self.__cursorRect(block, column, 0)
            rightCursorRect = self.__cursorRect(block, column + 1, 0)
            if leftCursorRect.top() == rightCursorRect.top():  # if on the same visual line
                middleHeight = (leftCursorRect.top() + leftCursorRect.bottom()) / 2
                if char == ' ':
                    painter.setPen(Qt.transparent)
                    painter.setBrush(QBrush(Qt.gray))
                    xPos = (leftCursorRect.x() + rightCursorRect.x()) / 2
                    painter.drawRect(QRect(xPos, middleHeight, 2, 2))
                else:
                    painter.setPen(QColor(Qt.gray).lighter(factor=120))
                    painter.drawLine(leftCursorRect.x() + 3, middleHeight,
                                     rightCursorRect.x() - 3, middleHeight)

        def effectiveEdgePos(text):
            """Position of edge in a block.
            Defined by self._lineLengthEdge, but visible width of \t is more than 1,
            therefore effective position depends on count and position of \t symbols
            Return -1 if line is too short to have edge
            """
            if self._lineLengthEdge is None:
                return -1

            tabExtraWidth = self.indentWidth - 1
            fullWidth = len(text) + (text.count('\t') * tabExtraWidth)
            if fullWidth <= self._lineLengthEdge:
                return -1

            currentWidth = 0
            for pos, char in enumerate(text):
                if char == '\t':
                    # Qt indents up to indentation level, so visible \t width depends on position
                    currentWidth += (self.indentWidth - (currentWidth % self.indentWidth))
                else:
                    currentWidth += 1
                if currentWidth > self._lineLengthEdge:
                    return pos
            # line too narrow, probably visible \t width is small
            return -1

        def drawEdgeLine(block, edgePos):
            painter.setPen(QPen(QBrush(self._lineLengthEdgeColor), 0))
            rect = self.__cursorRect(block, edgePos, 0)
            painter.drawLine(rect.topLeft(), rect.bottomLeft())

        def drawIndentMarker(block, column):
            painter.setPen(QColor(Qt.darkGray).lighter())
            rect = self.__cursorRect(block, column, offset=0)
            painter.drawLine(rect.topLeft(), rect.bottomLeft())

        def drawIndentMarkers(block, text, column):
            # this was 6 blocks deep  ~irgolic
            while text.startswith(self._indenter.text()) and \
                    len(text) > indentWidthChars and \
                    text[indentWidthChars].isspace():

                if column != self._lineLengthEdge and \
                        (block.blockNumber(),
                         column) != cursorPos:  # looks ugly, if both drawn
                    # on some fonts line is drawn below the cursor, if offset is 1
                    # Looks like Qt bug
                    drawIndentMarker(block, column)

                text = text[indentWidthChars:]
                column += indentWidthChars

        indentWidthChars = len(self._indenter.text())
        cursorPos = self.cursorPosition

        for block in iterateBlocksFrom(self.firstVisibleBlock()):
            blockGeometry = self.blockBoundingGeometry(block).translated(self.contentOffset())
            if blockGeometry.top() > paintEventRect.bottom():
                break

            if block.isVisible() and blockGeometry.toRect().intersects(paintEventRect):

                # Draw indent markers, if good indentation is not drawn
                if self._drawIndentations:
                    text = block.text()
                    if not self.drawAnyWhitespace:
                        column = indentWidthChars
                        drawIndentMarkers(block, text, column)

                # Draw edge, but not over a cursor
                if not self._drawSolidEdge:
                    edgePos = effectiveEdgePos(block.text())
                    if edgePos not in (-1, cursorPos[1]):
                        drawEdgeLine(block, edgePos)

                if self.drawAnyWhitespace or \
                        self.drawIncorrectIndentation:
                    text = block.text()
                    for column, draw in enumerate(self._chooseVisibleWhitespace(text)):
                        if draw:
                            drawWhiteSpace(block, column, text[column])
def _drawIndentMarkersAndEdge ( self , paintEventRect ) : painter = QPainter ( self . viewport ( ) ) def drawWhiteSpace ( block , column , char ) : leftCursorRect = self . __cursorRect ( block , column , 0 ) rightCursorRect = self . __cursorRect ( block , column + 1 , 0 ) if leftCursorRect . top ( ) == rightCursorRect . top ( ) : # if on the same visual line middleHeight = ( leftCursorRect . top ( ) + leftCursorRect . bottom ( ) ) / 2 if char == ' ' : painter . setPen ( Qt . transparent ) painter . setBrush ( QBrush ( Qt . gray ) ) xPos = ( leftCursorRect . x ( ) + rightCursorRect . x ( ) ) / 2 painter . drawRect ( QRect ( xPos , middleHeight , 2 , 2 ) ) else : painter . setPen ( QColor ( Qt . gray ) . lighter ( factor = 120 ) ) painter . drawLine ( leftCursorRect . x ( ) + 3 , middleHeight , rightCursorRect . x ( ) - 3 , middleHeight ) def effectiveEdgePos ( text ) : """Position of edge in a block.
            Defined by self._lineLengthEdge, but visible width of \t is more than 1,
            therefore effective position depends on count and position of \t symbols
            Return -1 if line is too short to have edge
            """ if self . _lineLengthEdge is None : return - 1 tabExtraWidth = self . indentWidth - 1 fullWidth = len ( text ) + ( text . count ( '\t' ) * tabExtraWidth ) if fullWidth <= self . _lineLengthEdge : return - 1 currentWidth = 0 for pos , char in enumerate ( text ) : if char == '\t' : # Qt indents up to indentation level, so visible \t width depends on position currentWidth += ( self . indentWidth - ( currentWidth % self . indentWidth ) ) else : currentWidth += 1 if currentWidth > self . _lineLengthEdge : return pos # line too narrow, probably visible \t width is small return - 1 def drawEdgeLine ( block , edgePos ) : painter . setPen ( QPen ( QBrush ( self . _lineLengthEdgeColor ) , 0 ) ) rect = self . __cursorRect ( block , edgePos , 0 ) painter . drawLine ( rect . topLeft ( ) , rect . bottomLeft ( ) ) def drawIndentMarker ( block , column ) : painter . setPen ( QColor ( Qt . darkGray ) . lighter ( ) ) rect = self . __cursorRect ( block , column , offset = 0 ) painter . drawLine ( rect . topLeft ( ) , rect . bottomLeft ( ) ) def drawIndentMarkers ( block , text , column ) : # this was 6 blocks deep  ~irgolic while text . startswith ( self . _indenter . text ( ) ) and len ( text ) > indentWidthChars and text [ indentWidthChars ] . isspace ( ) : if column != self . _lineLengthEdge and ( block . blockNumber ( ) , column ) != cursorPos : # looks ugly, if both drawn # on some fonts line is drawn below the cursor, if offset is 1 # Looks like Qt bug drawIndentMarker ( block , column ) text = text [ indentWidthChars : ] column += indentWidthChars indentWidthChars = len ( self . _indenter . text ( ) ) cursorPos = self . cursorPosition for block in iterateBlocksFrom ( self . firstVisibleBlock ( ) ) : blockGeometry = self . blockBoundingGeometry ( block ) . translated ( self . contentOffset ( ) ) if blockGeometry . top ( ) > paintEventRect . bottom ( ) : break if block . isVisible ( ) and blockGeometry . toRect ( ) . intersects ( paintEventRect ) : # Draw indent markers, if good indentation is not drawn if self . _drawIndentations : text = block . text ( ) if not self . drawAnyWhitespace : column = indentWidthChars drawIndentMarkers ( block , text , column ) # Draw edge, but not over a cursor if not self . _drawSolidEdge : edgePos = effectiveEdgePos ( block . text ( ) ) if edgePos not in ( - 1 , cursorPos [ 1 ] ) : drawEdgeLine ( block , edgePos ) if self . drawAnyWhitespace or self . drawIncorrectIndentation : text = block . text ( ) for column , draw in enumerate ( self . _chooseVisibleWhitespace ( text ) ) : if draw : drawWhiteSpace ( block , column , text [ column ] )