Exemplo n.º 1
0
    def paintEvent(self, event):
        """ Override the default paintEvent to add in line numbers.
        """
        vScrollPos = self.textWidget.verticalScrollBar().value()
        pageBtm = vScrollPos + self.textWidget.viewport().height()
        currBlock = self.doc.findBlock(self.textWidget.textCursor().position())

        fontMetric = self.fontMetrics()
        painter = QPainter(self)
        font = painter.font()

        # Find roughly the current top-most visible block.
        block = self.doc.begin()
        lineHeight = self.doc.documentLayout().blockBoundingRect(
            block).height()

        block = self.doc.findBlockByNumber(int(vScrollPos / lineHeight))
        currLine = block.blockNumber()

        while block.isValid():
            currLine += 1

            # Check if the position of the block is outside the visible area.
            yPos = self.doc.documentLayout().blockBoundingRect(
                block).topLeft().y()
            if yPos > pageBtm:
                break

            if block == currBlock:
                # Make the line number for the selected line bold.
                font.setBold(True)
                painter.setFont(font)
                # Draw the line number right justified at the y position of the line. 3 is a magic padding number.
                painter.drawText(
                    self.width() - fontMetric.width(str(currLine)) - 3,
                    round(yPos) - vScrollPos + fontMetric.ascent() + 3,
                    str(currLine))
                font.setBold(False)
                painter.setFont(font)
            else:
                painter.drawText(
                    self.width() - fontMetric.width(str(currLine)) - 3,
                    round(yPos) - vScrollPos + fontMetric.ascent() + 3,
                    str(currLine))

            # Go to the next block.
            block = block.next()

        painter.end()

        super(LineNumbers, self).paintEvent(event)
Exemplo n.º 2
0
    def paintEvent(self, event):
        contents_y = self.editor.verticalScrollBar().value()
        page_bottom = contents_y + self.editor.viewport().height()
        font_metrics = self.fontMetrics()
        current_block = self.editor.document().findBlock(
            self.editor.textCursor().position())
        painter = QPainter(self)
        line_count = 0

        # Iterate over all text blocks in the document
        block = self.editor.document().begin()
        font_size = self.editor.font().pointSize()
        font = painter.font()
        font.setPixelSize(font_size)
        offset = font_metrics.ascent() + font_metrics.descent()
        color = painter.pen().color()
        painter.setFont(font)
        align = Qt.AlignRight
        while block.isValid():
            line_count += 1

            # Get top left position of the block in the document and check if the position of the block is
            # outside of the visible area
            position = self.editor.document().documentLayout(
            ).blockBoundingRect(block).topLeft()
            if position.y() == page_bottom:
                break

            rect = QRect(0,
                         round(position.y()) - contents_y,
                         self.width() - 5, font_size + offset)

            # Draw line rect
            if block == current_block:
                painter.setPen(Qt.NoPen)
                painter.setBrush(QBrush(self.bg))
                painter.drawRect(
                    QRect(0,
                          round(position.y()) - contents_y, self.width(),
                          font_size + (offset / 2)))
                painter.setPen(QPen(color))

            # Draw text
            painter.drawText(rect, align, str(line_count))
            block = block.next()

        self.highest_line = line_count
        painter.end()
        super(ScriptEditorNumberBar, self).paintEvent(event)
Exemplo n.º 3
0
    def paintEvent(self, event):
        if self.isVisible():
            block = self.editor.firstVisibleBlock()
            height = self.fontMetrics().height()
            number = block.blockNumber()
            painter = QPainter(self)
            painter.fillRect(event.rect(), LINEBAR_COLOR)
            painter.drawRect(event.rect().width() - 1, 0,
                             event.rect().width(),
                             event.rect().height() - 1)
            font = painter.font()
            current_block = self.editor.textCursor().block().blockNumber() + 1

            condition = True
            while block.isValid() and condition:
                block_geometry = self.editor.blockBoundingGeometry(block)
                offset = self.editor.contentOffset()
                block_top = block_geometry.translated(offset).top()
                number += 1

                # NOTE set the linebar breakpoint color
                if self.paintLineNum > 0 and number == self.paintLineNum:
                    font.setBold(True)
                    block_rect = QRect(LINE_MARGIN, block_top,
                                       self.width() - LINE_MARGIN * 2, height)
                    painter.fillRect(block_rect, LINEBAR_BP_COLOR)
                # NOTE set the current line color
                elif number == current_block:
                    font.setBold(True)
                    block_rect = QRect(LINE_MARGIN, block_top,
                                       self.width() - LINE_MARGIN * 2, height)
                    painter.fillRect(block_rect, LINEBAR_NUM_COLOR)
                else:
                    font.setBold(False)

                painter.setFont(font)
                rect = QRect(0, block_top, self.width() - 5, height)
                painter.drawText(rect, Qt.AlignRight, '%i' % number)

                if block_top > event.rect().bottom():
                    condition = False

                block = block.next()

            painter.end()
Exemplo n.º 4
0
    def printDocument(self, printer):
        loop = QEventLoop()
        result = False

        def printPreview(success):
            nonlocal result
            result = success
            loop.quit()

        self.m_page.print(printer, printPreview)
        loop.exec_()
        if not result:
            painter = QPainter()
            if painter.begin(printer):
                font = painter.font()
                font.setPixelSize(20)
                painter.setFont(font)
                painter.drawText(QPointF(10, 25), "Could not generate print preview.")
                painter.end()
Exemplo n.º 5
0
 def paintEvent(self, event):
     painter = QPainter()
     painter.begin(self)
     painter.setRenderHint(painter.Antialiasing)
     font = painter.font()
     font.setBold(True)
     painter.setFont(font)
     x = self.rect().x()
     y = self.rect().y()
     w = self.rect().width() - 1
     h = self.rect().height() - 1
     _rect = 8
     if self._rolloutStyle == ExpanderStyles.Rounded:
         painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())
         self.__drawTriangle(painter, x, y)
         pen = QPen(self.palette().color(QPalette.Light))
         pen.setWidthF(0.6)
         painter.setPen(pen)
         painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, _rect, _rect)
         pen.setColor(self.palette().color(QPalette.Shadow))
         painter.setPen(pen)
         painter.drawRoundedRect(x, y, w - 1, h - 1, _rect, _rect)
     if self._rolloutStyle == ExpanderStyles.Square:
         painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())
         self.__drawTriangle(painter, x, y)
         pen = QPen(self.palette().color(QPalette.Light))
         pen.setWidthF(0.2)
         painter.setPen(pen)
         painter.drawRect(x + 1, y + 1, w - 1, h - 1)
         pen.setColor(self.palette().color(QPalette.Shadow))
         painter.setPen(pen)
         painter.drawRect(x, y, w - 1, h - 1)
     if self._rolloutStyle == ExpanderStyles.Maya:
         painter.drawText(
             x + (45 if self.dragDropMode() == ExpanderDragDropModes.InternalMove else 25),
             y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())
         painter.setRenderHint(QPainter.Antialiasing, False)
         self.__drawTriangle(painter, x, y)
         header_height = 20
         header_rect = QRect(x + 1, y + 1, w - 1, header_height)
         header_rect_shadow = QRect(x - 1, y - 1, w + 1, header_height + 2)
         pen = QPen(self.palette().color(QPalette.Light))
         pen.setWidthF(0.4)
         # painter.setPen(pen)
         painter.setPen(Qt.NoPen)
         painter.drawRect(header_rect)
         painter.fillRect(header_rect, QColor(255, 255, 255, 18))
         pen.setColor(self.palette().color(QPalette.Dark))
         painter.setPen(pen)
         painter.drawRect(header_rect_shadow)
         if not self.isCollapsed():
             # pen = QPen(self.palette().color(QPalette.Background))
             # painter.setPen(pen)
             offset = header_height + 3
             body_rect = QRect(x, y + offset, w, h - offset)
             # body_rect_shadow = QRect(x + 1, y + offSet, w + 1, h - offSet + 1)
             painter.drawRect(body_rect)
             # pen.setColor(self.palette().color(QPalette.Foreground))
             # pen.setWidthF(0.4)
             # painter.setPen(pen)
             # painter.drawRect(body_rect_shadow)
     elif self._rolloutStyle == ExpanderStyles.Boxed:
         if self.isCollapsed():
             arect = QRect(x + 1, y + 9, w - 1, 4)
             brect = QRect(x, y + 8, w - 1, 4)
             text = '+'
         else:
             arect = QRect(x + 1, y + 9, w - 1, h - 9)
             brect = QRect(x, y + 8, w - 1, h - 9)
             text = '-'
         pen = QPen(self.palette().color(QPalette.Light))
         pen.setWidthF(0.6)
         painter.setPen(pen)
         painter.drawRect(arect)
         pen.setColor(self.palette().color(QPalette.Shadow))
         painter.setPen(pen)
         painter.drawRect(brect)
         painter.setRenderHint(painter.Antialiasing, False)
         painter.setBrush(self.palette().color(QPalette.Window).darker(120))
         painter.drawRect(x + 10, y + 1, w - 20, 16)
         painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text)
         painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title())
     if self.dragDropMode():
         rect = self.dragDropRect()
         _layout = rect.left()
         _rect = rect.right()
         center_y = rect.center().y()
         pen = QPen(self.palette().color(self.isCollapsed() and QPalette.Shadow or QPalette.Mid))
         painter.setPen(pen)
         for y in (center_y - 3, center_y, center_y + 3):
             painter.drawLine(_layout, y, _rect, y)
     painter.end()
Exemplo n.º 6
0
    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHint(painter.Antialiasing)
        font = painter.font()
        font.setBold(True)
        painter.setFont(font)

        x = self.rect().x()
        y = self.rect().y()
        w = self.rect().width() - 1
        h = self.rect().height() - 1
        _rect = 8

        if self.rollout_style == AccordionStyle.ROUNDED:
            painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16,
                             Qt.AlignLeft | Qt.AlignTop, self.title())
            self._draw_triangle(painter, x, y)
            self._draw_icon(painter, x + 22, y + 3)
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.6)
            painter.setPen(pen)
            painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, _rect, _rect)
            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)
            painter.drawRoundedRect(x, y, w - 1, h - 1, _rect, _rect)
        elif self.rollout_style == AccordionStyle.SQUARE:
            painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16,
                             Qt.AlignLeft | Qt.AlignTop, self.title())
            self._draw_triangle(painter, x, y)
            self._draw_icon(painter, x + 22, y + 3)
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.3)
            painter.setPen(pen)
            painter.drawRect(x + 1, y + 1, w - 1, h - 1)
            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)
        elif self.rollout_style == AccordionStyle.MAYA:
            painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16,
                             Qt.AlignLeft | Qt.AlignTop, self.title())
            painter.setRenderHint(QPainter.Antialiasing, False)
            self._draw_triangle(painter, x, y)
            self._draw_icon(painter, x + 22, y + 3)
            header_height = 20
            header_rect = QRect(x + 1, y + 1, w - 1, header_height)
            header_rect_shadow = QRect(x - 1, y - 1, w + 1, header_height + 2)
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.4)
            painter.setPen(pen)
            painter.drawRect(header_rect)
            painter.fillRect(header_rect, QColor(255, 255, 255, 18))
            pen.setColor(self.palette().color(QPalette.Dark))
            painter.setPen(pen)
            painter.drawRect(header_rect_shadow)
            if not self.is_collapsed():
                pen = QPen(self.palette().color(QPalette.Dark))
                pen.setWidthF(0.8)
                painter.setPen(pen)
                offset = header_height + 3
                body_rect = QRect(x, y + offset, w, h - offset)
                body_rect_shadow = QRect(x + 1, y + offset, w + 1,
                                         h - offset + 1)
                painter.drawRect(body_rect)
                pen.setColor(self.palette().color(QPalette.Light))
                pen.setWidth(0.4)
                painter.setPen(pen)
                painter.drawRect(body_rect_shadow)
        elif self.rollout_style == AccordionStyle.BOXED:
            if self.is_collapsed():
                a_rect = QRect(x + 1, y + 9, w - 1, 4)
                b_rect = QRect(x, y + 8, w - 1, 4)
                text = '+'
            else:
                a_rect = QRect(x + 1, y + 9, w - 1, h - 9)
                b_rect = QRect(x, y + 8, w - 1, h - 9)
                text = '-'

            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.6)
            painter.setPen(pen)
            painter.drawRect(a_rect)
            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)
            painter.drawRect(b_rect)
            painter.setRenderHint(painter.Antialiasing, False)
            painter.setBrush(self.palette().color(QPalette.Window).darker(120))
            painter.drawRect(x + 10, y + 1, w - 20, 16)
            painter.drawText(x + 16, y + 1, w - 32, 16,
                             Qt.AlignLeft | Qt.AlignVCenter, text)
            painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter,
                             self.title())

        if self.drag_drop_mode:
            rect = self.get_drag_drop_rect()
            _layout = rect.left()
            _rect = rect.right()
            center_y = rect.center().y()
            for y in (center_y - 3, center_y, center_y + 3):
                painter.drawLine(_layout, y, _rect, y)

        painter.end()
Exemplo n.º 7
0
    def paintEvent( self, event ):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHint(painter.Antialiasing)
        font = painter.font()
        font.setBold(True)
        painter.setFont(font)

        x = self.rect().x()
        y = self.rect().y()
        w = self.rect().width() - 1
        h = self.rect().height() - 1
        r = 8

        # draw a rounded style
        if self._rolloutStyle == 2:
            # draw the text
            painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())

            # draw the triangle
            self.__drawTriangle(painter, x, y)

            # draw the borders
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.6)
            painter.setPen(pen)

            painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, r, r)

            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)

            painter.drawRoundedRect(x, y, w - 1, h - 1, r, r)

        # draw a square style
        if self._rolloutStyle == 3:
            # draw the text
            painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())

            self.__drawTriangle(painter, x, y)

            # draw the borders
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.6)
            painter.setPen(pen)

            painter.drawRect(x + 1, y + 1, w - 1, h - 1)

            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)

            painter.drawRect(x, y, w - 1, h - 1)

        # draw a Maya style
        if self._rolloutStyle == 4:
            # draw the text
            painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())

            painter.setRenderHint(QPainter.Antialiasing, False)

            self.__drawTriangle(painter, x, y)

            # draw the borders - top
            headerHeight = 20

            headerRect = QRect(x + 1, y + 1, w - 1, headerHeight)
            headerRectShadow = QRect(x - 1, y - 1, w + 1, headerHeight + 2)

            # Highlight
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.4)
            painter.setPen(pen)

            painter.drawRect(headerRect)
            painter.fillRect(headerRect, QColor(255, 255, 255, 18))

            # Shadow
            pen.setColor(self.palette().color(QPalette.Dark))
            painter.setPen(pen)
            painter.drawRect(headerRectShadow)

            if not self.isCollapsed():
                # draw the lover border
                pen = QPen(self.palette().color(QPalette.Dark))
                pen.setWidthF(0.8)
                painter.setPen(pen)

                offSet = headerHeight + 3
                bodyRect = QRect(x, y + offSet, w, h - offSet)
                bodyRectShadow = QRect(x + 1, y + offSet, w + 1, h - offSet + 1)
                painter.drawRect(bodyRect)

                pen.setColor(self.palette().color(QPalette.Light))
                pen.setWidthF(0.4)
                painter.setPen(pen)

                painter.drawRect(bodyRectShadow)

        # draw a boxed style
        elif self._rolloutStyle == 1:
            if self.isCollapsed():
                arect = QRect(x + 1, y + 9, w - 1, 4)
                brect = QRect(x, y + 8, w - 1, 4)
                text = '+'
            else:
                arect = QRect(x + 1, y + 9, w - 1, h - 9)
                brect = QRect(x, y + 8, w - 1, h - 9)
                text = '-'

            # draw the borders
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.6)
            painter.setPen(pen)

            painter.drawRect(arect)

            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)

            painter.drawRect(brect)

            painter.setRenderHint(painter.Antialiasing, False)
            painter.setBrush(self.palette().color(QPalette.Window).darker(120))
            painter.drawRect(x + 10, y + 1, w - 20, 16)
            painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text)
            painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title())

        if self.dragDropMode():
            rect = self.dragDropRect()

            # draw the lines
            l = rect.left()
            r = rect.right()
            cy = rect.center().y()

            for y in (cy - 3, cy, cy + 3):
                painter.drawLine(l, y, r, y)

        painter.end()