def __init__(self, *args): QHeaderView.__init__(self, *args) self.hover = -1 self.current_font = QFont(self.font()) self.current_font.setBold(True) self.current_font.setItalic(True) self.fm = QFontMetrics(self.current_font)
def currentCellWidth(self, searchedIndex: QModelIndex, leafIndex: QModelIndex, sectionIndex: int, hv: QHeaderView) -> int: leafsList = QModelIndexList(self.leafs(searchedIndex)) if leafsList.empty(): return hv.sectionSize(sectionIndex) width = 0 firstLeafSectionIndex = sectionIndex - leafsList.indexOf(leafIndex) for i in range(leafsList.size()): width += hv.sectionSize(firstLeafSectionIndex + i) return width
def currentCellLeft(self, searchedIndex: QModelIndex, leafIndex: QModelIndex, sectionIndex: int, left: int, hv: QHeaderView) -> int: leafsList = QModelIndexList(self.leafs(searchedIndex)) if not leafsList.empty(): n = leafsList.indexOf(leafIndex) firstLeafSectionIndex = sectionIndex - n n -= 1 for n in range(n, 0 - 1, -1): left -= hv.sectionSize(firstLeafSectionIndex + n) return left
def cellSize(self, leafIndex: QModelIndex, hv: QHeaderView, styleOptions: QStyleOptionHeader) -> QSize: res = QSize() variant = leafIndex.data(Qt.SizeHintRole) if variant: res = variant fnt = QFont(hv.font()) var = leafIndex.data(Qt.FontRole) if var: fnt = var fnt.setBold(True) fm = QFontMetrics(fnt) size = QSize( fm.size(0, leafIndex.data(Qt.DisplayRole)) + QSize(4, 0)) # WA: add more horizontal size (4px) if leafIndex.data(Qt.UserRole): size.transpose() decorationsSize = QSize(hv.style().sizeFromContents( QStyle.CT_HeaderSection, styleOptions, QSize(), hv)) emptyTextSize = QSize(fm.size(0, "")) return res.expandedTo(size + decorationsSize - emptyTextSize)
def paintVerticalCell(self, painter: QPainter, hv: QHeaderView, cellIndex: QModelIndex, leafIndex: QModelIndex, logicalLeafIndex: int, styleOptions: QStyleOptionHeader, sectionRect: QRect, left: int): uniopt = QStyleOptionHeader(styleOptions) self.setForegroundBrush(uniopt, cellIndex) self.setBackgroundBrush(uniopt, cellIndex) width = self.cellSize(cellIndex, hv, uniopt).width() if cellIndex == leafIndex: width = sectionRect.width() - left top = self.currentCellLeft(cellIndex, leafIndex, logicalLeafIndex, sectionRect.top(), hv) height = self.currentCellWidth(cellIndex, leafIndex, logicalLeafIndex, hv) r = QRect(left, top, width, height) uniopt.text = cellIndex.data(Qt.DisplayRole) painter.save() uniopt.rect = r if cellIndex.data(Qt.UserRole): hv.style().drawControl(QStyle.CE_HeaderSection, uniopt, painter, hv) m = QMatrix() m.rotate(-90) painter.setWorldMatrix(m, True) new_r = QRect(0, 0, r.height(), r.width()) new_r.moveCenter(QPoint(-r.center().y(), r.center().x())) uniopt.rect = new_r hv.style().drawControl(QStyle.CE_HeaderLabel, uniopt, painter, hv) else: hv.style().drawControl(QStyle.CE_Header, uniopt, painter, hv) painter.restore() return left + width
def event(self, e): if e.type() in (e.HoverMove, e.HoverEnter): self.hover = self.logicalIndexAt(e.pos()) elif e.type() in (e.Leave, e.HoverLeave): self.hover = -1 return QHeaderView.event(self, e)