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 generate_geometry_model(self,
                                component: Component,
                                pixel_data: PixelData = None):
        """
        Generates a geometry model depending on the type of geometry selected and the current values
        of the line edits that apply to the particular geometry type.
        :return: The generated model.
        """
        if self.CylinderRadioButton.isChecked():
            geometry = component.set_cylinder_shape(
                QVector3D(
                    self.cylinderXLineEdit.value(),
                    self.cylinderYLineEdit.value(),
                    self.cylinderZLineEdit.value(),
                ),
                self.cylinderHeightLineEdit.value(),
                self.cylinderRadiusLineEdit.value(),
                self.unitsLineEdit.text(),
                pixel_data=pixel_data,
            )
            if not geometry:
                show_warning_dialog(
                    "3D vector is zero length in cylinder geometry.", "")
        elif self.boxRadioButton.isChecked():
            component.set_box_shape(
                self.boxLengthLineEdit.value(),
                self.boxWidthLineEdit.value(),
                self.boxHeightLineEdit.value(),
                self.unitsLineEdit.text(),
            )
        elif self.meshRadioButton.isChecked() and self.cad_file_name:
            mesh_geometry = OFFGeometryNoNexus()
            geometry_model = load_geometry(self.cad_file_name,
                                           self.unitsLineEdit.text(),
                                           mesh_geometry)

            # Units have already been used during loading the file, but we store them and file name
            # so we can repopulate their fields in the edit component window
            geometry_model.units = self.unitsLineEdit.text()
            geometry_model.file_path = self.cad_file_name

            component.set_off_shape(
                geometry_model,
                units=self.unitsLineEdit.text(),
                filename=self.fileLineEdit.text(),
                pixel_data=pixel_data,
            )
Пример #3
0
 def open_json_file(self):
     filename = file_dialog(False, "Open File Writer JSON File",
                            JSON_FILE_TYPES)
     if filename:
         reader = JSONReader()
         success = reader.load_model_from_json(filename)
         if reader.warnings:
             show_warning_dialog(
                 "\n".join((json_warning.message
                            for json_warning in reader.warnings)),
                 "Warnings encountered loading JSON",
                 parent=self,
             )
         if success:
             self.model = reader.model
             self._setup_model_signals()
             self._update_views()
Пример #4
0
def add_fields_to_component(component: Component, fields_widget: QListWidget):
    """
    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:
            component.set_field(
                name=widget.name, value=widget.value, dtype=widget.dtype
            )
        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(),
            )
Пример #5
0
    def open_json_file(self):
        filename = file_dialog(False, "Open File Writer JSON File",
                               JSON_FILE_TYPES)
        if filename:
            with open(filename, "r") as json_file:
                json_data = json_file.read()

                try:
                    nexus_file = json_to_nexus(json_data)
                except Exception as exception:
                    show_warning_dialog(
                        "Provided file not recognised as valid JSON",
                        "Invalid JSON",
                        f"{exception}",
                        parent=self,
                    )
                    return

                existing_file = self.instrument.nexus.nexus_file
                if self.instrument.nexus.load_nexus_file(nexus_file):
                    self._update_views()
                    existing_file.close()