Skip to content

ProxyMapper

Qt Base Class: QObject

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

Base classes

Name Children Inherits
Object
prettyqt.core.object

⋔ Inheritance diagram

graph TD
  1473290764768["itemmodels.ProxyMapper"]
  1473299782816["core.Object"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473299782816 --> 1473290764768
  1473299815024 --> 1473299782816
  140713234304496 --> 1473299815024
  1473288842240 --> 1473299782816
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: Object

Class to map indexes / ItemSelections from one proxy to another.

Also handles cases like:

            Root model
                |
           shared proxy
           /                      Proxy_1_1     Proxy_2_1
          |            |
        Proxy_1_2     Proxy_2_2
When mapping from 1_2 to 2_2, it will find the closest parent ("shared proxy" here), use mapToSource / mapSelectionFromSource until it gets there, and then mapFromSource / mapSelectionFromSource to get down to 2_2.
mapper = ProxyMapper(proxy_1_2, proxy_2_1)
index = proxy_1_2.index(0, 0)
mapped_index = mapper.map_index(source=0, target=1, index)

Source code in prettyqt\itemmodels\proxies\proxymapper.py
class ProxyMapper(core.Object):
    """Class to map indexes / ItemSelections from one proxy to another.

    Also handles cases like:
    ```
                Root model
                    |
               shared proxy
               /          \
            Proxy_1_1     Proxy_2_1
              |            |
            Proxy_1_2     Proxy_2_2
    ```
    When mapping from 1_2 to 2_2, it will find the closest parent ("shared proxy" here),
    use mapToSource / mapSelectionFromSource until it gets there,
    and then mapFromSource / mapSelectionFromSource to get down to 2_2.
    ``` py
    mapper = ProxyMapper(proxy_1_2, proxy_2_1)
    index = proxy_1_2.index(0, 0)
    mapped_index = mapper.map_index(source=0, target=1, index)
    ```

    """

    def __init__(
        self,
        *proxies: core.QAbstractItemModel,
        **kwargs,
    ):
        super().__init__(**kwargs)
        chains = [modelhelpers.get_proxy_chain(proxy) for proxy in proxies]
        common_list = [
            element
            for element in chains[0]
            if all(element in sublist for sublist in chains[1:])
        ]
        if not common_list:
            raise RuntimeError("No common source model")
        common_source = common_list[0]
        logger.debug(f"Common source: {common_source}")
        self._chains = [chain[: chain.index(common_source)] for chain in chains]

    def map_index(
        self, source: int, target: int, index: core.ModelIndex
    ) -> core.ModelIndex:
        """Map index from source to target."""
        for model in self._chains[source]:
            logger.debug(f"mapping from {model!r} to {model.sourceModel()!r}")
            index = model.mapToSource(index)
        for model in reversed(self._chains[target]):
            logger.debug(f"mapping from {model.sourceModel()!r} to {model!r}")
            index = model.mapFromSource(index)
        return index

    def map_selection(
        self, source: int, target: int, selection: core.QItemSelection
    ) -> core.QItemSelection:
        """Map selection from source to target."""
        for model in self._chains[source]:
            logger.debug(f"mapping from {model!r} to {model.sourceModel()!r}")
            selection = model.mapSelectionToSource(selection)
        for model in reversed(self._chains[target]):
            logger.debug(f"mapping from {model.sourceModel()!r} to {model!r}")
            selection = model.mapSelectionFromSource(selection)
        return selection

map_index(source: int, target: int, index: core.ModelIndex) -> core.ModelIndex

Map index from source to target.

Source code in prettyqt\itemmodels\proxies\proxymapper.py
def map_index(
    self, source: int, target: int, index: core.ModelIndex
) -> core.ModelIndex:
    """Map index from source to target."""
    for model in self._chains[source]:
        logger.debug(f"mapping from {model!r} to {model.sourceModel()!r}")
        index = model.mapToSource(index)
    for model in reversed(self._chains[target]):
        logger.debug(f"mapping from {model.sourceModel()!r} to {model!r}")
        index = model.mapFromSource(index)
    return index

map_selection(source: int, target: int, selection: core.QItemSelection) -> core.QItemSelection

Map selection from source to target.

Source code in prettyqt\itemmodels\proxies\proxymapper.py
def map_selection(
    self, source: int, target: int, selection: core.QItemSelection
) -> core.QItemSelection:
    """Map selection from source to target."""
    for model in self._chains[source]:
        logger.debug(f"mapping from {model!r} to {model.sourceModel()!r}")
        selection = model.mapSelectionToSource(selection)
    for model in reversed(self._chains[target]):
        logger.debug(f"mapping from {model.sourceModel()!r} to {model!r}")
        selection = model.mapSelectionFromSource(selection)
    return selection

⌗ Property table

Qt Property Type Doc
objectName QString