Skip to content

HeaderCellDelegate

Base classes

Name Children Inherits
_PaletteColorsMixin
cutepandas.pandaswidgets._viewer_style
Colours set by the viewer (`set_colors`), else derived from the palette.
StyledItemDelegate
prettyqt.widgets.styleditemdelegate

⋔ Inheritance diagram

graph TD
  3119479182["_viewer_style.HeaderCellDelegate"]
  314031066["_viewer_style._PaletteColorsMixin"]
  2751680898["builtins.object"]
  4109708588["widgets.StyledItemDelegate"]
  3153451512["widgets.AbstractItemDelegateMixin"]
  3227531497["core.ObjectMixin"]
  4201154414["QtWidgets.QStyledItemDelegate"]
  3961416811["QtWidgets.QAbstractItemDelegate"]
  13951691["QtCore.QObject"]
  395147830["sip.wrapper"]
  46922758["sip.simplewrapper"]
  314031066 --> 3119479182
  2751680898 --> 314031066
  4109708588 --> 3119479182
  3153451512 --> 4109708588
  3227531497 --> 3153451512
  2751680898 --> 3227531497
  4201154414 --> 4109708588
  3961416811 --> 4201154414
  13951691 --> 3961416811
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: _PaletteColorsMixin, StyledItemDelegate

Renderer for the multi-level column and row header tables.

Outer MultiIndex levels get a slightly stronger surface, the leaf level a divider edge towards the data table, the sorted leaf column a sort arrow and (optionally) leaf column headers a palette-tinted dtype icon.

Source code in src/cutepandas/pandaswidgets/_viewer_style.py
class HeaderCellDelegate(_PaletteColorsMixin, widgets.StyledItemDelegate):
    """Renderer for the multi-level column and row header tables.

    Outer MultiIndex levels get a slightly stronger surface, the leaf level a divider
    edge towards the data table, the sorted leaf column a sort arrow and (optionally)
    leaf column headers a palette-tinted dtype icon.
    """

    def __init__(
        self,
        orientation: constants.Orientation,
        parent: core.QObject | None = None,
        *,
        sort_state_getter: Callable[[], tuple[int, constants.SortOrder] | None]
        | None = None,
        kind_getter: Callable[[int], Any] | None = None,
        show_dtype_icons: bool = False,
    ):
        super().__init__(parent=parent)
        self._horizontal = orientation == constants.HORIZONTAL
        self._sort_state_getter = sort_state_getter
        self._kind_getter = kind_getter
        self._show_dtype_icons = show_dtype_icons
        self._kind_icons = icons.KindIconCache()
        self._fonts = _FontCache()

    def set_colors(self, colors: ViewerColors):
        super().set_colors(colors)
        self._kind_icons.clear()

    def paint(
        self,
        painter: Any,
        option: Any,
        index: core.ModelIndex,
    ):
        colors = self.colors_for(option.palette)
        rect = option.rect
        is_selected = option.state.value & _SELECTED
        is_horizontal = self._horizontal

        model = index.model()
        is_leaf = True
        if model is not None:
            if is_horizontal:
                is_leaf = index.row() == model.rowCount() - 1
            else:
                is_leaf = index.column() == model.columnCount() - 1

        painter.save()
        # Surface fill: slightly richer tone for outer MultiIndex levels
        if is_selected:
            bg = colors.header_selected_bg
        elif not is_leaf:
            bg = colors.header_outer_bg
        else:
            bg = colors.header_bg
        painter.fillRect(rect, bg)
        # Grid hairlines; the leaf level's edge towards the data table is a divider.
        if is_horizontal:
            bottom = colors.divider if is_leaf else colors.grid
            _draw_grid_edges(painter, rect, colors.grid, bottom)
        else:
            right = colors.divider if is_leaf else colors.grid
            _draw_grid_edges(painter, rect, right, colors.grid)

        # Accent edge on selected leaf header touching the data table
        if is_selected and is_leaf:
            if is_horizontal:
                painter.fillRect(
                    rect.left(), rect.bottom() - 1, rect.width(), 2, colors.accent
                )
            else:
                painter.fillRect(
                    rect.right() - 1, rect.top(), 2, rect.height(), colors.accent
                )

        # Sort indicator on leaf column header
        sort_order: constants.SortOrder | None = None
        if is_horizontal and is_leaf and self._sort_state_getter is not None:
            sort_state = self._sort_state_getter()
            if sort_state is not None and sort_state[0] == index.column():
                sort_order = sort_state[1]

        left_pad = HEADER_HPAD
        right_pad = HEADER_HPAD + (SORT_ARROW_SPACE if sort_order is not None else 0)

        # Optional dtype icon on leaf horizontal headers
        if (
            is_horizontal
            and is_leaf
            and self._show_dtype_icons
            and self._kind_getter is not None
        ):
            kind = self._kind_getter(index.column())
            if kind is not None:
                icon = self._kind_icons.icon(kind, color=colors.muted_text.name())
                size = HEADER_ICON_SIZE
                iy = rect.top() + (rect.height() - size) // 2
                icon.paint(painter, rect.left() + left_pad, iy, size, size)
                left_pad += size + HEADER_ICON_GAP

        raw_text = index.data(constants.DISPLAY_ROLE)
        text = "" if raw_text is None else str(raw_text)
        text_rect = rect.adjusted(left_pad, 0, -right_pad, 0)
        if text and text_rect.width() > 0:
            if is_horizontal:
                weight = "demi_bold" if is_leaf else "medium"
            else:
                weight = None if is_leaf else "medium"
            painter.setFont(self._fonts.get(option.font, weight))
            painter.setPen(colors.header_fg)
            align = _ALIGN_HEADER_H if is_horizontal else _ALIGN_HEADER_V
            elided = painter.fontMetrics().elidedText(
                text, _ELIDE_RIGHT, text_rect.width()
            )
            painter.drawText(text_rect, align, elided)

        if sort_order is not None:
            _draw_sort_arrow(
                painter,
                rect.right() - 10,
                rect.center().y(),
                ascending=sort_order != constants.DESCENDING,
                color=colors.accent,
            )

        painter.restore()

:fa-brands-github: Show source on GitHub