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 = QTextDocument() text = options.text doc.setHtml(text) doc.setDocumentMargin(0) # This needs to be an empty string to avoid the overlapping the # normal text of the QTreeWidgetItem options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) 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) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() # Adjustments for the file switcher if hasattr(options.widget, 'files_list'): if style.objectName() in ['oxygen', 'qtcurve', 'breeze']: if options.widget.files_list: painter.translate(textRect.topLeft() + QPoint(4, -9)) else: painter.translate(textRect.topLeft()) else: if options.widget.files_list: painter.translate(textRect.topLeft() + QPoint(4, 4)) else: painter.translate(textRect.topLeft() + QPoint(2, 4)) else: painter.translate(textRect.topLeft() + QPoint(0, -3)) doc.documentLayout().draw(painter, ctx) painter.restore()
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 = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() if style.objectName() == "oxygen": painter.translate(textRect.topLeft() + QPoint(5, -9)) else: 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) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() if style.objectName() == 'oxygen': painter.translate(textRect.topLeft() + QPoint(5, -9)) else: painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) size = QSize(int(doc.idealWidth()), int(doc.size().height())) return size
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) return QSize(doc.idealWidth(), doc.size().height() - 2)
def _prepare_text_document(self, option, index): # This logic must be shared between paint and sizeHint for consistency options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) return options, doc
def _get_height(self): """ Return the expected height of this item's text, including the text margins. """ doc = QTextDocument() doc.setHtml('<hr>') doc.setDocumentMargin(self._PADDING) return doc.size().height()
def _get_height(self): """ Return the expected height of this item's text, including the text margins. """ doc = QTextDocument() try: doc.setHtml('<span style="font-size:{}pt">Title</span>' .format(self._styles['title_font_size'])) except KeyError: doc.setHtml('<span>Title</span>') doc.setDocumentMargin(self._PADDING) return doc.size().height()
def get_text_width(text) -> int: """Return the width required to render ``text`` (including rich text elements).""" if qtpy.PYSIDE2: from qtpy.QtGui import Qt as _Qt else: from qtpy.QtCore import Qt as _Qt if _Qt.mightBeRichText(text): doc = QTextDocument() doc.setHtml(text) return doc.size().width() else: fm = QFontMetrics(QFont("", 0)) return fm.boundingRect(text).width() + 5
def get_legend_items(self): plot = self.plot() if plot is None: return [] text_items = [] for item in plot.get_items(): if not isinstance(item, CurveItem) or not self.include_item(item): continue text = QTextDocument() text.setDefaultFont(self.font) text.setDefaultStyleSheet("div { color: %s; }" % self.color) text.setHtml("<div>%s</div>" % item.curveparam.label) text_items.append((text, item.pen(), item.brush(), item.symbol())) return text_items
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()) # Set background color for selected and hovered items. # Inspired by: # - https://stackoverflow.com/a/43253004/438386 # - https://stackoverflow.com/a/27274233/438386 # This is commented for now until we find a way to correctly colorize # the entire line with a single color. # if options.state & QStyle.State_Selected: # # This only applies when the selected item doesn't have focus # if not (options.state & QStyle.State_HasFocus): # options.palette.setBrush( # QPalette.Highlight, # QBrush(self._background_color) # ) if options.state & QStyle.State_MouseOver: painter.fillRect(option.rect, self._background_color) # Set text doc = QTextDocument() text = options.text doc.setHtml(text) doc.setDocumentMargin(0) # This needs to be an empty string to avoid overlapping the # normal text of the QTreeWidgetItem options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(textRect.topLeft() + QPoint(0, 4)) doc.documentLayout().draw(painter, ctx) painter.restore()
class LabelItem(AbstractLabelItem): __implements__ = (IBasePlotItem, ISerializableType) def __init__(self, text=None, labelparam=None): self.text_string = "" if text is None else text self.text = QTextDocument() super(LabelItem, self).__init__(labelparam) def __reduce__(self): return (self.__class__, (self.text_string, self.labelparam)) def serialize(self, writer): """Serialize object to HDF5 writer""" super(LabelItem, self).serialize(writer) writer.write(self.text_string, group_name="text") def deserialize(self, reader): """Deserialize object from HDF5 reader""" super(LabelItem, self).deserialize(reader) self.set_text(reader.read("text", func=reader.read_unicode)) def types(self): return (IShapeItemType, ISerializableType) def set_pos(self, x, y): self.G = x, y self.labelparam.xg, self.labelparam.yg = x, y def get_plain_text(self): return to_text_string(self.text.toPlainText()) def set_text(self, text=None): if text is not None: self.text_string = text self.text.setHtml("<div>%s</div>" % self.text_string) def set_text_style(self, font=None, color=None): if font is not None: self.text.setDefaultFont(font) if color is not None: self.text.setDefaultStyleSheet("div { color: %s; }" % color) self.set_text() def get_text_rect(self): sz = self.text.size() return QRectF(0, 0, sz.width(), sz.height()) def update_text(self): pass def draw(self, painter, xMap, yMap, canvasRect): self.update_text() x, y = self.get_top_left(xMap, yMap, canvasRect) x0, y0 = self.get_origin(xMap, yMap, canvasRect) painter.save() self.marker.drawSymbols(painter, [QPointF(x0, y0)]) painter.restore() sz = self.text.size() self.draw_frame(painter, x, y, sz.width(), sz.height()) painter.setPen(QPen(QColor(self.labelparam.color))) painter.translate(x, y) self.text.drawContents(painter)