Skip to content

SliceMapRoleProxyModel

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

⋔ Inheritance diagram

graph TD
  613321705["itemmodels.SliceMapRoleProxyModel"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4214724289 --> 613321705
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  2751680898 --> 2431222121
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model for mapping one role to another.

Mapping can be changed by passing a dictionary with source role as key and target_role as value. Py passing an optional converter function, values which are mapped can be modified.

Example

source_model = FsSpecTreemodel("file")
table = widgets.TableView()
mapping = {source_model.Roles.ProtocolPathRole: constants.DISPLAY_ROLE}
model = SliceMapRoleProxyModel(mapping, indexer=0, parent=table)
model.setSourceModel(source_model)
table.set_model(model)
table.show()
# or
table.proxifier.map_role(source_model.Roles.ProtocolPathRole, constants.DISPLAY_ROLE)
Source code in prettyqt/itemmodels/proxies/slicemaproleproxymodel.py
class SliceMapRoleProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model for mapping one role to another.

    Mapping can be changed by passing a dictionary with source role as key and target_role
    as value.
    Py passing an optional converter function, values which are mapped can be modified.

    ### Example

    ```py
    source_model = FsSpecTreemodel("file")
    table = widgets.TableView()
    mapping = {source_model.Roles.ProtocolPathRole: constants.DISPLAY_ROLE}
    model = SliceMapRoleProxyModel(mapping, indexer=0, parent=table)
    model.setSourceModel(source_model)
    table.set_model(model)
    table.show()
    # or
    table.proxifier.map_role(source_model.Roles.ProtocolPathRole, constants.DISPLAY_ROLE)
    ```
    """

    ID = "map_role"
    ICON = "mdi.directions-fork"

    def __init__(
        self,
        mapping: Mapping[constants.ItemDataRole, constants.ItemDataRole],
        converter: Callable | None = None,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._mapping = bidict(mapping)
        self._converter = converter

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if role in self._mapping.inverse and self.indexer_contains(index):
            value = super().data(index, self._mapping.inverse[role])
            return self._converter(value) if self._converter else value
        return super().data(index, role)

    def set_mapping(
        self,
        mapping: Mapping[constants.ItemDataRole, constants.ItemDataRole],
    ):
        with self.reset_model():
            self._mapping = bidict(mapping)

    def get_mapping(self) -> Mapping[constants.ItemDataRole, constants.ItemDataRole]:
        return self._mapping

    mapping = core.Property(
        dict,
        get_mapping,
        set_mapping,
        doc="Mapping of ItemRoles",
    )

:fa-brands-github: Show source on GitHub