Skip to content

delegates

Class info

Re-exported classes

Class Module Description
DTypeDelegate cutepandas.delegates.dtypedelegate Edits a dtype with an editable combo box (each entry with its dtype icon).
ValueColorDelegate cutepandas.delegates.valuecolordelegate Paints a data bar or a heat-map fill behind the text of a cell.

🛈 DocStrings

Item delegates for dataframe models.

DTypeDelegate

Bases: StyledItemDelegate

Edits a dtype with an editable combo box (each entry with its dtype icon).

The chosen text is written back with setData(index, text, role); the model converts the column (PandasColumnListModel does astype). A failed conversion leaves the column unchanged.

Source code in src/cutepandas/delegates/dtypedelegate.py
class DTypeDelegate(widgets.StyledItemDelegate):
    """Edits a dtype with an editable combo box (each entry with its dtype icon).

    The chosen text is written back with `setData(index, text, role)`; the model
    converts the column (`PandasColumnListModel` does `astype`). A failed conversion
    leaves the column unchanged.
    """

    ID = "dtype"

    def __init__(
        self,
        dtypes: Sequence[str] = PANDAS_DTYPES,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.dtypes = list(dtypes)
        self.role = role

    def createEditor(self, parent, option, index: core.ModelIndex):
        combo = widgets.ComboBox(parent)
        combo.set_editable(True)
        for dtype in self.dtypes:
            try:
                icon = icons.icon_for_dtype(dtype)
            except TypeError, ValueError:
                icon = None
            combo.add(dtype, icon=icon)
        combo.activated.connect(lambda _: self.commitData.emit(combo))
        return combo

    def setEditorData(self, editor, index: core.ModelIndex):
        editor.setCurrentText(str(index.data(constants.DISPLAY_ROLE)))

    def setModelData(self, editor, model, index: core.ModelIndex):
        text = editor.currentText().strip()
        if text and text != index.data(constants.DISPLAY_ROLE):
            model.setData(index, text, self.role)

ValueColorDelegate

Bases: StyledItemDelegate

Paints a data bar or a heat-map fill behind the text of a cell.

The value (0..1) is read from role; mode="bar" fills that share of the cell width, mode="heat" the whole cell with an opacity that follows the value.

With DataFrameModel the default role (NormalizedRole) is computed once per cached block (vectorised), so painting costs one lookup per cell. Any other model can provide the role, too; cells without a value are painted normally.

Source code in src/cutepandas/delegates/valuecolordelegate.py
class ValueColorDelegate(widgets.StyledItemDelegate):
    """Paints a data bar or a heat-map fill behind the text of a cell.

    The value (0..1) is read from `role`; `mode="bar"` fills that share of the cell
    width, `mode="heat"` the whole cell with an opacity that follows the value.

    With `DataFrameModel` the default role (`NormalizedRole`) is computed once per
    cached block (vectorised), so painting costs one lookup per cell. Any other model
    can provide the role, too; cells without a value are painted normally.
    """

    ID = "value_color"

    def __init__(
        self,
        mode: ModeStr = "bar",
        color: str | gui.QColor = "#4c8bf5",
        role: int = DataFrameModel.Roles.NormalizedRole,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._mode: ModeStr = mode
        self._color = gui.QColor(color)
        self._role = role

    def paint(
        self,
        painter: gui.QPainter,
        option: widgets.QStyleOptionViewItem,
        index: qcore.ModelIndex,
    ):
        value = index.data(self._role)
        if isinstance(value, int | float) and 0.0 <= value <= 1.0:
            rect = option.rect
            width = rect.width()
            color = gui.QColor(self._color)
            if self._mode == "bar":
                color.setAlpha(110)
                width = round(width * value)
            else:
                color.setAlpha(round(40 + 180 * value))
            painter.fillRect(rect.x(), rect.y(), width, rect.height(), color)  # ty: ignore[no-matching-overload]
        super().paint(painter, option, index)

    def set_mode(self, mode: ModeStr):
        self._mode = mode

    def get_mode(self) -> ModeStr:
        return self._mode

    def set_color(self, color: str | gui.QColor):
        self._color = gui.QColor(color)

    def get_color(self) -> gui.QColor:
        return self._color