def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex) -> None: opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) widget = option.widget style = QApplication.style() if widget is None else widget.style() # Keep ref to style wrapper. This is ugly, wrong but the wrapping of # C++ QStyle instance takes ~5% unless the wrapper already exists. self.__style = style text = opt.text opt.text = "" style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget) trect = style.subElementRect(QStyle.SE_ItemViewItemText, opt, widget) opt.text = text self.drawViewItemText(style, painter, opt, trect)
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = QApplication.style( ) if options.widget is None else options.widget.style() doc = QtGui.QTextDocument() doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() if options.state & QStyle.State_Selected: ctx.palette.setColor( QtGui.QPalette.Text, options.palette.color(QtGui.QPalette.Active, QtGui.QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
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: QStyleOptionViewItem, index: QModelIndex) -> QSize: widget = option.widget template = self.template(-10**self.ndigits, self.ndecimals) span = self.spanData(index) if span is not None: vmin, vmax = span t1 = self.template(vmin, self.ndecimals) t2 = self.template(vmax, self.ndecimals) template = max((t1, t2), key=len) style = widget.style() if widget is not None else QApplication.style() # Keep ref to style wrapper. This is ugly, wrong but the wrapping of # C++ QStyle instance takes ~5% unless the wrapper already exists. self.__style = style opt = QStyleOptionViewItem(option) opt.features |= QStyleOptionViewItem.HasDisplay sh = QSize() key = option.font.key(), template if key not in self.__sh_cache: for d in map(str, range(10)): opt.text = template.replace("X", d) sh_ = style.sizeFromContents(QStyle.CT_ItemViewItem, opt, QSize(), widget) sh = sh.expandedTo(sh_) self.__sh_cache[key] = sh else: sh = self.__sh_cache[key] return QSize(sh)
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options,index) style = QApplication.style() if options.widget is None else options.widget.style() doc = QtGui.QTextDocument() doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() if options.state & QStyle.State_Selected: ctx.palette.setColor(QtGui.QPalette.Text, options.palette.color(QtGui.QPalette.Active, QtGui.QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint( self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex ) -> None: opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) widget = option.widget style = QApplication.style() if widget is None else widget.style() self.__style = style text = opt.text opt.text = "" style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget) textrect = style.subElementRect( QStyle.SE_ItemViewItemText, opt, widget) ratio = self.barFillRatioData(index) if ratio is not None and 0. <= ratio <= 1.: color = self.barColorData(index) if color is None: color = self.color if not color.isValid(): color = opt.palette.color(QPalette.Foreground) rect = option.rect pw = self.penWidth hmargin = 3 + pw / 2 # + half pen width for the round line cap vmargin = 1 textoffset = pw + vmargin * 2 baseline = rect.bottom() - textoffset / 2 width = (rect.width() - 2 * hmargin) * ratio painter.save() painter.setRenderHint(QPainter.Antialiasing) pen = self.__pen pen.setColor(color) pen.setWidth(pw) painter.setPen(pen) line = self.__line left = rect.left() + hmargin line.setLine(left, baseline, left + width, baseline) painter.drawLine(line) painter.restore() textrect.adjust(0, 0, 0, -textoffset) opt.text = text self.drawViewItemText(style, painter, opt, textrect)
def init_style_option( delegate: QStyledItemDelegate, option: QStyleOptionViewItem, index: QModelIndex, data: Mapping[int, Any], roles: Optional[Container[int]] = None, ) -> None: """ Like `QStyledItemDelegate.initStyleOption` but fill in the fields from `data` mapping. If `roles` is not `None` init the `option` for the specified `roles` only. """ # pylint: disable=too-many-branches option.styleObject = None option.index = index if roles is None: roles = data features = 0 if Qt.DisplayRole in roles: value = data.get(Qt.DisplayRole) if value is not None: option.text = delegate.displayText(value, option.locale) features |= _QStyleOptionViewItem_HasDisplay if Qt.FontRole in roles: value = data.get(Qt.FontRole) font = cast_(QFont, value) if font is not None: font = font.resolve(option.font) option.font = font option.fontMetrics = QFontMetrics(option.font) if Qt.ForegroundRole in roles: value = data.get(Qt.ForegroundRole) foreground = cast_(QBrush, value) if foreground is not None: option.palette.setBrush(QPalette.Text, foreground) if Qt.BackgroundRole in roles: value = data.get(Qt.BackgroundRole) background = cast_(QBrush, value) if background is not None: option.backgroundBrush = background if Qt.TextAlignmentRole in roles: value = data.get(Qt.TextAlignmentRole) alignment = cast_(int, value) if alignment is not None: alignment = alignment & _AlignmentMask option.displayAlignment = _AlignmentCache[alignment] if Qt.CheckStateRole in roles: state = data.get(Qt.CheckStateRole) if state is not None: features |= _QStyleOptionViewItem_HasCheckIndicator state = cast_(int, state) if state is not None: option.checkState = state if Qt.DecorationRole in roles: value = data.get(Qt.DecorationRole) if value is not None: features |= _QStyleOptionViewItem_HasDecoration if isinstance(value, QIcon): option.icon = value elif isinstance(value, QColor): pix = QPixmap(option.decorationSize) pix.fill(value) option.icon = QIcon(pix) elif isinstance(value, QPixmap): option.icon = QIcon(value) option.decorationSize = (value.size() / value.devicePixelRatio()).toSize() elif isinstance(value, QImage): pix = QPixmap.fromImage(value) option.icon = QIcon(value) option.decorationSize = (pix.size() / pix.devicePixelRatio()).toSize() option.features |= features