コード例 #1
0
def test_get_data_fail():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    under_test.createIndex(0, 0, data_under_test[0])

    assert under_test.data(QModelIndex(), Qt.DisplayRole) is None
コード例 #2
0
def test_component_has_2_rows():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, data_under_test[0])

    assert under_test.rowCount(test_index) == 2
コード例 #3
0
def test_get_data_success_2():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, data_under_test[0])

    assert under_test.data(test_index, Qt.SizeHintRole) is None
コード例 #4
0
def test_get_flags_component():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    index = under_test.createIndex(0, 0, data_under_test[0])

    assert under_test.flags(index) == (Qt.ItemIsEnabled | Qt.ItemIsSelectable)
コード例 #5
0
def test_number_of_components_2():
    data_under_test = FakeInstrument([get_component(), get_component()])
    under_test = ComponentTreeModel(data_under_test)

    test_index = QModelIndex()

    assert under_test.rowCount(test_index) == 2
コード例 #6
0
 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)
コード例 #7
0
def test_get_component_parent():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, data_under_test[0])

    assert under_test.parent(test_index) == QModelIndex()
コード例 #8
0
def test_get_default_parent():
    data_under_test = FakeInstrument()
    under_test = ComponentTreeModel(data_under_test)

    test_index = QModelIndex()

    assert under_test.parent(test_index) == QModelIndex()
コード例 #9
0
def test_get_invalid_index():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    test_index = QModelIndex()

    assert under_test.index(2, 0, test_index) == QModelIndex()
コード例 #10
0
def test_get_flags_fail():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    under_test.createIndex(0, 0, data_under_test[0])

    assert under_test.flags(QModelIndex()) is Qt.NoItemFlags
コード例 #11
0
def test_get_flags_component_info():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    item = ComponentInfo(parent=data_under_test[0])
    index = under_test.createIndex(0, 0, item)

    assert under_test.flags(index) == Qt.ItemIsEnabled
コード例 #12
0
def test_rowCount_gets_unknown_type():
    data_under_test = FakeInstrument()
    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, {})

    with pytest.raises(RuntimeError):
        under_test.rowCount(test_index)
コード例 #13
0
def test_get_flags_transformation_list():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    component = data_under_test[0]
    component.stored_transforms = component.transforms
    index = under_test.createIndex(0, 0, component.stored_transforms)

    assert under_test.flags(index) == Qt.ItemIsEnabled | Qt.ItemIsSelectable
コード例 #14
0
def test_add_transformation_alt_1():
    data_under_test = FakeInstrument([])
    under_test = ComponentTreeModel(data_under_test)

    under_test.add_component(get_component())
    component_index = under_test.index(0, 0, QModelIndex())
    transformation_list_index = under_test.index(1, 0, component_index)
    assert under_test.rowCount(transformation_list_index) == 0
    under_test.add_translation(transformation_list_index)
    assert under_test.rowCount(transformation_list_index) == 1
コード例 #15
0
def test_transformation_list_has_0_rows():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    data_under_test[0].stored_transforms = data_under_test[0].transforms

    test_index = under_test.createIndex(0, 0,
                                        data_under_test[0].stored_transforms)

    assert under_test.rowCount(test_index) == 0
コード例 #16
0
def test_get_component_info_parent():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    # Creating ComponentInfo in-line causes a segmentation error
    temp_component_info = ComponentInfo(parent=data_under_test[0])
    test_index = under_test.createIndex(0, 0, temp_component_info)

    assert under_test.parent(
        test_index).internalPointer() is data_under_test[0]
コード例 #17
0
def test_transformation_link_has_0_rows():
    component = get_component()
    translation = component.add_translation(QVector3D(1.0, 0.0, 0.0))
    component.depends_on = translation
    data_under_test = FakeInstrument([component])
    component.stored_transforms = component.transforms
    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, component.stored_transforms[0])

    assert under_test.rowCount(test_index) == 0
コード例 #18
0
def test_add_link_alt_2():
    data_under_test = FakeInstrument([])
    under_test = ComponentTreeModel(data_under_test)

    under_test.add_component(get_component())
    component_index = under_test.index(0, 0, QModelIndex())
    transformation_list_index = under_test.index(1, 0, component_index)
    assert under_test.rowCount(transformation_list_index) == 0
    under_test.add_link(transformation_list_index)
    assert under_test.rowCount(transformation_list_index) == 1
    assert transformation_list_index.internalPointer().has_link
    assert len(transformation_list_index.internalPointer()) == 0
コード例 #19
0
def test_get_flags_other():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    class TestObject:
        pass

    test_item = TestObject()
    index = under_test.createIndex(0, 0, test_item)

    assert (under_test.flags(index) == Qt.ItemIsEnabled | Qt.ItemIsSelectable
            | Qt.ItemIsEditable)
コード例 #20
0
def test_get_transform_list_parent():
    data_under_test = FakeInstrument([get_component()])
    under_test = ComponentTreeModel(data_under_test)

    data_under_test[0].stored_transforms = data_under_test[0].transforms

    test_index = under_test.createIndex(0, 0,
                                        data_under_test[0].stored_transforms)

    temp_parent = under_test.parent(test_index)

    assert temp_parent.internalPointer() is data_under_test[0]
    assert temp_parent.row() == 0
コード例 #21
0
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)
コード例 #22
0
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
コード例 #23
0
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)
コード例 #24
0
def test_remove_component_with_transformation(nexus_wrapper):
    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)
    under_test = ComponentTreeModel(instrument)
    instrument.create_component("Some name", "some class", "desc")
    component_index = under_test.index(0, 0, QModelIndex())
    under_test.add_rotation(component_index)
    assert under_test.rowCount(QModelIndex()) == 1
    under_test.remove_node(component_index)
    assert under_test.rowCount(QModelIndex()) == 0, (
        "Expected component to be successfully deleted because it has "
        "a transformation that only has it as a dependent")
コード例 #25
0
def add_transformation(
    transformation_type: TransformationType,
    component_tree_view: QTreeView,
    component_model: ComponentTreeModel,
):
    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)
コード例 #26
0
def test_get_transformation_parent():
    component = get_component()
    translation = component.add_translation(QVector3D(1.0, 0.0, 0.0))
    component.depends_on = translation
    data_under_test = FakeInstrument([component])
    component.stored_transforms = component.transforms
    translation.parent = component.stored_transforms

    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, translation)

    found_parent = under_test.parent(test_index)
    assert found_parent.internalPointer(
    ) == data_under_test[0].stored_transforms
    assert found_parent.row() == 1
コード例 #27
0
def test_get_transformation_link_parent():
    component = get_component()
    data_under_test = FakeInstrument([component])
    component.stored_transforms = component.transforms
    transform_link = LinkTransformation(component.stored_transforms)
    component.stored_transforms.link = transform_link
    component.stored_transforms.has_link = True

    under_test = ComponentTreeModel(data_under_test)

    test_index = under_test.createIndex(0, 0, transform_link)

    found_parent = under_test.parent(test_index)
    assert found_parent.internalPointer(
    ) == data_under_test[0].stored_transforms
    assert found_parent.row() == 1
コード例 #28
0
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
コード例 #29
0
def test_remove_component(nexus_wrapper):
    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)
    under_test = ComponentTreeModel(instrument)
    instrument.create_component("Some name", "some class", "desc")
    component_index = under_test.index(0, 0, QModelIndex())
    assert under_test.rowCount(QModelIndex()) == 1
    under_test.remove_node(component_index)
    assert under_test.rowCount(QModelIndex()) == 0
コード例 #30
0
def expand_transformation_list(
    node: QModelIndex,
    component_tree_view: QTreeView,
    component_model: ComponentTreeModel,
):
    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)