Skip to content

SliceColorCategoriesProxyModel

Qt Base Class: QIdentityProxyModel

Signature: QIdentityProxyModel(self, parent: Optional[PySide6.QtCore.QObject] = None) -> None

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel

⋔ Inheritance diagram

graph TD
  1473290755984["itemmodels.SliceColorCategoriesProxyModel"]
  1473290716944["itemmodels.SliceIdentityProxyModel"]
  1473299892128["core.IdentityProxyModel"]
  1473299903840["core.AbstractProxyModelMixin"]
  1473299890176["core.AbstractItemModelMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289064768["QtCore.QIdentityProxyModel"]
  1473289061840["QtCore.QAbstractProxyModel"]
  1473289050128["QtCore.QAbstractItemModel"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473290716944 --> 1473290755984
  1473299892128 --> 1473290716944
  1473299903840 --> 1473299892128
  1473299890176 --> 1473299903840
  1473299815024 --> 1473299890176
  140713234304496 --> 1473299815024
  1473289064768 --> 1473299892128
  1473289061840 --> 1473289064768
  1473289050128 --> 1473289061840
  1473288842240 --> 1473289050128
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model to apply coloring to categories.

Example

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table[:, :3].proxify.color_categories()
table.show()
# or
indexer = (slice(None), slice(None, 3))
proxy = itemmodels.SliceColorCategoriesProxyModel(indexer=indexer)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt\itemmodels\proxies\slicecolorcategoriesproxymodel.py
class SliceColorCategoriesProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to apply coloring to categories.

    ### Example

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table[:, :3].proxify.color_categories()
    table.show()
    # or
    indexer = (slice(None), slice(None, 3))
    proxy = itemmodels.SliceColorCategoriesProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "color_categories"
    ICON = "mdi.palette-outline"

    def __init__(self, *args, **kwargs):
        self._role = constants.DISPLAY_ROLE
        self._color_map = {}
        self.color_generator = itertools.cycle(gui.Palette().iter_colors())
        self._color_none = False
        self._cast_to_str = False
        super().__init__(*args, **kwargs)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not self.indexer_contains(index):
            return super().data(index, role)
        match role:
            case constants.BACKGROUND_ROLE:
                return self.get_color_for_index(index)
            case _:
                return super().data(index, role)

    def get_color_for_index(self, index):
        value = super().data(index, self._role)
        key = str(value) if self._cast_to_str else value
        match value:
            case None if not self._color_none:
                return None
            case _:
                if key in self._color_map:
                    return self._color_map[key]
                new = next(self.color_generator)
                self._color_map[key] = new
                return self._color_map[key]

    def is_none_colored(self) -> bool:
        return self._color_none

    def set_none_colored(self, val: bool):
        with self.change_layout():
            self._color_none = val

    def is_casted_to_str(self) -> bool:
        return self._cast_to_str

    def set_cast_to_str(self, val: bool):
        with self.change_layout():
            self._color_map = {}
            self.color_generator = itertools.cycle(gui.Palette().iter_colors())
            self._cast_to_str = val

    color_none = core.Property(
        bool,
        is_none_colored,
        set_none_colored,
        doc="Whether None-values should also get colored",
    )
    """Color ItemData with value `None`."""

    cast_to_str = core.Property(
        bool,
        is_casted_to_str,
        set_cast_to_str,
        doc="Cast values to string in order to color / group them",
    )
    """Cast all values to a string for deciding whether cells are in same category."""

cast_to_str = core.Property(bool, is_casted_to_str, set_cast_to_str, doc='Cast values to string in order to color / group them') class-attribute instance-attribute

Cast all values to a string for deciding whether cells are in same category.

color_none = core.Property(bool, is_none_colored, set_none_colored, doc='Whether None-values should also get colored') class-attribute instance-attribute

Color ItemData with value None.

Info

This is a slice proxy and can be selectively applied to a model. Read more about slices.

⌗ Property table

Qt Property Type Doc
objectName QString
sourceModel QAbstractItemModel
column_slice QVariantList Column slice to include for the proxy
row_slice QVariantList Row slice to include for the proxy
color_none bool Whether None-values should also get colored
cast_to_str bool Cast values to string in order to color / group them