Python: Begin docs for model

This requires use of the pdoc API to work around an issue with the base methods not showing up in the docs.
This commit is contained in:
Simon Hausmann 2025-03-22 15:52:20 +01:00 committed by Simon Hausmann
parent d420bd8113
commit a313916ce6
4 changed files with 41 additions and 7 deletions

View file

@ -1,2 +1,3 @@
slint/*.so
uv.lock
docs

21
api/python/build_docs.py Normal file
View file

@ -0,0 +1,21 @@
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import slint
import pdoc
import os
if __name__ == "__main__":
doc = pdoc.doc.Module(slint)
model_cls = doc.get("Model")
for method in model_cls.inherited_members[("builtins", "PyModelBase")]:
method.is_inherited = False
if not method.name.startswith("_") and method.name != "init_self":
model_cls.own_members.append(method)
out = pdoc.render.html_module(module=doc, all_modules={"foo": doc})
os.makedirs("docs", exist_ok=True)
with open("docs/index.html", "w") as f:
f.write(out)

View file

@ -3,6 +3,7 @@
from . import slint as native
from collections.abc import Iterable
from abc import abstractmethod
import typing
from typing import Any, cast, Iterator
@ -32,21 +33,32 @@ class Model[T](native.PyModelBase, Iterable[T]):
def __iter__(self) -> Iterator[T]:
return ModelIterator(self)
def set_row_data(self, row: int, value: T) -> None:
"""Call this method on mutable models to change the data for the given row.
The UI will also call this method when modifying a model's data.
Re-implement this method in a sub-class to handle the change."""
super().set_row_data(row, value)
@abstractmethod
def row_data(self, row: int) -> typing.Optional[T]:
"""Returns the data for the given row.
Re-implement this method in a sub-class to provide the data."""
return cast(T, super().row_data(row))
def notify_row_changed(self, row: int) -> None:
"""Call this method from a sub-class to notify the views that a row has changed."""
super().notify_row_changed(row)
def notify_row_removed(self, row: int, count: int) -> None:
"""Call this method from a sub-class to notify the views that
`count` rows have been removed starting at `row`."""
super().notify_row_removed(row, count)
def notify_row_added(self, row: int, count: int) -> None:
"""Call this method from a sub-class to notify the views that
`count` rows have been added starting at `row`."""
super().notify_row_added(row, count)
def set_row_data(self, row: int, value: T) -> None:
super().set_row_data(row, value)
def row_data(self, row: int) -> typing.Optional[T]:
return cast(T, super().row_data(row))
class ListModel[T](Model[T]):
"""ListModel is a `Model` that stores its data in a Python list.