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 sizeHint(self, option, index): """Override sizeHint of QStyledItemDelegate. Return the cell size based on the QTextDocument size, but might not work correctly yet. Args: option: const QStyleOptionViewItem & option index: const QModelIndex & index Return: A QSize with the recommended size. """ value = index.data(Qt.SizeHintRole) if value is not None: return value self._opt = QStyleOptionViewItem(option) self.initStyleOption(self._opt, index) self._style = self._opt.widget.style() self._get_textdoc(index) docsize = self._doc.size().toSize() size = self._style.sizeFromContents(QStyle.CT_ItemViewItem, self._opt, docsize, self._opt.widget) qtutils.ensure_valid(size) return size + QSize(10, 3)
def paint(self, painter, option, index): """Performs custom painting of value of data in the model and decorations. Performs custom painting of value of data in the model at the specified index plus any decorations used in that column. Args: painter - QPainter option - QStyleOptionViewItem index - QModelIndex """ xOffset = 0 # First added for #15, the painting of custom amount information. This can # be used as a pattern for painting any column of information. value_painter = self._get_value_painter(index) self._display_text = value_painter is None QStyledItemDelegate.paint(self, painter, option, index) if value_painter is not None: value_option = QStyleOptionViewItem(option) rect = value_option.rect rect = QRect(rect.left(), rect.top(), rect.width() - xOffset, rect.height()) value_option.rect = rect value_painter.paint(painter, value_option, index) decorations = self._get_decorations(index, bool(option.state & QStyle.State_Selected)) for dec in decorations: pixmap = dec.pixmap x = option.rect.right() - pixmap.width() - xOffset y = option.rect.center().y() - (pixmap.height() // 2) rect = QRect(x, y, pixmap.width(), pixmap.height()) painter.drawPixmap(rect, pixmap) xOffset += pixmap.width()
def drawRow(self, painter, option, index): opt = QStyleOptionViewItem(option) hasValue = True if (self.m_editorPrivate): property = self.m_editorPrivate.indexToProperty(index) if (property): hasValue = property.hasValue() if (not hasValue and self.m_editorPrivate.markPropertiesWithoutValue()): c = option.palette.color(QPalette.Dark) painter.fillRect(option.rect, c) opt.palette.setColor(QPalette.AlternateBase, c) else: c = self.m_editorPrivate.calculatedBackgroundColor( self.m_editorPrivate.indexToBrowserItem(index)) if (c.isValid()): painter.fillRect(option.rect, c) opt.palette.setColor(QPalette.AlternateBase, c.lighter(112)) super(QtPropertyEditorView, self).drawRow(painter, opt, index) color = QApplication.style().styleHint(QStyle.SH_Table_GridLineColor, opt) painter.save() painter.setPen(QPen(QColor(color))) painter.drawLine(opt.rect.x(), opt.rect.bottom(), opt.rect.right(), opt.rect.bottom()) painter.restore()
def paint(self, painter, option, index): if not index.isValid(): return QStyledItemDelegate.paint(self, painter, option, index) else: item = index.internalPointer() d = item.data(index.column(), Qt.DisplayRole) if not d: d = 0 lbl = self.mdlLabels.item(int(d), 0) opt = QStyleOptionViewItem(option) self.initStyleOption(opt, self.mdlLabels.indexFromItem(lbl)) qApp.style().drawControl(QStyle.CE_ItemViewItem, opt, painter) # Drop down indicator if index.isValid() and index.internalPointer().data( Outline.label) not in ["", None, "0", 0]: opt = QStyleOptionComboBox() opt.rect = option.rect r = qApp.style().subControlRect(QStyle.CC_ComboBox, opt, QStyle.SC_ComboBoxArrow) option.rect = r qApp.style().drawPrimitive(QStyle.PE_IndicatorArrowDown, option, painter)
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 paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) for top, _, block in self._neditor.visible_blocks: if not self.is_foldable_block(block): continue branch_rect = QRect(0, top, self.sizeHint().width(), self.sizeHint().height()) opt = QStyleOptionViewItem() opt.rect = branch_rect opt.state = (QStyle.State_Active | QStyle.State_Item | QStyle.State_Children) folded = self.user_data(block).get("folded", default=False) if not folded: opt.state |= QStyle.State_Open # Draw item self.style().drawPrimitive(QStyle.PE_IndicatorBranch, opt, painter, self) # Draw folded region background if block == self.__mouse_over and not self.__timer.isActive(): fm_height = self._neditor.fontMetrics().height() rect_height = 0 color = self.palette().highlight().color() color.setAlpha(100) if not folded: foldable_blocks = self.code_folding.foldable_blocks(block) rect_height = (len(list(foldable_blocks))) * fm_height painter.fillRect( QRect(0, top, self.sizeHint().width(), rect_height + fm_height), color)
def sizeHint(self, option, index): """ Public method to get a size hint for the specified list item. @param option style option used for painting (QStyleOptionViewItem) @param index model index of the item (QModelIndex) @return size hint (QSize) """ if not self.__rowHeight: opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) widget = opt.widget style = widget.style() if widget is not None \ else QApplication.style() padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin) + 1 titleFont = opt.font titleFont.setBold(True) titleFont.setPointSize(titleFont.pointSize() + 1) self.__padding = padding \ if padding > GreaseMonkeyConfigurationListDelegate.MinPadding \ else GreaseMonkeyConfigurationListDelegate.MinPadding titleMetrics = QFontMetrics(titleFont) self.__rowHeight = 2 * self.__padding + \ opt.fontMetrics.leading() + \ opt.fontMetrics.height() + \ titleMetrics.height() return QSize(GreaseMonkeyConfigurationListDelegate.ItemWidth, self.__rowHeight)
def export_tv_graph(graph_filename, scene): # define the bounding rectangle for pixmap for painter to paint in w = int(scene.width()) + 40 h = int(scene.height()) + 40 pixmap = QPixmap(w, h) painter = QPainter(pixmap) painter.fillRect(0,0,w,h, QColor("#f0f0f0")) # get a default option object. It is neither a QStyleOptionViewItem # nor a QStyleOptionGraphicsItem and we don't use it, but we need something. option = QStyleOptionViewItem() # paint the annotation, texture graphs, and similarity lines painter.translate(20,12) scene.tv_g_annotation.paint(painter, option, None) painter.translate(0,scene.tv_g_annotation.boundingRect().height()+35) scene.tv_g_texture1.paint(painter, option, None) painter.translate(0,100) scene.tv_g_lines.paint(painter, option, None) painter.translate(0,200) scene.tv_g_texture2.paint(painter, option, None) painter.translate(0,140) scene.tv_g_histogram.paint(painter, option, None) painter.end() # export try: pixmap.save(graph_filename) except Exception as e: status = "Error exporting TV graph image file '%s': %s" % ( graph_filename, str(e))
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): ##option.rect.setWidth(option.rect.width() - 18) # QStyledItemDelegate.paint(self, painter, option, index) ##option.rect.setWidth(option.rect.width() + 18) itemIndex = QModelIndex() character = self.mdlCharacter.getCharacterByID(index.data()) if character: itemIndex = character.index(Character.name) opt = QStyleOptionViewItem(option) self.initStyleOption(opt, itemIndex) qApp.style().drawControl(QStyle.CE_ItemViewItem, opt, painter) # if index.isValid() and index.internalPointer().data(Outline.POV) not in ["", None]: if itemIndex.isValid() and self.mdlCharacter.data(itemIndex) not in [ "", None ]: opt = QStyleOptionComboBox() opt.rect = option.rect r = qApp.style().subControlRect(QStyle.CC_ComboBox, opt, QStyle.SC_ComboBoxArrow) option.rect = r qApp.style().drawPrimitive(QStyle.PE_IndicatorArrowDown, option, painter)
def paint(self, painter, option, index): """Paints the amount within the bounding box provided in the option parameter. Draws the currency left aligned and the value of the model amount right aligned. Some spacing between with left and right padding is also utilized. Args: painter - QPainter option - QStyleOptionViewItem index - QModelIndex in the model """ column_data = self._getDataFromIndex(index) if column_data is None: return option = QStyleOptionViewItem(option) painter.setFont(option.font) cur_width, val_width = self._getAmountTextWidths(column_data, option) font_height = option.fontMetrics.height() do_paint_currency = cur_width > 0 is_selected = bool(option.state & QStyle.State_Selected) is_active = bool(option.state & QStyle.State_Active) palette_active = QPalette.Active if is_active else QPalette.Inactive palette_text = QPalette.HighlightedText if is_selected else QPalette.Text pen_color = option.palette.color(palette_active, palette_text) painter.setPen(pen_color) if do_paint_currency: painter.drawText( QRectF(4 + option.rect.left(), option.rect.top(), cur_width, font_height), column_data.currency, QTextOption(Qt.AlignVCenter)) painter.drawText( QRectF(option.rect.right() - val_width - 5, option.rect.top(), val_width, font_height), column_data.value, QTextOption(Qt.AlignVCenter))
def sizeHint(self, option, index): option = QStyleOptionViewItem(option) self.initStyleOption(option, index) size = QStyledItemDelegate.sizeHint(self, option, index) # TODO: get the default QMenu item height from the current style. size.setHeight(max(size.height(), 25)) return size
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())
def paint(self, painter: QPainter, option: QStyleOptionViewItem, model_index: QModelIndex): column = model_index.column() new_rect = QRect(option.rect) if column == NAME_COL: # Part Name option.displayAlignment = Qt.AlignVCenter QStyledItemDelegate.paint(self, painter, option, model_index) if column == LOCKED_COL: # Visibility element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionButton() styleoption.rect = new_rect checked = model_index.model().data(model_index, Qt.EditRole) styleoption.state |= QStyle.State_On if checked else QStyle.State_Off # make the check box look a little more active by changing the pallete styleoption.palette.setBrush(QPalette.Button, Qt.white) styleoption.palette.setBrush(QPalette.HighlightedText, Qt.black) _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) if checked: # element = _QCOMMONSTYLE.PE_IndicatorMenuCheckMark # _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) # _QCOMMONSTYLE.drawItemText(painter, new_rect, Qt.AlignCenter, styleoption.palette, True, 'L') icon = QPixmap(":/outlinericons/lock") _QCOMMONSTYLE.drawItemPixmap(painter, new_rect, Qt.AlignCenter, icon) if column == VISIBLE_COL: # Visibility element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionButton() styleoption.rect = new_rect checked = model_index.model().data(model_index, Qt.EditRole) styleoption.state |= QStyle.State_On if checked else QStyle.State_Off # make the check box look a little more active by changing the pallete styleoption.palette.setBrush(QPalette.Button, Qt.white) styleoption.palette.setBrush(QPalette.HighlightedText, Qt.black) _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) if checked: # element = _QCOMMONSTYLE.PE_IndicatorMenuCheckMark # _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) icon = QPixmap(":/outlinericons/eye") _QCOMMONSTYLE.drawItemPixmap(painter, new_rect, Qt.AlignCenter, icon) elif column == COLOR_COL: # Color # Alternate way to get color # outline_tw = self.parent() # item = outline_tw.itemFromIndex(model_index) # color = item.getColor() # print("COLOR_COL", item) color = model_index.model().data(model_index, Qt.EditRole) element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionViewItem() brush = getBrushObj(color) styleoption.palette.setBrush(QPalette.Button, brush) styleoption.rect = new_rect _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) else: QStyledItemDelegate.paint(self, painter, option, model_index)
def paint(self, painter, option, index): view_option = QStyleOptionViewItem(option) view_option.decorationAlignment |= Qt.AlignHCenter # Even though we told Qt that we don't want any selection on our # list, it still adds a blue rectangle on focused items. # So we just get rid of focus. if option.state & QStyle.State_HasFocus: view_option.state &= ~QStyle.State_HasFocus super().paint(painter, view_option, index)
def paint(self, painter, option, index): view_option = QStyleOptionViewItem(option) view_option.decorationAlignment |= Qt.AlignHCenter # Qt tries to be nice and adds a lovely background color # on the focused item. Since we select items by rows and not # individually, we don't want that, so we remove the focus if option.state & QStyle.State_HasFocus: view_option.state &= ~QStyle.State_HasFocus super().paint(painter, view_option, index)
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QtGui.QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QtCore.QSize(doc.idealWidth() + 5, option.fontMetrics.height())
def paint(self, painter, option, index): hasValue = True if (self.m_editorPrivate): property = self.m_editorPrivate.indexToProperty(index) if (property): hasValue = property.hasValue() opt = QStyleOptionViewItem(option) if ((self.m_editorPrivate and index.column() == 0) or not hasValue): property = self.m_editorPrivate.indexToProperty(index) if (property and property.isModified()): opt.font.setBold(True) opt.fontMetrics = QFontMetrics(opt.font) c = QColor() if (not hasValue and self.m_editorPrivate.markPropertiesWithoutValue()): c = opt.palette.color(QPalette.Dark) opt.palette.setColor(QPalette.Text, opt.palette.color(QPalette.BrightText)) else: c = self.m_editorPrivate.calculatedBackgroundColor( self.m_editorPrivate.indexToBrowserItem(index)) if (c.isValid() and (opt.features & QStyleOptionViewItem.Alternate)): c = c.lighter(112) if (c.isValid()): painter.fillRect(option.rect, c) opt.state &= ~QStyle.State_HasFocus if (index.column() == 1): item = self.m_editorPrivate.indexToItem(index) if (self.m_editedItem and (self.m_editedItem == item)): self.m_disablePainting = True super(QtPropertyEditorDelegate, self).paint(painter, opt, index) if (option.type): self.m_disablePainting = False opt.palette.setCurrentColorGroup(QPalette.Active) color = QApplication.style().styleHint(QStyle.SH_Table_GridLineColor, opt) painter.save() painter.setPen(QPen(color)) if (not self.m_editorPrivate or (not self.m_editorPrivate.lastColumn(index.column()) and hasValue)): if option.direction == Qt.LeftToRight: right = option.rect.right() else: right = option.rect.left() painter.drawLine(right, option.rect.y(), right, option.rect.bottom()) painter.restore()
def paint(self, painter: QtGui.QPainter, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> None: viewOption = QStyleOptionViewItem(option) self.initStyleOption(viewOption, index) QStyledItemDelegate.paint(self, painter, viewOption, index) item = index.model().data(index, Qt.UserRole) if isinstance(item, QVariant): return value = item['value'] type = item['type'] if type == 'checkbox': # 数据转换 value = True if value == 1 else 0 # 绘制单选框 checkBoxStyle = QStyleOptionButton() checkBoxStyle.state = QStyle.State_On if value else QStyle.State_Off checkBoxStyle.state |= QStyle.State_Enabled # 计算位置 size = item['size'] rect = calculate_middle_rect(option.rect, size, size) checkBoxStyle.rect = rect checkBox = QCheckBox() QApplication.style().drawPrimitive(QStyle.PE_IndicatorCheckBox, checkBoxStyle, painter, checkBox) if type == 'tag': painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHints(QPainter.SmoothPixmapTransform) path = QPainterPath() # 绘制文本 self.font = ResourceLoader().qt_font_text_tag text_color = item['text_color'] border_color = item['border_color'] text = value padding_v = 2 padding_h = 4 border_radius = 2 border_width = 1 painter.setFont(item['font']) painter.setPen(text_color) fm = QFontMetrics(painter.font()) w = fm.width(text) h = fm.height() # 计算位置 rect = calculate_middle_rect(option.rect, w + padding_h * 2, h + padding_v * 2) rectf = QRectF(rect.x(), rect.y(), rect.width(), rect.height()) painter.drawText( QRect(rectf.x() + padding_h, rectf.y() + padding_v, w, h), Qt.TextWordWrap, text) # 绘制边框 path.addRoundedRect(rectf, border_radius, border_radius) painter.strokePath(path, QPen(border_color, border_width))
def paint(self, painter, option, index): if index.column() == 2: value = index.model().data(index, Qt.UserRole) if not self.isSupportedType(value): myOption = QStyleOptionViewItem(option) myOption.state &= ~QStyle.State_Enabled super(VariantDelegate, self).paint(painter, myOption, index) return super(VariantDelegate, self).paint(painter, option, index)
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, model_index): """Summary Args: painter (TYPE): Description option (TYPE): Description model_index (TYPE): Description Returns: TYPE: Description """ # row = model_index.row() column = model_index.column() if column == 0: # Part Name option.displayAlignment = Qt.AlignVCenter QStyledItemDelegate.paint(self, painter, option, model_index) if column == 1: # Visibility value = model_index.model().data(model_index, Qt.EditRole) data_type = type(value) if data_type is str: # print("val", value) if COLOR_PATTERN.search(value): # print("found color") element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionViewItem() styleoption.palette.setBrush(QPalette.Button, getBrushObj(value)) styleoption.rect = QRect(option.rect) _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) option.displayAlignment = Qt.AlignVCenter QStyledItemDelegate.paint(self, painter, option, model_index) elif data_type is int: option.displayAlignment = Qt.AlignVCenter QStyledItemDelegate.paint(self, painter, option, model_index) elif data_type is float: option.displayAlignment = Qt.AlignVCenter QStyledItemDelegate.paint(self, painter, option, model_index) elif data_type is bool: element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionButton() styleoption.rect = QRect(option.rect) checked = value styleoption.state |= QStyle.State_On if checked else QStyle.State_Off styleoption.palette.setBrush(QPalette.Button, Qt.white) styleoption.palette.setBrush(QPalette.HighlightedText, Qt.black) _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) if checked: element = _QCOMMONSTYLE.PE_IndicatorMenuCheckMark _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) else: QStyledItemDelegate.paint(self, painter, option, model_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( int(doc.idealWidth()), int(QStyledItemDelegate.sizeHint(self, option, index).height()))
def sizeHint(self, option, index): d = index.internalPointer().data(index.column(), Qt.DisplayRole) if not d: d = 0 item = self.mdlLabels.item(int(d), 0) idx = self.mdlLabels.indexFromItem(item) opt = QStyleOptionViewItem(option) self.initStyleOption(opt, idx) s = qApp.style().sizeFromContents(QStyle.CT_ItemViewItem, opt, QSize()) if s.width() > 150: s.setWidth(150) elif s.width() < 50: s.setWidth(50) return s + QSize(18, 0)
def on_paint_request(self, printer: QPrinter): """Paints to printer :param printer: Target printer """ painter = QPainter(printer) option = QStyleOptionViewItem() painter.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.SmoothPixmapTransform) page_rect = printer.pageRect() rows = list(self.workflows.get_paint_rows(self.print_area)) columns = list(self.workflows.get_paint_columns(self.print_area)) if not rows or not columns: return zeroidx = self.grid.model.index(0, 0) zeroidx_rect = self.grid.visualRect(zeroidx) minidx = self.grid.model.index(min(rows), min(columns)) minidx_rect = self.grid.visualRect(minidx) maxidx = self.grid.model.index(max(rows), max(columns)) maxidx_rect = self.grid.visualRect(maxidx) grid_width = maxidx_rect.x() + maxidx_rect.width() - minidx_rect.x() grid_height = maxidx_rect.y() + maxidx_rect.height() - minidx_rect.y() grid_rect = QRectF(minidx_rect.x() - zeroidx_rect.x(), minidx_rect.y() - zeroidx_rect.y(), grid_width, grid_height) self.settings.print_zoom = min(page_rect.width() / grid_width, page_rect.height() / grid_height) with painter_save(painter): painter.scale(self.settings.print_zoom, self.settings.print_zoom) # Translate so that the grid starts at upper left paper edge painter.translate(zeroidx_rect.x() - minidx_rect.x(), zeroidx_rect.y() - minidx_rect.y()) # Draw grid cells self.workflows.paint(painter, option, grid_rect, rows, columns) self.settings.print_zoom = None
def paintCheckBoxColumn(self, painter, option, index): opt = QStyleOptionViewItem(option) buttonStyle = QStyleOptionButton() buttonStyle.state = QStyle.State_On if index.model().data( index, Qt.CheckStateRole) == QVariant( Qt.Checked) else QStyle.State_Off if option.state & QStyle.State_MouseOver or option.state & QStyle.State_HasFocus: buttonStyle.state |= QStyle.State_HasFocus buttonStyle.rect = opt.rect.adjusted(4, -opt.rect.height() + ROW_HEIGHT, 0, 0) PackageDelegate.AppStyle().drawControl(QStyle.CE_CheckBox, buttonStyle, painter, None)
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 export_browser_graph(graph_filename, scene): # define the bounding rectangle for pixmap for painter to paint in w = int(scene.width()) + 20 if w < 840: # adjust for when zoomed small w = 840 h = int(scene.height()) + 30 pixmap = QPixmap(w, h) painter = QPainter(pixmap) painter.fillRect(0, 0, w, h, QColor("#f0f0f0")) # get a default option object. It is neither a QStyleOptionViewItem # nor a QStyleOptionGraphicsItem and we don't use it, but we need something. option = QStyleOptionViewItem() # paint the annotation painter.translate(20, 10) scene.g_annotation.paint(painter, option, None) # paint the graph painter.translate(65, 0) # SD nodes, edges, and axis for edge in scene.g_sd_edges: edge.paint(painter, option, None) for node in scene.g_sd_nodes: painter.save() painter.translate(node.pos()) node.paint(painter, option, None) painter.restore() painter.save() painter.translate(scene.g_sd_axis.pos()) scene.g_sd_axis.paint(painter, option, None) painter.restore() painter.end() # export try: pixmap.save(graph_filename) except Exception as e: status = "Error exporting browser graph image file '%s': %s" % ( graph_filename, str(e))
def paint(self, painter, option, index): self.initStyleOption(option, index) # No idea why, but this cast is required if we want to have access to the V4 valuess option = QStyleOptionViewItem(option) if (index.column() == 1) and (option.state & QStyle.State_Selected): cboption = QStyleOptionComboBox() cboption.rect = option.rect # On OS X (with Qt4.6.0), adding State_Enabled to the flags causes the whole drawing to # fail (draw nothing), but it's an OS X only glitch. On Windows, it works alright. cboption.state |= QStyle.State_Enabled QApplication.style().drawComplexControl(QStyle.CC_ComboBox, cboption, painter) painter.setBrush(option.palette.text()) rect = QRect(option.rect) rect.setLeft(rect.left()+4) painter.drawText(rect, Qt.AlignLeft, option.text) else: super().paint(painter, option, index)