def __init__(self, model, parent, *args): super(HexItemDelegate, self).__init__(parent) # compute size hint for hex view dh = QTextDocument() dh.setHtml("<font color='green'>DF</font>") self.hex_hint = QtCore.QSize(dh.idealWidth()-dh.documentMargin(), 22) # compute size hint for char view dc = QTextDocument() dc.setHtml("W") self.char_hint = QtCore.QSize(dc.idealWidth()-dc.documentMargin(), 22) self._model = model
def sizeHint(self, option, index): text = index.model().data(index) doc = QTextDocument() doc.setHtml(text) width = index.model().data(index, Qt.UserRole + 1337) doc.setTextWidth(width) return QSize(doc.idealWidth(), doc.size().height())
def sizeHint(self, option, index): self.initStyleOption(option, index) doc = QTextDocument() doc.setHtml(option.text) doc.setTextWidth(option.rect.width()) return QSize(doc.idealWidth(), max(doc.size().height(), option.decorationSize.height()))
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options,index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QSize(doc.idealWidth(), doc.size().height())
def sizeHint(self, option, index): self.initStyleOption(option, index) doc = QTextDocument() if index.column() == HistoryTableModel.columns_types.index('pubkey'): doc.setHtml(option.text) else: doc.setPlainText("") doc.setTextWidth(-1) return QSize(doc.idealWidth(), doc.size().height())
def sizeHint(self, option, index): self.initStyleOption(option, index) doc = QTextDocument() if index.column() in (NetworkTableModel.columns_types.index('address'), NetworkTableModel.columns_types.index('port')): doc.setHtml(option.text) else: doc.setPlainText("") doc.setTextWidth(option.rect.width()) return QSize(doc.idealWidth(), doc.size().height())
def sizeHint(self, option, index): fm = option.fontMetrics if index.column() == ATOM: return QSize(fm.width("9,999,999"), fm.height()) if index.column() == resName: text = index.model().data(index) document = QTextDocument() document.setDefaultFont(option.font) document.setHtml(text) return QSize(document.idealWidth() + 5, fm.height()) return QStyledItemDelegate.sizeHint(self, option, index)
def sizeHint(self, option, index): fm = option.fontMetrics if index.column() == residNum: return QSize(fm.width("9,999,999"), fm.height()) if index.column() == residName: text = index.model().data(index) document = QTextDocument() document.setDefaultFont(option.font) document.setHtml(text) return QSize(document.idealWidth() + 5, fm.height()) return QStyledItemDelegate.sizeHint(self, option, index)
def sizeHint(self, option, index): """QStyledItemDelegate.sizeHint implementation """ options = QStyleOptionViewItem(option) self.initStyleOption(options,index) doc = QTextDocument() doc.setDocumentMargin(1) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) doc.setHtml(options.text) return QSize(doc.idealWidth(), QStyledItemDelegate.sizeHint(self, option, index).height())
def sizeHint(self, option, index): self.initStyleOption(option, index) 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("") doc.setTextWidth(option.rect.width()) return QSize(doc.idealWidth(), doc.size().height())
def sizeHint(self, option, index): """QStyledItemDelegate.sizeHint implementation """ options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setDocumentMargin(1) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) doc.setHtml(options.text) return QSize( int(doc.idealWidth()), int(QStyledItemDelegate.sizeHint(self, option, index).height()))
class TableDelegate(QStyledItemDelegate): """Table 富文本""" def __init__(self, parent=None): super(TableDelegate, self).__init__(parent) self.doc = QTextDocument(self) 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 sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) self.doc.setTextWidth(options.rect.width()) return QSize(self.doc.idealWidth(), self.doc.size().height())
def sizeHint(self, option, index): msg = index.model().data(index, Qt.DisplayRole) field = QRect(option.rect) field.setWidth(variables.window_width - variables.WINDOW_PADDING) field = field.marginsRemoved(variables.TEXT_PADDING) doc = QTextDocument(msg.text) doc.setDocumentMargin(0) opt = QTextOption() opt.setWrapMode(opt.WrapAtWordBoundaryOrAnywhere) doc.setDefaultTextOption(opt) doc.setDefaultFont(variables.font) if msg.user == variables.USER_ME: doc.setTextWidth(field.size().width() - 20 - 50) else: doc.setTextWidth(field.size().width() - 20) field.setHeight(int(doc.size().height())) field.setWidth(int(doc.idealWidth())) field = field.marginsAdded(variables.TEXT_PADDING) return QSize(0, field.size().height())
class HTMLDelegate(QStyledItemDelegate): def __init__(self, parent=None): super(HTMLDelegate, self).__init__(parent) self.doc = QTextDocument(self) 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 sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) self.doc.setTextWidth(option.rect.width()) return QSize(self.doc.idealWidth(), self.doc.size().height())
class DetailsDialog(QWidget): '''Dialog showing mod details''' def __init__(self, parent: QWidget, mod: Mod): super().__init__(parent) self.setWindowFlags(QtCore.Qt.Window) self.setObjectName("Details") self.resize(700, 800) self.setMinimumSize(600, 600) self.layout = QHBoxLayout(self) self.layout.setObjectName("layout") self.document = QTextDocument() self.document.setPlainText(str(mod)) self.text = QTextEdit(self) self.text.setObjectName("text") self.text.setDocument(self.document) self.text.setAutoFormatting(QTextEdit.AutoAll) self.text.setReadOnly(True) self.text.setLineWrapMode(QTextEdit.NoWrap) self.layout.addWidget(self.text) self.setWindowTitle(mod.name + " " + TRANSLATE("Details", "Details")) QtCore.QMetaObject.connectSlotsByName(self) def adjustWidth(self): '''Fits size to content''' self.resize( self.document.idealWidth() + \ self.text.contentsMargins().left() + self.text.contentsMargins().right() + \ self.contentsMargins().left() + self.contentsMargins().right() + 50, self.height()) def showEvent(self, event): '''Qt show event''' super().showEvent(event) self.adjustWidth() def keyPressEvent(self, event): '''Qt KeyPressEvent override''' if event.key() == QtCore.Qt.Key_Escape: self.close()
def sizeHint(self, option, index): fm = option.fontMetrics document = QTextDocument() document.setDefaultFont(option.font) document.setHtml(index.model().data(index, Qt.DisplayRole)) return QSize(document.idealWidth() + 20, fm.height())
def paint(self, painter, option, index): msg = index.model().data(index, Qt.DisplayRole) field = QRect(option.rect) field = field.marginsRemoved(variables.TEXT_PADDING) doc = QTextDocument(msg.text) doc.setDocumentMargin(0) opt = QTextOption() opt.setWrapMode(opt.WrapAtWordBoundaryOrAnywhere) doc.setDefaultTextOption(opt) doc.setDefaultFont(variables.font) if msg.user == variables.USER_ME: doc.setTextWidth(field.size().width() - 20 - 50) else: doc.setTextWidth(field.size().width() - 20) field.setHeight(int(doc.size().height())) field.setWidth(int(doc.idealWidth())) field = field.marginsAdded(variables.TEXT_PADDING) line_height = QFontMetrics(variables.font).lineSpacing( ) + variables.TEXT_PADDING.bottom() - variables.BUBBLE_PADDING.bottom( ) + variables.TEXT_PADDING.top() - variables.BUBBLE_PADDING.top() if msg.user == variables.USER_ME: rect = QRect(option.rect.right() - field.size().width() - 20, option.rect.top(), field.size().width(), field.size().height()) else: rect = QRect(20, option.rect.top(), field.size().width(), field.size().height()) bubblerect = rect.marginsRemoved(variables.BUBBLE_PADDING) textrect = rect.marginsRemoved(variables.TEXT_PADDING) if msg.user == variables.USER_ME: p1 = bubblerect.topRight() p2 = bubblerect.bottomLeft() + QPoint(-36, -int(line_height / 2)) else: p1 = bubblerect.topLeft() painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.NoPen) color = QColor(variables.BUBBLE_COLORS[msg.user]) painter.setBrush(color) painter.drawRoundedRect(bubblerect, 10, 10) painter.drawPolygon(p1 + QPoint(-20, 0), p1 + QPoint(20, 0), p1 + QPoint(0, 15)) if msg.user == variables.USER_ME: if msg.status == variables.STATUS_UNREAD: painter.setPen(Qt.NoPen) painter.setBrush(QColor(variables.STATUS_COLOR)) painter.drawEllipse(p2, 7, 7) elif msg.status == variables.STATUS_UNDELIVERED: pen = QPen(QColor(variables.STATUS_COLOR)) pen.setWidth(2) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawEllipse(p2, 7, 7) painter.drawLine(p2, p2 + QPoint(0, -5)) painter.drawLine(p2, p2 + QPoint(3, 0)) painter.setPen(Qt.gray) painter.setFont(variables.font) painter.translate(textrect.x(), textrect.y()) textrectf = QRectF(textrect) textrectf.moveTo(0, 0) doc.drawContents(painter, textrectf) painter.translate(-textrect.x(), -textrect.y())
class LibraryDelegate(QStyledItemDelegate): """Delegate used for the library. The delegate draws the items. """ # Storing the styles makes the code more readable and faster IMHO # pylint: disable=too-many-instance-attributes def __init__(self): super().__init__() self.doc = QTextDocument(self) self.doc.setDocumentMargin(0) # Named properties for html self.font = styles.get("library.font") self.font_metrics = QFontMetrics(QFont(self.font)) self.fg = styles.get("library.fg") self.dir_fg = styles.get("library.directory.fg") self.search_fg = styles.get("library.search.highlighted.fg") # QColor options for background drawing self.selection_bg = QColor() self.selection_bg.setNamedColor(styles.get("library.selected.bg")) self.even_bg = QColor() self.odd_bg = QColor() self.even_bg.setNamedColor(styles.get("library.even.bg")) self.odd_bg.setNamedColor(styles.get("library.odd.bg")) self.search_bg = QColor() self.search_bg.setNamedColor( styles.get("library.search.highlighted.bg")) def createEditor(self, *args): """Library is not editable by the user.""" return None def paint(self, painter, option, index): """Override the QStyledItemDelegate paint function. Args: painter: The QPainter. option: The QStyleOptionViewItem. index: The QModelIndex. """ self._draw_background(painter, option, index) self._draw_text(painter, option, index) def _draw_text(self, painter, option, index): """Draw text for the library. Sets the font and the foreground color using html. The foreground color depends on whether the path is a directory and on whether it is highlighted as search result or not. Args: painter: The QPainter. option: The QStyleOptionViewItem. index: The QModelIndex. """ text = index.model().data(index) painter.save() color = self._get_foreground_color(index, text) text = self.elided(text, option.rect.width() - 1) text = wrap_style_span(f"color: {color}; font: {self.font}", text) self.doc.setHtml(text) self.doc.setTextWidth(option.rect.width() - 1) painter.translate(option.rect.x(), option.rect.y()) self.doc.drawContents(painter) painter.restore() def _draw_background(self, painter, option, index): """Draw the background rectangle of the text. The color depends on whether the item is selected, in an even row or in an odd row. Args: painter: The QPainter. option: The QStyleOptionViewItem. index: The QModelIndex. """ color = self._get_background_color(index, option.state) painter.save() painter.setBrush(color) painter.setPen(Qt.NoPen) painter.drawRect(option.rect) painter.restore() def _get_foreground_color(self, index, text): """Return the foreground color of an item. The color depends on highlighted as search result and whether it is a directory. Args: index: Index of the element indicating even/odd/highlighted. text: Text indicating directory or not. """ if index.model().is_highlighted(index): return self.search_fg return self.dir_fg if text.endswith("/") else self.fg def _get_background_color(self, index, state): """Return the background color of an item. The color depends on selected, highlighted as search result and even/odd. Args: index: Index of the element indicating even/odd/highlighted. state: State of the index indicating selected. """ if state & QStyle.State_Selected: return self.selection_bg if index.model().is_highlighted(index): return self.search_bg if index.row() % 2: return self.odd_bg return self.even_bg def elided(self, text, width): """Return an elided text preserving html tags. If the text is wider than width, it is elided by replacing characters from the middle by …. Surrounding html tags are not included in the calculation and left untouched. Args: text: The text to elide. width: Width in pixels that the text may take. Returns: Elided version of the text. """ mark_str = api.settings.statusbar.mark_indicator.value marked = text.startswith(mark_str) html_stripped = strip_html(text) elided = self.font_metrics.elidedText(html_stripped, Qt.ElideMiddle, width) # This becomes more complicated as the html is no longer simply surrounding # We must bring back the html from the mark indicator before replacing if marked: mark_stripped = strip_html(mark_str) html_stripped = html_stripped.replace(mark_stripped, mark_str) elided = elided.replace(mark_stripped, mark_str) return text.replace(html_stripped, elided) def sizeHint(self, option, index): """Return size of the QTextDocument as size hint.""" text = wrap_style_span(f"font: {self.font}", "any") self.doc.setHtml(text) return QSize(self.doc.idealWidth(), self.doc.size().height())
def sizeHint(self, option, index): text = index.model().data(index).toString() document = QTextDocument() document.setDefaultFont(option.font) document.setHtml(text) return QSize(document.idealWidth() + 5, option.fontMetrics.height())