def update_settings(self):
        """Updates ElementSimulation settings and saves them into a file.

        Return:
            boolean that indicates whether the dialog can be closed.
        """
        if not self.sim_widget.fields_are_valid:
            QtWidgets.QMessageBox.critical(self, "Warning",
                                           "Some of the setting values have"
                                           " not been set.\n" +
                                           "Please input values in fields "
                                           "indicated in red.",
                                           QtWidgets.QMessageBox.Ok,
                                           QtWidgets.QMessageBox.Ok)
            return False

        # TODO could compare default element simulation and this element
        #  simulation to see if reset is actually needed
        settings_changed = \
            not self.use_default_settings \
            and (self.sim_widget.are_values_changed() or self.are_values_changed())
        if self.use_request_settings_toggled() or settings_changed:
            simulation = self.element_simulation.simulation

            if self.use_default_settings:
                msg = "request settings"
            else:
                msg = "element specific settings"

            def filter_func(elem_sim):
                # Filter out other element simulations than the one used by this
                # dialog
                return elem_sim is self.element_simulation

            if not df.delete_element_simulations(
                self, simulation, msg=msg, tab=self.tab,
                filter_func=filter_func
            ):
                return False

        self.element_simulation.use_default_settings = self.use_default_settings
        if self.use_default_settings:
            self.element_simulation.clone_request_settings()
        else:
            self.sim_widget.update_settings()

        if self.element_simulation.name != self.sim_widget.name:
            # Remove current simu file if name has been changed
            self.element_simulation.remove_files()

        # TODO remove files with old name, if name has changed

        self.element_simulation.to_file()

        return True
Beispiel #2
0
    def __accept_settings(self):
        """Function for accepting the current settings and closing the dialog
        window.
        """
        if not self.fields_are_valid:
            if self.amount_mismatch:
                hint = "(Hint: element amounts need to sum up to either 1 or " \
                       "100.)"
            else:
                hint = ""
            QtWidgets.QMessageBox.critical(self, "Warning",
                                           "Some of the parameter values have"
                                           " not been set.\n\n" +
                                           "Please input values in fields "
                                           "indicated in red.\n" + hint,
                                           QtWidgets.QMessageBox.Ok,
                                           QtWidgets.QMessageBox.Ok)
            self.__close = False
            return

        if self.layer and not (self.are_values_changed() or
                               self.elements_changed()):
            self.__close = True
            self.fields_are_valid = True
            self.ok_pressed = False  # No update needed
            return

        if self.simulation is not None:
            if not df.delete_element_simulations(self,
                                                 self.simulation,
                                                 tab=self.tab,
                                                 msg="target"):
                self.__close = False
                return

        elements = self.find_elements()

        if self.layer:
            self.layer.name = self.name
            self.layer.elements = elements
            self.layer.thickness = self.thickness
            self.layer.density = self.density
        else:
            self.layer = Layer(self.name, elements, self.thickness,
                               self.density)
        if self.comboBox.currentText().startswith("Under"):
            self.placement_under = True
        else:
            self.placement_under = False
        self.ok_pressed = True
        self.__close = True
Beispiel #3
0
    def __delete_layer(self):
        """
        Delete selected layer.
        """
        reply = QtWidgets.QMessageBox.question(
            self, "Confirmation", "Are you sure you want to "
            "delete selected layer?", QtWidgets.QMessageBox.Yes
            | QtWidgets.QMessageBox.No | QtWidgets.QMessageBox.Cancel,
            QtWidgets.QMessageBox.Cancel)
        if reply == QtWidgets.QMessageBox.No or reply == \
                QtWidgets.QMessageBox.Cancel:
            return

        # If clicked Yes, then check if current simulation has any running or
        # finished element simulations and confirm whether these should be
        # deleted as well.
        if self.simulation is not None:
            if not df.delete_element_simulations(
                    self, self.simulation, tab=self.parent.tab, msg="target"):
                return

        # Delete from layers list
        if self.__selected_layer in self.layers:
            self.layers.remove(self.__selected_layer)
        # Remove as selected and remove selector
        self.__layer_selector.set_visible(False)
        self.__layer_selector = None
        self.__selected_layer = None
        # Update layer start depths
        self.update_start_depths()

        # Update canvas
        self.__update_figure(zoom_to_bottom=True)

        if self.simulation and not self.layers:
            self.parent.recoilRadioButton.setEnabled(False)
Beispiel #4
0
    def __update_settings(self):
        """Reads values from Request Settings dialog and updates them in
        default objects.
        """
        if self.measurement_settings_widget.isotopeComboBox.currentIndex()\
                == -1:
            QtWidgets.QMessageBox.critical(
                self, "Warning", "No isotope selected.\n\n"
                "Please select an isotope for the beam element.",
                QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
            return False

        # Check the target and detector angles
        if not self.measurement_settings_widget.check_angles():
            return False

        if not self.tabs.currentWidget().fields_are_valid:
            QtWidgets.QMessageBox.critical(
                self, "Warning",
                "Some of the setting values have not been set.\n"
                "Please input values in fields indicated in red.",
                QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
            return False

        if self.values_changed():
            if not self.simulation_settings_widget.are_values_changed():
                filter_func = lambda e: e.simulation.use_request_settings
            else:
                filter_func = lambda e: e.use_default_settings
            if not df.delete_element_simulations(self,
                                                 self.request,
                                                 msg="request settings",
                                                 filter_func=filter_func):
                return False

        try:
            self.measurement_settings_widget.update_settings()
            self.profile_settings_widget.update_settings()

            measurement_file = Path(self.request.default_measurement.directory,
                                    "Default.measurement")

            self.request.default_measurement.to_file(measurement_file)

            # Detector settings
            self.detector_settings_widget.update_settings()

            # Simulation settings
            self.simulation_settings_widget.update_settings()

            # TODO: Move the rest of this method to request

            # TODO: This overwrites measurement description and name.
            #       It is also the cause of a .measurement file
            #       duplication bug: changing the name in default
            #       request measurement writes two .measurement files
            #       (one with the old name and another with new name).
            self.request.default_simulation.to_file(
                Path(self.request.default_folder, "Default.simulation"))
            self.request.default_element_simulation.to_file(
                Path(self.request.default_folder, "Default.mcsimu"))

            # Update measurements and simulations
            for sample in self.request.samples.samples:
                for measurement in sample.measurements.measurements.values():
                    if measurement.use_request_settings:
                        measurement.clone_request_settings()
                for simulation in sample.simulations.simulations.values():
                    if simulation.use_request_settings:
                        simulation.clone_request_settings()
                    for elem_sim in simulation.element_simulations:
                        if elem_sim.use_default_settings:
                            elem_sim.clone_request_settings()

            # Update all element simulations that use request settings to
            #  have the correct simulation type
            current_sim_type = self.request.default_element_simulation.\
                simulation_type
            if self.original_simulation_type != current_sim_type:
                if current_sim_type == "ERD":
                    rec_type = "rec"
                    rec_suffix_to_delete = ".sct"
                else:
                    rec_type = "sct"
                    rec_suffix_to_delete = ".rec"

                for sample in self.request.samples.samples:
                    for simulation in sample.simulations.simulations.values():
                        for elem_sim in simulation.element_simulations:
                            if elem_sim.use_default_settings:
                                # TODO change to sim.use_req_settings?
                                elem_sim.simulation_type = current_sim_type
                                for recoil in elem_sim.recoil_elements:
                                    try:
                                        recoil.type = rec_type
                                        path_to_rec = Path(
                                            elem_sim.directory,
                                            recoil.prefix + "-" + recoil.name +
                                            rec_suffix_to_delete)
                                        os.remove(path_to_rec)
                                    except OSError:
                                        pass
                                    recoil.to_file(elem_sim.directory)
                                fp = Path(
                                    elem_sim.directory, elem_sim.name_prefix +
                                    "-" + elem_sim.name + ".mcsimu")
                                elem_sim.to_file(fp)

            return True
        except TypeError:
            # TODO: Make a better warning text.
            QtWidgets.QMessageBox.question(
                self, "Warning",
                "Some of the setting values have not been set.\n"
                "Please input setting values to save them.",
                QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
Beispiel #5
0
    def _update_parameters(self):
        """
         Update Simulation's Run, Detector and Target objects. If simulation
         specific parameters are in use, save them into a file.
        """
        if self.measurement_settings_widget.isotopeComboBox.currentIndex() \
                == -1:
            QtWidgets.QMessageBox.critical(
                self, "Warning", "No isotope selected.\n\n"
                "Please select an isotope for the beam element.",
                QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
            return False

        if not self.simulation.measurement_setting_file_name:
            self.simulation.measurement_setting_file_name = \
                self.simulation.name

        if not self.tabs.currentWidget().fields_are_valid:
            QtWidgets.QMessageBox.critical(
                self, "Warning",
                "Some of the setting values have not been set.\n"
                "Please input values in fields indicated in red.",
                QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
            return False

        if self.use_request_settings_toggled() \
                or (not self.use_request_settings and self.values_changed()):
            # User has switched from simulation settings to request settings,
            # or vice versa. Confirm if the user wants to delete old simulations
            # OR
            # If values that require rerunning simulations, prompt user
            # delete previous or currently running simulation results (if those
            # exists)
            if not df.delete_element_simulations(
                    self, self.simulation, tab=self.tab,
                    msg="simulation settings"):
                return False

        # Copy request settings without checking their validity. They
        # have been checked once in request settings anyway.
        if self.use_request_settings:
            self.simulation.use_request_settings = True

            # Remove simulation-specific efficiency files
            if self.simulation.detector is not \
                    self.simulation.request.default_detector:
                self.simulation.detector.remove_efficiency_files()

            self.simulation.clone_request_settings()

            self._remove_extra_files()
            self.simulation.to_file()
            return True

        try:
            # Update simulation settings
            self.simulation.use_request_settings = False

            # Set Detector object to settings widget
            self.detector_settings_widget.obj = self.simulation.detector

            # Update settings
            self.measurement_settings_widget.update_settings()
            self.detector_settings_widget.update_settings()

            self._remove_extra_files()
            self.simulation.to_file()
            return True

        except TypeError:
            QtWidgets.QMessageBox.question(
                self, "Warning",
                "Some of the setting values have not been set.\n"
                "Please input setting values to save them.",
                QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)

        return False