def setHoverForeground( self, column, brush ): """ Returns the brush to use when coloring when the user hovers over the item for the given column. :param column | <int> brush | <QtGui.QBrush> """ self._hoverForeground[column] = QtGui.QBrush(brush)
def setForeground(self, column, brush): """ Sets the default item foreground brush. :param brush | <QtGui.QBrush> || None """ if brush: self._foreground[column] = QtGui.QBrush(brush) elif column in self._background: self._foreground.pop(column)
def initGroupStyle(self, useIcons=True, columnCount=None): """ Initialzes this item with a grouping style option. """ flags = self.flags() if flags & QtCore.Qt.ItemIsSelectable: flags ^= QtCore.Qt.ItemIsSelectable self.setFlags(flags) if useIcons: ico = QtGui.QIcon(resources.find('img/treeview/triangle_right.png')) expand_ico = QtGui.QIcon(resources.find('img/treeview/triangle_down.png')) self.setIcon(0, ico) self.setExpandedIcon(0, expand_ico) palette = QtGui.QApplication.palette() line_clr = palette.color(palette.Mid) base_clr = palette.color(palette.Button) text_clr = palette.color(palette.ButtonText) gradient = QtGui.QLinearGradient() gradient.setColorAt(0.00, line_clr) gradient.setColorAt(0.03, line_clr) gradient.setColorAt(0.04, base_clr.lighter(105)) gradient.setColorAt(0.25, base_clr) gradient.setColorAt(0.96, base_clr.darker(105)) gradient.setColorAt(0.97, line_clr) gradient.setColorAt(1.00, line_clr) h = self._fixedHeight if not h: h = self.sizeHint(0).height() if not h: h = 18 gradient.setStart(0.0, 0.0) gradient.setFinalStop(0.0, h) brush = QtGui.QBrush(gradient) tree = self.treeWidget() columnCount = columnCount or (tree.columnCount() if tree else self.columnCount()) for i in range(columnCount): self.setForeground(i, text_clr) self.setBackground(i, brush)
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()