Icon in document tree

This commit is contained in:
2026-07-27 15:15:13 +02:00
parent 54788583a4
commit a4daaa8798
5 changed files with 191 additions and 23 deletions

View File

@@ -2,18 +2,21 @@ from collections.abc import Callable
from functools import partial
from typing import Protocol
from PySide6.QtCore import QObject, QPoint, Qt
from PySide6.QtWidgets import QDialog, QMenu
from PySide6.QtCore import QObject, QPoint, QSize, Qt
from PySide6.QtWidgets import QDialog, QHeaderView, QMenu
from bedit_core.models import Component, Port, PortID, Parameter, ParameterID
from bedit_core.models import Component, ComponentID, GraphImplementation, Port, PortID, Parameter, ParameterID
from bedit_core.models import Document as CoreDocument
from bedit_gui.documents import Document
from bedit_gui.models import Icon
from bedit_gui.views.dialogs.interface_editor_dialog import InterfaceEditorDialog
from bedit_gui.views.dialogs.param_editor_dialog import ParamEditorDialog
from bedit_gui.views.icon_editor_window import IconEditorWindow
from bedit_gui.views.main_window import MainWindow
from bedit_gui.views.models.document_tree_model import DocumentTreeModel
from bedit_gui.utils.icon import render_icon
ICON_SIZE = QSize(32, 32)
class InterfaceEditorLike(Protocol):
def exec(self) -> int: ...
@@ -51,13 +54,20 @@ class DocumentTreeController(QObject):
self.interface_editor_factory = interface_editor_factory
self.param_editor_factory = param_editor_factory
self._icon_editors: list[IconEditorWindow] = []
self._components: dict[ComponentID, Component] = {}
window.ui.documentTree.setModel(self.model)
document.model_changed.connect(self._on_document_changed)
document.icon_changed.connect(self._on_icon_changed)
self.model.rename_document_requested.connect(self.document.rename)
self.model.rename_component_requested.connect(self.document.rename_component)
window.ui.documentTree.setHeaderHidden(True)
window.ui.documentTree.setIconSize(QSize(48, 48))
window.ui.documentTree.header().setStretchLastSection(False)
window.ui.documentTree.header().setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch)
window.ui.documentTree.header().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed)
window.ui.documentTree.setColumnWidth(1, 56)
window.ui.documentTree.setContextMenuPolicy(
Qt.ContextMenuPolicy.CustomContextMenu
)
@@ -70,11 +80,28 @@ class DocumentTreeController(QObject):
def _on_document_changed(self, model: CoreDocument) -> None:
"""Rebuild the tree whenever New/Open replaces the core document."""
self.model.set_document(model)
self._components = {}
self._collect_components(model.root)
for component_id, component in self._components.items():
icon = self.document.component_icon(component_id)
self.model.set_component_icon(component_id, render_icon(icon, component.interface.ports, ICON_SIZE))
# Optional presentation behavior. Later, you could instead remember
# expanded component IDs and restore only those nodes.
self.window.ui.documentTree.expandAll()
def _on_icon_changed(self, component_id: ComponentID, icon: object) -> None:
component = self._components.get(component_id)
if component is None:
return
self.model.set_component_icon(component_id, render_icon(icon if isinstance(icon, Icon) else Icon(), component.interface.ports, ICON_SIZE))
def _collect_components(self, components: dict[ComponentID, Component]) -> None:
for component_id, component in components.items():
self._components[component_id] = component
if isinstance(component.implementation, GraphImplementation):
self._collect_components(component.implementation.graph.components)
def _show_context_menu(self, position: QPoint) -> None:
index = self.window.ui.documentTree.indexAt(position)
component = self.model.value(index)