Esempio n. 1
0
 def documentForScript(self, script=0):
     if type(script) != Script:
         script = self.libraryList[script]
     if script not in self._cachedDocuments:
         doc = QTextDocument(self)
         doc.setDocumentLayout(QPlainTextDocumentLayout(doc))
         doc.setPlainText(script.script)
         doc.setDefaultFont(QFont(self.defaultFont))
         doc.highlighter = PythonSyntaxHighlighter(doc)
         doc.modificationChanged[bool].connect(self.onModificationChanged)
         doc.setModified(False)
         self._cachedDocuments[script] = doc
     return self._cachedDocuments[script]
    def paint(self, painter, option, index):
        options = QStyleOptionViewItem(option)
        row_obj = index.data(Qt.DisplayRole)
        self.initStyleOption(options, index)
        # print(option.rect.width())
        style = QApplication.style(
        ) if options.widget is None else options.widget.style()

        doc = QTextDocument()
        doc.setHtml(row_obj.to_html())
        doc.setTextWidth(option.rect.width() - 10)

        # doc.setPageSize(300)
        # print(doc.loadResource(3))

        options.text = ""
        style.drawControl(QStyle.CE_ItemViewItem, options, painter)

        ctx = QAbstractTextDocumentLayout.PaintContext()

        text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
        painter.save()
        painter.translate(text_rect.topLeft())
        painter.setClipRect(text_rect.translated(-text_rect.topLeft()))
        doc.documentLayout().draw(painter, ctx)

        painter.restore()
    def sizeHint(self, option, index):
        options = QStyleOptionViewItem(option)
        gene_obj = index.data(Qt.DisplayRole)
        self.initStyleOption(options, index)

        doc = QTextDocument()
        doc.setHtml(gene_obj.to_html())
        doc.setTextWidth(options.rect.width() - 10)

        return QSize(doc.idealWidth(), doc.size().height())
def text_outline_path(doc: QTextDocument) -> QPainterPath:
    # return a path outlining all the text lines.
    margin = doc.documentMargin()
    path = QPainterPath()
    offset = min(margin, 2)
    for line in iter_lines(doc):
        rect = line.naturalTextRect()
        rect.translate(margin, margin)
        rect = rect.adjusted(-offset, -offset, offset, offset)
        p = QPainterPath()
        p.addRoundedRect(rect, 3, 3)
        path = path.united(p)
    return path
Esempio n. 5
0
    def _prepare_text_document(self, option, index):
        # This logic must be shared between paint and sizeHint for consitency
        options = QStyleOptionViewItem(option)
        self.initStyleOption(options, index)

        doc = QTextDocument()
        doc.setDocumentMargin(self._margin)
        doc.setHtml(options.text)
        icon_height = doc.size().height() - 2
        options.decorationSize = QSize(icon_height, icon_height)
        return options, doc
Esempio n. 6
0
    def drawContents(self, painter):
        """
        Reimplementation of drawContents to limit the drawing
        inside `textRext`.

        """
        painter.setPen(self.__color)
        painter.setFont(self.font())

        if self.__textRect:
            rect = self.__textRect
        else:
            rect = self.rect().adjusted(5, 5, -5, -5)

        tformat = self.__textFormat

        if tformat == Qt.AutoText:
            if mightBeRichText(self.__message):
                tformat = Qt.RichText
            else:
                tformat = Qt.PlainText

        if tformat == Qt.RichText:
            doc = QTextDocument()
            doc.setHtml(self.__message)
            doc.setTextWidth(rect.width())
            cursor = QTextCursor(doc)
            cursor.select(QTextCursor.Document)
            fmt = QTextBlockFormat()
            fmt.setAlignment(self.__alignment)
            cursor.mergeBlockFormat(fmt)
            painter.save()
            painter.translate(rect.topLeft())
            doc.drawContents(painter)
            painter.restore()
        else:
            painter.drawText(rect, self.__alignment, self.__message)
Esempio n. 7
0
    def drawContents(self, painter):
        """
        Reimplementation of drawContents to limit the drawing
        inside `textRext`.

        """
        painter.setPen(self.__color)
        painter.setFont(self.font())

        if self.__textRect:
            rect = self.__textRect
        else:
            rect = self.rect().adjusted(5, 5, -5, -5)

        tformat = self.__textFormat

        if tformat == Qt.AutoText:
            if mightBeRichText(self.__message):
                tformat = Qt.RichText
            else:
                tformat = Qt.PlainText

        if tformat == Qt.RichText:
            doc = QTextDocument()
            doc.setHtml(self.__message)
            doc.setTextWidth(rect.width())
            cursor = QTextCursor(doc)
            cursor.select(QTextCursor.Document)
            fmt = QTextBlockFormat()
            fmt.setAlignment(self.__alignment)
            cursor.mergeBlockFormat(fmt)
            painter.save()
            painter.translate(rect.topLeft())
            doc.drawContents(painter)
            painter.restore()
        else:
            painter.drawText(rect, self.__alignment, self.__message)
Esempio n. 8
0
 def documentForScript(self, script=0):
     if not isinstance(script, Script):
         script = self.libraryList[script]
     if script not in self._cachedDocuments:
         doc = QTextDocument(self)
         doc.setDocumentLayout(QPlainTextDocumentLayout(doc))
         doc.setPlainText(script.script)
         doc.setDefaultFont(QFont(self.defaultFont))
         doc.highlighter = PythonSyntaxHighlighter(doc)
         doc.modificationChanged[bool].connect(self.onModificationChanged)
         doc.setModified(False)
         self._cachedDocuments[script] = doc
     return self._cachedDocuments[script]