def paint(self, painter, option, index): """ Paint the items in the table. If the item referred to by <index> is a StarRating, we handle the painting ourselves. For the other items, we let the base class handle the painting as usual. In a polished application, we'd use a better check than the column number to find out if we needed to paint the stars, but it works for the purposes of this example. """ if index.column() == 3: starRating = StarRating(index.data()) # If the row is currently selected, we need to make sure we # paint the background accordingly. if option.state & QStyle.State_Selected: # The original C++ example used option.palette.foreground() to # get the brush for painting, but there are a couple of # problems with that: # - foreground() is obsolete now, use windowText() instead # - more importantly, windowText() just returns a brush # containing a flat color, where sometimes the style # would have a nice subtle gradient or something. # Here we just use the brush of the painter object that's # passed in to us, which keeps the row highlighting nice # and consistent. painter.fillRect(option.rect, painter.brush()) # Now that we've painted the background, call starRating.paint() # to paint the stars. starRating.paint(painter, option.rect, option.palette) else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): if not self.draw_focus: option.state = option.state & (~QStyle.State_HasFocus) if not isinstance(index.data(), GraphRow): QStyledItemDelegate.paint(self, painter, option, index) return row = index.data() has_focus = option.state & QStyle.State_HasFocus option.state = option.state & (~QStyle.State_HasFocus) QStyledItemDelegate.paint(self, painter, option, index) lane_size = option.rect.height() painter.save() painter.setClipRect(option.rect) painter.translate(option.rect.x(), option.rect.y()) painter.scale(lane_size, lane_size) painter.setRenderHint(QPainter.Antialiasing, True) max_lane = row.commit_node.lane if row.prev_row: for edge in row.prev_row.edges: max_lane = max(max_lane, edge.from_lane, edge.to_lane) self.draw_edge(painter, option, edge, -1) for edge in row.edges: max_lane = max(max_lane, edge.from_lane, edge.to_lane) self.draw_edge(painter, option, edge, 0) pen = QPen(Qt.black, self.node_thickness) painter.setPen(pen) painter.setBrush(option.palette.window()) painter.drawEllipse( QPointF(row.commit_node.lane + 0.5, 0.5), self.node_radius, self.node_radius) if row.log_entry.refs: painter.resetTransform() painter.translate( option.rect.x() + (max_lane + 1) * lane_size, option.rect.y()) ref_x = 0 for ref in row.log_entry.refs: ref_x = self.draw_ref(painter, option, ref, row.repo, ref_x) painter.restore() if has_focus: painter.save() focus_option = QStyleOptionFocusRect() focus_option.rect = option.rect QApplication.style().drawPrimitive(QStyle.PE_FrameFocusRect, focus_option, painter) painter.restore()
def paint(self, painter, option, index): icon = index.data(ItemUserRole.IconRole) if (not icon) or icon.isNull(): return QStyledItemDelegate.paint(self, painter, option, index) opt = QStyleOptionViewItemV4(option) self.initStyleOption(opt, index) widget = opt.widget style = widget.style() if widget else QApplication.style() style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget) optState = opt.state mode = QIcon.Normal if (not (optState & QStyle.State_Enabled)): mode = QIcon.Disabled elif (optState & QStyle.State_Selected): mode = QIcon.Selected state = QIcon.On if (optState & QStyle.State_Open) else QIcon.Off decorSize = opt.decorationSize iconSize = icon.actualSize(decorSize, mode, state) iconHeight = iconSize.height() iconWidth = iconSize.width() iconRect = style.subElementRect(QStyle.SE_ItemViewItemDecoration, opt, widget) decorHeight = decorSize.height() decorWidth = decorSize.width() wadj = hadj = 0 if iconHeight < decorHeight: hadj = (decorHeight - iconHeight) * .5 if iconWidth < decorWidth: wadj = (decorWidth - iconWidth) * .5 if wadj or hadj: iconRect.adjust(wadj, hadj, -wadj, -hadj) icon.paint(painter, iconRect, opt.decorationAlignment, mode, state)
def paint(self, painter, option, index): icon = index.data(ItemUserRole.IconRole) if (not icon) or icon.isNull(): return QStyledItemDelegate.paint(self, painter, option, index) opt = QStyleOptionViewItemV4(option) self.initStyleOption(opt, index) widget = opt.widget style = widget.style() if widget else QApplication.style() style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget) optState = opt.state mode = QIcon.Normal if (not (optState & QStyle.State_Enabled)): mode = QIcon.Disabled elif (optState & QStyle.State_Selected): mode = QIcon.Selected; state = QIcon.On if (optState & QStyle.State_Open) else QIcon.Off decorSize = opt.decorationSize iconSize = icon.actualSize(decorSize, mode, state) iconHeight = iconSize.height() iconWidth = iconSize.width() iconRect = style.subElementRect(QStyle.SE_ItemViewItemDecoration, opt, widget) decorHeight = decorSize.height() decorWidth = decorSize.width() wadj = hadj = 0 if iconHeight < decorHeight: hadj = (decorHeight - iconHeight) * .5 if iconWidth < decorWidth: wadj = (decorWidth - iconWidth) * .5 if wadj or hadj: iconRect.adjust(wadj, hadj, -wadj, -hadj) icon.paint(painter, iconRect, opt.decorationAlignment, mode, state)
def paint(self, painter, option, index): # NODE_REFERENCE --------------------------------------------------- if index.column() == NODE_REFERENCE: # backup painter painter.save() # paint background palette = QApplication.palette() if option.state & QStyle.State_Selected: bg_color = palette.highlight().color() else: bg_color = Qt.transparent painter.fillRect(option.rect, bg_color) # paint value = float(index.model().data(index)) if value == 0.0: painter.setPen( Qt.black if option.state & QStyle.State_Selected else Qt.darkGray ) painter.setBrush(Qt.NoBrush) mid_size = 3.0 else: painter.setPen(Qt.darkCyan) painter.setBrush(Qt.cyan) mid_size = 4.0 h_center = round(option.rect.x() + (option.rect.width() * .5)) v_center = round(option.rect.y() + (option.rect.height() * .5)) losange = QPolygonF() losange.append(QPointF(h_center, v_center - mid_size)) losange.append(QPointF(h_center - mid_size, v_center)) losange.append(QPointF(h_center, v_center + mid_size)) losange.append(QPointF(h_center + mid_size, v_center)) painter.drawPolygon(losange) # restore painter painter.restore() # FILE_STATE ------------------------------------------------------- elif index.column() == FILE_STATE: # backup painter painter.save() # paint background palette = QApplication.palette() bg_color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else Qt.transparent painter.fillRect(option.rect, bg_color) # paint circle value = index.model().data(index) pen_color = [ Qt.darkRed, Qt.black if option.state & QStyle.State_Selected else Qt.gray, Qt.darkGreen][value + 1] brush_color = [Qt.red, Qt.NoBrush, Qt.green][value + 1] painter.setPen(pen_color) painter.setBrush(brush_color) h_center = round(option.rect.x() + (option.rect.width() * .5)) v_center = round(option.rect.y() + (option.rect.height() * .5)) center = QPointF(h_center, v_center) painter.drawEllipse(center, 3.0, 3.0) # restore painter painter.restore() # NODE_NAME -------------------------------------------------------- elif index.column() == NODE_NAME: text = index.model().data(index, Qt.DisplayRole) palette = QApplication.palette() bg_color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else Qt.transparent txt_color = palette.highlightedText().color() \ if option.state & QStyle.State_Selected \ else palette.text().color() if MTTSettings.value('vizWrongNameState') \ and not MTTSettings.value('showWrongNameState'): file_name = os.path.splitext(os.path.basename( index.model().data( index.sibling(index.row(), NODE_FILE), Qt.DisplayRole ) ))[0] if not re.split('[0-9]*$', text.rsplit(':')[-1])[0] == \ re.split('[0-9]*$', file_name)[0]: bg_color = QBrush( Qt.red if option.state & QStyle.State_Selected else Qt.darkRed, Qt.Dense4Pattern ) if not MTTSettings.value('showNamespaceState'): splits = text.split(':') text = splits[len(splits) > 1] painter.save() painter.fillRect(option.rect, bg_color) painter.setPen(txt_color) rect = option.rect rect.setX(4) QApplication.style().drawItemText( painter, rect, Qt.AlignLeft | Qt.AlignVCenter, palette, True, text ) painter.restore() # NODE_FILE ------------------------------------------------------------ elif index.column() == NODE_FILE: palette = QApplication.palette() bg_color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else Qt.transparent txt_color = palette.highlightedText().color() \ if option.state & QStyle.State_Selected \ else palette.text().color() text = index.model().data(index, Qt.DisplayRole) if MTTSettings.value('vizExternalState'): if not text.startswith(self.ws_path): bg_color = QBrush( '#ef7900' if option.state & QStyle.State_Selected else '#b05100', Qt.Dense4Pattern) if MTTSettings.value('vizWrongPathState'): if not re.match(MTTSettings.PATH_PATTERN, text): bg_color = QBrush( Qt.red if option.state & QStyle.State_Selected else Qt.darkRed, Qt.Dense4Pattern) if MTTSettings.value('showBasenameState'): text = os.path.basename(text) elif not MTTSettings.value('showRealAttributeValue'): if not text.startswith('\\'): text = os.path.normpath(cmds.workspace(projectPath=text)) painter.save() painter.fillRect(option.rect, bg_color) painter.setPen(txt_color) QApplication.style().drawItemText( painter, option.rect, Qt.AlignLeft | Qt.AlignVCenter, palette, True, text) painter.restore() else: QStyledItemDelegate.paint(self, painter, option, index)
def paint(self, painter, option, index): # NODE_REFERENCE --------------------------------------------------- if index.column() == NODE_REFERENCE: # backup painter painter.save() # paint background palette = QApplication.palette() if option.state & QStyle.State_Selected: bg_color = palette.highlight().color() else: bg_color = Qt.transparent painter.fillRect(option.rect, bg_color) # paint value = float(index.model().data(index)) if value == 0.0: painter.setPen(Qt.black if option.state & QStyle.State_Selected else Qt.darkGray) painter.setBrush(Qt.NoBrush) mid_size = 3.0 else: painter.setPen(Qt.darkCyan) painter.setBrush(Qt.cyan) mid_size = 4.0 h_center = round(option.rect.x() + (option.rect.width() * .5)) v_center = round(option.rect.y() + (option.rect.height() * .5)) losange = QPolygonF() losange.append(QPointF(h_center, v_center - mid_size)) losange.append(QPointF(h_center - mid_size, v_center)) losange.append(QPointF(h_center, v_center + mid_size)) losange.append(QPointF(h_center + mid_size, v_center)) painter.drawPolygon(losange) # restore painter painter.restore() # FILE_STATE ------------------------------------------------------- elif index.column() == FILE_STATE: # backup painter painter.save() # paint background palette = QApplication.palette() bg_color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else Qt.transparent painter.fillRect(option.rect, bg_color) # paint circle value = index.model().data(index) pen_color = [ Qt.darkRed, Qt.black if option.state & QStyle.State_Selected else Qt.gray, Qt.darkGreen ][value + 1] brush_color = [Qt.red, Qt.NoBrush, Qt.green][value + 1] painter.setPen(pen_color) painter.setBrush(brush_color) h_center = round(option.rect.x() + (option.rect.width() * .5)) v_center = round(option.rect.y() + (option.rect.height() * .5)) center = QPointF(h_center, v_center) painter.drawEllipse(center, 3.0, 3.0) # restore painter painter.restore() # NODE_NAME -------------------------------------------------------- elif index.column() == NODE_NAME: text = index.model().data(index, Qt.DisplayRole) palette = QApplication.palette() bg_color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else Qt.transparent txt_color = palette.highlightedText().color() \ if option.state & QStyle.State_Selected \ else palette.text().color() if MTTSettings.value('vizWrongNameState') \ and not MTTSettings.value('showWrongNameState'): file_name = os.path.splitext( os.path.basename(index.model().data( index.sibling(index.row(), NODE_FILE), Qt.DisplayRole)))[0] if not re.split('[0-9]*$', text.rsplit(':')[-1])[0] == \ re.split('[0-9]*$', file_name)[0]: bg_color = QBrush( Qt.red if option.state & QStyle.State_Selected else Qt.darkRed, Qt.Dense4Pattern) if not MTTSettings.value('showNamespaceState'): splits = text.split(':') text = splits[len(splits) > 1] painter.save() painter.fillRect(option.rect, bg_color) painter.setPen(txt_color) rect = option.rect rect.setX(4) QApplication.style().drawItemText(painter, rect, Qt.AlignLeft | Qt.AlignVCenter, palette, True, text) painter.restore() # NODE_FILE ------------------------------------------------------------ elif index.column() == NODE_FILE: palette = QApplication.palette() bg_color = palette.highlight().color() \ if option.state & QStyle.State_Selected \ else Qt.transparent txt_color = palette.highlightedText().color() \ if option.state & QStyle.State_Selected \ else palette.text().color() text = index.model().data(index, Qt.DisplayRole) if MTTSettings.value('vizExternalState'): if not text.startswith(self.ws_path): bg_color = QBrush( '#ef7900' if option.state & QStyle.State_Selected else '#b05100', Qt.Dense4Pattern) if MTTSettings.value('vizWrongPathState'): if not re.match(MTTSettings.PATH_PATTERN, text): bg_color = QBrush( Qt.red if option.state & QStyle.State_Selected else Qt.darkRed, Qt.Dense4Pattern) if MTTSettings.value('showBasenameState'): text = os.path.basename(text) elif not MTTSettings.value('showRealAttributeValue'): if not text.startswith('\\'): text = os.path.normpath(cmds.workspace(projectPath=text)) painter.save() painter.fillRect(option.rect, bg_color) painter.setPen(txt_color) QApplication.style().drawItemText(painter, option.rect, Qt.AlignLeft | Qt.AlignVCenter, palette, True, text) painter.restore() else: QStyledItemDelegate.paint(self, painter, option, index)