def paint(self, p, option, index): col = index.column() if col == DevMdl.LWT: if option.state & QStyle.State_Selected: p.fillRect(option.rect, option.palette.highlight()) px = self.icons.get(index.data().lower()) x = option.rect.center().x() + 1 - px.rect().width() / 2 y = option.rect.center().y() - px.rect().height() / 2 p.drawPixmap(QRect(x, y, px.rect().width(), px.rect().height()), px) elif col == DevMdl.POWER: if option.state & QStyle.State_Selected: p.fillRect(option.rect, option.palette.highlight()) if isinstance(index.data(), dict): for i, s in enumerate(index.data().keys()): px = self.icons.get(index.data()[s].lower()) if px: x = option.rect.center().x() + 1 - len( index.data()) * 14 / 2 + i * 14 y = option.rect.center().y() - px.rect().height() / 2 p.drawPixmap( QRect(x, y, px.rect().width(), px.rect().height()), px) else: QStyledItemDelegate.paint(self, p, option, index)
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 paint(self, painter, option, index): if option.state & QStyle.State_Selected: option.palette.setColor(QPalette.HighlightedText, Qt.black) color = self.combineColors(self.color_default, self.background(option, index)) option.palette.setColor(QPalette.Highlight, color) QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): opt = option self.initStyleOption(opt, index) if opt.state and QStyle.State_MouseOver: self.d_ptr.view.SetMouseOver(index.row()) opt.state &= ~QStyle.State_MouseOver QStyledItemDelegate.paint(self, painter, opt, index)
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): QStyledItemDelegate.paint(self, painter, option, index) if index.isValid() and index.internalPointer().data(Outline.status.value) 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: 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): QStyledItemDelegate.paint(self, painter, option, index) if index.isValid() and index.internalPointer().data( Outline.status) 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): if not index.parent().isValid(): QStyledItemDelegate.paint(self, painter, option, index) else: widget = option.widget style = widget.style() if widget else QApplication.style() opt = QStyleOptionButton() opt.rect = option.rect opt.text = index.data() opt.state |= QStyle.State_On if index.data( Qt.CheckStateRole) else QStyle.State_Off style.drawControl(QStyle.CE_RadioButton, opt, painter, widget)
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 paint(self, painter, option, index): if index.row() == 0 or index.column() == 0: textMargin = QApplication.style().pixelMetric( QStyle.PM_FocusFrameHMargin) + 1 newRect = QStyle.alignedRect( option.direction, Qt.AlignCenter, QSize(option.decorationSize.width() + 5, option.decorationSize.height()), QRect(option.rect.x() + textMargin, option.rect.y(), option.rect.width() - (2 * textMargin), option.rect.height())) option.rect = newRect QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): painter.save() try: if index.column()==2: item = index.data(Qt.DisplayRole) painter.drawText(option.rect, Qt.AlignCenter | Qt.AlignVCenter, datetime.datetime.fromtimestamp(item).strftime(self.time_format) ) else: QStyledItemDelegate.paint(self, painter, option, index) pass except Exception as e: print('UnixDateDelegate error: ', e) dbg_except() 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, option, index): if option.state & QtWidgets.QStyle.State_Selected: # font color, foreground color # fgcolor = self.getColor(option, index, "FG") option.palette.setColor(QtGui.QPalette.HighlightedText, QtCore.Qt.black) # background color bgcolor = self.combineColors(self.getColor(option, index, "BG"), self.color_default) option.palette.setColor(QtGui.QPalette.Highlight, bgcolor) # change color for listView painter.fillRect(option.rect, bgcolor) # change color for tableView QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter: QPainter, option: QStyleOptionViewItem, model_index: QModelIndex): """ Args: painter: Description option: Description model_index: 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 paint(self, painter, option, index): extra = index.data(Qt.UserRole + 1) if not extra: return QStyledItemDelegate.paint(self, painter, option, index) else: if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.color(QPalette.Highlight)) title = index.data() painter.drawText(option.rect.adjusted(2, 1, 0, 0), Qt.AlignLeft, title) fm = QFontMetrics(option.font) w = fm.width(title) r = QRect(option.rect) r.setLeft(r.left() + w) painter.save() if option.state & QStyle.State_Selected: painter.setPen(Qt.white) else: painter.setPen(Qt.gray) painter.drawText(r.adjusted(2, 1, 0, 0), Qt.AlignLeft, " - {}".format(extra)) painter.restore()
def paint(self, painter, option, index): extra = index.data(Qt.UserRole + 1) if not extra: return QStyledItemDelegate.paint(self, painter, option, index) else: if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.color(QPalette.Inactive, QPalette.Highlight)) title = index.data() extra = " - {}".format(extra) painter.drawText(option.rect, Qt.AlignLeft, title) fm = QFontMetrics(option.font) w = fm.width(title) r = QRect(option.rect) r.setLeft(r.left() + w) painter.save() if option.state & QStyle.State_Selected: painter.setPen(QColor(S.highlightedTextLight)) else: painter.setPen(QColor(S.textLight)) painter.drawText(r, Qt.AlignLeft, extra) 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): extra = index.data(Qt.UserRole + 1) if not extra: return QStyledItemDelegate.paint(self, painter, option, index) else: if option.state & QStyle.State_Selected: painter.fillRect( option.rect, option.palette.color(QPalette.Inactive, QPalette.Highlight)) title = index.data() extra = " - {}".format(extra) painter.drawText(option.rect, Qt.AlignLeft, title) fm = QFontMetrics(option.font) w = fm.width(title) r = QRect(option.rect) r.setLeft(r.left() + w) painter.save() if option.state & QStyle.State_Selected: painter.setPen(QColor(S.highlightedTextLight)) else: painter.setPen(QColor(S.textLight)) painter.drawText(r, Qt.AlignLeft, extra) painter.restore()
def paint(self, painter, option, index): if index.column() != 1: QStyledItemDelegate.paint(self, painter, option, index) return model: Any[FileSystemTreeModel | QAbstractItemModel] = index.model() node = model.getNode(index) if node.is_fetch_more: QStyledItemDelegate.paint(self, painter, option, index) return rect = option.rect # btn = QStyleOptionToolButton() # btn.rect = QRect(rect.left() + rect.width() - 30, # rect.top(), 30, rect.height()) # btn.text = '...' # btn.toolButtonStyle = Qt.ToolButtonIconOnly # btn.icon = COG_ICON # btn.iconSize = QSize(16, 16) # btn.features = QStyleOptionToolButton.Menu | \ # QStyleOptionToolButton.MenuButtonPopup | \ # QStyleOptionToolButton.HasMenu # btn.features = QStyleOptionToolButton.Menu # btn.state = QStyle.State_Enabled | \ # (QStyle.State_MouseOver # if option.state & QStyle.State_MouseOver # else QStyle.State_None) # btn.state = QStyle.State_Enabled # QApplication.style().drawComplexControl( # QStyle.CC_ToolButton, btn, painter) btn = QStyleOptionButton() btn.icon = COG_ICON btn.iconSize = QSize(16, 16) btn.features = QStyleOptionButton.Flat btn.features |= QStyleOptionButton.HasMenu btn.state = QStyle.State_Enabled btn.state |= (QStyle.State_MouseOver if option.state & QStyle.State_MouseOver else QStyle.State_Enabled) btn.state |= (QStyle.State_Selected if option.state & QStyle.State_Selected else QStyle.State_Enabled) btn.state |= (QStyle.State_Active if option.state & QStyle.State_Active else QStyle.State_Enabled) btn.rect = QRect(rect.left() + rect.width() - 30, rect.top(), 30, rect.height()) QApplication.style().drawControl(QStyle.CE_PushButton, btn, painter)
def paint(self, painter, option, index): """Renders the delegate for the item specified by index. This method handles specially the result returned by the model data() method for the Qt.BackgroundRole role. Typically, if the cell being rendered is selected then the data() returned value is ignored and the value set by the desktop (KDE, Gnome...) is used. We need to change that behavior as explained in the module docstring. The following properties of the style option are used for customising the painting: state (which holds the state flags), rect (which holds the area that should be used for painting) and palette (which holds the palette that should be used when painting) :Parameters: - `painter`: the painter used for rendering - `option`: the style option used for rendering - `index`: the index of the rendered item """ # option.state is an ORed combination of flags if (option.state & QStyle.State_Selected): model = index.model() buffer_start = model.start cell = index.model().selected_cell if ((index == cell['index']) and \ (buffer_start != cell['buffer_start'])): painter.save() self.initStyleOption(option, index) background = option.palette.color(QPalette.Base) foreground = option.palette.color(QPalette.Text) painter.setBrush(QBrush(background)) painter.fillRect(option.rect, painter.brush()) painter.translate(option.rect.x() + 3, option.rect.y()) painter.setBrush(QBrush(foreground)) try: painter.drawText(option.rect, Qt.AlignLeft | Qt.AlignTop, model.data(index)) except Exception as e: print(model.data(index)) print(e) painter.restore() else: QStyledItemDelegate.paint(self, painter, option, index) else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): if index.column() == 2: button = QStyleOptionButton() r = option.rect # getting the rect of the cell x = r.left() y = r.top() w = r.width() h = r.height() button.rect = QRect(x, y, w, h) button.text = "X" button.state = QStyle.State_Enabled; QApplication.style().drawControl(QStyle.CE_PushButton, button, painter) else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): def draw_my(option, painter, brush, text, icon): if brush is None: brush = QColor( 255, 255, 255, 0 ) # This is original background color. I just set alpha to 0 which means it is transparent x, y = (option.rect.x(), option.rect.y()) h = option.rect.height() painter.save() painter.setFont(option.font) if icon: icon = icon.pixmap(h, h) painter.drawPixmap(QRect(x, y, h, h), icon) painter.drawText(option.rect.adjusted(h, 0, 0, 0), Qt.AlignLeft, text) else: painter.drawText(option.rect, Qt.AlignLeft, text) painter.setCompositionMode(QPainter.CompositionMode_SourceAtop) painter.setPen(QPen(Qt.NoPen)) painter.fillRect(option.rect, brush) painter.setBackgroundMode(Qt.OpaqueMode) painter.setBackground(brush) painter.drawRect(option.rect) painter.restore() # Also should be activated in StyleSheet # Selected if (option.state & QStyle.State_Selected): option.font.setWeight(QFont.Bold) brush = index.data(Qt.BackgroundRole) text = index.data(Qt.DisplayRole) icon = index.data(Qt.DecorationRole) draw_my(option=option, painter=painter, brush=brush, text=text, icon=icon) else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): if index.column() == 2: button = QStyleOptionButton() r = option.rect # getting the rect of the cell x = r.left() y = r.top() w = r.width() h = r.height() button.rect = QRect(x, y, w, h) button.text = "X" button.state = QStyle.State_Enabled QApplication.style().drawControl(QStyle.CE_PushButton, button, painter) else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): if index.column() == DESCRIPTION: text = index.model().data(index) palette = QApplication.palette() document = QTextDocument() document.setDefaultFont(option.font) if option.state & QStyle.State_Selected: document.setHtml("<font color={}>{}</font>".format( palette.highlightedText().color().name(), text)) else: document.setHtml(text) color = (palette.highlight().color() if option.state & QStyle.State_Selected else QColor(index.model().data( index, Qt.BackgroundColorRole))) painter.save() painter.fillRect(option.rect, color) painter.translate(option.rect.x(), option.rect.y()) document.drawContents(painter) painter.restore() else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, idx): painter.save() # set background color painter.setPen(QPen(Qt.NoPen)) if idx.column() == 0: ccode = 245 painter.setBrush(QBrush(QColor(ccode, ccode, ccode))) else: painter.setBrush(QBrush(Qt.transparent)) if not idx.parent().isValid(): painter.setBrush(QBrush(QColor(0xFF, 0xDC, 0xA4) ) ) painter.drawRect(option.rect) # draw the rest QStyledItemDelegate.paint(self, painter, option, idx) painter.restore()
def paint(self, painter, option, idx): painter.save() # set background color painter.setPen(QPen(Qt.NoPen)) if idx.column() == 0: ccode = 245 painter.setBrush(QBrush(QColor(ccode, ccode, ccode))) else: painter.setBrush(QBrush(Qt.transparent)) if not idx.parent().isValid(): painter.setBrush(QBrush(QColor(0xFF, 0xDC, 0xA4))) painter.drawRect(option.rect) # draw the rest QStyledItemDelegate.paint(self, painter, option, idx) painter.restore()
def paint(self, painter, option, index): value = index.data() if not isinstance(value, float): QStyledItemDelegate.paint(self, painter, option, index) return painter.save() if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.highlight()) else: painter.fillRect(option.rect, option.palette.base()) percent = value if percent is None: percent = 0 if percent > 100: percent = 100 w = option.rect.width() * percent / 100 color = percent_color(percent / 100) painter.fillRect(option.rect.x(), option.rect.y(), w, option.rect.height(), color) painter.drawText(option.rect, 0, str(value) + " %") painter.restore()
def paint(self, painter, option, index): if not index.isValid(): return QStyledItemDelegate.paint(self, painter, option, index) QStyledItemDelegate.paint(self, painter, option, index) item = index.internalPointer() if not item.data(Outline.goal.value): return p = toFloat(item.data(Outline.goalPercentage.value)) typ = item.data(Outline.type.value) level = item.level() if self.rootIndex and self.rootIndex.isValid(): level -= self.rootIndex.internalPointer().level() + 1 margin = 5 height = max(min(option.rect.height() - 2 * margin, 12) - 2 * level, 6) painter.save() rect = option.rect.adjusted(margin, margin, -margin, -margin) # Move rect.translate(level * rect.width() / 10, 0) rect.setWidth(rect.width() - level * rect.width() / 10) rect.setHeight(height) rect.setTop(option.rect.top() + (option.rect.height() - height) / 2) drawProgress(painter, rect, p) # from functions painter.restore()
def paint(self, painter, option, index): if not index.isValid(): return QStyledItemDelegate.paint(self, painter, option, index) QStyledItemDelegate.paint(self, painter, option, index) item = index.internalPointer() if not item.data(Outline.goal): return p = toFloat(item.data(Outline.goalPercentage)) typ = item.data(Outline.type) level = item.level() if self.rootIndex and self.rootIndex.isValid(): level -= self.rootIndex.internalPointer().level() + 1 margin = 5 height = max(min(option.rect.height() - 2 * margin, 12) - 2 * level, 6) painter.save() rect = option.rect.adjusted(margin, margin, -margin, -margin) # Move rect.translate(level * rect.width() / 10, 0) rect.setWidth(rect.width() - level * rect.width() / 10) rect.setHeight(height) rect.setTop(option.rect.top() + (option.rect.height() - height) / 2) drawProgress(painter, rect, p) # from functions 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.value) 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, idx): new_idx = idx.sibling(idx.row(), 0) item = self.model.itemFromIndex(new_idx) if item and item.data(Qt.UserRole) in self.added_node_list: option.font.setWeight(QFont.Bold) QStyledItemDelegate.paint(self, painter, option, idx)
def paint(self, p, option, index): if option.state & QStyle.State_Selected: p.fillRect(option.rect, option.palette.highlight()) col = index.column() col_name = index.model().sourceModel().columns[col] if col_name == "FriendlyName": if index.data(): px = QPixmap(":/status_offline.png") if index.data(LWTRole) == "Online": rssi = index.data(RSSIRole) if rssi > 0 and rssi < 50: px = QPixmap(":/status_low.png") elif rssi < 75: px = QPixmap(":/status_medium.png") elif rssi >= 75: px = QPixmap(":/status_high.png") px_y = (option.rect.height() - 24) / 2 p.drawPixmap(option.rect.x() + 2, option.rect.y() + px_y, px.scaled(24, 24)) p.drawText(option.rect.adjusted(28, 0, 0, 0), Qt.AlignVCenter | Qt.AlignLeft, index.data()) alerts = [] if index.data(RestartReasonRole) == "Exception": alerts.append("Exception") if "minimal" in index.data(FirmwareRole).lower(): alerts.append("Minimal") if alerts: message = ", ".join(alerts) p.save() pen = QPen(p.pen()) pen.setColor(QColor("red")) p.setPen(pen) text_width = p.boundingRect(option.rect, Qt.AlignCenter, message).width() exc_rect = option.rect.adjusted( option.rect.width() - text_width - 8, 4, -4, -4) p.drawText(exc_rect, Qt.AlignCenter, message) p.drawRect(exc_rect) p.restore() elif col_name == "RSSI": if index.data(): rect = option.rect.adjusted(4, 4, -4, -4) rssi = index.data() pen = QPen(p.pen()) p.save() if rssi > 0 and rssi < 50: color = QColor("#ef4522") elif rssi > 75: color = QColor("#7eca27") elif rssi > 0: color = QColor("#fcdd0f") p.fillRect(rect.adjusted(2, 2, -1, -1), color) p.drawText(rect, Qt.AlignCenter, str(rssi)) pen.setColor(QColor("#cccccc")) p.setPen(pen) p.drawRect(rect) p.restore() elif col_name == "Power": if isinstance(index.data(), dict): num = len(index.data().keys()) if num <= 4: for i, k in enumerate(sorted(index.data().keys())): x = option.rect.x() + i * 24 + (option.rect.width() - num * 24) / 2 y = option.rect.y() + (option.rect.height() - 24) / 2 if num == 1: p.drawPixmap( x, y, 24, 24, QPixmap(":/P_{}".format(index.data()[k]))) else: p.drawPixmap( x, y, 24, 24, QPixmap(":/P{}_{}".format( i + 1, index.data()[k]))) else: i = 0 for row in range(2): for col in range(4): x = col * 24 + option.rect.x() y = option.rect.y() + row * 24 if i < num: p.drawPixmap( x, y, 24, 24, QPixmap(":/P{}_{}".format( i + 1, list(index.data().values())[i]))) i += 1 elif col_name == "Color": if index.data(): rect = option.rect.adjusted(4, 4, -4, -4) d = index.data() pen = QPen(p.pen()) color = d.get("Color") so = d.get(68) if color and not so: p.save() if len(color) > 6: color = color[:6] p.fillRect(rect.adjusted(2, 2, -1, -1), QColor("#{}".format(color))) dimmer = d.get("Dimmer") if dimmer: if dimmer >= 60: pen.setColor(QColor("black")) else: pen.setColor(QColor("white")) p.setPen(pen) p.drawText(rect, Qt.AlignCenter, "{}%".format(dimmer)) pen.setColor(QColor("#cccccc")) p.setPen(pen) p.drawRect(rect) p.restore() else: QStyledItemDelegate.paint(self, p, option, index)
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): myoption = QStyleOptionViewItem(option) myoption.displayAlignment |= (Qt.AlignRight|Qt.AlignVCenter) QStyledItemDelegate.paint(self, painter, myoption, index)
def paint(self, painter, option, index): delegate = self.delegates.get(index.column()) if delegate is not None: delegate.paint(painter, option, index) else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter: QPainter, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex): QStyledItemDelegate.paint(self, painter, option, index)