Skip to content

SliceMapRoleProxyModel

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
  1473290742320["itemmodels.SliceMapRoleProxyModel"]
  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 --> 1473290742320
  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 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",
    )

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
mapping QVariantMap Mapping of ItemRoles