Skip to content

NestedModel

Base classes

Name Children Inherits
ListMixin
prettyqt.itemmodels.listmixin
Mixin for flat models which are backed by a python list (`self.items`).
ModelMixin
prettyqt.itemmodels.modelmixin
AbstractItemModel
prettyqt.core.abstractitemmodel
The abstract interface for item model classes.

⋔ Inheritance diagram

graph TD
  3923371969["itemmodels.NestedModel"]
  2612381175["itemmodels.ListMixin"]
  2751680898["builtins.object"]
  3514613245["itemmodels.ModelMixin"]
  874904086["core.AbstractItemModel"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2612381175 --> 3923371969
  2751680898 --> 2612381175
  3514613245 --> 3923371969
  2751680898 --> 3514613245
  874904086 --> 3923371969
  3882849576 --> 874904086
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  1898060022 --> 874904086
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: ListMixin, ModelMixin, AbstractItemModel

Source code in prettyqt/itemmodels/nestedmodel.py
class NestedModel(itemmodels.ListMixin, itemmodels.ModelMixin, core.AbstractItemModel):
    DEFAULT_FLAGS = (
        constants.DRAG_ENABLED | constants.IS_ENABLED | constants.IS_SELECTABLE
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.root = Root()
        self.items = self.root.children

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        if not index.isValid():
            return constants.NO_FLAGS
        if index.column() in self.SET_DATA:
            return self.DEFAULT_FLAGS | constants.IS_EDITABLE
        return self.DEFAULT_FLAGS

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        if parent.column() > 0:
            return 0
        return (
            len(parent.internalPointer().children)
            if parent.isValid()
            else len(self.items)
        )

    def index(
        self, row: int, column: int, parent: core.ModelIndex | None = None
    ) -> core.ModelIndex:
        parent = parent or core.ModelIndex()
        if not self.hasIndex(row, column, parent):
            return core.ModelIndex()

        parent_item = parent.internalPointer()
        if not parent_item:
            parent_item = self.root

        return self.createIndex(row, column, parent_item.children[row])

    def parent(self, index: core.ModelIndex | None = None) -> core.ModelIndex:
        # hacky way to let the case without any arguments get through.
        # not really nice, a proper dispatch library would be better.
        # functools.singledispatchmethod doesnt work here.
        if index is None:
            return super().parent()  # ty: ignore[invalid-explicit-return]  # QObject.parent()
        if not index.isValid():
            return core.ModelIndex()

        if item := index.internalPointer():
            return (
                core.ModelIndex()
                if item.parent in [self.root, None]
                else self.createIndex(item.parent.row(), 0, item.parent)
            )
        return core.ModelIndex()

    def data_by_index(self, index):
        return index.internalPointer()

:fa-brands-github: Show source on GitHub