Library tree filled with items

This commit is contained in:
2026-08-17 17:47:26 +02:00
parent 8d37b2441a
commit b1f453a6df
5 changed files with 115 additions and 16 deletions

View File

@@ -22,19 +22,21 @@ class DocumentTreeModel(QAbstractItemModel):
rename_document_requested = Signal(str)
rename_component_requested = Signal(Component, str)
def __init__(self) -> None:
def __init__(self, editable: bool = True) -> None:
super().__init__()
self._document: CoreDocument | None = None
self._editable = editable
self._root = DocumentTreeNode("Document", None, None, None, [])
self._component_icons: dict[ComponentID, QIcon] = {}
self._component_nodes: dict[ComponentID, DocumentTreeNode] = {}
def set_document(self, document: CoreDocument) -> None:
self.set_documents([document])
def set_documents(self, documents: list[CoreDocument]) -> None:
self.beginResetModel()
self._document = document
self._component_icons = {}
self._component_nodes = {}
self._root = self._build_tree(document)
self._root = self._build_tree(documents)
self.endResetModel()
def set_component_icon(self, component_id: ComponentID, icon: QIcon) -> None:
@@ -146,12 +148,12 @@ class DocumentTreeModel(QAbstractItemModel):
node = index.internalPointer()
# Make the document root node editable
if index.column() == 0 and isinstance(node, DocumentTreeNode) and isinstance(node.value, (CoreDocument, Component)):
if self._editable and index.column() == 0 and isinstance(node, DocumentTreeNode) and isinstance(node.value, (CoreDocument, Component)):
flags |= Qt.ItemFlag.ItemIsEditable
return flags
def _build_tree(self, document: CoreDocument) -> DocumentTreeNode:
def _build_tree(self, documents: list[CoreDocument]) -> DocumentTreeNode:
# QT's invisible root
root = DocumentTreeNode(
name="",
@@ -160,19 +162,18 @@ class DocumentTreeModel(QAbstractItemModel):
parent=None,
children=[],
)
# Add itself as a child so the document root is visible in the tree
document_root = DocumentTreeNode(name=document.name, value=document, component_id=None, parent=root, children=[])
root.children.append(document_root)
def _list_children(root: DocumentTreeNode, components: dict[ComponentID, Component]) -> None:
def _list_children(parent: DocumentTreeNode, components: dict[ComponentID, Component]) -> None:
for component_id, component in sorted(components.items(), key=lambda item: (item[1].name.casefold(), item[1].name, str(item[0]))):
component_node = DocumentTreeNode(name=component.name, value=component, component_id=component_id, parent=root, children=[])
root.children.append(component_node)
component_node = DocumentTreeNode(name=component.name, value=component, component_id=component_id, parent=parent, children=[])
parent.children.append(component_node)
self._component_nodes[component_id] = component_node
if isinstance(component.implementation, GraphImplementation):
_list_children(component_node, component.implementation.graph.components)
_list_children(document_root, document.root)
for document in documents:
document_root = DocumentTreeNode(name=document.name, value=document, component_id=None, parent=root, children=[])
root.children.append(document_root)
_list_children(document_root, document.root)
return root