Hooked up port editor in the document tree

This commit is contained in:
2026-07-26 17:20:37 +02:00
parent b417d85477
commit 8b45674cd2
9 changed files with 610 additions and 30 deletions

View File

@@ -4,7 +4,8 @@ from dataclasses import dataclass
from PySide6.QtCore import QAbstractItemModel, QModelIndex, Qt, Signal
from bedit_core.models import Document as CoreDocument, ComponentID, Component, GraphImplementation
from bedit_core.models import Component, ComponentID, GraphImplementation
from bedit_core.models import Document as CoreDocument
@dataclass
@@ -36,7 +37,7 @@ class DocumentTreeModel(QAbstractItemModel):
def columnCount(self, _parent: QModelIndex | None = None) -> int:
return 1
def index(self,row: int,column: int,parent: QModelIndex | None = None) -> QModelIndex:
def index(self, row: int, column: int, parent: QModelIndex | None = None) -> QModelIndex:
parent_node = self._node(parent)
if column != 0 or row < 0 or row >= len(parent_node.children):
return QModelIndex()
@@ -60,7 +61,7 @@ class DocumentTreeModel(QAbstractItemModel):
row = grandparent.children.index(parent_node)
return self.createIndex(row, 0, parent_node)
def data(self,index: QModelIndex,role: int = Qt.ItemDataRole.DisplayRole) -> object | None:
def data(self, index: QModelIndex, role: int = Qt.ItemDataRole.DisplayRole) -> object | None:
if not index.isValid():
return None
@@ -70,10 +71,10 @@ class DocumentTreeModel(QAbstractItemModel):
if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole):
return node.name
return None
def setData(self,index: QModelIndex,value: object,role: int = Qt.ItemDataRole.EditRole) -> bool:
def setData(self, index: QModelIndex, value: object, role: int = Qt.ItemDataRole.EditRole) -> bool:
if role != Qt.ItemDataRole.EditRole or not index.isValid():
return False
@@ -99,6 +100,12 @@ class DocumentTreeModel(QAbstractItemModel):
node = index.internalPointer()
return node if isinstance(node, DocumentTreeNode) else self._root
def value(self, index: QModelIndex) -> object | None:
if not index.isValid():
return None
node = index.internalPointer()
return node.value if isinstance(node, DocumentTreeNode) else None
def flags(self, index: QModelIndex) -> Qt.ItemFlag:
flags = super().flags(index)
@@ -108,8 +115,7 @@ class DocumentTreeModel(QAbstractItemModel):
node = index.internalPointer()
# Make the document root node editable
if (isinstance(node, DocumentTreeNode)
and (isinstance(node.value, CoreDocument) or isinstance(node.value, Component))):
if isinstance(node, DocumentTreeNode) and isinstance(node.value, (CoreDocument, Component)):
flags |= Qt.ItemFlag.ItemIsEditable
return flags
@@ -123,26 +129,17 @@ class DocumentTreeModel(QAbstractItemModel):
children=[],
)
# Add itself as a child so the document root is visible in the tree
document_root = DocumentTreeNode(
name = document.name,
value = document,
parent = root,
children = []
)
document_root = DocumentTreeNode(name=document.name, value=document, parent=root, children=[])
root.children.append(document_root)
def _list_children(root: DocumentTreeNode, components: dict[ComponentID, Component]) -> None:
for component_id, component in components.items():
component_node = DocumentTreeNode(
name = component.name,
value = component,
parent = root,
children = []
)
for component in components.values():
component_node = DocumentTreeNode(name=component.name, value=component, parent=root, children=[])
root.children.append(component_node)
if isinstance(component.implementation, GraphImplementation):
_list_children(component_node, component.implementation.graph.components)
_list_children(document_root, document.root)
return root