def add_fields_to_component(component: Group, fields_widget: QListWidget, component_model: NexusTreeModel = None): """ Adds fields from a list widget to a component. :param component: Component to add the field to. :param fields_widget: The field list widget to extract field information such the name and value of each field. """ for i in range(fields_widget.count()): widget = fields_widget.itemWidget(fields_widget.item(i)) try: if not isinstance(widget.value, (Link, Dataset)): stream_module = deepcopy(widget.value) stream_module.parent_node = component component.children.append(stream_module) else: component[widget.name] = widget.value except ValueError as error: show_warning_dialog( f"Warning: field {widget.name} not added", title="Field invalid", additional_info=str(error), parent=fields_widget.parent().parent(), ) if component_model and component_model.current_nxs_obj[1]: row = component_model.rowCount(component_model.current_nxs_obj[1]) component_model.createIndex(row, 0, component_model.current_nxs_obj[1])
def set_up_model(self, model: Model): model.signals.group_edited.connect( self.component_tree_view.collapse_group_in_tree) self.component_model = NexusTreeModel(model) self.component_delegate = ComponentEditorDelegate( self.component_tree_view, model) self.component_tree_view.setItemDelegate(self.component_delegate) self.component_tree_view.setModel(self.component_model) self.parameters_widget.set_up_model(model)
def add_link_at_index( component_model: ComponentTreeModel, component_tree_view: QTreeView, component_index: QModelIndex, ): """ Adds a link to the component model at a given index. :param component_model: The component model. :param component_tree_view: The component tree view. :param component_index: The QModelIndex that is selected when the link is added. """ component_tree_view.setCurrentIndex(component_index) component_model.add_link(component_index)
def add_transformation_at_index( component_model: ComponentTreeModel, component_tree_view: QTreeView, component_index: QModelIndex, ): """ Adds a transformation to the component model at a given index. :param component_model: The component model. :param component_tree_view: The component tree view. :param component_index: The QModelIndex that is selected when the transformation is added. """ component_tree_view.setCurrentIndex(component_index) component_model.add_transformation(component_index, TransformationType.TRANSLATION)
def add_transformation( transformation_type: str, component_tree_view: QTreeView, component_model: NexusTreeModel, ): selected = component_tree_view.selectedIndexes() if len(selected) > 0: current_index = selected[0] if transformation_type == TransformationType.TRANSLATION: component_model.add_translation(current_index) elif transformation_type == TransformationType.ROTATION: component_model.add_rotation(current_index) else: raise ValueError( f"Unknown transformation type: {transformation_type}") expand_transformation_list(current_index, component_tree_view, component_model)
def expand_transformation_list( node: QModelIndex, component_tree_view: QTreeView, component_model: NexusTreeModel, ): current_pointer = node.internalPointer() if isinstance(current_pointer, TransformationsList) or isinstance( current_pointer, Component): component_tree_view.expand(node) if isinstance(current_pointer, Component): trans_list_index = component_model.index(1, 0, node) component_tree_view.expand(trans_list_index) else: component_index = component_model.parent(node) component_tree_view.expand(component_index) elif isinstance(current_pointer, Transformation): trans_list_index = component_model.parent(node) component_tree_view.expand(trans_list_index) component_index = component_model.parent(trans_list_index) component_tree_view.expand(component_index)
def get_transformation_or_link_index( component_model: ComponentTreeModel, component_tree_view: QTreeView, transformation_list_index: QModelIndex, ) -> QModelIndex: """ Retrieves the index of a component's first transformation or link. :param component_model: The component tree model. :param component_tree_view: The component tree view. :param transformation_list_index: The index of the component's transformation list. :return: The index of the component's first transformation/link. """ component_tree_view.expand(transformation_list_index) return component_model.index(0, 0, transformation_list_index)
def get_transformation_list_index( component_model: ComponentTreeModel, component_tree_view: QTreeView, component_index: QModelIndex, ) -> QModelIndex: """ Retrieves the index of a component's transformation list from the component tree view. :param component_model: The component model. :param component_tree_view: The component tree view. :param component_index: The index of the component. :return: The index of the component's transformation list. """ component_tree_view.expand(component_index) return component_model.index(1, 0, component_index)
class ComponentTreeViewTab(QWidget): def __init__(self, scene_widget: InstrumentView, parent=None): super().__init__() self.setLayout(QVBoxLayout()) self.setParent(parent) self.componentsTabLayout = QVBoxLayout() self.component_tree_view = QNexusTreeView() self.parameters_widget = ParametersView(parent) self.componentsTabLayout.addWidget(self.parameters_widget) self.componentsTabLayout.addWidget(self.component_tree_view) self.layout().addLayout(self.componentsTabLayout) self.sceneWidget = scene_widget self.component_tree_view.setDragEnabled(True) self.component_tree_view.setAcceptDrops(True) self.component_tree_view.setDropIndicatorShown(True) self.component_tree_view.header().hide() self.component_tree_view.updateEditorGeometries() self.component_tree_view.updateGeometries() self.component_tree_view.updateGeometry() self.component_tree_view.clicked.connect(self._set_button_state) self.component_tree_view.setSelectionMode( QAbstractItemView.SingleSelection) self.component_tool_bar = QToolBar("Actions", self) self.new_component_action = create_and_add_toolbar_action( "new_component.png", "Group", self.parent().show_add_component_dialog, self.component_tool_bar, self, False, ) self.new_translation_action = create_and_add_toolbar_action( "new_translation.png", "Translation", lambda: self._add_transformation(TransformationType.TRANSLATION), self.component_tool_bar, self, ) self.new_rotation_action = create_and_add_toolbar_action( "new_rotation.png", "Rotation", lambda: self._add_transformation(TransformationType.ROTATION), self.component_tool_bar, self, ) self.create_link_action = create_and_add_toolbar_action( "create_link.png", "Link", self.on_create_link, self.component_tool_bar, self, ) self.edit_component_action = create_and_add_toolbar_action( "edit_component.png", "Edit", self.parent().show_edit_component_dialog, self.component_tool_bar, self, ) self.zoom_action = create_and_add_toolbar_action( "zoom.svg", "Zoom", self.on_zoom_item, self.component_tool_bar, self, ) self.component_tool_bar.insertSeparator(self.zoom_action) self.spacer = QWidget() self.spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) self.component_tool_bar.addWidget(self.spacer) self.delete_action = create_and_add_toolbar_action( "delete.png", "Delete", self.on_delete_item, self.component_tool_bar, self) self.component_tool_bar.insertSeparator(self.delete_action) self.componentsTabLayout.insertWidget(0, self.component_tool_bar) def set_up_model(self, model: Model): model.signals.group_edited.connect( self.component_tree_view.collapse_group_in_tree) self.component_model = NexusTreeModel(model) self.component_delegate = ComponentEditorDelegate( self.component_tree_view, model) self.component_tree_view.setItemDelegate(self.component_delegate) self.component_tree_view.setModel(self.component_model) self.parameters_widget.set_up_model(model) def reset_model(self): self.set_up_model(self.component_model.model) def _set_button_state(self): set_button_states( self.component_tree_view, self.new_component_action, self.delete_action, self.new_rotation_action, self.new_translation_action, self.create_link_action, self.zoom_action, self.edit_component_action, ) def on_create_link(self): selected = self.component_tree_view.selectedIndexes() if len(selected) > 0: self.component_model.add_link(selected[0]) self._expand_transformation_list(selected[0]) self._set_button_state() def _expand_transformation_list(self, node: QModelIndex): expand_transformation_list(node, self.component_tree_view, self.component_model) def _add_transformation(self, transformation_type: str): add_transformation(transformation_type, self.component_tree_view, self.component_model) self._set_button_state() def on_delete_item(self): selected = self.component_tree_view.selectedIndexes() if len(selected[0].data().parent_node.children) == 1: new_selection_index = selected[0].parent() elif selected[0].row() > 0: new_selection_index = self.component_model.parent( selected[0]).child(selected[0].row() - 1, 0) elif selected[-1].row() <= len( selected[-1].data().parent_node.children) - 1: new_selection_index = self.component_model.parent( selected[-1]).child(selected[-1].row(), 0) else: new_selection_index = selected[0].parent() for item in selected: self.component_model.remove_node(item) self.component_tree_view.setCurrentIndex(new_selection_index) self._set_button_state() def on_zoom_item(self): selected = self.component_tree_view.selectedIndexes()[0] component = selected.internalPointer() self.sceneWidget.zoom_to_component( self.sceneWidget.get_entity(component.name), self.sceneWidget.view.camera())