Skip to content

ValueColorDelegate

Base classes

Name Children Inherits
StyledItemDelegate
prettyqt.widgets.styleditemdelegate

⋔ Inheritance diagram

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

🛈 DocStrings

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

:fa-brands-github: Show source on GitHub