def test_GIVEN_component_with_cylindrical_shape_information_WHEN_duplicating_component_THEN_shape_information_is_stored_in_nexus_file( nexus_wrapper, ): instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS) first_component_name = "component1" first_component_nx_class = "NXdetector" description = "desc" first_component = instrument.create_component(first_component_name, first_component_nx_class, description) axis_direction = QVector3D(1, 0, 0) height = 2 radius = 3 units = "cm" first_component.set_cylinder_shape(axis_direction=axis_direction, height=height, radius=radius, units=units) tree_model = ComponentTreeModel(instrument) first_component_index = tree_model.index(0, 0, QModelIndex()) tree_model.duplicate_node(first_component_index) assert tree_model.rowCount(QModelIndex()) == 3 second_component_index = tree_model.index(2, 0, QModelIndex()) second_component = second_component_index.internalPointer() second_shape, _ = second_component.shape assert second_shape.axis_direction == axis_direction assert second_shape.height == height assert second_shape.units == units
def test_duplicate_component(): data_under_test = Instrument( NexusWrapper("test_component_model_duplicate"), NX_CLASS_DEFINITIONS) under_test = ComponentTreeModel(data_under_test) assert under_test.rowCount(QModelIndex()) == 1 # Sample under_test.add_component(get_component()) component_index = under_test.index(0, 0, QModelIndex()) under_test.duplicate_node(component_index) assert under_test.rowCount(QModelIndex()) == 3
def test_duplicate_transform_fail(): data_under_test = Instrument( NexusWrapper("test_component_model_duplicate_fail"), NX_CLASS_DEFINITIONS) under_test = ComponentTreeModel(data_under_test) under_test.add_component(get_component()) component_index = under_test.index(0, 0, QModelIndex()) under_test.add_rotation(component_index) transformation_list_index = under_test.index(1, 0, component_index) transformation_index = under_test.index(0, 0, transformation_list_index) try: under_test.duplicate_node(transformation_index) except (NotImplementedError, AttributeError): return # Success assert False # Failure
def test_GIVEN_component_with_off_shape_information_WHEN_duplicating_component_THEN_shape_information_is_stored_in_nexus_file( ): wrapper = NexusWrapper("test_duplicate_off_shape") instrument = Instrument(wrapper, NX_CLASS_DEFINITIONS) first_component_name = "component1" first_component_nx_class = "NXdetector" description = "desc" first_component = instrument.create_component(first_component_name, first_component_nx_class, description) vertices = [ QVector3D(-0.5, -0.5, 0.5), QVector3D(0.5, -0.5, 0.5), QVector3D(-0.5, 0.5, 0.5), QVector3D(0.5, 0.5, 0.5), QVector3D(-0.5, 0.5, -0.5), QVector3D(0.5, 0.5, -0.5), QVector3D(-0.5, -0.5, -0.5), QVector3D(0.5, -0.5, -0.5), ] faces = [ [0, 1, 3, 2], [2, 3, 5, 4], [4, 5, 7, 6], [6, 7, 1, 0], [1, 7, 5, 3], [6, 0, 2, 4], ] first_component.set_off_shape( OFFGeometryNoNexus(vertices=vertices, faces=faces)) tree_model = ComponentTreeModel(instrument) first_component_index = tree_model.index(0, 0, QModelIndex()) tree_model.duplicate_node(first_component_index) assert tree_model.rowCount(QModelIndex()) == 3 second_component_index = tree_model.index(2, 0, QModelIndex()) second_component = second_component_index.internalPointer() second_shape, _ = second_component.shape assert second_shape.vertices == vertices assert second_shape.faces == faces
class ComponentTreeViewTab(QWidget): def __init__(self, parent=None): super().__init__() self.setLayout(QVBoxLayout()) self.setParent(parent) self.componentsTabLayout = QVBoxLayout() self.component_tree_view = QTreeView() self.componentsTabLayout.addWidget(self.component_tree_view) self.layout().addLayout(self.componentsTabLayout) 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", "New Component", self.parent().show_add_component_window, self.component_tool_bar, self, True, ) self.new_translation_action = create_and_add_toolbar_action( "new_translation.png", "New Translation", lambda: self._add_transformation(TransformationType.TRANSLATION), self.component_tool_bar, self, ) self.new_rotation_action = create_and_add_toolbar_action( "new_rotation.png", "New Rotation", lambda: self._add_transformation(TransformationType.ROTATION), self.component_tool_bar, self, ) self.create_link_action = create_and_add_toolbar_action( "create_link.png", "Create Link", self.on_create_link, self.component_tool_bar, self, ) self.duplicate_action = create_and_add_toolbar_action( "duplicate.png", "Duplicate", self.on_duplicate_node, self.component_tool_bar, self, ) self.edit_component_action = create_and_add_toolbar_action( "edit_component.png", "Edit Component", self.parent().show_edit_component_dialog, self.component_tool_bar, self, ) self.delete_action = create_and_add_toolbar_action( "delete.png", "Delete", self.on_delete_item, self.component_tool_bar, self ) self.zoom_action = create_and_add_toolbar_action( "zoom.svg", "Zoom To Component", self.on_zoom_item, self.component_tool_bar, self, ) self.component_tool_bar.insertSeparator(self.zoom_action) self.componentsTabLayout.insertWidget(0, self.component_tool_bar) def set_up_model(self, instrument): self.component_model = ComponentTreeModel(instrument) self.component_delegate = ComponentEditorDelegate( self.component_tree_view, instrument ) self.component_tree_view.setItemDelegate(self.component_delegate) self.component_tree_view.setModel(self.component_model) def _set_button_state(self): set_button_states( self.component_tree_view, self.delete_action, self.duplicate_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 on_duplicate_node(self): selected = self.component_tree_view.selectedIndexes() if len(selected) > 0: self.component_model.duplicate_node(selected[0]) self._expand_transformation_list(selected[0]) def _expand_transformation_list(self, node: QModelIndex): expand_transformation_list(node, self.component_tree_view, self.component_model) def _add_transformation(self, transformation_type: TransformationType): add_transformation( transformation_type, self.component_tree_view, self.component_model ) def on_delete_item(self): selected = self.component_tree_view.selectedIndexes() for item in selected: self.component_model.remove_node(item) 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() )