def drawCheck(self, painter, option, rect, state): """ Renders a check indicator within the rectangle based on the inputed \ check state. :param painter | <QtGui.QPainter> option | <QtGui.QStyleOptionViewItem> rect | <QtGui.QRect> state | <QtCore.Qt.CheckState> """ if not self.useCheckMaps(): return super(XTreeWidgetDelegate, self).drawCheck(painter, option, rect, state) pixmap = None if state == QtCore.Qt.Checked: pixmap = self.checkOnMap() elif state == QtCore.Qt.PartiallyChecked: pixmap = self.checkPartialMap() elif state == QtCore.Qt.Unchecked: pixmap = self.checkOffMap() if type(pixmap) in (str, unicode): pixmap = QtGui.QPixmap(pixmap) if not pixmap: return x = rect.x() + (rect.width() - 16) / 2.0 y = rect.y() + (rect.height() - 16) / 2.0 painter.drawPixmap(int(x), int(y), pixmap)
def setCheckPartialMap(self, pixmap): """ Sets the pixmap to be used when rendering a check state in the \ partial state. :param pixmap | <QtGui.QPixmap> || <str> || None """ self._checkPartialMap = QtGui.QPixmap(pixmap)
def paint(self, painter, opt, index): """ Overloads the paint method to draw the grid and other options for \ this delegate. :param painter | <QtGui.QPainter> opt | <QtGui.QStyleOptionItem> index | <QtGui.QModelIndex> """ # prepare the painter painter.save() painter.resetTransform() # extract data from the tree tree = self.parent() column = index.column() item = tree.itemFromIndex(index) is_xtreeitem = isinstance(item, XTreeWidgetItem) hovered = False font = item.font(index.column()) opt.font = font palette = tree.palette() painter.translate(opt.rect.x(), opt.rect.y()) rect_w = opt.rect.width() rect_h = opt.rect.height() painter.setClipRect(0, 0, rect_w, rect_h) # grab the check information checkState = None size = opt.decorationSize value = unwrapVariant(index.data(QtCore.Qt.CheckStateRole)) if value is not None: checkState = item.checkState(index.column()) check_size = min(size.width(), size.height()) check_size = min(14, check_size) checkRect = QtCore.QRect(2, (rect_h - check_size) / 2.0, check_size, check_size) else: checkRect = QtCore.QRect() # determine hovering options if tree.hoverMode() != xtreewidget.XTreeWidget.HoverMode.NoHover and \ item.flags() & XTreeWidgetItem.ItemIsHoverable: # hover particular columns if tree.hoverMode() == xtreewidget.XTreeWidget.HoverMode.HoverItems and \ item == tree.hoveredItem() and \ column == tree.hoveredColumn(): hovered = True # hover particular items elif tree.hoverMode() == xtreewidget.XTreeWidget.HoverMode.HoverRows and \ id(item) == id(tree.hoveredItem()): hovered = True # setup the decoration information if item.isExpanded() and is_xtreeitem and item.expandedIcon(column): icon = item.expandedIcon(column) elif hovered and tree.hoveredColumn() == column and \ is_xtreeitem and \ item.hoverIcon(column): icon = item.hoverIcon(column) else: icon = item.icon(column) if icon and not icon.isNull(): size = icon.actualSize(opt.decorationSize) pixmap = icon.pixmap(size) if checkRect: x = checkRect.right() + 2 else: x = 0 y = 0 w = opt.decorationSize.width() h = opt.decorationSize.height() x += 2 y += (rect_h - size.height()) / 2.0 decorationRect = QtCore.QRect(x, y, w, h) else: pixmap = QtGui.QPixmap() overlay = QtGui.QIcon() decorationRect = QtCore.QRect() if is_xtreeitem: overlay = item.iconOverlay(column) dec_w = decorationRect.width() dec_h = decorationRect.height() over_w = int(dec_w / 1.7) over_h = int(dec_h / 1.7) overlayRect = QtCore.QRect(decorationRect.right() - over_w, decorationRect.bottom() - over_h, over_w, over_h) else: overlay = QtGui.QPixmap() overlayRect = QtCore.QRect() # setup the coloring information bg = None fg = None if self.showHighlights() and tree.selectionModel().isSelected(index): palette = tree.palette() bg = QtGui.QBrush(palette.color(palette.Highlight)) fg = QtGui.QBrush(palette.color(palette.HighlightedText)) elif hovered: bg = tree.hoverBackground() fg = tree.hoverForeground() if is_xtreeitem: bg = item.hoverBackground(column, bg) fg = item.hoverForeground(column, fg) if not bg: bg_role = unwrapVariant(item.data(column, QtCore.Qt.BackgroundRole)) if bg_role is not None: bg = item.background(column) else: bg = self.background(column) if not fg: fg_role = unwrapVariant(item.data(column, QtCore.Qt.ForegroundRole)) if fg_role is not None: fg = item.foreground(column) else: fg = self.foreground(column) if not fg: fg = QtGui.QBrush(palette.color(palette.Text)) # draw custom text mapper = self.displayMapper(column) if mapper: text = mapper(unwrapVariant(index.data(), '')) # draw specific type text else: data = unwrapVariant(index.data(QtCore.Qt.EditRole), None) # map the data to python if type(data) in (QtCore.QDate, QtCore.QDateTime, QtCore.QTime): data = data.toPython() # render a standard date format if type(data) == datetime.date: text = data.strftime(self.dateFormat()) # render a standard datetime format elif type(data) == datetime.time: text = data.strftime(self.timeFormat()) # render a standard datetime format elif type(data) == datetime.datetime: text = data.strftime(self.datetimeFormat()) # draw standard text else: text = unwrapVariant(index.data(QtCore.Qt.DisplayRole), '') # display hint information if not text: hint = unwrapVariant(index.data(XTreeWidgetItem.HintRole)) if hint: text = hint fg = QtGui.QBrush(palette.color(palette.Disabled, palette.Text)) opt.displayAlignment = QtCore.Qt.Alignment( item.textAlignment(index.column())) if not opt.displayAlignment & (QtCore.Qt.AlignVCenter | \ QtCore.Qt.AlignTop | QtCore.Qt.AlignBottom): opt.displayAlignment |= QtCore.Qt.AlignVCenter if decorationRect: x = decorationRect.right() + 5 elif checkRect: x = checkRect.right() + 5 else: x = 5 w = rect_w - x - 5 h = rect_h displayRect = QtCore.QRect(x, 0, w, h) # create the background rect backgroundRect = QtCore.QRect(0, 0, opt.rect.width(), opt.rect.height()) # draw the item self.drawBackground(painter, opt, backgroundRect, bg) painter.setBrush(QtCore.Qt.NoBrush) painter.setPen(fg.color()) self.drawCheck(painter, opt, checkRect, checkState) self.drawDecoration(painter, opt, decorationRect, pixmap) self.drawOverlay(painter, opt, overlayRect, overlay) self.drawDisplay(painter, opt, displayRect, text) self.drawGrid(painter, opt, backgroundRect, index) painter.restore()