Skip to content

SliceHighlightCurrentProxyModel

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
  1473290744272["itemmodels.SliceHighlightCurrentProxyModel"]
  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 --> 1473290744272
  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 which highlights all cells with same data as current index.

Highlights all cells with same content in given role as currently focused cell with a user-specified color.

Possible modes are:

  • all: Highlight all cells with same value.
  • column: Highlight all cells with same value and same column as current.
  • row: Highlight all cells with same value and same row as current.

dct = dict(
    a=["a", "b", "a", "b"],
    b=["a", "b", "a", "b"],
    c=["a", "b", "a", "b"],
    d=["b", "a", "b", "a"],
    e=["a", "b", "a", "a"],
)
model = gui.StandardItemModel.from_dict(dct)
table = widgets.TableView()
table.set_model(model)
# apply proxy to every 2nd column
# table.proxifier[:, ::2].highlight_current(mode="column")
Image title

dct = dict(
    a=["a", "b", "a", "b"],
    b=["a", "b", "a", "b"],
    c=["a", "b", "a", "b"],
    d=["b", "a", "b", "a"],
    e=["a", "b", "a", "a"],
)
model = gui.StandardItemModel.from_dict(dct)
table = widgets.TableView()
table.set_model(model)
# apply proxy to every 2nd column
table.proxifier[:, ::2].highlight_current(mode="row")
Image title

dct = dict(
    a=["a", "b", "a", "b"],
    b=["a", "b", "a", "b"],
    c=["a", "b", "a", "b"],
    d=["b", "a", "b", "a"],
    e=["a", "b", "a", "a"],
)
model = gui.StandardItemModel.from_dict(dct)
table = widgets.TableView()
table.set_model(model)
# apply proxy to every 2nd column
table.proxifier[:, ::2].highlight_current(mode="column")
Image title

dct = dict(
    a=["a", "b", "a", "b"],
    b=["a", "b", "a", "b"],
    c=["a", "b", "a", "b"],
    d=["b", "a", "b", "a"],
    e=["a", "b", "a", "a"],
)
model = gui.StandardItemModel.from_dict(dct)
table = widgets.TableView()
table.set_model(model)
# apply proxy to every 2nd column
table.proxifier[:, ::2].highlight_current(mode="all")
Image title

Example

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table[:, :3].proxify.highlight_current(mode="all")
table.show()
# or
indexer = (slice(None), slice(None, 3))
proxy = itemmodels.SliceFilterProxyModel(indexer=indexer)
proxy.set_source_model(model)
table.set_model(proxy)
table.selectionModel().currentChanged.connect(proxy.highlight_index)
table.show()
Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
class SliceHighlightCurrentProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model which highlights all cells with same data as current index.

    Highlights all cells with same content in given role as currently focused cell
    with a user-specified color.

    Possible modes are:

    * `all`: Highlight all cells with same value.
    * `column`: Highlight all cells with same value and same column as current.
    * `row`: Highlight all cells with same value and same row as current.

    === "Without proxy"

        ```py
        dct = dict(
            a=["a", "b", "a", "b"],
            b=["a", "b", "a", "b"],
            c=["a", "b", "a", "b"],
            d=["b", "a", "b", "a"],
            e=["a", "b", "a", "a"],
        )
        model = gui.StandardItemModel.from_dict(dct)
        table = widgets.TableView()
        table.set_model(model)
        # apply proxy to every 2nd column
        # table.proxifier[:, ::2].highlight_current(mode="column")
        ```
        <figure markdown>
          ![Image title](../../images/slicehighlightcurrentproxymodel_none.png)
        </figure>

    === "Row mode"

        ```py
        dct = dict(
            a=["a", "b", "a", "b"],
            b=["a", "b", "a", "b"],
            c=["a", "b", "a", "b"],
            d=["b", "a", "b", "a"],
            e=["a", "b", "a", "a"],
        )
        model = gui.StandardItemModel.from_dict(dct)
        table = widgets.TableView()
        table.set_model(model)
        # apply proxy to every 2nd column
        table.proxifier[:, ::2].highlight_current(mode="row")
        ```
        <figure markdown>
          ![Image title](../../images/slicehighlightcurrentproxymodel_row.png)
        </figure>

    === "Column mode"

        ```py
        dct = dict(
            a=["a", "b", "a", "b"],
            b=["a", "b", "a", "b"],
            c=["a", "b", "a", "b"],
            d=["b", "a", "b", "a"],
            e=["a", "b", "a", "a"],
        )
        model = gui.StandardItemModel.from_dict(dct)
        table = widgets.TableView()
        table.set_model(model)
        # apply proxy to every 2nd column
        table.proxifier[:, ::2].highlight_current(mode="column")
        ```
        <figure markdown>
          ![Image title](../../images/slicehighlightcurrentproxymodel_column.png)
        </figure>

    === "All mode"

        ```py
        dct = dict(
            a=["a", "b", "a", "b"],
            b=["a", "b", "a", "b"],
            c=["a", "b", "a", "b"],
            d=["b", "a", "b", "a"],
            e=["a", "b", "a", "a"],
        )
        model = gui.StandardItemModel.from_dict(dct)
        table = widgets.TableView()
        table.set_model(model)
        # apply proxy to every 2nd column
        table.proxifier[:, ::2].highlight_current(mode="all")
        ```
        <figure markdown>
          ![Image title](../../images/slicehighlightcurrentproxymodel_all.png)
        </figure>


    ### Example

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table[:, :3].proxify.highlight_current(mode="all")
    table.show()
    # or
    indexer = (slice(None), slice(None, 3))
    proxy = itemmodels.SliceFilterProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.selectionModel().currentChanged.connect(proxy.highlight_index)
    table.show()
    ```
    """

    ID = "highlight_current"

    def __init__(
        self,
        role=constants.DISPLAY_ROLE,
        mode: HighlightModeStr = "column",
        highlight_color: datatypes.ColorType = "red",
        **kwargs,
    ):
        self._mode = mode
        self._current_value = ...  # Sentinel value
        self._data_role = role
        self._current_column = None
        self._current_row = None
        self._highlight_color = colors.get_color(highlight_color).as_qt()
        super().__init__(**kwargs)

    def set_highlight_color(self, color: datatypes.ColorType):
        """Set color used for highlighting cells."""
        self._highlight_color = colors.get_color(color).as_qt()

    def get_highlight_color(self) -> QtGui.QColor:
        """Get color used for higlighting cells."""
        return self._highlight_color

    def set_highlight_mode(self, mode: HighlightModeStr):
        """Set highlight mode."""
        self._highlight_mode = mode

    def get_highlight_mode(self) -> HighlightModeStr:
        """Get highlight mode."""
        return self._highlight_mode

    def set_highlight_role(self, mode: constants.ItemDataRole):
        """Set highlight mode."""
        self._data_role = mode

    def get_highlight_role(self) -> constants.ItemDataRole:
        """Get highlight mode."""
        return self._data_role

    def set_highlight_column(self, column: int):
        with self.change_layout():
            self._current_column = column

    def get_highlight_column(self) -> int:
        return self._current_column

    def set_highlight_row(self, row: int):
        with self.change_layout():
            self._current_row = row

    def get_highlight_row(self) -> int:
        return self._current_row

    def set_current_value(self, value):
        with self.change_layout():
            self._current_value = value

    def highlight_index(self, index: core.ModelIndex):
        with self.change_layout():
            self._current_value = index.data(self._data_role)  # super().data(index, role)
            self._current_row = index.row()
            self._current_column = index.column()

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if (
            role == constants.BACKGROUND_ROLE
            and index.data(self._data_role) == self._current_value
            and self.indexer_contains(index)
            and (
                (self._mode == "column" and index.column() == self._current_column)
                or (
                    self._mode == "row"
                    and (index.row() == self._current_row)
                    or self._mode == "all"
                )
            )
        ):
            return self._highlight_color
        return super().data(index, role)

    highlightMode = core.Property(
        str,
        get_highlight_mode,
        set_highlight_mode,
        doc="Highlight mode",
    )
    highlightColor = core.Property(
        QtGui.QColor,
        get_highlight_color,
        set_highlight_color,
        doc="Color to use for highlighting",
    )
    highlightRole = core.Property(
        constants.ItemDataRole,
        get_highlight_role,
        set_highlight_role,
        doc="ItemRole to use for highlighting",
    )
    highlight_column = core.Property(
        int,
        get_highlight_column,
        set_highlight_column,
        doc="Currently highlighted column",
    )
    highlight_row = core.Property(
        int,
        get_highlight_row,
        set_highlight_row,
        doc="Currently highlighted row",
    )

get_highlight_color() -> QtGui.QColor

Get color used for higlighting cells.

Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
def get_highlight_color(self) -> QtGui.QColor:
    """Get color used for higlighting cells."""
    return self._highlight_color

get_highlight_mode() -> HighlightModeStr

Get highlight mode.

Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
def get_highlight_mode(self) -> HighlightModeStr:
    """Get highlight mode."""
    return self._highlight_mode

get_highlight_role() -> constants.ItemDataRole

Get highlight mode.

Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
def get_highlight_role(self) -> constants.ItemDataRole:
    """Get highlight mode."""
    return self._data_role

set_highlight_color(color: datatypes.ColorType)

Set color used for highlighting cells.

Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
def set_highlight_color(self, color: datatypes.ColorType):
    """Set color used for highlighting cells."""
    self._highlight_color = colors.get_color(color).as_qt()

set_highlight_mode(mode: HighlightModeStr)

Set highlight mode.

Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
def set_highlight_mode(self, mode: HighlightModeStr):
    """Set highlight mode."""
    self._highlight_mode = mode

set_highlight_role(mode: constants.ItemDataRole)

Set highlight mode.

Source code in prettyqt\itemmodels\proxies\slicehighlightcurrentproxymodel.py
def set_highlight_role(self, mode: constants.ItemDataRole):
    """Set highlight mode."""
    self._data_role = mode

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
highlightMode QString Highlight mode
highlightColor QColor Color to use for highlighting
highlightRole PySide::PyObjectWrapper ItemRole to use for highlighting
highlight_column int Currently highlighted column
highlight_row int Currently highlighted row