Skip to content

DataCellDelegate

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
  1231394384["_viewer_style.DataCellDelegate"]
  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 --> 1231394384
  2751680898 --> 314031066
  4109708588 --> 1231394384
  3153451512 --> 4109708588
  3227531497 --> 3153451512
  2751680898 --> 3227531497
  4201154414 --> 4109708588
  3961416811 --> 4201154414
  13951691 --> 3961416811
  395147830 --> 13951691
  46922758 --> 395147830
  2751680898 --> 46922758

🛈 DocStrings

Bases: _PaletteColorsMixin, StyledItemDelegate

Clean cell renderer: horizontal padding, muted null marker, no dotted focus.

Source code in src/cutepandas/pandaswidgets/_viewer_style.py
class DataCellDelegate(_PaletteColorsMixin, widgets.StyledItemDelegate):
    """Clean cell renderer: horizontal padding, muted null marker, no dotted focus."""

    def __init__(
        self,
        parent: core.QObject | None = None,
        *,
        null_placeholder: str = "—",
    ):
        super().__init__(parent=parent)
        self._null_placeholder = null_placeholder

    def paint(
        self,
        painter: Any,
        option: Any,
        index: core.ModelIndex,
    ):
        colors = self.colors_for(option.palette)
        rect = option.rect
        state = option.state.value
        is_selected = state & _SELECTED
        has_focus = state & _HAS_FOCUS
        # A plain DataFrameModel has no colour roles unless it colours values: skip
        # those queries (each data() call is a round trip into Python).
        model = index.model()
        style_roles = not isinstance(model, DataFrameModel) or model.get_color_values()

        painter.save()
        # Background
        if is_selected:
            bg = colors.selection_bg
        elif (
            style_roles and (role_bg := index.data(constants.BACKGROUND_ROLE)) is not None
        ):
            bg = role_bg
        elif option.features.value & _ALTERNATE:
            bg = colors.alt_row
        else:
            bg = colors.base
        x, y, w, h = rect.getRect()
        painter.fillRect(x, y, w, h, bg)
        # grid hairlines (right, bottom); the view's own grid is off
        painter.fillRect(x + w - 1, y, 1, h, colors.grid)
        painter.fillRect(x, y + h - 1, w, 1, colors.grid)

        # Text (only query IsNullRole when display text is empty)
        raw_text = index.data(constants.DISPLAY_ROLE)
        text = "" if raw_text is None else str(raw_text)
        text_rect = rect.adjusted(CELL_HPAD, 0, -CELL_HPAD, 0)
        if text_rect.width() > 0:
            is_null_placeholder = False
            if (
                not text
                and self._null_placeholder
                and index.data(DataFrameModel.Roles.IsNullRole)
            ):
                text = self._null_placeholder
                is_null_placeholder = True

            if text:
                if is_null_placeholder:
                    font: Any = gui.QFont(option.font)
                    font.setItalic(True)
                    painter.setFont(font)
                    fg = colors.muted_text
                else:
                    painter.setFont(option.font)
                    if is_selected:
                        fg = colors.selection_fg
                    elif (
                        style_roles
                        and (role_fg := index.data(constants.FOREGROUND_ROLE)) is not None
                    ):
                        fg = role_fg
                    else:
                        fg = colors.text
                painter.setPen(fg)
                align = _align_value(index.data(constants.ALIGNMENT_ROLE), _ALIGN_CELL)
                elided = painter.fontMetrics().elidedText(
                    text, _ELIDE_RIGHT, text_rect.width()
                )
                painter.drawText(text_rect, align, elided)

        # Crisp accent inset border on the focused cell
        if has_focus:
            painter.setPen(gui.Pen(colors.accent, 1))
            painter.setBrush(constants.BrushStyle.NoBrush)
            painter.drawRect(rect.adjusted(0, 0, -1, -1))
        painter.restore()

:fa-brands-github: Show source on GitHub