"""
Function table browser widget using AbstractTableBrowser.
Displays function metadata in a searchable table with static columns.
Used as the table portion of FunctionSelectorDialog.
"""
from enum import Enum
from typing import ClassVar, List, Optional, Protocol, Sequence
from pyqt_reactive.theming import ColorScheme
from pyqt_reactive.widgets.shared.abstract_table_browser import (
AbstractTableBrowser, ColumnDef, TableSelectionMode
)
[docs]
class FunctionTableRow(Protocol):
"""Structural contract for function metadata shown in the selector table."""
name: str
module: str
contract: object
tags: Sequence[str]
doc: str
display_name: str
[docs]
def get_memory_type(self) -> str: ...
[docs]
def get_registry_name(self) -> str: ...
[docs]
class FunctionTableBrowser(AbstractTableBrowser[FunctionTableRow]):
"""
Table browser for function metadata.
Static columns: Name, Module, Backend, Registry, Contract, Tags, Description
Single-select mode.
"""
# Column widths
MODULE_WIDTH = 250
DESCRIPTION_WIDTH = 300
COLUMN_SPECS: ClassVar[tuple[tuple[str, str, int], ...]] = (
("Name", "name", 150),
("Module", "module", MODULE_WIDTH),
("Backend", "backend", 80),
("Registry", "registry", 80),
("Contract", "contract", 100),
("Tags", "tags", 100),
("Description", "doc", DESCRIPTION_WIDTH),
)
[docs]
def __init__(self, color_scheme: Optional[ColorScheme] = None, parent=None):
super().__init__(
color_scheme=color_scheme,
selection_mode=TableSelectionMode.SINGLE,
parent=parent,
)
@staticmethod
def _contract_display_name(contract: object, *, unknown_label: str) -> str:
if contract is None:
return unknown_label
if isinstance(contract, Enum):
return contract.name
return str(contract)
[docs]
def get_columns(self) -> List[ColumnDef]:
"""Static column definitions for function table."""
return [
ColumnDef(name=name, key=key, width=width)
for name, key, width in self.COLUMN_SPECS
]
[docs]
def get_searchable_text(self, item: FunctionTableRow) -> str:
"""Return searchable text for function metadata."""
contract_name = self._contract_display_name(item.contract, unknown_label="")
return " ".join([
item.display_name,
item.name,
item.module,
contract_name,
" ".join(item.tags),
item.doc,
])
[docs]
def get_search_placeholder(self) -> str:
return "Search functions by name, module, contract, or tags..."