Skip to content

RegexMatchesModel

Base classes

Name Children Inherits
AbstractTableModel
prettyqt.core.abstracttablemodel
Abstract model that can be subclassed to create table models.

⋔ Inheritance diagram

graph TD
  3723509453["itemmodels.RegexMatchesModel"]
  656210031["core.AbstractTableModel"]
  4092674192["core.AbstractTableModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  232985924["QtCore.QAbstractTableModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  656210031 --> 3723509453
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  2751680898 --> 4092674192
  232985924 --> 656210031
  1898060022 --> 232985924
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: AbstractTableModel

Model to display a list of re.Matches.

Source code in prettyqt/itemmodels/regexmatchesmodel.py
class RegexMatchesModel(core.AbstractTableModel):
    """Model to display a list of re.Matches."""

    HEADER: ClassVar = ["Start", "End", "Value", "Groups"]
    SUPPORTS = list[re.Match]

    def __init__(self, matches: list | None = None, **kwargs):
        super().__init__(**kwargs)
        self.matches = matches or []

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case (re.Match(), *_):
                return True
            case _:
                return False

    def columnCount(self, parent=None):
        return len(self.HEADER)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role:
            case constants.HORIZONTAL, constants.DISPLAY_ROLE:
                return self.HEADER[section]

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        item = self.matches[index.row()]
        match role, index.column():
            case constants.DISPLAY_ROLE, 0:
                return str(item.span()[0])
            case constants.DISPLAY_ROLE, 1:
                return str(item.span()[1])
            case constants.DISPLAY_ROLE, 2:
                return repr(item.group())
            case constants.DISPLAY_ROLE, 3:
                return str(len(item.groups()))
            case constants.USER_ROLE, _:
                return item.span()

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        """Override for AbstractitemModel base method."""
        parent = parent or core.ModelIndex()
        if parent.column() > 0:
            return 0
        return 0 if parent.isValid() else len(self.matches)

rowCount(parent: core.ModelIndex | None = None) -> int

Override for AbstractitemModel base method.

Source code in prettyqt/itemmodels/regexmatchesmodel.py
def rowCount(self, parent: core.ModelIndex | None = None) -> int:
    """Override for AbstractitemModel base method."""
    parent = parent or core.ModelIndex()
    if parent.column() > 0:
        return 0
    return 0 if parent.isValid() else len(self.matches)

:fa-brands-github: Show source on GitHub