def paint(self, painter, option, index): option = QStyleOptionViewItem(option) # copy option self.initStyleOption(option, index) style = option.widget.style() if option.widget else QApplication.style() doc = QTextDocument() doc.setHtml(option.text) # painting item without text option.text = "" style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Hilight text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paintPage(self, pageNum, painter): """Paint data for the given page to the printer. Arguments: pageNum -- the page number to be printed painter -- the painter for this print job """ totalNumPages = self.outputGroup[-1].pageNum headerDoc = self.headerFooterDoc(True, pageNum, totalNumPages) if headerDoc: layout = headerDoc.documentLayout() layout.setPaintDevice(self.printer) headerDoc.setTextWidth(self.pageLayout.paintRect().width() * self.printer.logicalDpiX()) painter.save() topMargin = self.pageLayout.margins(QPageLayout.Inch).top() headerDelta = ((self.headerMargin - topMargin) * self.printer.logicalDpiX()) painter.translate(0, int(headerDelta)) layout.draw(painter, QAbstractTextDocumentLayout.PaintContext()) painter.restore() painter.save() columnSpacing = int(self.columnSpacing * self.printer.logicalDpiX()) columnDelta = ( (self.pageLayout.paintRect().width() * self.printer.logicalDpiX() - columnSpacing * (self.numColumns - 1)) / self.numColumns) + columnSpacing for columnNum in range(self.numColumns): if columnNum > 0: painter.translate(columnDelta, 0) self.paintColumn(pageNum, columnNum, painter) painter.restore() footerDoc = self.headerFooterDoc(False, pageNum, totalNumPages) if footerDoc: layout = footerDoc.documentLayout() layout.setPaintDevice(self.printer) footerDoc.setTextWidth(self.pageLayout.paintRect().width() * self.printer.logicalDpiX()) painter.save() bottomMargin = self.pageLayout.margins(QPageLayout.Inch).bottom() footerDelta = ((bottomMargin - self.footerMargin) * self.printer.logicalDpiX()) painter.translate( 0, self.pageLayout.paintRect().height() * self.printer.logicalDpiX() + int(footerDelta) - self.lineSpacing) layout.draw(painter, QAbstractTextDocumentLayout.PaintContext()) painter.restore()
def _draw_textdoc(self, rect, col): """Draw the QTextDocument of an item. Args: rect: The QRect to clip the drawing to. """ # We can't use drawContents because then the color would be ignored. clip = QRectF(0, 0, rect.width(), rect.height()) self._painter.save() if self._opt.state & QStyle.State_Selected: color = config.val.colors.completion.item.selected.fg elif not self._opt.state & QStyle.State_Enabled: color = config.val.colors.completion.category.fg else: colors = config.val.colors.completion.fg # if multiple colors are set, use different colors per column color = colors[col % len(colors)] self._painter.setPen(color) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette.setColor(QPalette.Text, self._painter.pen().color()) if clip.isValid(): self._painter.setClipRect(clip) ctx.clip = clip self._doc.documentLayout().draw(self._painter, ctx) self._painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) self.doc.setTextWidth(option.rect.width()) options.text = "" style = QApplication.style() if options.widget is None else options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, option.palette.color( QPalette.Active, QPalette.HighlightedText)) else: ctx.palette.setColor(QPalette.Text, option.palette.color( QPalette.Active, QPalette.Text)) text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): self.initStyleOption(option, index) foreground = index.data(Qt.ForegroundRole) font = index.data(Qt.FontRole) style = QApplication.style() doc = QTextDocument() if index.column() == HistoryTableModel.columns_types.index('pubkey'): doc.setHtml(option.text) else: doc.setPlainText(option.text) option.text = "" style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if foreground: if foreground.isValid(): ctx.palette.setColor(QPalette.Text, foreground) if font: doc.setDefaultFont(font) text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def _draw_textdoc(self, rect): """Draw the QTextDocument of an item. Args: rect: The QRect to clip the drawing to. """ # We can't use drawContents because then the color would be ignored. # See: https://qt-project.org/forums/viewthread/21492 clip = QRectF(0, 0, rect.width(), rect.height()) self._painter.save() if self._opt.state & QStyle.State_Selected: option = 'completion.item.selected.fg' elif not self._opt.state & QStyle.State_Enabled: option = 'completion.category.fg' else: option = 'completion.fg' try: self._painter.setPen(config.get('colors', option)) except configexc.NoOptionError: self._painter.setPen(config.get('colors', 'completion.fg')) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette.setColor(QPalette.Text, self._painter.pen().color()) if clip.isValid(): self._painter.setClipRect(clip) ctx.clip = clip self._doc.documentLayout().draw(self._painter, ctx) self._painter.restore()
def paint(self, painter, option, index): self.initStyleOption(option, index) style = QApplication.style() doc = QTextDocument() if index.column() in (NetworkTableModel.columns_types.index('address'), NetworkTableModel.columns_types.index('port'), NetworkTableModel.columns_types.index('api')): doc.setHtml(option.text) else: doc.setPlainText(option.text) option.text = "" style.drawControl(QStyle.CE_ItemViewItem, option, painter) ctx = QAbstractTextDocumentLayout.PaintContext() text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option: QStyleOptionViewItem, index: QModelIndex): self.initStyleOption(option, index) mouse_over = option.state & QStyle.State_MouseOver painter.save() color = '' if option.state & QStyle.State_Selected: if option.state & QStyle.State_HasFocus: painter.fillRect(option.rect, QBrush(option.palette.color(QPalette.Active, option.palette.Highlight))) color = "color: white" else: painter.fillRect(option.rect, QBrush(option.palette.color(QPalette.Inactive, option.palette.Highlight))) else: painter.setBrush(QBrush(Qt.white)) if mouse_over: doc = self.doc_hovered_item self.last_hovered_pos = option.rect.topLeft() doc.setDefaultStyleSheet(f"a {{{color}}}") else: doc = self.doc_not_hovered self.parent().unsetCursor() doc.setDefaultStyleSheet(f"a {{text-decoration: none;{color}}}") doc.setDefaultFont(option.font) doc.setHtml(option.text) painter.translate(option.rect.topLeft() + QPoint(HTML_LINK_HORZ_MARGIN, 0)) ctx = QAbstractTextDocumentLayout.PaintContext() ctx.palette = option.palette clip = QRect(0, 0, option.rect.width() - HTML_LINK_HORZ_MARGIN * 2, option.rect.height()) painter.setClipRect(clip) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): painter.save() options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) options.text = "" # 原字符 style = QApplication.style( ) if options.widget is None else options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) else: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.Text)) text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, options) the_fuck_your_shit_up_constant = 3 #  ̄へ ̄ # margin = (option.rect.height() - options.fontMetrics.height()) // 2 margin = margin - the_fuck_your_shit_up_constant text_rect.setTop(text_rect.top() + margin) painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): painter.save() options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setPlainText(options.text) column = index.column() if column in self.disasm_columns: options.font.setWeight(QFont.Bold) self.highlight(self.doc, self.disasm_rules) elif column in self.value_columns: options.font.setWeight(QFont.Bold) self.highlight(self.doc, self.value_rules) self.doc.setDefaultFont(options.font) options.text = "" style = (QApplication.style() if options.widget is None else options.widget.style()) style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText), ) else: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.Text), ) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) if index.column() != 0: textRect.adjust(5, 0, 0, 0) the_constant = 4 margin = (option.rect.height() - options.fontMetrics.height()) // 2 margin = margin - the_constant textRect.setTop(textRect.top() + margin) painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): text = index.data() if isinstance(text, str): painter.save() doc = QTextDocument() doc.setHtml(text) doc.setPageSize(QSizeF(option.rect.size())) painter.setClipRect(option.rect) painter.translate(option.rect.x(), option.rect.y()) doc.documentLayout().draw( painter, QAbstractTextDocumentLayout.PaintContext()) painter.restore() else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter: QPainter, option, index): # reference following link # https://stackoverflow.com/questions/53353450/how-to-highlight-a-words-in-qtablewidget-from-a-searchlist # https://stackoverflow.com/questions/34623036/implementing-a-delegate-for-wordwrap-in-a-qtreeview-qt-pyside-pyqt painter.save() doc = QTextDocument(self) # option.palette.setColor(QPalette.HighlightedText, QColor.fromRgb(30, 136, 200)) # remove dotted border in table if option.state & QStyle.State_HasFocus: option.state = option.state ^ QStyle.State_HasFocus options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc.setPlainText(options.text) # keyword = index.data(Qt.UserRole) # if self.keywords: self.keywordHighlight(doc) options.text = "" # print(dir(options.palette)) # print(options.palette.Highlight) style = QApplication.style() if options.widget is None \ else options.widget.style() style.drawControl(QStyle.CE_ItemViewItem, options, painter, options.widget) # for selection highlight ctx = QAbstractTextDocumentLayout.PaintContext() if index.data(Qt.UserRole) == 'reserved': ctx.palette.setColor(QPalette.Text, QColor.fromRgb(100, 100, 100)) doc.setDefaultFont( QFont(option.font.family(), option.font.pointSize() * 2 // 3)) else: ctx.palette.setColor(QPalette.Text, QColor.fromRgb(217, 217, 217)) doc.setDefaultFont(option.font) textRect = option.rect # margin = 4 # textRect.setTop(textRect.top() + margin) textRect.setTop(textRect.top()) painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.setTextWidth(option.rect.width()) doc.documentLayout().draw(painter, ctx) painter.restore()
def paintColumn(self, pageNum, columnNum, painter): """Paint data for the given column to the printer. Arguments: pageNum -- the page number to be printed columnNum -- the column number to be printed painter -- the painter for this print job """ columnItems = [ item for item in self.outputGroup if item.pageNum == pageNum and item.columnNum == columnNum ] for item in columnItems: layout = item.doc.documentLayout() painter.save() painter.translate(item.level * self.indentSize, item.pagePos) layout.draw(painter, QAbstractTextDocumentLayout.PaintContext()) painter.restore() if self.drawLines: self.addPrintLines(pageNum, columnNum, columnItems, painter)
def paint(self, painter, option, index): self.initStyleOption(option, index) style = option.widget.style() if option.widget else QApplication.style() palette = QApplication.palette() color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else palette.base() ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option) painter.save() painter.fillRect(option.rect, color) painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc = QTextDocument() doc.setHtml(option.text) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): """QStyledItemDelegate.paint implementation """ option.state &= ~QStyle.State_HasFocus # never draw focus rect options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = QApplication.style( ) if options.widget is None else options.widget.style() doc = QTextDocument() doc.setDocumentMargin(1) doc.setHtml(options.text) if options.widget is not None: doc.setDefaultFont(options.widget.font()) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) """Original example contained line painter.setClipRect(textRect.translated(-textRect.topLeft())) but text is drawn clipped with it on kubuntu 12.04 """ doc.documentLayout().draw(painter, ctx) painter.restore()