class CreateAdvancedWidget(CreateTaskBase):
    """Widget used to create advanced collection methods
    """

    def __init__(self, parent=None, name=None, fl=0):
        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "Advanced"
        )

        if not name:
            self.setObjectName("create_advanced_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self._advanced_methods = None
        self._grid_map = {}
        self.spacing = [0, 0]
        self.dc_selected = False

        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._advanced_methods_widget = QtImport.load_ui_file(
            "advanced_methods_layout.ui"
        )
        self._acq_widget = AcquisitionWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )
        self._acq_widget.grid_mode = True

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._advanced_methods_widget)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)
        self._acq_widget.madEnergySelectedSignal.connect(self.mad_energy_selected)

        self._acq_widget.acq_widget_layout.osc_range_ledit.textEdited.connect(
            self.grid_osc_range_ledit_changed
        )
        self._acq_widget.acq_widget_layout.osc_total_range_ledit.textEdited.connect(
            self.grid_osc_total_range_ledit_changed
        )

        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )

        self._advanced_methods_widget.grid_treewidget.itemSelectionChanged.connect(
            self.grid_treewidget_item_selection_changed
        )
        self._advanced_methods_widget.draw_grid_button.clicked.connect(
            self.draw_grid_button_clicked
        )
        self._advanced_methods_widget.remove_grid_button.clicked.connect(
            self.remove_grid_button_clicked
        )
        self._advanced_methods_widget.hor_spacing_ledit.textEdited.connect(
            self.hor_spacing_changed
        )
        self._advanced_methods_widget.ver_spacing_ledit.textEdited.connect(
            self.ver_spacing_changed
        )

        self._advanced_methods_widget.move_right_button.clicked.connect(
            lambda: self.move_grid("right")
        )
        self._advanced_methods_widget.move_left_button.clicked.connect(
            lambda: self.move_grid("left")
        )
        self._advanced_methods_widget.move_up_button.clicked.connect(
            lambda: self.move_grid("up")
        )
        self._advanced_methods_widget.move_down_button.clicked.connect(
            lambda: self.move_grid("down")
        )

        self._advanced_methods_widget.overlay_cbox.toggled.connect(self.overlay_toggled)
        self._advanced_methods_widget.overlay_slider.valueChanged.connect(
            self.overlay_alpha_changed
        )
        self._advanced_methods_widget.overlay_color_button.clicked.connect(
            self.overlay_change_color
        )
        self._advanced_methods_widget.move_to_grid_button.clicked.connect(
            self.move_to_grid
        )

        # Other ---------------------------------------------------------------
        self._acq_widget.use_osc_start(False)
        self._acq_widget.use_kappa(False)
        self._acq_widget.acq_widget_layout.num_images_label.setEnabled(False)
        self._acq_widget.acq_widget_layout.num_images_ledit.setEnabled(False)
        for col in range(self._advanced_methods_widget.grid_treewidget.columnCount()):
            self._advanced_methods_widget.grid_treewidget.resizeColumnToContents(col)

        self._acq_widget.acq_widget_layout.osc_total_range_label.setText(
            "Total osc. range per line"
        )

        self._advanced_methods = (
            api.beamline_setup.get_advanced_methods()
        )
        if self._advanced_methods:
            for method in self._advanced_methods:
                self._advanced_methods_widget.method_combo.addItem(method)
        else:
            self.setEnabled(False)

    def enable_widgets(self, state):
        return
        self._acq_widget.setEnabled(state)
        self._data_path_widget.setEnabled(state)

    def init_models(self):
        """
        Descript. :
        """
        CreateTaskBase.init_models(self)
        self._init_models()

    def _init_models(self):
        """
        Descript. :
        """
        CreateTaskBase.init_models(self)
        self._processing_parameters = queue_model_objects.ProcessingParameters()

        has_shutter_less = api.beamline_setup.detector_has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = api.beamline_setup.get_default_acquisition_parameters(
            "default_advanced_values"
        )

    def init_api(self):
        """
        In plate mode oscillation start is in the middle of the grid
        """
        CreateTaskBase.init_api(self)

        self._acq_widget.acq_widget_layout.osc_start_label.setText(
            "Oscillation middle:"
        )

        hor_size, ver_size = api.beam_info.get_beam_size()
        self.spacing[0] = hor_size
        self.spacing[1] = ver_size

        self._advanced_methods_widget.hor_spacing_ledit.setText(
            "%.1f" % (hor_size * 1000)
        )
        self._advanced_methods_widget.ver_spacing_ledit.setText(
            "%.1f" % (ver_size * 1000)
        )

        api.graphics.connect("shapeCreated", self.shape_created)
        api.graphics.connect("shapeChanged", self.shape_changed)
        api.graphics.connect("shapeDeleted", self.shape_deleted)

    def set_beam_info(self, beam_info):
        self.spacing[0] = beam_info["size_x"]
        self.spacing[1] = beam_info["size_y"]
        self._advanced_methods_widget.hor_spacing_ledit.setText(
            "%.1f" % (beam_info["size_x"] * 1000)
        )
        self._advanced_methods_widget.ver_spacing_ledit.setText(
            "%.1f" % (beam_info["size_x"] * 1000)
        )

    def approve_creation(self):
        """
        Descript. :
        """
        result = CreateTaskBase.approve_creation(self)
        selected_grid = self.get_selected_shapes()[0]

        if not selected_grid:
            msg = "No grid selected. Please select a grid to continue!"
            logging.getLogger("GUI").warning(msg)
            result = False
            # selected_grid = api.graphics.get_auto_grid()
        else:
            grid_properties = selected_grid.get_properties()
            exp_time = float(self._acq_widget.acq_widget_layout.exp_time_ledit.text())
            speed = grid_properties["yOffset"] / exp_time
            if speed >= 2.25:
                logging.getLogger("GUI").error(
                    "Translation speed %.3f is above the limit 2.25" % speed
                )
                return False
            osc_range_per_frame = float(
                self._acq_widget.acq_widget_layout.osc_range_ledit.text()
            )
            speed = osc_range_per_frame / exp_time
            if speed >= 300:
                logging.getLogger("GUI").error(
                    "Rotation speed per frame %.3f is above the limit 300" % speed
                )
                return False
        return result

    def update_processing_parameters(self, crystal):
        """
        Descript. :
        """
        return

    def single_item_selection(self, tree_item):
        """
        Descript. :
        """
        CreateTaskBase.single_item_selection(self, tree_item)
        self.dc_selected = False

        if isinstance(tree_item, queue_item.SampleQueueItem):
            pass
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            pass
            # self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem) or isinstance(
            tree_item, queue_item.XrayCenteringQueueItem
        ):

            if isinstance(tree_item, queue_item.XrayCenteringQueueItem):
                data_collection = tree_item.get_model().reference_image_collection
            else:
                data_collection = tree_item.get_model()

            if tree_item.get_model().is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            if data_collection.is_mesh():
                # sample_data_model = self.get_sample_item(tree_item).get_model()
                # self._acq_widget.disable_inverse_beam(True)
                # api.graphics.de_select_all()
                api.graphics.select_shape(data_collection.grid)
                self._advanced_methods_widget.grid_treewidget.setCurrentItem(
                    self._grid_map[data_collection.grid]
                )

                self._path_template = data_collection.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)

                self._acquisition_parameters = data_collection.acquisitions[
                    0
                ].acquisition_parameters
                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template
                )
                # self.get_acquisition_widget().use_osc_start(True)
            self.dc_selected = True
        else:
            self.setDisabled(True)
        self.grid_treewidget_item_selection_changed()

    def _create_task(self, sample, shape):
        """Creates tasks based on selected grids

        :param sample: selected sample object
        :type sample: SampleQueueItem
        :param shape: selected shape
        :type shape: GraphicsLib.GraphicsItem
        """
        tasks = []
        selected_grid = self.get_selected_shapes()[0]
        mesh_dc = self._create_dc_from_grid(sample, selected_grid)

        exp_type = str(self._advanced_methods_widget.method_combo.currentText())
        if exp_type == "MeshScan":
            tasks.append(mesh_dc)
        elif exp_type == "XrayCentering":
            xray_centering = queue_model_objects.XrayCentering(mesh_dc)
            tasks.append(xray_centering)
        else:
            logging.getLogger("GUI").warning(
                "Method %s does not exist in queue_model_objects" % exp_type
            )
        mesh_dc.run_processing_parallel = exp_type

        return tasks

    def shape_created(self, shape, shape_type):
        """If a grid is created then adds it to the treewidget.
           A binding between graphical grid and treewidget item is based
           on the self._grid_map

        :param shape: graphics object
        :type shape: GraphicsLib.GraphicsItem
        :param shape_type: type of the object (point, line, grid)
        :type shape_type: str
        """
        if shape_type == "Grid":
            self._advanced_methods_widget.grid_treewidget.clearSelection()
            grid_properties = shape.get_properties()
            info_str_list = []
            info_str_list.append(grid_properties["name"])
            info_str_list.append("%d" % grid_properties["num_lines"])
            info_str_list.append("%d" % grid_properties["num_images_per_line"])

            if not self.dc_selected:
                exp_time = max(
                    float(grid_properties["yOffset"] / 2.245),
                    self._acq_widget.exp_time_validator.bottom() + 0.00001,
                )
                self._acq_widget.acq_widget_layout.exp_time_ledit.setText(
                    "%.6f" % exp_time
                )

            grid_treewidget_item = QtImport.QTreeWidgetItem(
                self._advanced_methods_widget.grid_treewidget, info_str_list
            )
            self._grid_map[shape] = grid_treewidget_item

            if not self.dc_selected:
                grid_treewidget_item.setSelected(True)
                self.grid_treewidget_item_selection_changed()
                if self._acq_widget.acq_widget_layout.max_osc_range_cbx.isChecked():
                    self._acq_widget.update_osc_total_range_limits(
                        grid_properties["num_images_per_line"]
                    )
                else:
                    self.update_grid_osc_total_range()

    def shape_deleted(self, shape, shape_type):
        """Removes shape from QTreeWidget and self._grid_map

        :param shape: graphics object
        :type shape: QtGraphicsLib.GraphicsItem
        :param shape_type: type of the object (point, line, grid)
        :type shape_type: str
        """
        if self._grid_map.get(shape):
            treewidget_item_modelindex = self._advanced_methods_widget.grid_treewidget.indexFromItem(
                self._grid_map[shape]
            )
            self._advanced_methods_widget.grid_treewidget.takeTopLevelItem(
                treewidget_item_modelindex.row()
            )
            self._grid_map.pop(shape)

    def grid_treewidget_item_selection_changed(self):
        """Updates acquisition parameters based on the selected grid.
        """
        self.enable_grid_controls(False)

        for item in self._grid_map.items():
            grid = item[0]
            treewidget_item = item[1]

            if treewidget_item.isSelected():
                grid_properties = grid.get_properties()
                cell_count = (
                    grid_properties["num_lines"]
                    * grid_properties["num_images_per_line"]
                )
                self._acq_widget.acq_widget_layout.num_images_ledit.setText(
                    "%d" % cell_count
                )
                self._acq_widget.acq_widget_layout.first_image_ledit.setText(
                    "%d" % grid_properties["first_image_num"]
                )
                centred_point = grid.get_centred_position()
                self._acq_widget.acq_widget_layout.kappa_ledit.setText(
                    "%.2f" % float(centred_point.kappa)
                )
                self._acq_widget.acq_widget_layout.kappa_phi_ledit.setText(
                    "%.2f" % float(centred_point.kappa_phi)
                )
                self._advanced_methods_widget.hor_spacing_ledit.setText(
                    "%.2f" % (float(grid_properties["xOffset"]) * 1000)
                )
                self._advanced_methods_widget.ver_spacing_ledit.setText(
                    "%.2f" % (float(grid_properties["yOffset"]) * 1000)
                )

                treewidget_item.setText(3, str(grid_properties["num_lines"]))
                treewidget_item.setText(4, str(grid_properties["num_images_per_line"]))

                grid.setSelected(True)
                self.enable_grid_controls(True)
            else:
                grid.setSelected(False)

    def get_selected_shapes(self):
        """Returns selected grids

        :returns: selected grid objects
        """
        shapes = []
        for grid, grid_treewidget_item in self._grid_map.items():
            if grid_treewidget_item.isSelected():
                shapes.append(grid)
        return shapes

    def get_selected_grid_properties(self):
        """Returns properties of the selected grid

        :returns: dict with properties
        """

        for grid, grid_treewidget_item in self._grid_map.items():
            if grid_treewidget_item.isSelected():
                return grid.get_properties()

    def draw_grid_button_clicked(self):
        """Starts grid drawing
        """
        api.graphics.create_grid(self.spacing)

    def remove_grid_button_clicked(self):
        """Removes selected grid
        """
        grid_to_delete = self.get_selected_shapes()[0]

        if grid_to_delete:
            api.graphics.delete_shape(grid_to_delete)
            self._advanced_methods_widget.move_to_grid_button.setEnabled(False)

    def hor_spacing_changed(self, value):
        """Updates spacing of the selected grid
        """
        try:
            self.spacing[0] = (
                float(self._advanced_methods_widget.hor_spacing_ledit.text()) / 1000
            )
            self.set_spacing()
        except BaseException:
            pass

    def ver_spacing_changed(self, value):
        """Updates spacing of the selected grid
        """
        try:
            self.spacing[1] = (
                float(self._advanced_methods_widget.ver_spacing_ledit.text()) / 1000
            )
            self.set_spacing()
        except BaseException:
            pass

    def set_spacing(self):
        if 0 in self.spacing:
            return

        for grid, treewidget_item in self._grid_map.items():
            if treewidget_item.isSelected():
                grid.set_spacing(
                    self.spacing,
                    adjust_size=self._advanced_methods_widget.adjust_size_cbox.isChecked(),
                )
                grid_properties = grid.get_properties()

                treewidget_item.setText(1, str(grid_properties["num_lines"]))
                treewidget_item.setText(2, str(grid_properties["num_images_per_line"]))
                cell_count = (
                    grid_properties["num_lines"]
                    * grid_properties["num_images_per_line"]
                )
                self._acq_widget.acq_widget_layout.num_images_ledit.setText(
                    str(
                        grid_properties["num_lines"]
                        * grid_properties["num_images_per_line"]
                    )
                )

    def move_to_grid(self):
        """Moves diffractometer to the center of the grid
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            api.diffractometer.move_to_centred_position(
                grid.get_centred_position()
            )

    def overlay_toggled(self, state):
        """Toggles (on/off) overlay
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            grid.set_display_overlay(state)

    def overlay_alpha_changed(self, alpha_value):
        """Changes the transperency of the grid
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            grid.set_fill_alpha(alpha_value)

    def overlay_change_color(self):
        """Changes the default color (blue) of overlay
        """
        color = QtImport.QColorDialog.getColor()
        grid = self.get_selected_shapes()[0]

        if color.isValid() and grid:
            grid.set_base_color(color)

    def move_grid(self, direction):
        """Moves grid by one pix in selected direction

        :param direction: direction to move (right, left, up, down)
        :type direction: str
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            grid.move_by_pix(direction)
            api.graphics.update_grid_motor_positions(grid)

    def enable_grid_controls(self, state):
        """Enables grid controls if a grid is selectd
        """
        self._advanced_methods_widget.overlay_cbox.setEnabled(state)
        self._advanced_methods_widget.overlay_slider.setEnabled(state)
        self._advanced_methods_widget.overlay_color_button.setEnabled(state)
        self._advanced_methods_widget.move_to_grid_button.setEnabled(state)
        self._advanced_methods_widget.remove_grid_button.setEnabled(state)

        self._advanced_methods_widget.move_right_button.setEnabled(state)
        self._advanced_methods_widget.move_left_button.setEnabled(state)
        self._advanced_methods_widget.move_up_button.setEnabled(state)
        self._advanced_methods_widget.move_down_button.setEnabled(state)

        self._acq_widget.acq_widget_layout.max_osc_range_cbx.setEnabled(
            state and self._in_plate_mode
        )

        self.enable_widgets(state)
        self._acq_widget.emit_acq_parameters_changed()

    def grid_osc_range_ledit_changed(self, new_value):
        """Osc range per frame changed

        :param new_value: new value
        :type new_value: str
        """

        self.update_grid_osc_total_range()

    def grid_osc_total_range_ledit_changed(self, new_value):
        """Updates osc range per frame


        :param new_value: new value
        :type new_value: str
        """

        grid_properties = self.get_selected_grid_properties()
        if grid_properties:
            try:
                self._acq_widget.acq_widget_layout.osc_range_ledit.setText(
                    "%.4f"
                    % (float(new_value) / grid_properties["num_images_per_line"] - 1e-5)
                )
                self._acq_widget.emit_acq_parameters_changed()
            except BaseException:
                pass

    def update_grid_osc_total_range(self):
        """Updates osc range per line
        """

        grid_properties = self.get_selected_grid_properties()
        if grid_properties:
            self._acq_widget.acq_widget_layout.osc_total_range_ledit.setText(
                str(
                    float(self._acq_widget.acq_widget_layout.osc_range_ledit.text())
                    * grid_properties["num_images_per_line"]
                )
            )

    def set_osc_total_range(self, num_images=None, mesh=False):
        grid_properties = self.get_selected_grid_properties()
        if grid_properties:
            CreateTaskBase.set_osc_total_range(
                self, grid_properties["num_images_per_line"], mesh=True
            )
class CreateXRFSpectrumWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "XRF spectrum"
        )

        if name is not None:
            self.setObjectName(name)

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.count_time = None
        self.xrf_spectrum_model = None

        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._data_path_widget = DataPathWidget(
            self, data_model=self._path_template, layout="vertical"
        )

        _parameters_gbox = QtImport.QGroupBox("Parameters", self)
        _count_time_label = QtImport.QLabel("Count time (sec.):", _parameters_gbox)
        self.count_time_ledit = QtImport.QLineEdit("1", _parameters_gbox)
        # self.count_time_ledit.setMaximumWidth(75)
        self.adjust_transmission_cbox = QtImport.QCheckBox(
            "Adjust transmission", _parameters_gbox
        )
        self.adjust_transmission_cbox.setChecked(True)

        # Layout --------------------------------------------------------------
        _parameters_gbox_hlayout = QtImport.QHBoxLayout(_parameters_gbox)
        _parameters_gbox_hlayout.addWidget(_count_time_label)
        _parameters_gbox_hlayout.addWidget(self.count_time_ledit)
        _parameters_gbox_hlayout.addWidget(self.adjust_transmission_cbox)
        _parameters_gbox_hlayout.addStretch(0)
        _parameters_gbox_hlayout.setSpacing(2)
        _parameters_gbox_hlayout.setContentsMargins(0, 0, 0, 0)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(_parameters_gbox)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        _main_vlayout.addStretch(0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self.adjust_transmission_cbox.stateChanged.connect(
            self.adjust_transmission_state_changed
        )

        # Other ---------------------------------------------------------------
        self._data_path_widget.data_path_layout.compression_cbox.setVisible(False)

    def set_expert_mode(self, state):
        self.adjust_transmission_cbox.setEnabled(state)

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self.xrf_spectrum_model = queue_model_objects.XRFSpectrum()
        self._path_template.start_num = 1
        self._path_template.num_files = 1
        self._path_template.suffix = "raw"
        self._path_template.compression = False

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        self.xrf_spectrum_model = tree_item.get_model()

        if isinstance(tree_item, queue_item.XRFSpectrumQueueItem):
            if self.xrf_spectrum_model.is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            if self.xrf_spectrum_model.get_path_template():
                self._path_template = self.xrf_spectrum_model.get_path_template()

            self._data_path_widget.update_data_model(self._path_template)
        elif not (
            isinstance(tree_item, queue_item.SampleQueueItem)
            or isinstance(tree_item, queue_item.DataCollectionGroupQueueItem)
        ):
            self.setDisabled(True)

    def approve_creation(self):
        # base_result = CreateTaskBase.approve_creation(self)
        base_result = True
        self.count_time = None

        try:
            self.count_time = float(str(self.count_time_ledit.text()))
        except BaseException:
            logging.getLogger("GUI").error("Incorrect count time value.")

        return base_result and self.count_time

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.

    def _create_task(self, sample, shape):
        data_collections = []

        if self.count_time is not None:
            if not shape:
                cpos = queue_model_objects.CentredPosition()
                cpos.snapshot_image = api.graphics.get_scene_snapshot()
            else:
                # Shapes selected and sample is mounted, get the
                # centred positions for the shapes
                if isinstance(shape, GraphicsItemPoint):
                    snapshot = api.graphics.get_scene_snapshot(shape)

                    cpos = copy.deepcopy(shape.get_centred_position())
                    cpos.snapshot_image = snapshot

            path_template = self._create_path_template(sample, self._path_template)

            xrf_spectrum = queue_model_objects.XRFSpectrum(sample, path_template, cpos)
            xrf_spectrum.set_name(path_template.get_prefix())
            xrf_spectrum.set_number(path_template.run_number)
            xrf_spectrum.count_time = self.count_time
            xrf_spectrum.adjust_transmission = self.adjust_transmission_cbox.isChecked()

            data_collections.append(xrf_spectrum)
        else:
            logging.getLogger("GUI").error("No count time specified.")

        return data_collections

    def adjust_transmission_state_changed(self, state):
        self.xrf_spectrum_model.adjust_transmission = state > 0
class XRFSpectrumParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="xrf_spectrum_parameters_widget"):
        QtImport.QWidget.__init__(self, parent)

        if name is not None:
            self.setObjectName(name)

        # Internal variables --------------------------------------------------
        self.xrf_spectrum_model = queue_model_objects.XRFSpectrum()
        self._tree_view_item = None

        # Graphic elements ----------------------------------------------------
        _top_widget = QtImport.QWidget(self)
        _parameters_widget = QtImport.QWidget(_top_widget)
        self.data_path_widget = DataPathWidget(_parameters_widget)
        self.other_parameters_gbox = QtImport.QGroupBox(
            "Other parameters", _parameters_widget)
        self.count_time_label = QtImport.QLabel("Count time:",
                                                self.other_parameters_gbox)
        self.count_time_ledit = QtImport.QLineEdit(self.other_parameters_gbox)
        self.count_time_ledit.setFixedWidth(50)
        self.adjust_transmission_cbox = QtImport.QCheckBox(
            "Adjust transmission", self.other_parameters_gbox)
        self.adjust_transmission_cbox.hide()
        self.mca_spectrum_widget = McaSpectrumWidget(self)
        self.snapshot_widget = SnapshotWidget(self)

        # Layout -------------------------------------------------------------
        _other_parameters_gbox_hlayout = QtImport.QHBoxLayout(
            self.other_parameters_gbox)
        _other_parameters_gbox_hlayout.addWidget(self.count_time_label)
        _other_parameters_gbox_hlayout.addWidget(self.count_time_ledit)
        _other_parameters_gbox_hlayout.addWidget(self.adjust_transmission_cbox)
        _other_parameters_gbox_hlayout.addStretch(0)
        _other_parameters_gbox_hlayout.setSpacing(2)
        _other_parameters_gbox_hlayout.setContentsMargins(0, 0, 0, 0)

        _parameters_widget_layout = QtImport.QVBoxLayout(_parameters_widget)
        _parameters_widget_layout.addWidget(self.data_path_widget)
        _parameters_widget_layout.addWidget(self.other_parameters_gbox)
        _parameters_widget_layout.addStretch(0)
        _parameters_widget_layout.setSpacing(2)
        _parameters_widget_layout.setContentsMargins(0, 0, 0, 0)

        _top_widget_layout = QtImport.QHBoxLayout(_top_widget)
        _top_widget_layout.addWidget(_parameters_widget)
        _top_widget_layout.addWidget(self.snapshot_widget)
        _top_widget_layout.setSpacing(2)
        _top_widget_layout.addStretch(0)
        _top_widget_layout.setContentsMargins(0, 0, 0, 0)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(_top_widget)
        _main_vlayout.addWidget(self.mca_spectrum_widget)
        _main_vlayout.setSpacing(2)
        _main_vlayout.setContentsMargins(0, 0, 0, 0)

        # SizePolicies -------------------------------------------------------
        self.mca_spectrum_widget.setSizePolicy(QtImport.QSizePolicy.Expanding,
                                               QtImport.QSizePolicy.Expanding)
        _top_widget.setSizePolicy(QtImport.QSizePolicy.Expanding,
                                  QtImport.QSizePolicy.Fixed)

        # Qt signal/slot connections ------------------------------------------
        self.data_path_widget.data_path_layout.prefix_ledit.textChanged.connect(
            self._prefix_ledit_change)
        self.data_path_widget.data_path_layout.run_number_ledit.textChanged.connect(
            self._run_number_ledit_change)
        self.count_time_ledit.textChanged.connect(
            self._count_time_ledit_change)

        # Other ---------------------------------------------------------------
        self.data_path_widget.data_path_layout.compression_cbox.setVisible(
            False)

        if HWR.beamline.xrf_spectrum is None:
            HWR.beamline.xrf_spectrum.connect("xrfSpectrumFinished",
                                              self.spectrum_finished)

    def _prefix_ledit_change(self, new_value):
        self.xrf_spectrum_model.set_name(str(new_value))
        self._tree_view_item.setText(
            0, self.xrf_spectrum_model.get_display_name())

    def _run_number_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self.xrf_spectrum_model.set_number(int(new_value))
            self._tree_view_item.setText(
                0, self.xrf_spectrum_model.get_display_name())

    def _count_time_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self.xrf_spectrum_model.set_count_time(float(new_value))

    def populate_widget(self, item):
        self._tree_view_item = item
        self.xrf_spectrum_model = item.get_model()
        executed = self.xrf_spectrum_model.is_executed()

        self.data_path_widget.setEnabled(not executed)
        self.other_parameters_gbox.setEnabled(not executed)
        # self.mca_spectrum_widget.setEnabled(executed)

        if executed:
            result = self.xrf_spectrum_model.get_spectrum_result()
            self.mca_spectrum_widget.set_data(result.mca_data,
                                              result.mca_calib,
                                              result.mca_config)
        else:
            self.mca_spectrum_widget.clear()

        self.data_path_widget.update_data_model(
            self.xrf_spectrum_model.path_template)
        self.count_time_ledit.setText(str(self.xrf_spectrum_model.count_time))

        image = self.xrf_spectrum_model.centred_position.snapshot_image
        self.snapshot_widget.display_snapshot(image, width=400)

    def spectrum_finished(self, mca_data, mca_calib, mca_config):
        self.mca_spectrum_widget.set_data(mca_data, mca_calib, mca_config)
class CreateEnergyScanWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name, fl, "Energy scan")

        if not name:
            self.setObjectName("create_energy_scan_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._periodic_table_widget = PeriodicTableWidget(self)
        self._data_path_widget = DataPathWidget(
            self, data_model=self._path_template, layout="vertical"
        )

        _parameters_gbox = QtImport.QGroupBox("Parameters", self)
        self._adjust_transmission_cbox = QtImport.QCheckBox(
            "Adjust transmission", _parameters_gbox
        )
        self._adjust_transmission_cbox.setChecked(False)
        self._adjust_transmission_cbox.setEnabled(True)
        self._max_transmission_label = QtImport.QLabel("Maximum transmission:")
        self._max_transmission_ledit = QtImport.QLineEdit("20", _parameters_gbox)
        self._max_transmission_ledit.setFixedWidth(80)
        self._max_transmission_ledit.setEnabled(False)

        self._comments_widget = CommentsWidget(self)

        # Layout --------------------------------------------------------------
        _parameters_gbox_hlayout = QtImport.QGridLayout(_parameters_gbox)
        _parameters_gbox_hlayout.addWidget(self._adjust_transmission_cbox, 0, 0)
        _parameters_gbox_hlayout.addWidget(self._max_transmission_label, 1, 0)
        _parameters_gbox_hlayout.addWidget(self._max_transmission_ledit, 1, 1)
        _parameters_gbox_hlayout.setColumnStretch(2, 1)
        _parameters_gbox_hlayout.setSpacing(2)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._periodic_table_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(_parameters_gbox)
        _main_vlayout.addWidget(self._comments_widget)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        _main_vlayout.setSpacing(6)
        _main_vlayout.addStretch(10)

        # SizePolicies --------------------------------------------------------
        self._comments_widget.setFixedHeight(100)

        # Qt signal/slot connections ------------------------------------------
        # self._periodic_table_widget.elementEdgeSelectedSignal.connect(\
        #     self.acq_parameters_changed)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self._adjust_transmission_cbox.stateChanged.connect(
            self.adjust_transmission_state_changed
        )
        self._max_transmission_ledit.textEdited.connect(
            self.max_transmission_value_changed
        )

        self._data_path_widget.data_path_layout.compression_cbox.setVisible(False)

        try:
            self._periodic_table_widget.set_elements(
                HWR.beamline.energy_scan.getElements()
            )

            max_transmission_value = (
                HWR.beamline.energy_scan.get_max_transmission_value()
            )

            self._adjust_transmission_cbox.setEnabled(True)
            self._adjust_transmission_cbox.setChecked(True)
            HWR.beamline.energy_scan.adjust_transmission(True)

            if max_transmission_value:
                self._max_transmission_ledit.setText("%.2f" % max_transmission_value)
        except BaseException:
            pass

    def set_expert_mode(self, state):
        self._adjust_transmission_cbox.setEnabled(state)
        self._max_transmission_label.setEnabled(state)
        self._max_transmission_ledit.setEnabled(state)

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def init_models(self):

        CreateTaskBase.init_models(self)
        self.enery_scan = queue_model_objects.EnergyScan()
        self._path_template.start_num = 1
        self._path_template.num_files = 1
        self._path_template.suffix = "raw"
        self._path_template.compression = False

    def init_data_path_model(self):
        # Initialize the path_template of the widget to default
        # values read from the beamline setup
        if self._data_path_widget:
            if hasattr(HWR.beamline.session, 'get_secondary_image_directory'):
                self._data_path_widget.set_base_image_directory(
                    HWR.beamline.session.get_secondary_image_directory()
                )
            self._data_path_widget.set_base_process_directory(
                HWR.beamline.session.get_base_process_directory()
            )

            (data_directory, proc_directory) = self.get_default_directory()
            self._path_template = HWR.beamline.get_default_path_template()
            self._path_template.directory = data_directory
            self._path_template.process_directory = proc_directory
            self._path_template.base_prefix = self.get_default_prefix()
            self._path_template.run_number = HWR.beamline.queue_model.get_next_run_number(
                  self._path_template
            )
            self._path_template.compression = self._enable_compression
        else:
            self._path_template = queue_model_objects.PathTemplate()

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        escan_model = tree_item.get_model()

        if isinstance(tree_item, queue_item.EnergyScanQueueItem):
            if tree_item.get_model().is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            if escan_model.get_path_template():
                self._path_template = escan_model.get_path_template()

            self._data_path_widget.update_data_model(self._path_template)
        elif not (
            isinstance(tree_item, queue_item.SampleQueueItem)
            or isinstance(tree_item, queue_item.DataCollectionGroupQueueItem)
        ):
            self.setDisabled(True)

    def approve_creation(self):
        base_result = CreateTaskBase.approve_creation(self)
        selected_element, selected_edge = (
            self._periodic_table_widget.get_selected_element_edge()
        )
        if not selected_element:
            logging.getLogger("GUI").warning(
                "No element selected, please select an element."
            )

        return base_result and selected_element

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.
    def _create_task(self, sample, shape, comments=None):
        data_collections = []
        selected_element, selected_edge = (
            self._periodic_table_widget.get_selected_element_edge()
        )

        if selected_element:
            if not shape:
                cpos = queue_model_objects.CentredPosition()
                cpos.snapshot_image = HWR.beamline.sample_view.get_scene_snapshot()
            else:
                # Shapes selected and sample is mounted, get the
                # centred positions for the shapes
                if isinstance(shape, GraphicsItemPoint):
                    snapshot = HWR.beamline.sample_view.get_scene_snapshot(shape)

                    cpos = copy.deepcopy(shape.get_centred_position())
                    cpos.snapshot_image = snapshot

            path_template = self._create_path_template(sample, self._path_template)

            energy_scan = queue_model_objects.EnergyScan(sample, path_template, cpos)
            energy_scan.set_name(path_template.get_prefix())
            energy_scan.set_number(path_template.run_number)
            energy_scan.element_symbol = selected_element
            energy_scan.edge = selected_edge

            data_collections.append(energy_scan)
            self._path_template.run_number += 1
        else:
            logging.getLogger("GUI").info(
                "No element selected, please select an element."
            )

        return data_collections

    def element_edge_selected(self, element, edge):
        if len(self._current_selected_items) == 1:
            item = self._current_selected_items[0]
            if isinstance(item, queue_item.EnergyScanQueueItem):
                item.get_model().element_symbol = str(element)
                item.get_model().edge = str(edge)

    def adjust_transmission_state_changed(self, state):
        self._max_transmission_ledit.setEnabled(state)
        HWR.beamline.energy_scan.adjust_transmission(state)

    def max_transmission_value_changed(self, value):
        try:
            max_transmission = float(value)
            HWR.beamline.energy_scan.set_max_transmission(max_transmission)
        except BaseException:
            pass
class EnergyScanParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="energy_scan_tab_widget"):
        QtImport.QWidget.__init__(self, parent)

        if name is not None:
            self.setObjectName(name)

        # Internal variables --------------------------------------------------
        self.energy_scan_model = queue_model_objects.EnergyScan()
        self._tree_view_item = None

        # Graphic elements ----------------------------------------------------
        _top_widget = QtImport.QWidget(self)
        _parameters_widget = QtImport.QWidget(_top_widget)
        self.periodic_table_widget = PeriodicTableWidget(_parameters_widget)
        self.data_path_widget = DataPathWidget(_parameters_widget)
        self.data_path_widget.data_path_layout.file_name_label.setText("")
        self.data_path_widget.data_path_layout.file_name_value_label.hide()
        self.snapshot_widget = SnapshotWidget(self)

        self.scan_actual_plot_widget = PymcaPlotWidget(self, True)
        self.scan_result_plot_widget = PymcaPlotWidget(self, False)
        self.chooch_plot_widget = PymcaPlotWidget(self, False)
        # self.scan_actual_plot_widget = TwoAxisPlotWidget(self, True)
        # self.chooch_plot_widget = TwoAxisPlotWidget(self, False)

        # Layout -------------------------------------------------------------
        _parameters_widget_layout = QtImport.QVBoxLayout()
        _parameters_widget_layout.addWidget(self.periodic_table_widget)
        _parameters_widget_layout.addWidget(self.data_path_widget)
        _parameters_widget_layout.addStretch(0)
        _parameters_widget_layout.setSpacing(2)
        _parameters_widget_layout.setContentsMargins(0, 0, 0, 0)
        _parameters_widget.setLayout(_parameters_widget_layout)

        _top_widget_hlayout = QtImport.QHBoxLayout(self)
        _top_widget_hlayout.addWidget(_parameters_widget)
        _top_widget_hlayout.addWidget(self.snapshot_widget)
        _top_widget_hlayout.addStretch(0)
        _top_widget_hlayout.setSpacing(2)
        _top_widget_hlayout.setContentsMargins(0, 0, 0, 0)
        _top_widget.setLayout(_top_widget_hlayout)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(_top_widget)
        _main_vlayout.addWidget(self.scan_actual_plot_widget)
        _main_vlayout.addWidget(self.scan_result_plot_widget)
        _main_vlayout.addWidget(self.chooch_plot_widget)
        _main_vlayout.setSpacing(5)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        # _main_vlayout.addStretch(0)

        self.setLayout(_main_vlayout)

        # SizePolicies --------------------------------------------------------
        self.scan_actual_plot_widget.setSizePolicy(
            QtImport.QSizePolicy.Fixed, QtImport.QSizePolicy.Expanding
        )
        self.chooch_plot_widget.setSizePolicy(
            QtImport.QSizePolicy.Fixed, QtImport.QSizePolicy.Expanding
        )

        # Qt signal/slot connections ------------------------------------------
        # qt.QObject.connect(self.periodic_table_widget, qt.PYSIGNAL('edgeSelected'),
        #                   self.element_clicked)

        self.data_path_widget.data_path_layout.prefix_ledit.textChanged.connect(
            self._prefix_ledit_change
        )

        self.data_path_widget.data_path_layout.run_number_ledit.textChanged.connect(
            self._run_number_ledit_change
        )

        # Other ---------------------------------------------------------------
        self.scan_actual_plot_widget.hide()
        self.scan_result_plot_widget.hide()
        self.data_path_widget.data_path_layout.compression_cbox.setVisible(False)

        if api.energyscan is not None:
            api.energyscan.connect(
                "energyScanStarted", self.energy_scan_started
            )
            api.energyscan.connect("scanNewPoint", self.energy_scan_new_point)
            api.energyscan.connect("choochFinished", self.chooch_finished)

    def _prefix_ledit_change(self, new_value):
        self.energy_scan_model.set_name(str(new_value))
        self._tree_view_item.setText(0, self.energy_scan_model.get_name())

    def _run_number_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self.energy_scan_model.set_number(int(new_value))
            self._tree_view_item.setText(0, self.energy_scan_model.get_name())

    def tab_changed(self):
        if self._tree_view_item:
            self.populate_widget(self._tree_view_item)

    def populate_widget(self, item):
        self._tree_view_item = item
        self.energy_scan_model = item.get_model()
        executed = self.energy_scan_model.is_executed()
        is_running = self.energy_scan_model.is_running()

        self.data_path_widget.setDisabled(executed or is_running)
        self.periodic_table_widget.setDisabled(executed or is_running)
        # self.scan_actual_plot_widget.setEnabled()
        # self.scan_actual_plot_widget.setEnabled(not executed)
        # self.chooch_plot_widget.setEnabled(not executed)

        width = self.data_path_widget.width() + self.snapshot_widget.width()
        self.scan_actual_plot_widget.setFixedWidth(width)
        self.scan_result_plot_widget.setFixedWidth(width)
        self.chooch_plot_widget.setFixedWidth(width)

        self.chooch_plot_widget.clear()
        title = "Element: %s, Edge: %s" % (
            self.energy_scan_model.element_symbol,
            self.energy_scan_model.edge,
        )

        if executed:
            self.scan_actual_plot_widget.hide()
            self.scan_result_plot_widget.show()

            result = self.energy_scan_model.get_scan_result()
            self.scan_result_plot_widget.plot_energy_scan_curve(result.data, title)

            self.chooch_plot_widget.plot_energy_scan_results(
                result.pk,
                result.fppPeak,
                result.fpPeak,
                result.ip,
                result.fppInfl,
                result.fpInfl,
                result.rm,
                result.chooch_graph_x,
                result.chooch_graph_y1,
                result.chooch_graph_y2,
                result.title,
            )
        elif is_running:
            self.scan_actual_plot_widget.show()
            self.scan_result_plot_widget.hide()
        else:
            self.scan_actual_plot_widget.hide()
            self.scan_result_plot_widget.show()
            self.scan_result_plot_widget.clear()

        self.data_path_widget.update_data_model(self.energy_scan_model.path_template)
        self.periodic_table_widget.set_current_element_edge(
            self.energy_scan_model.element_symbol, self.energy_scan_model.edge
        )

        image = self.energy_scan_model.centred_position.snapshot_image
        self.snapshot_widget.display_snapshot(image, width=400)

    def element_clicked(self, symbol, energy):
        self.energy_scan_model.element_symbol = symbol
        self.energy_scan_model.edge = energy

    def energy_scan_started(self, scan_info):
        self.scan_actual_plot_widget.clear()
        self.scan_actual_plot_widget.show()
        self.scan_result_plot_widget.clear()
        self.scan_result_plot_widget.hide()
        self.chooch_plot_widget.clear()
        self.scan_actual_plot_widget.start_new_scan(scan_info)
        self.data_path_widget.setEnabled(False)
        self.periodic_table_widget.setEnabled(False)

    def energy_scan_new_point(self, x, y):
        self.scan_actual_plot_widget.add_new_plot_value(x, y)

    def chooch_finished(
        self,
        pk,
        fppPeak,
        fpPeak,
        ip,
        fppInfl,
        fpInfl,
        rm,
        chooch_graph_x,
        chooch_graph_y1,
        chooch_graph_y2,
        title,
    ):
        self.chooch_plot_widget.plot_energy_scan_results(
            pk,
            fppPeak,
            fpPeak,
            ip,
            fppInfl,
            fpInfl,
            rm,
            chooch_graph_x,
            chooch_graph_y1,
            chooch_graph_y2,
            title,
        )
        self.scan_actual_plot_widget.plot_finished()
Beispiel #6
0
class CreateCharWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name, fl, "Characterisation")
        self.setObjectName("create_char_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self._vertical_dimension_widget = None
        self._current_selected_item = None
        self._char = None
        self._char_params = None

        self.init_models()
        self._char_params_mib = DataModelInputBinder(self._char_params)

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionWidgetSimple(
            self,
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(
            self, data_model=self._path_template, layout="vertical"
        )

        self._vertical_dimension_widget = QtImport.load_ui_file(
            "vertical_crystal_dimension_widget_layout.ui"
        )

        self._char_widget = QtImport.load_ui_file(
            "characterise_simple_widget_vertical_layout.ui"
        )

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._char_widget)
        _main_vlayout.addWidget(self._vertical_dimension_widget)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        _main_vlayout.setSpacing(6)
        _main_vlayout.addStretch(0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)

        self._vertical_dimension_widget.space_group_ledit.activated.connect(
            self._space_group_change
        )
        # self.connect(induced_burn_cbx, QtCore.SIGNAL("toggled(bool)"),
        #             self.use_induced_burn)

        self._char_widget.characterisation_gbox.toggled.connect(
            self.characterisation_gbox_toggled
        )
        self._char_widget.wait_result_cbx.toggled.connect(self.wait_results_cbx_toggled)
        self._char_widget.execute_plan_cbx.toggled.connect(
            self.run_diffraction_plan_cbx_toggled
        )

        # Other ---------------------------------------------------------------
        self._char_params_mib.bind_value_update(
            "opt_sad", self._char_widget.optimised_sad_cbx, bool, None
        )

        self._char_params_mib.bind_value_update(
            "account_rad_damage", self._char_widget.account_rad_dmg_cbx, bool, None
        )

        # self._char_params_mib.bind_value_update('determine_rad_params',
        #                                        induced_burn_cbx,
        #                                        bool, None)

        self._char_params_mib.bind_value_update(
            "strategy_complexity", self._char_widget.start_comp_cbox, int, None
        )

        self._char_params_mib.bind_value_update(
            "max_crystal_vdim",
            self._vertical_dimension_widget.max_vdim_ledit,
            float,
            QtImport.QDoubleValidator(0.0, 1000, 2, self),
        )

        self._char_params_mib.bind_value_update(
            "min_crystal_vdim",
            self._vertical_dimension_widget.min_vdim_ledit,
            float,
            QtImport.QDoubleValidator(0.0, 1000, 2, self),
        )

        self._char_params_mib.bind_value_update(
            "min_crystal_vphi",
            self._vertical_dimension_widget.min_vphi_ledit,
            float,
            QtImport.QDoubleValidator(0.0, 1000, 2, self),
        )

        self._char_params_mib.bind_value_update(
            "max_crystal_vphi",
            self._vertical_dimension_widget.max_vphi_ledit,
            float,
            QtImport.QDoubleValidator(0.0, 1000, 2, self),
        )

        self._vertical_dimension_widget.space_group_ledit.addItems(XTAL_SPACEGROUPS)

        self._data_path_widget.data_path_layout.compression_cbox.setVisible(False)

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def use_induced_burn(self, state):
        self._acquisition_parameters.induce_burn = state

    def _space_group_change(self, index):
        self._char_params.space_group = queue_model_enumerables.XTAL_SPACEGROUPS[index]

    def _set_space_group(self, space_group):
        index = 0

        if self._vertical_dimension_widget:
            if space_group in XTAL_SPACEGROUPS:
                index = XTAL_SPACEGROUPS.index(space_group)

            self._space_group_change(index)
            self._vertical_dimension_widget.space_group_ledit.setCurrentIndex(index)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._init_models()

    def _init_models(self):
        self._char = queue_model_objects.Characterisation()
        self._char_params = self._char.characterisation_parameters
        self._processing_parameters = queue_model_objects.ProcessingParameters()
        self._set_space_group(self._processing_parameters.space_group)

        self._acquisition_parameters = (
            api.beamline_setup.get_default_char_acq_parameters()
        )

        self._char_params = (
            api.beamline_setup.get_default_characterisation_parameters()
        )
        self._path_template.reference_image_prefix = "ref"
        # The num images drop down default value is 1
        # we would like it to be 2
        self._acquisition_parameters.num_images = 2
        self._char.characterisation_software = (
            queue_model_enumerables.COLLECTION_ORIGIN.EDNA
        )
        self._path_template.num_files = 2
        self._path_template.compression = False
        self._acquisition_parameters.shutterless = False

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            # self._init_models()
            if self._char_params.space_group == "":
                sample_model = tree_item.get_model()
                self._set_space_group(sample_model.processing_parameters.space_group)
            # self._acq_widget.update_data_model(self._acquisition_parameters,
            #                                   self._path_template)
            # self._char_params_mib.set_model(self._char_params)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.CharacterisationQueueItem):
            if tree_item.get_model().is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            self._char = tree_item.get_model()

            if self._char.get_path_template():
                self._path_template = self._char.get_path_template()

            self._data_path_widget.update_data_model(self._path_template)

            data_collection = self._char.reference_image_collection

            self._char_params = self._char.characterisation_parameters
            self._char_params_mib.set_model(self._char_params)

            self._acquisition_parameters = data_collection.acquisitions[
                0
            ].acquisition_parameters

            self._acq_widget.update_data_model(
                self._acquisition_parameters, self._path_template
            )
            # self.get_acquisition_widget().use_osc_start(True)

            if len(data_collection.acquisitions) == 1:
                self.select_shape_with_cpos(
                    self._acquisition_parameters.centred_position
                )

            self._processing_parameters = data_collection.processing_parameters
        else:
            self.setDisabled(True)

    def update_processing_parameters(self, crystal):
        self._processing_parameters.space_group = crystal.space_group
        self._char_params.space_group = crystal.space_group
        self._processing_parameters.cell_a = crystal.cell_a
        self._processing_parameters.cell_alpha = crystal.cell_alpha
        self._processing_parameters.cell_b = crystal.cell_b
        self._processing_parameters.cell_beta = crystal.cell_beta
        self._processing_parameters.cell_c = crystal.cell_c
        self._processing_parameters.cell_gamma = crystal.cell_gamma

    def approve_creation(self):
        result = CreateTaskBase.approve_creation(self)
        selected_shapes = api.graphics.get_selected_shapes()

        for shape in selected_shapes:
            if isinstance(shape, GraphicsItemPoint):
                result = True
        return result

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. when a data collection group is selected.
    def _create_task(self, sample, shape):
        tasks = []

        if not shape or not isinstance(shape, GraphicsItemPoint):
            cpos = queue_model_objects.CentredPosition()
            cpos.snapshot_image = api.graphics.get_scene_snapshot()
        else:
            # Shapes selected and sample is mounted, get the
            # centred positions for the shapes
            snapshot = api.graphics.get_scene_snapshot(shape)
            cpos = copy.deepcopy(shape.get_centred_position())
            cpos.snapshot_image = snapshot

        char_params = copy.deepcopy(self._char_params)
        acq = self._create_acq(sample)
        dc = queue_model_objects.DataCollection(
            [acq], sample.crystals[0], self._processing_parameters
        )

        # Reference images for characterisations should be taken 90 deg apart
        # this is achived by setting overap to -89
        acq.acquisition_parameters.overlap = -89
        acq.acquisition_parameters.centred_position = cpos

        dc.acquisitions[0] = acq
        dc.experiment_type = queue_model_enumerables.EXPERIMENT_TYPE.EDNA_REF
        dc.run_processing_parallel = False

        char = queue_model_objects.Characterisation(dc, char_params)
        char.set_name(dc.acquisitions[0].path_template.get_prefix())
        char.set_number(dc.acquisitions[0].path_template.run_number)
        char.run_characterisation = self._char_widget.characterisation_gbox.isChecked()
        char.wait_result = self._char_widget.wait_result_cbx.isChecked()
        char.run_diffraction_plan = self._char_widget.execute_plan_cbx.isChecked()
        char.diff_plan_compression = self._tree_brick.compression_state

        tasks.append(char)
        self._path_template.run_number += 1

        if api.flux.get_flux() < 1e9:
            logging.getLogger("GUI").error(
                "No flux reading is available! "
                + "Characterisation result may be wrong. "
                "Measure flux before running the characterisation."
            )

        return tasks

    def characterisation_gbox_toggled(self, state):
        if self._char:
            self._char.run_characterisation = state

    def wait_results_cbx_toggled(self, state):
        if self._char:
            self._char.wait_result = state

    def run_diffraction_plan_cbx_toggled(self, state):
        if self._char:
            self._char.run_diffraction_plan = state
class XRFSpectrumParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="xrf_spectrum_parameters_widget"):
        QtImport.QWidget.__init__(self, parent)

        if name is not None:
            self.setObjectName(name)


        # Internal variables --------------------------------------------------
        self.xrf_spectrum_model = queue_model_objects.XRFSpectrum()
        self._tree_view_item = None

        # Graphic elements ----------------------------------------------------
        _top_widget = QtImport.QWidget(self)
        _parameters_widget = QtImport.QWidget(_top_widget)
        self.data_path_widget = DataPathWidget(_parameters_widget)
        self.other_parameters_gbox = QtImport.QGroupBox(
            "Other parameters", _parameters_widget
        )
        self.count_time_label = QtImport.QLabel(
            "Count time:", self.other_parameters_gbox
        )
        self.count_time_ledit = QtImport.QLineEdit(self.other_parameters_gbox)
        self.count_time_ledit.setFixedWidth(50)
        self.adjust_transmission_cbox = QtImport.QCheckBox(
            "Adjust transmission", self.other_parameters_gbox
        )
        self.adjust_transmission_cbox.hide()
        self.mca_spectrum_widget = McaSpectrumWidget(self)
        self.snapshot_widget = SnapshotWidget(self)

        # Layout -------------------------------------------------------------
        _other_parameters_gbox_hlayout = QtImport.QHBoxLayout(
            self.other_parameters_gbox
        )
        _other_parameters_gbox_hlayout.addWidget(self.count_time_label)
        _other_parameters_gbox_hlayout.addWidget(self.count_time_ledit)
        _other_parameters_gbox_hlayout.addWidget(self.adjust_transmission_cbox)
        _other_parameters_gbox_hlayout.addStretch(0)
        _other_parameters_gbox_hlayout.setSpacing(2)
        _other_parameters_gbox_hlayout.setContentsMargins(0, 0, 0, 0)

        _parameters_widget_layout = QtImport.QVBoxLayout(_parameters_widget)
        _parameters_widget_layout.addWidget(self.data_path_widget)
        _parameters_widget_layout.addWidget(self.other_parameters_gbox)
        _parameters_widget_layout.addStretch(0)
        _parameters_widget_layout.setSpacing(2)
        _parameters_widget_layout.setContentsMargins(0, 0, 0, 0)

        _top_widget_layout = QtImport.QHBoxLayout(_top_widget)
        _top_widget_layout.addWidget(_parameters_widget)
        _top_widget_layout.addWidget(self.snapshot_widget)
        _top_widget_layout.setSpacing(2)
        _top_widget_layout.addStretch(0)
        _top_widget_layout.setContentsMargins(0, 0, 0, 0)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(_top_widget)
        _main_vlayout.addWidget(self.mca_spectrum_widget)
        _main_vlayout.setSpacing(2)
        _main_vlayout.setContentsMargins(0, 0, 0, 0)

        # SizePolicies -------------------------------------------------------
        self.mca_spectrum_widget.setSizePolicy(
            QtImport.QSizePolicy.Expanding, QtImport.QSizePolicy.Expanding
        )
        _top_widget.setSizePolicy(
            QtImport.QSizePolicy.Expanding, QtImport.QSizePolicy.Fixed
        )

        # Qt signal/slot connections ------------------------------------------
        self.data_path_widget.data_path_layout.prefix_ledit.textChanged.connect(
            self._prefix_ledit_change
        )
        self.data_path_widget.data_path_layout.run_number_ledit.textChanged.connect(
            self._run_number_ledit_change
        )
        self.count_time_ledit.textChanged.connect(self._count_time_ledit_change)

        # Other ---------------------------------------------------------------
        self.data_path_widget.data_path_layout.compression_cbox.setVisible(False)

        if api.xrf_spectrum is None:
            api.xrf_spectrum.connect(
                "xrfSpectrumFinished", self.spectrum_finished
            )

    def _prefix_ledit_change(self, new_value):
        self.xrf_spectrum_model.set_name(str(new_value))
        self._tree_view_item.setText(0, self.xrf_spectrum_model.get_display_name())

    def _run_number_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self.xrf_spectrum_model.set_number(int(new_value))
            self._tree_view_item.setText(0, self.xrf_spectrum_model.get_display_name())

    def _count_time_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self.xrf_spectrum_model.set_count_time(float(new_value))

    def tab_changed(self):
        if self._tree_view_item:
            self.populate_widget(self._tree_view_item)

    def populate_widget(self, item):
        self._tree_view_item = item
        self.xrf_spectrum_model = item.get_model()
        executed = self.xrf_spectrum_model.is_executed()

        self.data_path_widget.setEnabled(not executed)
        self.other_parameters_gbox.setEnabled(not executed)
        # self.mca_spectrum_widget.setEnabled(executed)

        if executed:
            result = self.xrf_spectrum_model.get_spectrum_result()
            self.mca_spectrum_widget.set_data(
                result.mca_data, result.mca_calib, result.mca_config
            )
        else:
            self.mca_spectrum_widget.clear()

        self.data_path_widget.update_data_model(self.xrf_spectrum_model.path_template)
        self.count_time_ledit.setText(str(self.xrf_spectrum_model.count_time))

        image = self.xrf_spectrum_model.centred_position.snapshot_image
        self.snapshot_widget.display_snapshot(image, width=400)

    def spectrum_finished(self, mca_data, mca_calib, mca_config):
        self.mca_spectrum_widget.set_data(mca_data, mca_calib, mca_config)
class AdvancedParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="advanced_parameters_widget"):
        QtImport.QWidget.__init__(self, parent)

        # Internal values -----------------------------------------------------
        self._data_collection = None
        self._tree_view_item = None
        self._acquisition_mib = None

        # Properties ----------------------------------------------------------

        # Signals -------------------------------------------------------------

        # Slots ---------------------------------------------------------------

        # Graphic elements ----------------------------------------------------
        _dc_parameters_widget = QtImport.QWidget(self)
        self._data_path_widget = DataPathWidget(_dc_parameters_widget)
        self._acq_widget = AcquisitionWidget(_dc_parameters_widget, layout="horizontal")

        # Layout --------------------------------------------------------------
        _dc_parameters_widget_layout = QtImport.QVBoxLayout(_dc_parameters_widget)
        _dc_parameters_widget_layout.addWidget(self._data_path_widget)
        _dc_parameters_widget_layout.addWidget(self._acq_widget)
        _dc_parameters_widget_layout.setSpacing(2)
        _dc_parameters_widget_layout.addStretch(10)
        _dc_parameters_widget_layout.setContentsMargins(0, 0, 0, 0)

        _main_hlayout = QtImport.QHBoxLayout(self)
        _main_hlayout.addWidget(_dc_parameters_widget)
        _main_hlayout.setSpacing(2)
        _main_hlayout.setContentsMargins(2, 2, 2, 2)
        _main_hlayout.addStretch(0)

        # Qt signal/slot connections ------------------------------------------
        # self._acq_widget.acqParametersChangedSignal.\
        #     connect(self.acq_parameters_changed)
        # self._data_path_widget.pathTemplateChangedSignal.\
        #     connect(self.acq_parameters_changed)
        self._acq_widget.madEnergySelectedSignal.connect(self.mad_energy_selected)

        # Ohter ---------------------------------------------------------------
        self._acq_widget.use_osc_start(False)
        self._acq_widget.acq_widget_layout.mad_cbox.hide()
        self._acq_widget.acq_widget_layout.energies_combo.hide()
        self._acq_widget.acq_widget_layout.shutterless_cbx.hide()

    def init_api(self):
        self._acq_widget.init_api()

    def mad_energy_selected(self, name, energy, state):
        path_template = self._data_collection.acquisitions[0].path_template

        if state:
            path_template.mad_prefix = name
        else:
            path_template.mad_prefix = ""

        run_number = api.queue_model.get_next_run_number(
            path_template
        )

        self._data_path_widget.set_run_number(run_number)
        self._data_path_widget.set_prefix(path_template.base_prefix)
        model = self._tree_view_item.get_model()
        model.set_name(path_template.get_prefix())
        self._tree_view_item.setText(0, model.get_name())

    def tab_changed(self):
        if self._tree_view_item:
            self.populate_widget(self._tree_view_item, None)

    def populate_widget(self, tree_view_item, data_collection):
        self._tree_view_item = tree_view_item
        self._data_collection = data_collection

        # if isinstance(tree_view_item, queue_item.XrayCenteringQueueItem):
        #    self._data_collection = tree_view_item.get_model().reference_image_collection
        # else:
        #    self._data_collection = tree_view_item.get_model()
        executed = self._data_collection.is_executed()

        self._acq_widget.setEnabled(not executed)
        self._data_path_widget.setEnabled(not executed)

        self._acquisition_mib = DataModelInputBinder(
            self._data_collection.acquisitions[0].acquisition_parameters
        )

        # The acq_widget sends a signal to the path_widget, and it relies
        # on that both models upto date, we need to refactor this part
        # so that both models are set before taking ceratin actions.
        # This workaround, works for the time beeing.
        self._data_path_widget._data_model = self._data_collection.acquisitions[
            0
        ].path_template
        self._data_path_widget.update_data_model(
            self._data_collection.acquisitions[0].path_template
        )

        self._acq_widget.update_data_model(
            self._data_collection.acquisitions[0].acquisition_parameters,
            self._data_collection.acquisitions[0].path_template,
        )
        # self._acq_widget.use_osc_start(False)

        self._acq_widget.acq_widget_layout.num_images_ledit.setDisabled(
            data_collection.is_mesh()
        )
        invalid = self._acquisition_mib.validate_all()
        if invalid:
            msg = (
                "This data collection has one or more incorrect parameters,"
                + " correct the fields marked in red to solve the problem."
            )
            logging.getLogger("GUI").warning(msg)
class CreateGphlWorkflowWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "GphlWorkflow"
        )

        if not name:
            self.setObjectName("create_gphl_workflow_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.current_prefix = None

        self.init_models()

    def _initialize_graphics(self):
        # Graphic elements ----------------------------------------------------
        self._workflow_type_widget = QtImport.QGroupBox("Workflow type", self)

        self._workflow_cbox = QtImport.QComboBox(self._workflow_type_widget)
        self._gphl_acq_widget = QtImport.QGroupBox("Acquisition", self)
        self._gphl_acq_param_widget = GphlAcquisitionWidget(
            self._gphl_acq_widget, "gphl_acquisition_parameter_widget"
        )
        self._gphl_diffractcal_widget = GphlDiffractcalWidget(
            self._gphl_acq_widget, "gphl_diffractcal_widget",
        )

        self._data_path_widget = DataPathWidget(
            self, "create_dc_path_widget", layout="vertical"
        )
        data_path_layout = self._data_path_widget.data_path_layout
        data_path_layout.file_name_label.hide()
        data_path_layout.file_name_value_label.hide()
        data_path_layout.run_number_label.hide()
        data_path_layout.run_number_ledit.hide()
        data_path_layout.prefix_ledit.setReadOnly(True)
        data_path_layout.folder_ledit.setReadOnly(True)

        # Layout --------------------------------------------------------------
        _workflow_type_vlayout = QtImport.QVBoxLayout(self._workflow_type_widget)
        _workflow_type_vlayout.addWidget(self._workflow_cbox)
        _gphl_acq_vlayout = QtImport.QVBoxLayout(self._gphl_acq_widget)
        _gphl_acq_vlayout.addWidget(self._gphl_acq_param_widget)
        _gphl_acq_vlayout.addWidget(self._gphl_diffractcal_widget)
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._workflow_type_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._gphl_acq_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(2)
        _main_vlayout.setContentsMargins(0, 0, 0, 0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._workflow_cbox.currentIndexChanged.connect(self.workflow_selected)

        # set up popup data dialog
        self.gphl_data_dialog = GphlDataDialog(self, "GPhL Workflow Data")
        self.gphl_data_dialog.setModal(True)
        self.gphl_data_dialog.continueClickedSignal.connect(self.data_acquired)

    def initialise_workflows(self):

        workflow_hwobj = api.gphl_workflow
        if workflow_hwobj is not None:
            workflow_hwobj.setup_workflow_object()
            workflow_names = list(workflow_hwobj.get_available_workflows())
            self._initialize_graphics()
            self._workflow_cbox.clear()
            for workflow_name in workflow_names:
                self._workflow_cbox.addItem(workflow_name)
            self.workflow_selected()
            workflow_hwobj.connect(
                "gphlParametersNeeded", self.gphl_data_dialog.open_dialog
            )

    def workflow_selected(self):
        # necessary as this comes in as a QString object
        name = str(self._workflow_cbox.currentText())
        # if reset or name != self._previous_workflow:
        xx = self._workflow_cbox
        xx.setCurrentIndex(xx.findText(name))
        self.init_models()
        self._data_path_widget.update_data_model(self._path_template)

        parameters = api.gphl_workflow.get_available_workflows()[name]
        strategy_type = parameters.get("strategy_type")
        if strategy_type == "transcal":
            self._gphl_acq_widget.hide()
        elif strategy_type == "diffractcal":
            # TODO update this
            self._gphl_diffractcal_widget.populate_widget()
            self._gphl_acq_widget.show()
            self._gphl_diffractcal_widget.show()
            self._gphl_acq_param_widget.hide()
        else:
            # acquisition type strategy
            self._gphl_acq_param_widget.populate_widget()
            self._gphl_acq_widget.show()
            self._gphl_diffractcal_widget.hide()
            self._gphl_acq_param_widget.show()

        prefix = parameters.get("prefix")
        if prefix is not None:
            self.current_prefix = prefix

    def data_acquired(self):
        """Data gathered from popup, continue execution"""
        pass

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        wf_model = tree_item.get_model()

        if isinstance(tree_item, queue_item.SampleQueueItem):
            self.init_models()

        else:
            if isinstance(tree_item, queue_item.GphlWorkflowQueueItem):
                if tree_item.get_model().is_executed():
                    self.setDisabled(True)
                else:
                    self.setDisabled(False)

                if wf_model.get_path_template():
                    self._path_template = wf_model.get_path_template()

                self._data_path_widget.update_data_model(self._path_template)

            elif isinstance(tree_item, queue_item.BasketQueueItem):
                self.setDisabled(False)
            elif not isinstance(tree_item, queue_item.DataCollectionGroupQueueItem):
                self.setDisabled(True)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._init_models()

    def _init_models(self):
        pass

    def continue_button_click(self, sample_items, checked_items):
        """Intercepts the datacollection continue_button click for parameter setting"""
        tree_brick = self._tree_brick
        if tree_brick:
            for item in checked_items:
                model = item.get_model()
                if isinstance(model, queue_model_objects.GphlWorkflow):
                    dialog = tree_brick.dc_tree_widget.confirm_dialog
                    ss = dialog.conf_dialog_layout.take_snapshots_combo.currentText()
                    model.set_snapshot_count(int(ss) if ss else 0)

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.
    def _create_task(self, sample, shape):
        tasks = []

        path_template = self._create_path_template(sample, self._path_template)
        path_template.num_files = 0
        path_template.compression = False

        workflow_hwobj = api.gphl_workflow
        if workflow_hwobj.get_state() == States.OFF:
            # We will be setting up the connection now - time to connect to quit
            QtImport.QApplication.instance().aboutToQuit.connect(
                workflow_hwobj.shutdown
            )

            tree_brick = self._tree_brick
            if tree_brick:
                tree_brick.dc_tree_widget.confirm_dialog.continueClickedSignal.connect(
                    self.continue_button_click
                )

        wf = queue_model_objects.GphlWorkflow(workflow_hwobj)
        wf_type = str(self._workflow_cbox.currentText())
        wf.set_type(wf_type)

        if self.current_prefix:
            path_template.base_prefix = self.current_prefix
        wf.path_template = path_template
        wf.set_name(wf.path_template.get_prefix())
        wf.set_number(wf.path_template.run_number)

        wf_parameters = workflow_hwobj.get_available_workflows()[wf_type]
        strategy_type = wf_parameters.get("strategy_type")
        wf.set_interleave_order(wf_parameters.get("interleaveOrder", ""))
        if strategy_type.startswith("transcal"):
            pass

        elif strategy_type.startswith("diffractcal"):
            ss = self._gphl_diffractcal_widget.get_parameter_value("test_crystal")
            crystal_data = self._gphl_diffractcal_widget.test_crystals.get(ss)
            wf.set_space_group(crystal_data.space_group)
            wf.set_cell_parameters(
                tuple(
                    getattr(crystal_data, tag)
                    for tag in ("a", "b", "c", "alpha", "beta", "gamma")
                )
            )
        else:
            # Coulds be native_... phasing_... etc.

            wf.set_space_group(
                self._gphl_acq_param_widget.get_parameter_value("space_group")
            )
            tag = self._gphl_acq_param_widget.get_parameter_value("crystal_system")
            crystal_system, point_group = None, None
            if tag:
                data = self._gphl_acq_param_widget._CRYSTAL_SYSTEM_DATA[tag]
                crystal_system = data.crystal_system
                point_groups = data.point_groups
                if len(point_groups) == 1 or point_groups[0] == "32":
                    # '32' is a special case; '312' and '321' are also returned as '32'
                    point_group = point_groups[0]
            wf.set_point_group(point_group)
            wf.set_crystal_system(crystal_system)
            wf.set_beam_energies(wf_parameters["beam_energies"])

        tasks.append(wf)

        return tasks
class CreateGphlWorkflowWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name,
                                QtImport.Qt.WindowFlags(fl), "GphlWorkflow")

        if not name:
            self.setObjectName("create_gphl_workflow_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.current_prefix = None

        self.init_models()

    def _initialize_graphics(self):
        # Graphic elements ----------------------------------------------------
        self._workflow_type_widget = QtImport.QGroupBox("Workflow type", self)

        self._workflow_cbox = QtImport.QComboBox(self._workflow_type_widget)
        self._gphl_acq_widget = QtImport.QGroupBox("Acquisition", self)
        self._gphl_acq_param_widget = GphlAcquisitionWidget(
            self._gphl_acq_widget, "gphl_acquisition_parameter_widget")
        self._gphl_diffractcal_widget = GphlDiffractcalWidget(
            self._gphl_acq_widget, "gphl_diffractcal_widget")

        self._data_path_widget = DataPathWidget(self,
                                                "create_dc_path_widget",
                                                layout="vertical")
        data_path_layout = self._data_path_widget.data_path_layout
        data_path_layout.file_name_label.hide()
        data_path_layout.file_name_value_label.hide()
        data_path_layout.run_number_label.hide()
        data_path_layout.run_number_ledit.hide()
        data_path_layout.folder_ledit.setReadOnly(True)

        # Layout --------------------------------------------------------------
        _workflow_type_vlayout = QtImport.QVBoxLayout(
            self._workflow_type_widget)
        _workflow_type_vlayout.addWidget(self._workflow_cbox)
        _gphl_acq_vlayout = QtImport.QVBoxLayout(self._gphl_acq_widget)
        _gphl_acq_vlayout.addWidget(self._gphl_acq_param_widget)
        _gphl_acq_vlayout.addWidget(self._gphl_diffractcal_widget)
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._workflow_type_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._gphl_acq_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(2)
        _main_vlayout.setContentsMargins(0, 0, 0, 0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._workflow_cbox.currentIndexChanged.connect(self.workflow_selected)

        # set up popup data dialog
        self.gphl_data_dialog = GphlDataDialog(self, "GPhL Workflow Data")
        self.gphl_data_dialog.setModal(True)
        self.gphl_data_dialog.continueClickedSignal.connect(self.data_acquired)

    def initialise_workflows(self):

        workflow_hwobj = HWR.beamline.gphl_workflow
        if workflow_hwobj is not None:
            workflow_hwobj.setup_workflow_object()
            workflow_names = list(workflow_hwobj.get_available_workflows())
            self._initialize_graphics()
            self._workflow_cbox.clear()
            for workflow_name in workflow_names:
                self._workflow_cbox.addItem(workflow_name)
            self.workflow_selected()
            workflow_hwobj.connect("gphlParametersNeeded",
                                   self.gphl_data_dialog.open_dialog)

    def workflow_selected(self):
        # necessary as this comes in as a QString object
        name = ConvertUtils.text_type(self._workflow_cbox.currentText())
        # if reset or name != self._previous_workflow:
        xx0 = self._workflow_cbox
        xx0.setCurrentIndex(xx0.findText(name))
        self.init_models()
        self._data_path_widget.update_data_model(self._path_template)

        parameters = HWR.beamline.gphl_workflow.get_available_workflows()[name]
        strategy_type = parameters.get("strategy_type")
        if strategy_type == "transcal":
            # NB Once we do not have to set unique prefixes, this should be readOnly
            self._data_path_widget.data_path_layout.prefix_ledit.setReadOnly(
                False)
            self._gphl_acq_widget.hide()
        elif strategy_type == "diffractcal":
            # TODO update this
            self._data_path_widget.data_path_layout.prefix_ledit.setReadOnly(
                True)
            self._gphl_diffractcal_widget.populate_widget()
            self._gphl_acq_widget.show()
            self._gphl_diffractcal_widget.show()
            self._gphl_acq_param_widget.hide()
        else:
            # acquisition type strategy
            self._data_path_widget.data_path_layout.prefix_ledit.setReadOnly(
                True)
            self._gphl_acq_param_widget.populate_widget()
            self._gphl_acq_widget.show()
            self._gphl_diffractcal_widget.hide()
            self._gphl_acq_param_widget.show()

        prefix = parameters.get("prefix")
        if prefix is not None:
            self.current_prefix = prefix

    def data_acquired(self):
        """Data gathered from popup, continue execution"""
        pass

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        wf_model = tree_item.get_model()

        if isinstance(tree_item, queue_item.SampleQueueItem):
            self.init_models()

        else:
            if isinstance(tree_item, queue_item.GphlWorkflowQueueItem):
                if tree_item.get_model().is_executed():
                    self.setDisabled(True)
                else:
                    self.setDisabled(False)

                if wf_model.get_path_template():
                    self._path_template = wf_model.get_path_template()

                self._data_path_widget.update_data_model(self._path_template)

            elif isinstance(tree_item, queue_item.BasketQueueItem):
                self.setDisabled(False)
            elif not isinstance(tree_item,
                                queue_item.DataCollectionGroupQueueItem):
                self.setDisabled(True)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._init_models()

    def _init_models(self):
        pass

    def continue_button_click(self, sample_items, checked_items):
        """Intercepts the datacollection continue_button click for parameter setting"""
        tree_brick = self._tree_brick
        if tree_brick:
            for item in checked_items:
                model = item.get_model()
                if isinstance(model, queue_model_objects.GphlWorkflow):
                    dialog = tree_brick.dc_tree_widget.confirm_dialog
                    ss0 = dialog.conf_dialog_layout.take_snapshots_combo.currentText(
                    )
                    model.set_snapshot_count(int(ss0) if ss0 else 0)

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.
    def _create_task(self, sample, shape):
        tasks = []

        path_template = self._create_path_template(sample, self._path_template)
        path_template.num_files = 0
        path_template.compression = False

        workflow_hwobj = HWR.beamline.gphl_workflow
        if workflow_hwobj.get_state() == States.OFF:
            # We will be setting up the connection now - time to connect to quit
            QtImport.QApplication.instance().aboutToQuit.connect(
                workflow_hwobj.shutdown)

            tree_brick = self._tree_brick
            if tree_brick:
                tree_brick.dc_tree_widget.confirm_dialog.continueClickedSignal.connect(
                    self.continue_button_click)

        wf = queue_model_objects.GphlWorkflow()
        wf_type = ConvertUtils.text_type(self._workflow_cbox.currentText())
        wf.set_type(wf_type)

        if self.current_prefix:
            path_template.base_prefix = self.current_prefix
        wf.path_template = path_template
        wf.set_name(wf.path_template.get_prefix())
        wf.set_number(wf.path_template.run_number)

        wf_parameters = workflow_hwobj.get_available_workflows()[wf_type]
        strategy_type = wf_parameters.get("strategy_type")
        wf.set_interleave_order(wf_parameters.get("interleaveOrder", ""))
        if strategy_type.startswith("transcal"):
            pass

        elif strategy_type.startswith("diffractcal"):
            ss0 = self._gphl_diffractcal_widget.get_parameter_value(
                "test_crystal")
            crystal_data = self._gphl_diffractcal_widget.test_crystals.get(ss0)
            wf.set_space_group(crystal_data.space_group)
            wf.set_cell_parameters(
                tuple(
                    getattr(crystal_data, tag)
                    for tag in ("a", "b", "c", "alpha", "beta", "gamma")))
            tag = self._gphl_acq_param_widget.get_parameter_value(
                "dose_budget")
            wf.set_dose_budget(
                HWR.beamline.gphl_workflow.dose_budgets.get(tag))
            # The entire strategy runs as a 'characterisation'
            wf.set_characterisation_budget_fraction(1.0)
        else:
            # Coulds be native_... phasing_... etc.

            wf.set_space_group(
                self._gphl_acq_param_widget.get_parameter_value("space_group"))
            wf.set_characterisation_strategy(
                self._gphl_acq_param_widget.get_parameter_value(
                    "characterisation_strategy"))
            tag = self._gphl_acq_param_widget.get_parameter_value(
                "crystal_system")
            crystal_system, point_group = None, None
            if tag:
                data = self._gphl_acq_param_widget._CRYSTAL_SYSTEM_DATA[tag]
                crystal_system = data.crystal_system
                point_groups = data.point_groups
                if len(point_groups) == 1 or point_groups[0] == "32":
                    # '32' is a special case; '312' and '321' are also returned as '32'
                    point_group = point_groups[0]
            wf.set_point_group(point_group)
            wf.set_crystal_system(crystal_system)
            wf.set_beam_energies(wf_parameters["beam_energies"])
            tag = self._gphl_acq_param_widget.get_parameter_value(
                "dose_budget")
            wf.set_dose_budget(
                HWR.beamline.gphl_workflow.dose_budgets.get(tag))
            val = self._gphl_acq_param_widget.get_parameter_value(
                "relative_rad_sensitivity")
            wf.set_relative_rad_sensitivity(val)
            wf.set_characterisation_budget_fraction(
                HWR.beamline.gphl_workflow.getProperty(
                    "characterisation_budget_percent", 5.0) / 100.0)

        tasks.append(wf)

        return tasks
Beispiel #11
0
class CreateCharWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name, fl, "Characterisation")
        self.setObjectName("create_char_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self._vertical_dimension_widget = None
        self._current_selected_item = None
        self._char = None
        self._char_params = None

        self.init_models()
        self._char_params_mib = DataModelInputBinder(self._char_params)

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionWidgetSimple(
            self,
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(self,
                                                data_model=self._path_template,
                                                layout="vertical")

        self._vertical_dimension_widget = QtImport.load_ui_file(
            "vertical_crystal_dimension_widget_layout.ui")

        self._char_widget = QtImport.load_ui_file(
            "characterise_simple_widget_vertical_layout.ui")

        self._comments_widget = CommentsWidget(self)

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._char_widget)
        _main_vlayout.addWidget(self._vertical_dimension_widget)
        _main_vlayout.addWidget(self._comments_widget)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        _main_vlayout.setSpacing(6)
        _main_vlayout.addStretch(0)

        # SizePolicies --------------------------------------------------------
        self._comments_widget.setFixedHeight(100)

        # Qt signal/slot connections ------------------------------------------
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed)
        self._acq_widget.acqParametersChangedSignal.connect(
            self.acq_parameters_changed)

        self._vertical_dimension_widget.space_group_ledit.activated.connect(
            self._space_group_change)

        self._char_widget.characterisation_gbox.toggled.connect(
            self.characterisation_gbox_toggled)
        self._char_widget.wait_result_cbx.toggled.connect(
            self.wait_results_cbx_toggled)
        self._char_widget.execute_plan_cbx.toggled.connect(
            self.run_diffraction_plan_cbx_toggled)

        # Other ---------------------------------------------------------------
        if False:
            self._char_params_mib.bind_value_update(
                "opt_sad", self._char_widget.optimised_sad_cbx, bool, None)

            self._char_params_mib.bind_value_update(
                "account_rad_damage", self._char_widget.account_rad_dmg_cbx,
                bool, None)

            self._char_params_mib.bind_value_update(
                "strategy_complexity", self._char_widget.start_comp_cbox, int,
                None)

            self._char_params_mib.bind_value_update(
                "max_crystal_vdim",
                self._vertical_dimension_widget.max_vdim_ledit,
                float,
                QtImport.QDoubleValidator(0.0, 1000, 2, self),
            )

            self._char_params_mib.bind_value_update(
                "min_crystal_vdim",
                self._vertical_dimension_widget.min_vdim_ledit,
                float,
                QtImport.QDoubleValidator(0.0, 1000, 2, self),
            )

            self._char_params_mib.bind_value_update(
                "min_crystal_vphi",
                self._vertical_dimension_widget.min_vphi_ledit,
                float,
                QtImport.QDoubleValidator(0.0, 1000, 2, self),
            )

            self._char_params_mib.bind_value_update(
                "max_crystal_vphi",
                self._vertical_dimension_widget.max_vphi_ledit,
                float,
                QtImport.QDoubleValidator(0.0, 1000, 2, self),
            )

        self._vertical_dimension_widget.space_group_ledit.addItems(
            XTAL_SPACEGROUPS)

        self._data_path_widget.data_path_layout.compression_cbox.setVisible(
            False)

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def use_induced_burn(self, state):
        self._acquisition_parameters.induce_burn = state

    def _space_group_change(self, index):
        self._char_params.space_group = queue_model_enumerables.XTAL_SPACEGROUPS[
            index]

    def _set_space_group(self, space_group):
        index = 0

        if self._vertical_dimension_widget:
            if space_group in XTAL_SPACEGROUPS:
                index = XTAL_SPACEGROUPS.index(space_group)

            self._space_group_change(index)
            self._vertical_dimension_widget.space_group_ledit.setCurrentIndex(
                index)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._init_models()

    def _init_models(self):
        self._char = queue_model_objects.Characterisation()
        self._char_params = self._char.characterisation_parameters
        self._processing_parameters = queue_model_objects.ProcessingParameters(
        )
        self._set_space_group(self._processing_parameters.space_group)

        self._acquisition_parameters = (
            HWR.beamline.get_default_acquisition_parameters("default"))

        self._char_params = (HWR.beamline.get_default_acquisition_parameters(
            "characterisation"))
        self._path_template.reference_image_prefix = "ref"
        # The num images drop down default value is 1
        # we would like it to be 2
        self._acquisition_parameters.num_images = 2
        self._char.characterisation_software = (
            queue_model_enumerables.COLLECTION_ORIGIN.EDNA)
        self._path_template.num_files = 2
        self._path_template.compression = False
        self._acquisition_parameters.shutterless = False

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            if self._char_params.space_group == "":
                sample_model = tree_item.get_model()
                self._set_space_group(
                    sample_model.processing_parameters.space_group)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.CharacterisationQueueItem):
            if tree_item.get_model().is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            self._char = tree_item.get_model()

            if self._char.get_path_template():
                self._path_template = self._char.get_path_template()

            self._data_path_widget.update_data_model(self._path_template)

            data_collection = self._char.reference_image_collection

            self._char_params = self._char.characterisation_parameters
            self._char_params_mib.set_model(self._char_params)

            self._acquisition_parameters = data_collection.acquisitions[
                0].acquisition_parameters

            self._acq_widget.update_data_model(self._acquisition_parameters,
                                               self._path_template)
            # self.get_acquisition_widget().use_osc_start(True)

            if len(data_collection.acquisitions) == 1:
                HWR.beamline.sample_view.select_shape_with_cpos(
                    self._acquisition_parameters.centred_position)

            self._processing_parameters = data_collection.processing_parameters
        else:
            self.setDisabled(True)

    def update_processing_parameters(self, crystal):
        self._processing_parameters.space_group = crystal.space_group
        self._char_params.space_group = crystal.space_group
        self._processing_parameters.cell_a = crystal.cell_a
        self._processing_parameters.cell_alpha = crystal.cell_alpha
        self._processing_parameters.cell_b = crystal.cell_b
        self._processing_parameters.cell_beta = crystal.cell_beta
        self._processing_parameters.cell_c = crystal.cell_c
        self._processing_parameters.cell_gamma = crystal.cell_gamma

    def approve_creation(self):
        result = CreateTaskBase.approve_creation(self)
        selected_shapes = HWR.beamline.sample_view.get_selected_shapes()

        for shape in selected_shapes:
            if isinstance(shape, GraphicsItemPoint):
                result = True
        return result

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. when a data collection group is selected.
    def _create_task(self, sample, shape, comments=None):
        tasks = []

        if not shape or not isinstance(shape, GraphicsItemPoint):
            cpos = queue_model_objects.CentredPosition()
            cpos.snapshot_image = HWR.beamline.sample_view.get_scene_snapshot()
        else:
            # Shapes selected and sample is mounted, get the
            # centred positions for the shapes
            snapshot = HWR.beamline.sample_view.get_scene_snapshot(shape)
            cpos = copy.deepcopy(shape.get_centred_position())
            cpos.snapshot_image = snapshot

        char_params = copy.deepcopy(self._char_params)
        acq = self._create_acq(sample)
        dc = queue_model_objects.DataCollection([acq], sample.crystals[0],
                                                self._processing_parameters)

        # Reference images for characterisations should be taken 90 deg apart
        # this is achived by setting overap to -89
        acq.acquisition_parameters.overlap = -89
        acq.acquisition_parameters.centred_position = cpos

        dc.acquisitions[0] = acq
        dc.experiment_type = queue_model_enumerables.EXPERIMENT_TYPE.EDNA_REF
        dc.run_processing_parallel = False

        char = queue_model_objects.Characterisation(dc, char_params)
        char.set_name(dc.acquisitions[0].path_template.get_prefix())
        char.set_number(dc.acquisitions[0].path_template.run_number)
        char.run_characterisation = self._char_widget.characterisation_gbox.isChecked(
        )
        char.wait_result = self._char_widget.wait_result_cbx.isChecked()
        char.run_diffraction_plan = self._char_widget.execute_plan_cbx.isChecked(
        )
        char.diff_plan_compression = self._tree_brick.compression_state

        tasks.append(char)
        self._path_template.run_number += 1

        if HWR.beamline.flux.get_value() < 1e9:
            logging.getLogger("GUI").error(
                "No flux reading is available! " +
                "Characterisation result may be wrong. "
                "Measure flux before running the characterisation.")

        return tasks

    def characterisation_gbox_toggled(self, state):
        if self._char:
            self._char.run_characterisation = state

    def wait_results_cbx_toggled(self, state):
        if self._char:
            self._char.wait_result = state

    def run_diffraction_plan_cbx_toggled(self, state):
        if self._char:
            self._char.run_diffraction_plan = state
Beispiel #12
0
class CreateDiscreteWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name,
                                QtImport.Qt.WindowFlags(fl), "Standard")

        if not name:
            self.setObjectName("create_discrete_widget")

        # Internal variables --------------------------------------------------
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        self._processing_widget = ProcessingWidget(
            self, data_model=self._processing_parameters)

        self._comments_widget = CommentsWidget(self)
        self._comments_widget.setHidden(True)

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._processing_widget)
        _main_vlayout.addWidget(self._comments_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------
        self._comments_widget.setFixedHeight(100)

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(
            self.acq_parameters_changed)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed)

        self._acq_widget.madEnergySelectedSignal.connect(
            self.mad_energy_selected)
        self._processing_widget.enableProcessingSignal.connect(
            self._run_processing_toggled)

        # Other ---------------------------------------------------------------
        self._processing_widget.processing_widget.run_processing_parallel_cbox.\
            setChecked(HWR.beamline.run_processing_parallel)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._processing_parameters = queue_model_objects.ProcessingParameters(
        )

        has_shutter_less = HWR.beamline.detector.has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = \
            HWR.beamline.get_default_acquisition_parameters()

    def set_tunable_energy(self, state):
        self._acq_widget.set_tunable_energy(state)

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            sample_model = tree_item.get_model()
            # self._processing_parameters = copy.deepcopy(self._processing_parameters)
            self._processing_parameters = sample_model.processing_parameters
            self._processing_widget.update_data_model(
                self._processing_parameters)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem):
            dc_model = tree_item.get_model()
            self._acq_widget.use_kappa(False)

            if not dc_model.is_helical():
                if dc_model.is_executed():
                    self.setDisabled(True)
                else:
                    self.setDisabled(False)

                sample_data_model = self.get_sample_item(tree_item).get_model()
                energy_scan_result = sample_data_model.crystals[
                    0].energy_scan_result
                self._acq_widget.set_energies(energy_scan_result)

                # self._acq_widget.disable_inverse_beam(True)

                self._path_template = dc_model.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)

                self._acquisition_parameters =\
                    dc_model.acquisitions[0].acquisition_parameters

                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template)
                # self.get_acquisition_widget().use_osc_start(True)
                if len(dc_model.acquisitions) == 1:
                    HWR.beamline.sample_view.select_shape_with_cpos(
                        self._acquisition_parameters.centred_position)

                self._processing_parameters = dc_model.processing_parameters
                self._processing_widget.update_data_model(
                    self._processing_parameters)
            else:
                self.setDisabled(True)
        else:
            self.setDisabled(True)

    def approve_creation(self):
        result = CreateTaskBase.approve_creation(self)
        return result

    def _create_task(self, sample, shape, comments=None):
        """
        Called by the owning widget (task_toolbox_widget) to create
        a collection. When a data collection group is selected.
        :param sample:
        :param shape:
        :return:
        """
        tasks = []

        if isinstance(shape, GraphicsItemPoint):
            snapshot = HWR.beamline.sample_view.get_snapshot(shape)
            cpos = copy.deepcopy(shape.get_centred_position())
            cpos.snapshot_image = snapshot
        else:
            cpos = queue_model_objects.CentredPosition()
            cpos.snapshot_image = HWR.beamline.sample_view.get_snapshot()

        tasks.extend(self.create_dc(sample, cpos=cpos))
        self._path_template.run_number += 1

        return tasks

    def create_dc(
        self,
        sample,
        run_number=None,
        start_image=None,
        num_images=None,
        osc_start=None,
        sc=None,
        cpos=None,
        inverse_beam=False,
    ):
        tasks = []

        # Acquisition for start position
        acq = self._create_acq(sample)

        if run_number:
            acq.path_template.run_number = run_number

        if start_image:
            acq.acquisition_parameters.first_image = start_image
            acq.path_template.start_num = start_image

        if num_images:
            acq.acquisition_parameters.num_images = num_images
            acq.path_template.num_files = num_images

        if osc_start:
            acq.acquisition_parameters.osc_start = osc_start

        if inverse_beam:
            acq.acquisition_parameters.inverse_beam = False

        acq.acquisition_parameters.centred_position = cpos

        processing_parameters = copy.deepcopy(self._processing_parameters)
        data_collection = queue_model_objects.DataCollection(
            [acq], sample.crystals[0], processing_parameters)

        data_collection.set_name(acq.path_template.get_prefix())
        data_collection.set_number(acq.path_template.run_number)
        data_collection.experiment_type = queue_model_enumerables.EXPERIMENT_TYPE.NATIVE

        run_processing_after, run_processing_parallel = \
            self._processing_widget.get_processing_state()
        data_collection.run_processing_after = run_processing_after
        if run_processing_parallel:
            data_collection.run_processing_parallel = "Undefined"

        tasks.append(data_collection)

        return tasks
class CreateStillScanWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, flags=0):

        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(flags), "Still"
        )

        if not name:
            self.setObjectName("create_still_scan_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionStillWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        self._col_seq_widget = SSXSequenceWidget(self)

        self._processing_widget = ProcessingWidget(
            self, data_model=self._processing_parameters
        )

        #self._comment_widget = CommentWidget(self)

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._col_seq_widget)
        _main_vlayout.addWidget(self._processing_widget)
        #_main_vlayout.addWidget(self._comment_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(
            self.acq_parameters_changed
        )
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self._processing_widget.enableProcessingSignal.connect(
            self._run_processing_toggled
        )

        # Other ---------------------------------------------------------------
        self._processing_widget.processing_widget.run_online_processing_cbox.setChecked(
            HWR.beamline.run_online_processing
        )

        #Rename to self._processing_widget.layout
        self._processing_widget.processing_widget.resolution_cutoff_label.setHidden(False)
        self._processing_widget.processing_widget.resolution_cutoff_ledit.setHidden(False)
        self._processing_widget.processing_widget.pdb_file_label.setHidden(False)
        self._processing_widget.processing_widget.pdb_file_ledit.setHidden(False)
        self._processing_widget.processing_widget.pdb_file_browse_button.setHidden(False)

    def use_osc_start(self, status):
        """
        Enables osc start QLineEdit
        :param status: boolean
        :return:
        """
        return

    def update_exp_time_limits(self):
        """
        Updates exposure time limits
        :return:
        """
        return

    def init_models(self):
        """
        Inits data model
        :return: None
        """
        CreateTaskBase.init_models(self)
        self._processing_parameters = queue_model_objects.ProcessingParameters()

        has_shutter_less = HWR.beamline.detector.has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = (
            HWR.beamline.get_default_acquisition_parameters()
        )
        self._acquisition_parameters.num_triggers = 1
        self._acquisition_parameters.num_images_per_trigger = 1

    def set_tunable_energy(self, state):
        """
        Sets tunable energy
        :param state: boolean
        :return: None
        """
        self._acq_widget.set_tunable_energy(state)

    def single_item_selection(self, tree_item):
        """
        Method called when a queue item in the tree is selected
        :param tree_item: queue_item
        :return: None
        """
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            sample_model = tree_item.get_model()
            # self._processing_parameters = copy.deepcopy(self._processing_parameters)
            self._processing_parameters = sample_model.processing_parameters
            self._processing_widget.update_data_model(self._processing_parameters)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem):
            dc_model = tree_item.get_model()
            self._acq_widget.use_kappa(False)

            if not dc_model.is_helical():
                if dc_model.is_executed():
                    self.setDisabled(True)
                else:
                    self.setDisabled(False)

                sample_data_model = self.get_sample_item(tree_item).get_model()
                energy_scan_result = sample_data_model.crystals[0].energy_scan_result
                self._acq_widget.set_energies(energy_scan_result)

                # self._acq_widget.disable_inverse_beam(True)

                self._path_template = dc_model.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)

                self._acquisition_parameters = dc_model.acquisitions[0].acquisition_parameters
                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template
                )
                # self.get_acquisition_widget().use_osc_start(True)
                if len(dc_model.acquisitions) == 1:
                    HWR.beamline.sample_view.select_shape_with_cpos(
                        self._acquisition_parameters.centred_position
                    )

                self._processing_parameters = dc_model.processing_parameters
                self._processing_widget.update_data_model(self._processing_parameters)
            else:
                self.setDisabled(True)
        else:
            self.setDisabled(True)

    def _create_task(self, sample, shape, comments=None):
        """
        Creates a new Still scan task
        :param sample: sample node
        :param shape: centering point
        :return: Acquisition item
        """
        tasks = []

        cpos = queue_model_objects.CentredPosition()
        cpos.snapshot_image = HWR.beamline.sample_view.get_snapshot()

        tasks.extend(self.create_dc(sample, cpos=cpos, comments=comments))
        self._path_template.run_number += 1

        return tasks

    def create_dc(
        self,
        sample,
        run_number=None,
        start_image=None,
        num_images=None,
        osc_start=None,
        sc=None,
        cpos=None,
        inverse_beam=False,
        comments=None
    ):
        """
        Creates a new data collection item
        :param sample: Sample
        :param run_number: int
        :param start_image: int
        :param num_images: int
        :param osc_start: float
        :param sc:
        :param cpos: centered position
        :param inverse_beam: boolean
        :return:
        """
        tasks = []

        # Acquisition for start position
        acq = self._create_acq(sample)

        if run_number:
            acq.path_template.run_number = run_number

        if start_image:
            acq.acquisition_parameters.first_image = start_image
            acq.path_template.start_num = start_image

        if num_images:
            acq.acquisition_parameters.num_images = num_images
            acq.path_template.num_files = num_images

        if osc_start:
            acq.acquisition_parameters.osc_start = osc_start

        if inverse_beam:
            acq.acquisition_parameters.inverse_beam = False

        acq.acquisition_parameters.centred_position = cpos
        if comments:
            acq.acquisition_parameters.comments = comments

        processing_parameters = copy.deepcopy(self._processing_parameters)
        data_collection = queue_model_objects.DataCollection(
            [acq], sample.crystals[0], processing_parameters
        )
        data_collection.set_name(acq.path_template.get_prefix())
        data_collection.set_number(acq.path_template.run_number)
        data_collection.experiment_type = queue_model_enumerables.EXPERIMENT_TYPE.OSC
        run_processing_after, run_online_processing = \
            self._processing_widget.get_processing_state()

        data_collection.run_processing_after = run_processing_after
        if run_online_processing:
            data_collection.run_online_processing = "Still"
        data_collection.set_requires_centring(False)

        tasks.append(data_collection)

        return tasks
Beispiel #14
0
class DCParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="parameter_widget"):

        QtImport.QWidget.__init__(self, parent)
        if name is not None:
            self.setObjectName(name)

        # Properties ----------------------------------------------------------

        # Signals ------------------------------------------------------------

        # Slots ---------------------------------------------------------------

        # Internal variables --------------------------------------------------
        self._data_collection = None
        self._tree_view_item = None
        self._acquisition_mib = None

        # Graphic elements ----------------------------------------------------
        _dc_parameters_widget = QtImport.QWidget(self)
        self._data_path_widget = DataPathWidget(_dc_parameters_widget)
        self._acq_widget = AcquisitionWidget(_dc_parameters_widget, layout="horizontal")
        self._processing_widget = ProcessingWidget(_dc_parameters_widget)

        # Layout --------------------------------------------------------------
        _dc_parameters_widget_layout = QtImport.QVBoxLayout(_dc_parameters_widget)
        _dc_parameters_widget_layout.addWidget(self._data_path_widget)
        _dc_parameters_widget_layout.addWidget(self._acq_widget)
        _dc_parameters_widget_layout.addWidget(self._processing_widget)
        _dc_parameters_widget_layout.setContentsMargins(0, 0, 0, 0)
        _dc_parameters_widget_layout.setSpacing(2)
        _dc_parameters_widget_layout.addStretch(10)

        _main_hlayout = QtImport.QHBoxLayout(self)
        _main_hlayout.addWidget(_dc_parameters_widget)
        _main_hlayout.setContentsMargins(0, 0, 0, 0)
        _main_hlayout.setSpacing(2)
        _main_hlayout.addStretch(0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._data_path_widget.data_path_layout.prefix_ledit.textChanged.connect(
            self._prefix_ledit_change
        )
        self._data_path_widget.data_path_layout.run_number_ledit.textChanged.connect(
            self._run_number_ledit_change
        )
        self._acq_widget.madEnergySelectedSignal.connect(self.mad_energy_selected)
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)

        # Other ---------------------------------------------------------------

    def _prefix_ledit_change(self, new_value):
        prefix = self._data_collection.acquisitions[0].path_template.get_prefix()
        self._data_collection.set_name(prefix)
        self._tree_view_item.setText(0, self._data_collection.get_name())

    def _run_number_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self._data_collection.set_number(int(new_value))
            self._tree_view_item.setText(0, self._data_collection.get_name())

    def acq_parameters_changed(self):
        if self._tree_view_item is None:
            # TODO fix this
            return

        # TODO  get tree view in another way
        dc_tree_widget = self._tree_view_item.listView().parent().parent()
        dc_tree_widget.check_for_path_collisions()
        path_template = self._data_collection.acquisitions[0].path_template
        path_conflict = api.queue_model.check_for_path_collisions(path_template)

    def mad_energy_selected(self, name, energy, state):
        path_template = self._data_collection.acquisitions[0].path_template

        if state:
            path_template.mad_prefix = str(name)
        else:
            path_template.mad_prefix = ""

        run_number = api.queue_model.get_next_run_number(
            path_template
        )

        self._data_path_widget.set_run_number(run_number)
        self._data_path_widget.set_prefix(path_template.base_prefix)
        model = self._tree_view_item.get_model()
        model.set_name(path_template.get_prefix())
        self._tree_view_item.setText(0, model.get_name())

    def tab_changed(self):
        if self._tree_view_item:
            self.populate_parameter_widget(self._tree_view_item)

    def set_enabled(self, state):
        self._acq_widget.setEnabled(state)
        self._data_path_widget.setEnabled(state)
        self._processing_widget.setEnabled(state)

    def populate_widget(self, item):
        data_collection = item.get_model()
        self._tree_view_item = item
        self._data_collection = data_collection
        self._acquisition_mib = DataModelInputBinder(
            self._data_collection.acquisitions[0].acquisition_parameters
        )

        # The acq_widget sends a signal to the path_widget, and it relies
        # on that both models upto date, we need to refactor this part
        # so that both models are set before taking ceratin actions.
        # This workaround, works for the time beeing.
        self._data_path_widget._data_model = data_collection.acquisitions[
            0
        ].path_template

        self._acq_widget.set_energies(data_collection.crystal.energy_scan_result)
        self._acq_widget.update_data_model(
            data_collection.acquisitions[0].acquisition_parameters,
            data_collection.acquisitions[0].path_template,
        )
        self._data_path_widget.update_data_model(
            data_collection.acquisitions[0].path_template
        )
        self._processing_widget.update_data_model(data_collection.processing_parameters)

        invalid = self._acquisition_mib.validate_all()
        if invalid:
            msg = (
                "This data collection has one or more incorrect parameters,"
                + " correct the fields marked in red to solve the problem."
            )

            logging.getLogger("GUI").warning(msg)
Beispiel #15
0
class AdvancedParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="advanced_parameters_widget"):
        QtImport.QWidget.__init__(self, parent)

        # Internal values -----------------------------------------------------
        self._data_collection = None
        self._tree_view_item = None
        self._acquisition_mib = None

        # Properties ----------------------------------------------------------

        # Signals -------------------------------------------------------------

        # Slots ---------------------------------------------------------------

        # Graphic elements ----------------------------------------------------
        _dc_parameters_widget = QtImport.QWidget(self)
        self._data_path_widget = DataPathWidget(_dc_parameters_widget)
        self._acq_widget = AcquisitionWidget(_dc_parameters_widget,
                                             layout="horizontal")

        # Layout --------------------------------------------------------------
        _dc_parameters_widget_layout = QtImport.QVBoxLayout(
            _dc_parameters_widget)
        _dc_parameters_widget_layout.addWidget(self._data_path_widget)
        _dc_parameters_widget_layout.addWidget(self._acq_widget)
        _dc_parameters_widget_layout.setSpacing(2)
        _dc_parameters_widget_layout.addStretch(10)
        _dc_parameters_widget_layout.setContentsMargins(0, 0, 0, 0)

        _main_hlayout = QtImport.QHBoxLayout(self)
        _main_hlayout.addWidget(_dc_parameters_widget)
        _main_hlayout.setSpacing(2)
        _main_hlayout.setContentsMargins(2, 2, 2, 2)
        _main_hlayout.addStretch(0)

        # Qt signal/slot connections ------------------------------------------
        # self._acq_widget.acqParametersChangedSignal.\
        #     connect(self.acq_parameters_changed)
        # self._data_path_widget.pathTemplateChangedSignal.\
        #     connect(self.acq_parameters_changed)
        self._acq_widget.madEnergySelectedSignal.connect(
            self.mad_energy_selected)

        # Ohter ---------------------------------------------------------------
        self._acq_widget.use_osc_start(False)
        self._acq_widget.acq_widget_layout.mad_cbox.hide()
        self._acq_widget.acq_widget_layout.energies_combo.hide()
        self._acq_widget.acq_widget_layout.shutterless_cbx.hide()

    def mad_energy_selected(self, name, energy, state):
        path_template = self._data_collection.acquisitions[0].path_template

        if state:
            path_template.mad_prefix = name
        else:
            path_template.mad_prefix = ""

        run_number = HWR.beamline.queue_model.get_next_run_number(
            path_template)

        self._data_path_widget.set_run_number(run_number)
        self._data_path_widget.set_prefix(path_template.base_prefix)
        model = self._tree_view_item.get_model()
        model.set_name(path_template.get_prefix())
        self._tree_view_item.setText(0, model.get_name())

    def populate_widget(self, tree_view_item, data_collection):
        self._tree_view_item = tree_view_item
        self._data_collection = data_collection

        # if isinstance(tree_view_item, queue_item.XrayCenteringQueueItem):
        #    self._data_collection = tree_view_item.get_model().reference_image_collection
        # else:
        #    self._data_collection = tree_view_item.get_model()
        executed = self._data_collection.is_executed()

        self._acq_widget.setEnabled(not executed)
        self._data_path_widget.setEnabled(not executed)

        self._acquisition_mib = DataModelInputBinder(
            self._data_collection.acquisitions[0].acquisition_parameters)

        # The acq_widget sends a signal to the path_widget, and it relies
        # on that both models upto date, we need to refactor this part
        # so that both models are set before taking ceratin actions.
        # This workaround, works for the time beeing.
        self._data_path_widget.update_data_model(
            self._data_collection.acquisitions[0].path_template)

        self._acq_widget.update_data_model(
            self._data_collection.acquisitions[0].acquisition_parameters,
            self._data_collection.acquisitions[0].path_template,
        )
        # self._acq_widget.use_osc_start(False)

        self._acq_widget.acq_widget_layout.num_images_ledit.setDisabled(
            data_collection.is_mesh())
        invalid = self._acquisition_mib.validate_all()
        if invalid:
            msg = (
                "This data collection has one or more incorrect parameters," +
                " correct the fields marked in red to solve the problem.")
            logging.getLogger("GUI").warning(msg)
class CreateHelicalWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name, fl, "Helical")

        if not name:
            self.setObjectName("create_helical_widget")
        self.init_models()

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self._lines_map = {}

        # Graphic elements ----------------------------------------------------
        self._lines_widget = QtImport.load_ui_file("helical_line_widget_layout.ui")

        self._acq_widget = AcquisitionWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        self._processing_widget = ProcessingWidget(
            self, data_model=self._processing_parameters
        )

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._lines_widget)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._processing_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._lines_widget.lines_treewidget.itemSelectionChanged.connect(
            self.lines_treewidget_selection_changed
        )
        self._lines_widget.create_line_button.clicked.connect(
            self.create_line_button_clicked
        )
        self._lines_widget.create_auto_line_button.clicked.connect(
            self.create_auto_line_button_clicked
        )
        self._lines_widget.remove_line_button.clicked.connect(
            self.remove_line_button_clicked
        )
        self._lines_widget.overlay_cbox.stateChanged.connect(self.overlay_toggled)
        self._lines_widget.overlay_slider.valueChanged.connect(
            self.overlay_alpha_changed
        )
        self._lines_widget.swap_points_button.clicked.connect(self.swap_points_clicked)

        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)
        self._acq_widget.madEnergySelectedSignal.connect(self.mad_energy_selected)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self._processing_widget.enableProcessingSignal.connect(
            self._run_processing_toggled
        )
        # Other ---------------------------------------------------------------
        for col in range(self._lines_widget.lines_treewidget.columnCount()):
            self._lines_widget.lines_treewidget.resizeColumnToContents(col)
        # self._processing_widget.processing_widget.\
        #     run_processing_parallel_cbox.setChecked(False)


        self._processing_widget.processing_widget.run_processing_parallel_cbox.setChecked(
            api.beamline_setup._get_run_processing_parallel()
        )
        self.enable_widgets(False)

    def enable_widgets(self, state):
        self._acq_widget.setEnabled(state)
        self._data_path_widget.setEnabled(state)
        self._processing_widget.setEnabled(state)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._energy_scan_result = queue_model_objects.EnergyScanResult()
        self._processing_parameters = queue_model_objects.ProcessingParameters()

        has_shutter_less = api.beamline_setup.detector_has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = api.beamline_setup.get_default_acquisition_parameters(
             "default_helical_values"
        )

    def init_api(self):
        # At startup, if scene loaded from file, then update listwidget
        shapes = api.graphics.get_shapes()
        for shape in shapes:
            if isinstance(shape, GraphicsItemLine):
                self.shape_created(shape, "Line")
        api.graphics.connect("shapeCreated", self.shape_created)
        api.graphics.connect("shapeChanged", self.shape_changed)
        api.graphics.connect("shapeDeleted", self.shape_deleted)

    def shape_created(self, shape, shape_type):
        if shape_type == "Line":
            self._lines_widget.lines_treewidget.clearSelection()
            # info_str_list = QStringList()
            info_str_list = []
            info_str_list.append(shape.get_display_name())
            info_str_list.append("%d" % shape.get_points_index()[0])
            info_str_list.append("%d" % shape.get_points_index()[1])

            lines_treewidget_item = QtImport.QTreeWidgetItem(
                self._lines_widget.lines_treewidget, info_str_list
            )
            lines_treewidget_item.setSelected(True)
            self._lines_map[shape] = lines_treewidget_item

            self.lines_treewidget_selection_changed()

    def shape_deleted(self, shape, shape_type):
        if shape_type == "Line" and self._lines_map.get(shape):
            shape_index = self._lines_widget.lines_treewidget.indexFromItem(
                self._lines_map[shape]
            )
            self._lines_widget.lines_treewidget.takeTopLevelItem(shape_index.row())
            self._lines_map.pop(shape)

    def approve_creation(self):
        base_result = CreateTaskBase.approve_creation(self)

        if len(self._lines_widget.lines_treewidget.selectedItems()) == 0:
            logging.getLogger("GUI").warning(
                "No lines selected, please select one or more lines."
            )
            return False
        else:
            return base_result

    def update_processing_parameters(self, crystal):
        self._processing_parameters.space_group = crystal.space_group
        self._processing_parameters.cell_a = crystal.cell_a
        self._processing_parameters.cell_alpha = crystal.cell_alpha
        self._processing_parameters.cell_b = crystal.cell_b
        self._processing_parameters.cell_beta = crystal.cell_beta
        self._processing_parameters.cell_c = crystal.cell_c
        self._processing_parameters.cell_gamma = crystal.cell_gamma
        self._processing_widget.update_data_model(self._processing_parameters)

    def select_line_with_cpos(self, start_cpos, end_cpos, num_images):
        """Selects graphical line which contains two cpositions
           Adds number of frames next to the graphical line
        """
        self._lines_widget.overlay_slider.setEnabled(False)
        self._lines_widget.overlay_cbox.setEnabled(False)

        api.graphics.de_select_all()
        for shape in api.graphics.get_shapes():
            if isinstance(shape, GraphicsItemLine):
                (start_cpos_index, end_cpos_index) = shape.get_points_index()
                if (
                    start_cpos_index == start_cpos.index
                    and end_cpos_index == end_cpos.index
                ):
                    api.graphics.select_shape(shape)
                    shape.set_num_images(num_images)

                    self._lines_widget.overlay_slider.setEnabled(True)
                    self._lines_widget.overlay_cbox.setEnabled(True)

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            sample_model = tree_item.get_model()
            self._processing_parameters = sample_model.processing_parameters
            # self._processing_parameters = copy.deepcopy(self._processing_parameters)
            self._processing_widget.update_data_model(self._processing_parameters)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem):
            data_collection = tree_item.get_model()

            if data_collection.is_helical():
                self.setDisabled(tree_item.get_model().is_executed())

                self._path_template = data_collection.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)
                self._acquisition_parameters = data_collection.acquisitions[
                    0
                ].acquisition_parameters

                if len(data_collection.acquisitions) == 2:
                    start_cpos = data_collection.acquisitions[
                        0
                    ].acquisition_parameters.centred_position
                    end_cpos = data_collection.acquisitions[
                        1
                    ].acquisition_parameters.centred_position
                    num_images = data_collection.acquisitions[
                        0
                    ].acquisition_parameters.num_images
                    self.select_line_with_cpos(start_cpos, end_cpos, num_images)

                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template
                )
                # self.get_acquisition_widget().use_osc_start(True)

                self._processing_parameters = data_collection.processing_parameters
                self._processing_widget.update_data_model(self._processing_parameters)
            else:
                self.setDisabled(True)
        else:
            self.setDisabled(True)

        if (
            isinstance(tree_item, queue_item.SampleQueueItem)
            or isinstance(tree_item, queue_item.DataCollectionGroupQueueItem)
            or isinstance(tree_item, queue_item.DataCollectionQueueItem)
        ):

            self._processing_widget.update_data_model(self._processing_parameters)
            self._acq_widget.update_data_model(
                self._acquisition_parameters, self._path_template
            )

    def _create_task(self, sample, shape):
        data_collections = []

        for shape in self.get_selected_shapes():
            snapshot = api.graphics.get_scene_snapshot(shape)

            # Acquisition for start position
            start_acq = self._create_acq(sample)

            start_graphical_point, end_graphical_point = shape.get_graphical_points()

            start_acq.acquisition_parameters.centred_position = copy.deepcopy(
                start_graphical_point.get_centred_position()
            )
            start_acq.acquisition_parameters.centred_position.snapshot_image = snapshot

            start_acq.path_template.suffix = api.session.suffix

            # Add another acquisition for the end position
            end_acq = self._create_acq(sample)

            end_acq.acquisition_parameters.centred_position = copy.deepcopy(
                end_graphical_point.get_centred_position()
            )
            end_acq.acquisition_parameters.centred_position.snapshot_image = snapshot

            end_acq.path_template.suffix = api.session.suffix

            processing_parameters = copy.deepcopy(self._processing_parameters)

            dc = queue_model_objects.DataCollection(
                [start_acq, end_acq], sample.crystals[0], processing_parameters
            )

            dc.set_name(start_acq.path_template.get_prefix())
            dc.set_number(start_acq.path_template.run_number)
            dc.set_experiment_type(EXPERIMENT_TYPE.HELICAL)
            dc.set_requires_centring(False)
            dc.run_processing_after = (
                self._processing_widget.processing_widget.run_processing_after_cbox.isChecked()
            )
            if (
                self._processing_widget.processing_widget.run_processing_parallel_cbox.isChecked()
            ):
                dc.run_processing_parallel = "LineScan"

            data_collections.append(dc)
            self._path_template.run_number += 1

        return data_collections

    def lines_treewidget_selection_changed(self):
        self.enable_widgets(
            len(self._lines_widget.lines_treewidget.selectedItems()) > 0
        )
        self._lines_widget.remove_line_button.setEnabled(
            len(self._lines_widget.lines_treewidget.selectedItems()) > 0
        )
        self._lines_widget.swap_points_button.setEnabled(
            len(self._lines_widget.lines_treewidget.selectedItems()) > 0
        )

        for shape, list_item in self._lines_map.items():
            api.graphics.select_shape(shape, list_item.isSelected())
        self._acq_widget.emit_acq_parameters_changed()

    def create_line_button_clicked(self):
        api.graphics.create_line()

    def create_auto_line_button_clicked(self):
        api.graphics.create_auto_line()

    def remove_line_button_clicked(self):
        line_to_delete = None
        for line, treewidget_item in self._lines_map.items():
            if treewidget_item.isSelected():
                line_to_delete = line
                break
        if line_to_delete:
            api.graphics.delete_shape(line_to_delete)
        self.lines_treewidget_selection_changed()

    def get_selected_shapes(self):
        selected_lines = []
        for line, treewidget_item in self._lines_map.items():
            if treewidget_item.isSelected():
                selected_lines.append(line)
        return selected_lines

    def overlay_toggled(self, state):
        api.graphics.set_display_overlay(state)

    def overlay_alpha_changed(self, alpha_value):
        for line, treewidget_item in self._lines_map.items():
            if treewidget_item.isSelected():
                line.set_fill_alpha(alpha_value)

    def swap_points_clicked(self):
        for line, treewidget_item in self._lines_map.items():
            if treewidget_item.isSelected():
                api.graphics.swap_line_points(line)
Beispiel #17
0
class DCParametersWidget(QtImport.QWidget):
    def __init__(self, parent=None, name="parameter_widget"):

        QtImport.QWidget.__init__(self, parent)
        if name is not None:
            self.setObjectName(name)

        # Properties ----------------------------------------------------------

        # Signals ------------------------------------------------------------

        # Slots ---------------------------------------------------------------

        # Internal variables --------------------------------------------------
        self._data_collection = None
        self._tree_view_item = None
        self._acquisition_mib = None

        # Graphic elements ----------------------------------------------------
        _dc_parameters_widget = QtImport.QWidget(self)
        self._data_path_widget = DataPathWidget(_dc_parameters_widget)
        self._acq_widget = AcquisitionWidget(_dc_parameters_widget,
                                             layout="horizontal")
        self._processing_widget = ProcessingWidget(_dc_parameters_widget)

        # Layout --------------------------------------------------------------
        _dc_parameters_widget_layout = QtImport.QVBoxLayout(
            _dc_parameters_widget)
        _dc_parameters_widget_layout.addWidget(self._data_path_widget)
        _dc_parameters_widget_layout.addWidget(self._acq_widget)
        _dc_parameters_widget_layout.addWidget(self._processing_widget)
        _dc_parameters_widget_layout.setContentsMargins(0, 0, 0, 0)
        _dc_parameters_widget_layout.setSpacing(2)
        _dc_parameters_widget_layout.addStretch(10)

        _main_hlayout = QtImport.QHBoxLayout(self)
        _main_hlayout.addWidget(_dc_parameters_widget)
        _main_hlayout.setContentsMargins(0, 0, 0, 0)
        _main_hlayout.setSpacing(2)
        _main_hlayout.addStretch(0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._data_path_widget.data_path_layout.prefix_ledit.textChanged.connect(
            self._prefix_ledit_change)
        self._data_path_widget.data_path_layout.run_number_ledit.textChanged.connect(
            self._run_number_ledit_change)
        self._acq_widget.madEnergySelectedSignal.connect(
            self.mad_energy_selected)
        self._acq_widget.acqParametersChangedSignal.connect(
            self.acq_parameters_changed)

        # Other ---------------------------------------------------------------

    def _prefix_ledit_change(self, new_value):
        prefix = self._data_collection.acquisitions[
            0].path_template.get_prefix()
        self._data_collection.set_name(prefix)
        self._tree_view_item.setText(0, self._data_collection.get_name())

    def _run_number_ledit_change(self, new_value):
        if str(new_value).isdigit():
            self._data_collection.set_number(int(new_value))
            self._tree_view_item.setText(0, self._data_collection.get_name())

    def acq_parameters_changed(self):
        if self._tree_view_item is None:
            return

        # TODO  get tree view in another way
        dc_tree_widget = self._tree_view_item.listView().parent().parent()
        dc_tree_widget.check_for_path_collisions()
        path_template = self._data_collection.acquisitions[0].path_template
        HWR.beamline.queue_model.check_for_path_collisions(path_template)

    def mad_energy_selected(self, name, energy, state):
        path_template = self._data_collection.acquisitions[0].path_template

        if state:
            path_template.mad_prefix = str(name)
        else:
            path_template.mad_prefix = ""

        run_number = HWR.beamline.queue_model.get_next_run_number(
            path_template)

        self._data_path_widget.set_run_number(run_number)
        self._data_path_widget.set_prefix(path_template.base_prefix)
        model = self._tree_view_item.get_model()
        model.set_name(path_template.get_prefix())
        self._tree_view_item.setText(0, model.get_name())

    def set_enabled(self, state):
        self._acq_widget.setEnabled(state)
        self._data_path_widget.setEnabled(state)
        self._processing_widget.setEnabled(state)

    def populate_widget(self, item):
        data_collection = item.get_model()
        self._tree_view_item = item
        self._data_collection = data_collection
        self._acquisition_mib = DataModelInputBinder(
            self._data_collection.acquisitions[0].acquisition_parameters)

        # The acq_widget sends a signal to the path_widget, and it relies
        # on that both models upto date, we need to refactor this part
        # so that both models are set before taking ceratin actions.
        # This workaround, works for the time beeing.
        self._data_path_widget._data_model = data_collection.acquisitions[
            0].path_template

        self._acq_widget.set_energies(
            data_collection.crystal.energy_scan_result)
        self._acq_widget.update_data_model(
            data_collection.acquisitions[0].acquisition_parameters,
            data_collection.acquisitions[0].path_template,
        )
        self._data_path_widget.update_data_model(
            data_collection.acquisitions[0].path_template)
        self._processing_widget.update_data_model(
            data_collection.processing_parameters)

        invalid = self._acquisition_mib.validate_all()
        if invalid:
            msg = (
                "This data collection has one or more incorrect parameters," +
                " correct the fields marked in red to solve the problem.")

            logging.getLogger("GUI").warning(msg)
class CreateDiscreteWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "Standard"
        )

        if not name:
            self.setObjectName("create_discrete_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        self._processing_widget = ProcessingWidget(
            self, data_model=self._processing_parameters
        )

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._processing_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )

        self._acq_widget.madEnergySelectedSignal.connect(self.mad_energy_selected)
        self._processing_widget.enableProcessingSignal.connect(
            self._run_processing_toggled
        )

        # Other ---------------------------------------------------------------
        self._processing_widget.processing_widget.run_processing_parallel_cbox.setChecked(
             api.beamline_setup._get_run_processing_parallel()
        )

    def init_models(self):
        CreateTaskBase.init_models(self)
        # self._energy_scan_result = queue_model_objects.EnergyScanResult()
        self._processing_parameters = queue_model_objects.ProcessingParameters()

        has_shutter_less = api.beamline_setup.detector_has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = api.beamline_setup.get_default_acquisition_parameters(
            "default_acquisition_values"
        )

    def set_tunable_energy(self, state):
        self._acq_widget.set_tunable_energy(state)

    def update_processing_parameters(self, crystal):
        self._processing_parameters.space_group = crystal.space_group
        self._processing_parameters.cell_a = crystal.cell_a
        self._processing_parameters.cell_alpha = crystal.cell_alpha
        self._processing_parameters.cell_b = crystal.cell_b
        self._processing_parameters.cell_beta = crystal.cell_beta
        self._processing_parameters.cell_c = crystal.cell_c
        self._processing_parameters.cell_gamma = crystal.cell_gamma
        self._processing_widget.update_data_model(self._processing_parameters)

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            sample_model = tree_item.get_model()
            # self._processing_parameters = copy.deepcopy(self._processing_parameters)
            self._processing_parameters = sample_model.processing_parameters
            self._processing_widget.update_data_model(self._processing_parameters)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem):
            dc = tree_item.get_model()
            self._acq_widget.use_kappa(False)

            if not dc.is_helical():
                if dc.is_executed():
                    self.setDisabled(True)
                else:
                    self.setDisabled(False)

                sample_data_model = self.get_sample_item(tree_item).get_model()
                energy_scan_result = sample_data_model.crystals[0].energy_scan_result
                self._acq_widget.set_energies(energy_scan_result)

                # self._acq_widget.disable_inverse_beam(True)

                self._path_template = dc.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)

                self._acquisition_parameters = dc.acquisitions[0].acquisition_parameters
                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template
                )
                # self.get_acquisition_widget().use_osc_start(True)
                if len(dc.acquisitions) == 1:
                    self.select_shape_with_cpos(
                        self._acquisition_parameters.centred_position
                    )

                self._processing_parameters = dc.processing_parameters
                self._processing_widget.update_data_model(self._processing_parameters)
            else:
                self.setDisabled(True)
        else:
            self.setDisabled(True)

    def approve_creation(self):
        result = CreateTaskBase.approve_creation(self)
        return result

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.
    def _create_task(self, sample, shape):
        tasks = []

        if isinstance(shape, GraphicsItemPoint):
            snapshot = api.graphics.get_scene_snapshot(shape)
            cpos = copy.deepcopy(shape.get_centred_position())
            cpos.snapshot_image = snapshot
        else:
            cpos = queue_model_objects.CentredPosition()
            cpos.snapshot_image = api.graphics.get_scene_snapshot()

        tasks.extend(self.create_dc(sample, cpos=cpos))
        self._path_template.run_number += 1

        return tasks

    def create_dc(
        self,
        sample,
        run_number=None,
        start_image=None,
        num_images=None,
        osc_start=None,
        sc=None,
        cpos=None,
        inverse_beam=False,
    ):
        tasks = []

        # Acquisition for start position
        acq = self._create_acq(sample)

        if run_number:
            acq.path_template.run_number = run_number

        if start_image:
            acq.acquisition_parameters.first_image = start_image
            acq.path_template.start_num = start_image

        if num_images:
            acq.acquisition_parameters.num_images = num_images
            acq.path_template.num_files = num_images

        if osc_start:
            acq.acquisition_parameters.osc_start = osc_start

        if inverse_beam:
            acq.acquisition_parameters.inverse_beam = False

        acq.acquisition_parameters.centred_position = cpos

        processing_parameters = copy.deepcopy(self._processing_parameters)
        dc = queue_model_objects.DataCollection(
            [acq], sample.crystals[0], processing_parameters
        )
        dc.set_name(acq.path_template.get_prefix())
        dc.set_number(acq.path_template.run_number)
        dc.experiment_type = queue_model_enumerables.EXPERIMENT_TYPE.NATIVE
        dc.run_processing_after = (
            self._processing_widget.processing_widget.run_processing_after_cbox.isChecked()
        )
        if (
            self._processing_widget.processing_widget.run_processing_parallel_cbox.isChecked()
        ):
            dc.run_processing_parallel = "Undefined"

        tasks.append(dc)

        return tasks

    def execute_task(self, sample):
        # All this should be in queue_entry level
        group_data = {
            "sessionId": api.session.session_id,
            "experimentType": "OSC",
        }
        gid = api.lims._store_data_collection_group(group_data)
        sample.lims_group_id = gid

        task_list = self._create_task(sample, None)
        task_list[0].lims_group_id = gid

        param_list = queue_model_objects.to_collect_dict(
            task_list[0], api.session, sample, None
        )

        api.collect.collect(
            queue_model_enumerables.COLLECTION_ORIGIN_STR.MXCUBE, param_list
        )
Beispiel #19
0
class CreateAdvancedWidget(CreateTaskBase):
    """Widget used to create advanced collection methods
    """

    def __init__(self, parent=None, name=None, fl=0):
        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "Advanced"
        )

        if not name:
            self.setObjectName("create_advanced_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self._advanced_methods = None
        self._grid_map = {}
        self.spacing = [0, 0]
        self.dc_selected = False

        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._advanced_methods_widget = QtImport.load_ui_file(
            "advanced_methods_layout.ui"
        )
        self._acq_widget = AcquisitionWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )
        self._acq_widget.grid_mode = True

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        self._comments_widget = CommentsWidget(self)

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._advanced_methods_widget)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._comments_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)
        self._acq_widget.madEnergySelectedSignal.connect(self.mad_energy_selected)

        self._acq_widget.acq_widget_layout.osc_range_ledit.textEdited.connect(
            self.grid_osc_range_ledit_changed
        )
        self._acq_widget.acq_widget_layout.osc_total_range_ledit.textEdited.connect(
            self.grid_osc_total_range_ledit_changed
        )

        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )

        self._advanced_methods_widget.grid_treewidget.itemSelectionChanged.connect(
            self.grid_treewidget_item_selection_changed
        )
        self._advanced_methods_widget.draw_grid_button.clicked.connect(
            self.draw_grid_button_clicked
        )
        self._advanced_methods_widget.remove_grid_button.clicked.connect(
            self.remove_grid_button_clicked
        )
        self._advanced_methods_widget.hor_spacing_ledit.textEdited.connect(
            self.hor_spacing_changed
        )
        self._advanced_methods_widget.ver_spacing_ledit.textEdited.connect(
            self.ver_spacing_changed
        )

        self._advanced_methods_widget.move_right_button.clicked.connect(
            lambda: self.move_grid("right")
        )
        self._advanced_methods_widget.move_left_button.clicked.connect(
            lambda: self.move_grid("left")
        )
        self._advanced_methods_widget.move_up_button.clicked.connect(
            lambda: self.move_grid("up")
        )
        self._advanced_methods_widget.move_down_button.clicked.connect(
            lambda: self.move_grid("down")
        )

        self._advanced_methods_widget.overlay_cbox.toggled.connect(self.overlay_toggled)
        self._advanced_methods_widget.overlay_slider.valueChanged.connect(
            self.overlay_alpha_changed
        )
        self._advanced_methods_widget.overlay_color_button.clicked.connect(
            self.overlay_change_color
        )
        self._advanced_methods_widget.move_to_grid_button.clicked.connect(
            self.move_to_grid
        )
        self._advanced_methods_widget.method_combo.activated.connect(
            self.method_combo_activated
        )

        # Other ---------------------------------------------------------------
        self._acq_widget.use_osc_start(False)
        self._acq_widget.use_kappa(False)
        self._acq_widget.acq_widget_layout.num_images_label.setEnabled(False)
        self._acq_widget.acq_widget_layout.num_images_ledit.setEnabled(False)
        for col in range(self._advanced_methods_widget.grid_treewidget.columnCount()):
            self._advanced_methods_widget.grid_treewidget.resizeColumnToContents(col)

        self._acq_widget.acq_widget_layout.osc_total_range_label.setText(
            "Total osc. range per line"
        )

        self._advanced_methods = list(HWR.beamline.advanced_methods)
        if self._advanced_methods:
            for method in self._advanced_methods:
                self._advanced_methods_widget.method_combo.addItem(method)
        else:
            self.setEnabled(False)

        self._acq_widget.acq_widget_layout.osc_start_label.setText(
            "Oscillation middle:"
        )

        hor_size, ver_size = HWR.beamline.beam.get_beam_size()
        self.spacing[0] = hor_size
        self.spacing[1] = ver_size

        self._advanced_methods_widget.hor_spacing_ledit.setText(
            "%.1f" % (hor_size * 1000)
        )
        self._advanced_methods_widget.ver_spacing_ledit.setText(
            "%.1f" % (ver_size * 1000)
        )

        HWR.beamline.sample_view.connect("shapeCreated", self.shape_created)
        HWR.beamline.sample_view.connect("shapeChanged", self.shape_changed)
        HWR.beamline.sample_view.connect("shapeDeleted", self.shape_deleted)
        self._comments_widget.setHidden(True)

    def enable_widgets(self, state):
        return
        self._acq_widget.setEnabled(state)
        self._data_path_widget.setEnabled(state)

    def init_models(self):
        CreateTaskBase.init_models(self)

        self._processing_parameters = queue_model_objects.ProcessingParameters()

        has_shutter_less = HWR.beamline.detector.has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = (
            HWR.beamline.get_default_acquisition_parameters("advanced")
        )

    def set_beam_info(self, beam_info):
        try:
            self.spacing[0] = beam_info["size_x"]
            self.spacing[1] = beam_info["size_y"]
            self._advanced_methods_widget.hor_spacing_ledit.setText(
                "%.1f" % (beam_info["size_x"] * 1000)
            )
            self._advanced_methods_widget.ver_spacing_ledit.setText(
                "%.1f" % (beam_info["size_x"] * 1000)
            )
        except:
            pass

    def approve_creation(self):
        """
        Descript. :
        """
        result = CreateTaskBase.approve_creation(self)
        selected_grid = self.get_selected_shapes()[0]

        if not selected_grid:
            msg = "No grid selected. Please select a grid to continue!"
            logging.getLogger("GUI").warning(msg)
            result = False
            # selected_grid = HWR.beamline.sample_view.get_auto_grid()
        else:
            grid_properties = selected_grid.get_properties()
            exp_time = float(self._acq_widget.acq_widget_layout.exp_time_ledit.text())
            speed = grid_properties["yOffset"] / exp_time
            if speed >= 2.25:
                logging.getLogger("GUI").error(
                    "Translation speed %.3f is above the limit 2.25" % speed
                )
                return False
            osc_range_per_frame = float(
                self._acq_widget.acq_widget_layout.osc_range_ledit.text()
            )
            speed = osc_range_per_frame / exp_time
            if speed >= 300:
                logging.getLogger("GUI").error(
                    "Rotation speed per frame %.3f is above the limit 300" % speed
                )
                return False
        return result

    def single_item_selection(self, tree_item):
        """
        Descript. :
        """
        CreateTaskBase.single_item_selection(self, tree_item)
        self.dc_selected = False

        if isinstance(tree_item, queue_item.SampleQueueItem):
            pass
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            pass
            # self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem) or isinstance(
            tree_item, queue_item.XrayCenteringQueueItem
        ):

            if isinstance(tree_item, queue_item.XrayCenteringQueueItem):
                data_collection = tree_item.get_model().reference_image_collection
            else:
                data_collection = tree_item.get_model()

            if tree_item.get_model().is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            if data_collection.is_mesh():
                HWR.beamline.sample_view.select_shape(data_collection.grid)
                self._advanced_methods_widget.grid_treewidget.setCurrentItem(
                    self._grid_map[data_collection.grid]
                )

                self._path_template = data_collection.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)

                self._acquisition_parameters = data_collection.acquisitions[
                    0
                ].acquisition_parameters
                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template
                )
                # self.get_acquisition_widget().use_osc_start(True)
            self.dc_selected = True
        else:
            self.setDisabled(True)
        self.grid_treewidget_item_selection_changed()

    def _create_task(self, sample, shape, comments=None):
        """Creates tasks based on selected grids

        :param sample: selected sample object
        :type sample: SampleQueueItem
        :param shape: selected shape
        :type shape: GraphicsLib.GraphicsItem
        """

        tasks = []
        selected_grid = self.get_selected_shapes()[0]
        mesh_dc = self._create_dc_from_grid(sample, selected_grid)

        cpos = queue_model_objects.CentredPosition()
        cpos.snapshot_image = HWR.beamline.sample_view.get_snapshot()

        exp_type = str(self._advanced_methods_widget.method_combo.currentText())
        if exp_type == "MeshScan":
            tasks.append(mesh_dc)
        elif exp_type == "XrayCentering":
            xray_centering = queue_model_objects.XrayCentering(mesh_dc)
            tasks.append(xray_centering)
        elif exp_type == "MXPressO":
            optical_centering = queue_model_objects.OpticalCentring()
 
            dc_task = self.create_dc(sample, cpos=cpos)[0]
            dc_task.set_requires_centring(False)
            
            tasks.append(optical_centering)
            tasks.append(dc_task)

        else:
            logging.getLogger("GUI").warning(
                "Method %s does not exist in queue_model_objects" % exp_type
            )
        mesh_dc.run_online_processing = exp_type

        return tasks

    def shape_created(self, shape, shape_type):
        """If a grid is created then adds it to the treewidget.
           A binding between graphical grid and treewidget item is based
           on the self._grid_map

        :param shape: graphics object
        :type shape: GraphicsLib.GraphicsItem
        :param shape_type: type of the object (point, line, grid)
        :type shape_type: str
        """

        if shape_type == "Grid":
            self._advanced_methods_widget.grid_treewidget.clearSelection()
            grid_properties = shape.get_properties()
            info_str_list = []
            info_str_list.append(grid_properties["name"])
            info_str_list.append("%d" % grid_properties["num_lines"])
            info_str_list.append("%d" % grid_properties["num_images_per_line"])

            if not self.dc_selected:
                exp_time = max(
                    float(grid_properties["yOffset"] / 2.245),
                    self._acq_widget.exp_time_validator.bottom() + 0.00001,
                )
                self._acq_widget.acq_widget_layout.exp_time_ledit.setText(
                    "%.6f" % exp_time
                ) 

            grid_treewidget_item = QtImport.QTreeWidgetItem(
                self._advanced_methods_widget.grid_treewidget, info_str_list
            )
            self._grid_map[shape] = grid_treewidget_item

            if not self.dc_selected:
                grid_treewidget_item.setSelected(True)
                self.grid_treewidget_item_selection_changed()
                if self._acq_widget.acq_widget_layout.max_osc_range_cbx.isChecked():
                    self._acq_widget.update_osc_total_range_limits(
                        grid_properties["num_images_per_line"]
                    )
                else:
                    self.update_grid_osc_total_range()

    def shape_deleted(self, shape, shape_type):
        """Removes shape from QTreeWidget and self._grid_map

        :param shape: graphics object
        :type shape: QtGraphicsLib.GraphicsItem
        :param shape_type: type of the object (point, line, grid)
        :type shape_type: str
        """
        if self._grid_map.get(shape):
            treewidget_item_modelindex = self._advanced_methods_widget.grid_treewidget.indexFromItem(
                self._grid_map[shape]
            )
            self._advanced_methods_widget.grid_treewidget.takeTopLevelItem(
                treewidget_item_modelindex.row()
            )
            self._grid_map.pop(shape)

    def grid_treewidget_item_selection_changed(self):
        """Updates acquisition parameters based on the selected grid.
        """
        self.enable_grid_controls(False)

        for item in self._grid_map.items():
            grid = item[0]
            treewidget_item = item[1]

            if treewidget_item.isSelected():
                grid_properties = grid.get_properties()
                cell_count = (
                    grid_properties["num_lines"]
                    * grid_properties["num_images_per_line"]
                )
                self._acq_widget.acq_widget_layout.num_images_ledit.setText(
                    "%d" % cell_count
                )
                self._acq_widget.acq_widget_layout.first_image_ledit.setText(
                    "%d" % grid_properties["first_image_num"]
                )
                centred_point = grid.get_centred_position()
                self._acq_widget.acq_widget_layout.kappa_ledit.setText(
                    "%.2f" % float(centred_point.kappa)
                )
                self._acq_widget.acq_widget_layout.kappa_phi_ledit.setText(
                    "%.2f" % float(centred_point.kappa_phi)
                )
                self._advanced_methods_widget.hor_spacing_ledit.setText(
                    "%.2f" % (float(grid_properties["xOffset"]) * 1000)
                )
                self._advanced_methods_widget.ver_spacing_ledit.setText(
                    "%.2f" % (float(grid_properties["yOffset"]) * 1000)
                )

                treewidget_item.setText(3, str(grid_properties["num_lines"]))
                treewidget_item.setText(4, str(grid_properties["num_images_per_line"]))

                grid.setSelected(True)
                self.enable_grid_controls(True)
            else:
                grid.setSelected(False)

    def get_selected_shapes(self):
        """Returns selected grids

        :returns: selected grid objects
        """
        shapes = []
        for grid, grid_treewidget_item in self._grid_map.items():
            if grid_treewidget_item.isSelected():
                shapes.append(grid)
        return shapes

    def get_selected_grid_properties(self):
        """Returns properties of the selected grid

        :returns: dict with properties
        """

        for grid, grid_treewidget_item in self._grid_map.items():
            if grid_treewidget_item.isSelected():
                return grid.get_properties()

    def draw_grid_button_clicked(self):
        """Starts grid drawing
        """
        HWR.beamline.sample_view.create_grid(self.spacing)

    def remove_grid_button_clicked(self):
        """Removes selected grid
        """
        grid_to_delete = self.get_selected_shapes()[0]

        if grid_to_delete:
            HWR.beamline.sample_view.delete_shape(grid_to_delete)
            self._advanced_methods_widget.move_to_grid_button.setEnabled(False)

    def hor_spacing_changed(self, value):
        """Updates spacing of the selected grid
        """
        try:
            self.spacing[0] = (
                float(self._advanced_methods_widget.hor_spacing_ledit.text()) / 1000
            )
            self.set_spacing()
        except BaseException:
            pass

    def ver_spacing_changed(self, value):
        """Updates spacing of the selected grid
        """
        try:
            self.spacing[1] = (
                float(self._advanced_methods_widget.ver_spacing_ledit.text()) / 1000
            )
            self.set_spacing()
        except BaseException:
            pass

    def set_spacing(self):
        if 0 in self.spacing:
            return

        for grid, treewidget_item in self._grid_map.items():
            if treewidget_item.isSelected():
                grid.set_spacing(
                    self.spacing,
                    adjust_size=self._advanced_methods_widget.adjust_size_cbox.isChecked(),
                )
                grid_properties = grid.get_properties()

                treewidget_item.setText(1, str(grid_properties["num_lines"]))
                treewidget_item.setText(2, str(grid_properties["num_images_per_line"]))
                self._acq_widget.acq_widget_layout.num_images_ledit.setText(
                    str(
                        grid_properties["num_lines"]
                        * grid_properties["num_images_per_line"]
                    )
                )

    def move_to_grid(self):
        """Moves diffractometer to the center of the grid
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            HWR.beamline.diffractometer.move_to_centred_position(
                grid.get_centred_position()
            )

    def method_combo_activated(self, index):
        self._task_node_name = self._advanced_methods_widget.method_combo.currentText()

    def overlay_toggled(self, state):
        """Toggles (on/off) overlay
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            grid.set_display_overlay(state)

    def overlay_alpha_changed(self, alpha_value):
        """Changes the transperency of the grid
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            grid.set_fill_alpha(alpha_value)

    def overlay_change_color(self):
        """Changes the default color (blue) of overlay
        """
        color = QtImport.QColorDialog.getColor()
        grid = self.get_selected_shapes()[0]

        if color.isValid() and grid:
            grid.set_base_color(color)

    def move_grid(self, direction):
        """Moves grid by one pix in selected direction

        :param direction: direction to move (right, left, up, down)
        :type direction: str
        """
        grid = self.get_selected_shapes()[0]

        if grid:
            grid.move_by_pix(direction)
            HWR.beamline.sample_view.update_grid_motor_positions(grid)

    def enable_grid_controls(self, state):
        """Enables grid controls if a grid is selectd
        """
        self._advanced_methods_widget.overlay_cbox.setEnabled(state)
        self._advanced_methods_widget.overlay_slider.setEnabled(state)
        self._advanced_methods_widget.overlay_color_button.setEnabled(state)
        self._advanced_methods_widget.move_to_grid_button.setEnabled(state)
        self._advanced_methods_widget.remove_grid_button.setEnabled(state)

        self._advanced_methods_widget.move_right_button.setEnabled(state)
        self._advanced_methods_widget.move_left_button.setEnabled(state)
        self._advanced_methods_widget.move_up_button.setEnabled(state)
        self._advanced_methods_widget.move_down_button.setEnabled(state)

        self._acq_widget.acq_widget_layout.max_osc_range_cbx.setEnabled(
            state and self._in_plate_mode
        )

        self.enable_widgets(state)
        self._acq_widget.emit_acq_parameters_changed()

    def grid_osc_range_ledit_changed(self, new_value):
        """Osc range per frame changed

        :param new_value: new value
        :type new_value: str
        """

        self.update_grid_osc_total_range()

    def grid_osc_total_range_ledit_changed(self, new_value):
        """Updates osc range per frame


        :param new_value: new value
        :type new_value: str
        """

        grid_properties = self.get_selected_grid_properties()
        if grid_properties:
            try:
                self._acq_widget.acq_widget_layout.osc_range_ledit.setText(
                    "%.4f"
                    % (float(new_value) / grid_properties["num_images_per_line"] - 1e-5)
                )
                self._acq_widget.emit_acq_parameters_changed()
            except BaseException:
                pass

    def update_grid_osc_total_range(self):
        """Updates osc range per line
        """

        grid_properties = self.get_selected_grid_properties()
        if grid_properties:
            self._acq_widget.acq_widget_layout.osc_total_range_ledit.setText(
                str(
                    float(self._acq_widget.acq_widget_layout.osc_range_ledit.text())
                    * grid_properties["num_images_per_line"]
                )
            )

    def set_osc_total_range(self, num_images=None, mesh=False):
        grid_properties = self.get_selected_grid_properties()
        if grid_properties:
            CreateTaskBase.set_osc_total_range(
                self, grid_properties["num_images_per_line"], mesh=True
            )
class CreateStillScanWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):
        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "Still"
        )

        if not name:
            self.setObjectName("create_still_scan_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionStillWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        self._processing_widget = ProcessingWidget(
            self, data_model=self._processing_parameters
        )

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(self._processing_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self._processing_widget.enableProcessingSignal.connect(
            self._run_processing_toggled
        )

        # Other ---------------------------------------------------------------
        self._processing_widget.processing_widget.run_processing_parallel_cbox.setChecked(
                api.beamline_setup._get_run_processing_parallel()
        )

    def use_osc_start(self, status):
        pass

    def update_exp_time_limits(self, status):
        pass

    def init_models(self):
        CreateTaskBase.init_models(self)
        self._processing_parameters = queue_model_objects.ProcessingParameters()

        has_shutter_less = api.beamline_setup.detector_has_shutterless()
        self._acquisition_parameters.shutterless = has_shutter_less

        self._acquisition_parameters = api.beamline_setup.get_default_acquisition_parameters(
            "default_acquisition_values"
        )
        self._acquisition_parameters.num_triggers = 1
        self._acquisition_parameters.num_images_per_trigger = 1

    def set_tunable_energy(self, state):
        self._acq_widget.set_tunable_energy(state)

    def update_processing_parameters(self, crystal):
        self._processing_parameters.space_group = crystal.space_group
        self._processing_parameters.cell_a = crystal.cell_a
        self._processing_parameters.cell_alpha = crystal.cell_alpha
        self._processing_parameters.cell_b = crystal.cell_b
        self._processing_parameters.cell_beta = crystal.cell_beta
        self._processing_parameters.cell_c = crystal.cell_c
        self._processing_parameters.cell_gamma = crystal.cell_gamma
        self._processing_widget.update_data_model(self._processing_parameters)

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            sample_model = tree_item.get_model()
            # self._processing_parameters = copy.deepcopy(self._processing_parameters)
            self._processing_parameters = sample_model.processing_parameters
            self._processing_widget.update_data_model(self._processing_parameters)
        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
        elif isinstance(tree_item, queue_item.DataCollectionQueueItem):
            dc = tree_item.get_model()
            self._acq_widget.use_kappa(False)

            if not dc.is_helical():
                if dc.is_executed():
                    self.setDisabled(True)
                else:
                    self.setDisabled(False)

                sample_data_model = self.get_sample_item(tree_item).get_model()
                energy_scan_result = sample_data_model.crystals[0].energy_scan_result
                self._acq_widget.set_energies(energy_scan_result)

                # self._acq_widget.disable_inverse_beam(True)

                self._path_template = dc.get_path_template()
                self._data_path_widget.update_data_model(self._path_template)

                self._acquisition_parameters = dc.acquisitions[0].acquisition_parameters
                self._acq_widget.update_data_model(
                    self._acquisition_parameters, self._path_template
                )
                # self.get_acquisition_widget().use_osc_start(True)
                if len(dc.acquisitions) == 1:
                    self.select_shape_with_cpos(
                        self._acquisition_parameters.centred_position
                    )

                self._processing_parameters = dc.processing_parameters
                self._processing_widget.update_data_model(self._processing_parameters)
            else:
                self.setDisabled(True)
        else:
            self.setDisabled(True)

    def _create_task(self, sample, shape):
        tasks = []

        cpos = queue_model_objects.CentredPosition()
        cpos.snapshot_image = api.graphics.get_scene_snapshot()

        tasks.extend(self.create_dc(sample, cpos=cpos))
        self._path_template.run_number += 1

        return tasks

    def create_dc(
        self,
        sample,
        run_number=None,
        start_image=None,
        num_images=None,
        osc_start=None,
        sc=None,
        cpos=None,
        inverse_beam=False,
    ):
        tasks = []

        # Acquisition for start position
        acq = self._create_acq(sample)

        if run_number:
            acq.path_template.run_number = run_number

        if start_image:
            acq.acquisition_parameters.first_image = start_image
            acq.path_template.start_num = start_image

        if num_images:
            acq.acquisition_parameters.num_images = num_images
            acq.path_template.num_files = num_images

        if osc_start:
            acq.acquisition_parameters.osc_start = osc_start

        if inverse_beam:
            acq.acquisition_parameters.inverse_beam = False

        acq.acquisition_parameters.centred_position = cpos

        processing_parameters = copy.deepcopy(self._processing_parameters)
        dc = queue_model_objects.DataCollection(
            [acq], sample.crystals[0], processing_parameters
        )
        dc.set_name(acq.path_template.get_prefix())
        dc.set_number(acq.path_template.run_number)
        dc.experiment_type = queue_model_enumerables.EXPERIMENT_TYPE.OSC
        dc.run_processing_after = (
            self._processing_widget.processing_widget.run_processing_after_cbox.isChecked()
        )
        if (
            self._processing_widget.processing_widget.run_processing_parallel_cbox.isChecked()
        ):
            dc.run_processing_parallel = "Still"
        dc.set_requires_centring(False)

        tasks.append(dc)

        return tasks
class CreateEnergyScanWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name, fl, "Energy scan")

        if not name:
            self.setObjectName("create_energy_scan_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._periodic_table_widget = PeriodicTableWidget(self)
        self._data_path_widget = DataPathWidget(
            self, data_model=self._path_template, layout="vertical"
        )

        _parameters_gbox = QtImport.QGroupBox("Parameters", self)
        self._adjust_transmission_cbox = QtImport.QCheckBox(
            "Adjust transmission", _parameters_gbox
        )
        self._adjust_transmission_cbox.setChecked(False)
        self._adjust_transmission_cbox.setEnabled(True)
        self._max_transmission_label = QtImport.QLabel("Maximum transmission:")
        self._max_transmission_ledit = QtImport.QLineEdit("20", _parameters_gbox)
        self._max_transmission_ledit.setFixedWidth(80)
        self._max_transmission_ledit.setEnabled(False)

        # Layout --------------------------------------------------------------
        _parameters_gbox_hlayout = QtImport.QGridLayout(_parameters_gbox)
        _parameters_gbox_hlayout.addWidget(self._adjust_transmission_cbox, 0, 0)
        _parameters_gbox_hlayout.addWidget(self._max_transmission_label, 1, 0)
        _parameters_gbox_hlayout.addWidget(self._max_transmission_ledit, 1, 1)
        _parameters_gbox_hlayout.setColumnStretch(2, 1)
        _parameters_gbox_hlayout.setSpacing(2)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._periodic_table_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(_parameters_gbox)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        _main_vlayout.setSpacing(6)
        _main_vlayout.addStretch(10)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        # self._periodic_table_widget.elementEdgeSelectedSignal.connect(\
        #     self.acq_parameters_changed)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )
        self._adjust_transmission_cbox.stateChanged.connect(
            self.adjust_transmission_state_changed
        )
        self._max_transmission_ledit.textEdited.connect(
            self.max_transmission_value_changed
        )

        self._data_path_widget.data_path_layout.compression_cbox.setVisible(False)

        try:
            self._periodic_table_widget.set_elements(api.energyscan.getElements())

            max_transmission_value = api.energyscan.get_max_transmission_value()

            self._adjust_transmission_cbox.setEnabled(True)
            self._adjust_transmission_cbox.setChecked(True)
            api.energyscan.adjust_transmission(True)

            if max_transmission_value:
                self._max_transmission_ledit.setText("%.2f" % max_transmission_value)
        except BaseException:
            pass

    def set_expert_mode(self, state):
        self._adjust_transmission_cbox.setEnabled(state)
        self._max_transmission_label.setEnabled(state)
        self._max_transmission_ledit.setEnabled(state)

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def init_models(self):

        CreateTaskBase.init_models(self)
        self.enery_scan = queue_model_objects.EnergyScan()
        self._path_template.start_num = 1
        self._path_template.num_files = 1
        self._path_template.suffix = "raw"
        self._path_template.compression = False

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        escan_model = tree_item.get_model()

        if isinstance(tree_item, queue_item.EnergyScanQueueItem):
            if tree_item.get_model().is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            if escan_model.get_path_template():
                self._path_template = escan_model.get_path_template()

            self._data_path_widget.update_data_model(self._path_template)
        elif not (
            isinstance(tree_item, queue_item.SampleQueueItem)
            or isinstance(tree_item, queue_item.DataCollectionGroupQueueItem)
        ):
            self.setDisabled(True)

    def approve_creation(self):
        base_result = CreateTaskBase.approve_creation(self)
        selected_element, selected_edge = (
            self._periodic_table_widget.get_selected_element_edge()
        )
        if not selected_element:
            logging.getLogger("GUI").warning(
                "No element selected, please select an element."
            )

        return base_result and selected_element

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.
    def _create_task(self, sample, shape):
        data_collections = []
        selected_element, selected_edge = (
            self._periodic_table_widget.get_selected_element_edge()
        )

        if selected_element:
            if not shape:
                cpos = queue_model_objects.CentredPosition()
                cpos.snapshot_image = api.graphics.get_scene_snapshot()
            else:
                # Shapes selected and sample is mounted, get the
                # centred positions for the shapes
                if isinstance(shape, GraphicsItemPoint):
                    snapshot = api.graphics.get_scene_snapshot(shape)

                    cpos = copy.deepcopy(shape.get_centred_position())
                    cpos.snapshot_image = snapshot

            path_template = self._create_path_template(sample, self._path_template)

            energy_scan = queue_model_objects.EnergyScan(sample, path_template, cpos)
            energy_scan.set_name(path_template.get_prefix())
            energy_scan.set_number(path_template.run_number)
            energy_scan.element_symbol = selected_element
            energy_scan.edge = selected_edge

            data_collections.append(energy_scan)
            self._path_template.run_number += 1
        else:
            logging.getLogger("GUI").info(
                "No element selected, please select an element."
            )

        return data_collections

    def element_edge_selected(self, element, edge):
        if len(self._current_selected_items) == 1:
            item = self._current_selected_items[0]
            if isinstance(item, queue_item.EnergyScanQueueItem):
                item.get_model().element_symbol = str(element)
                item.get_model().edge = str(edge)

    def adjust_transmission_state_changed(self, state):
        self._max_transmission_ledit.setEnabled(state)
        api.energyscan.adjust_transmission(state)

    def max_transmission_value_changed(self, value):
        try:
            max_transmission = float(value)
            api.energyscan.set_max_transmission(max_transmission)
        except BaseException:
            pass
class CreateXrayImagingWidget(CreateTaskBase):
    """Widget used to create xray imaging method
    """

    def __init__(self, parent=None, name=None, fl=0):
        CreateTaskBase.__init__(
            self, parent, name, QtImport.Qt.WindowFlags(fl), "XrayImaging"
        )

        if not name:
            self.setObjectName("create_xray_imaging_widget")

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self._xray_imaging_parameters = None
        self._processing_parameters = None
        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._acq_widget = AcquisitionWidget(
            self,
            "acquisition_widget",
            layout="vertical",
            acq_params=self._acquisition_parameters,
            path_template=self._path_template,
        )
        self._acq_widget.grid_mode = False

        self._xray_imaging_parameters_widget = XrayImagingParametersWidget(
            self,
            "xray_imaging_widget",
            xray_imaging_params=self._xray_imaging_parameters,
        )

        self._data_path_widget = DataPathWidget(
            self,
            "create_dc_path_widget",
            data_model=self._path_template,
            layout="vertical",
        )

        # Layout --------------------------------------------------------------
        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._acq_widget)
        _main_vlayout.addWidget(self._xray_imaging_parameters_widget)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addStretch(0)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------
        self._acq_widget.acqParametersChangedSignal.connect(self.acq_parameters_changed)
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed
        )

        # Other ---------------------------------------------------------------
        self._acq_widget.use_osc_start(False)
        self._acq_widget.use_kappa(False)
        self._acq_widget.acq_widget_layout.max_osc_range_cbx.setVisible(False)
        self._acq_widget.acq_widget_layout.first_image_label.setVisible(False)
        self._acq_widget.acq_widget_layout.first_image_ledit.setVisible(False)
        self._acq_widget.acq_widget_layout.detector_roi_mode_label.setVisible(False)
        self._acq_widget.acq_widget_layout.detector_roi_mode_combo.setVisible(False)
        self._acq_widget.acq_widget_layout.energies_combo.setVisible(False)
        self._acq_widget.acq_widget_layout.mad_cbox.setVisible(False)
        self._acq_widget.acq_widget_layout.energy_label.setVisible(False)
        self._acq_widget.acq_widget_layout.energy_ledit.setVisible(False)
        self._acq_widget.acq_widget_layout.transmission_label.setVisible(False)
        self._acq_widget.acq_widget_layout.transmission_ledit.setVisible(False)
        self._acq_widget.acq_widget_layout.resolution_label.setVisible(False)
        self._acq_widget.acq_widget_layout.resolution_ledit.setVisible(False)
        self._acq_widget.acq_widget_layout.kappa_label.setVisible(False)
        self._acq_widget.acq_widget_layout.kappa_ledit.setVisible(False)
        self._acq_widget.acq_widget_layout.kappa_phi_label.setVisible(False)
        self._acq_widget.acq_widget_layout.kappa_phi_ledit.setVisible(False)
        self._acq_widget.acq_widget_layout.shutterless_cbx.setVisible(False)

        self.distance_listwidget = self._xray_imaging_parameters_widget._parameters_widget.detector_distance_listwidget

        api.detector_distance.connect(
            "positionChanged",
            self._xray_imaging_parameters_widget.set_detector_distance,
        )

    def approve_creation(self):
        return True

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def init_models(self):
        CreateTaskBase.init_models(self)

        self._xray_imaging_parameters = queue_model_objects.XrayImagingParameters()
        self._acquisition_parameters = api.beamline_setup.get_default_acquisition_parameters(
            "default_imaging_values"
        )
        self._path_template.suffix = "tiff"

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        self.setDisabled(True)

        if isinstance(tree_item, queue_item.SampleQueueItem):
            self._xray_imaging_parameters = copy.deepcopy(self._xray_imaging_parameters)
            self._xray_imaging_parameters_widget.update_data_model(
                self._xray_imaging_parameters
            )
            self._xray_imaging_parameters_widget.set_detector_distance(
                api.detector_distance.get_position()
            )
            self.setDisabled(False)
            self._xray_imaging_parameters_widget.enable_distance_tools(True)

        elif isinstance(tree_item, queue_item.BasketQueueItem):
            self.setDisabled(False)
            self._xray_imaging_parameters_widget.enable_distance_tools(True)

        elif isinstance(tree_item, queue_item.XrayImagingQueueItem):
            data_model = tree_item.get_model()

            self._path_template = data_model.get_path_template()
            self._data_path_widget.update_data_model(self._path_template)

            self._acquisition_parameters = data_model.acquisitions[0].acquisition_parameters
            self._acq_widget.update_data_model(
                self._acquisition_parameters, self._path_template
            )

            self._xray_imaging_parameters = data_model.xray_imaging_parameters
            self._xray_imaging_parameters_widget.update_data_model(
                self._xray_imaging_parameters
            )

            self.setDisabled(False)
            self._xray_imaging_parameters_widget.enable_distance_tools(False)

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.
    def _create_task(self, sample, shape):
        if isinstance(shape, GraphicsItemPoint):
            snapshot = api.graphics.get_scene_snapshot(shape)
            cpos = copy.deepcopy(shape.get_centred_position())
            cpos.snapshot_image = snapshot
        else:
            cpos = queue_model_objects.CentredPosition()
            cpos.snapshot_image = api.graphics.get_scene_snapshot()
 
        detector_distance_list = []
        dc_list = []

        if self.distance_listwidget.count() > 1:
            for index in range(self.distance_listwidget.count()):
                detector_distance_list.append(int(self.distance_listwidget.item(index).text()))
        else:
            detector_distance_list.append(None)

        do_it = True
        #run_num = 1

        for detector_distance in detector_distance_list:
            xray_imaging_parameters = copy.deepcopy(self._xray_imaging_parameters)
            if detector_distance:
                xray_imaging_parameters.detector_distance = detector_distance

            acq = self._create_acq(sample)
            acq.acquisition_parameters.centred_position = cpos
            acq.path_template.base_prefix = self.get_default_prefix(sample)

            if do_it:
                do_it = False
                self._path_template.run_number = \
                    api.queue_model.get_next_run_number(
                    acq.path_template)
                acq.path_template.run_number = self._path_template.run_number

            dc = queue_model_objects.XrayImaging(
                xray_imaging_parameters, acq, sample.crystals[0]
            )
            dc.set_name(acq.path_template.get_prefix())
            dc.set_number(acq.path_template.run_number)

            dc_list.append(dc)

            self._path_template.run_number += 1
        return dc_list
class CreateXRFSpectrumWidget(CreateTaskBase):
    def __init__(self, parent=None, name=None, fl=0):

        CreateTaskBase.__init__(self, parent, name,
                                QtImport.Qt.WindowFlags(fl), "XRF spectrum")

        if name is not None:
            self.setObjectName(name)

        # Hardware objects ----------------------------------------------------

        # Internal variables --------------------------------------------------
        self.count_time = None
        self.xrf_spectrum_model = None

        self.init_models()

        # Graphic elements ----------------------------------------------------
        self._data_path_widget = DataPathWidget(self,
                                                data_model=self._path_template,
                                                layout="vertical")

        _parameters_gbox = QtImport.QGroupBox("Parameters", self)
        _count_time_label = QtImport.QLabel("Count time (sec.):",
                                            _parameters_gbox)
        self.count_time_ledit = QtImport.QLineEdit("1", _parameters_gbox)
        # self.count_time_ledit.setMaximumWidth(75)
        self.adjust_transmission_cbox = QtImport.QCheckBox(
            "Adjust transmission", _parameters_gbox)
        self.adjust_transmission_cbox.setChecked(True)

        self._comments_widget = CommentsWidget(self)

        # Layout --------------------------------------------------------------
        _parameters_gbox_hlayout = QtImport.QHBoxLayout(_parameters_gbox)
        _parameters_gbox_hlayout.addWidget(_count_time_label)
        _parameters_gbox_hlayout.addWidget(self.count_time_ledit)
        _parameters_gbox_hlayout.addWidget(self.adjust_transmission_cbox)
        _parameters_gbox_hlayout.addStretch(0)
        _parameters_gbox_hlayout.setSpacing(2)
        _parameters_gbox_hlayout.setContentsMargins(0, 0, 0, 0)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(self._data_path_widget)
        _main_vlayout.addWidget(_parameters_gbox)
        _main_vlayout.addWidget(self._comments_widget)
        _main_vlayout.setSpacing(6)
        _main_vlayout.setContentsMargins(2, 2, 2, 2)
        _main_vlayout.addStretch(0)

        # SizePolicies --------------------------------------------------------
        self._comments_widget.setFixedHeight(100)

        # Qt signal/slot connections ------------------------------------------
        self._data_path_widget.pathTemplateChangedSignal.connect(
            self.path_template_changed)
        self.adjust_transmission_cbox.stateChanged.connect(
            self.adjust_transmission_state_changed)

        # Other ---------------------------------------------------------------
        self._data_path_widget.data_path_layout.compression_cbox.setVisible(
            False)

    def set_expert_mode(self, state):
        self.adjust_transmission_cbox.setEnabled(state)

    def enable_compression(self, state):
        CreateTaskBase.enable_compression(self, False)

    def init_models(self):
        CreateTaskBase.init_models(self)
        self.xrf_spectrum_model = queue_model_objects.XRFSpectrum()
        self._path_template.start_num = 1
        self._path_template.num_files = 1
        self._path_template.suffix = "raw"
        self._path_template.compression = False

    def single_item_selection(self, tree_item):
        CreateTaskBase.single_item_selection(self, tree_item)
        self.xrf_spectrum_model = tree_item.get_model()

        if isinstance(tree_item, queue_item.XRFSpectrumQueueItem):
            if self.xrf_spectrum_model.is_executed():
                self.setDisabled(True)
            else:
                self.setDisabled(False)

            if self.xrf_spectrum_model.get_path_template():
                self._path_template = self.xrf_spectrum_model.get_path_template(
                )

            self._data_path_widget.update_data_model(self._path_template)
        elif not (isinstance(tree_item, queue_item.SampleQueueItem)
                  or isinstance(tree_item,
                                queue_item.DataCollectionGroupQueueItem)):
            self.setDisabled(True)

    def approve_creation(self):
        # base_result = CreateTaskBase.approve_creation(self)
        base_result = True
        self.count_time = None

        try:
            self.count_time = float(str(self.count_time_ledit.text()))
        except BaseException:
            logging.getLogger("GUI").error("Incorrect count time value.")

        return base_result and self.count_time

    # Called by the owning widget (task_toolbox_widget) to create
    # a collection. When a data collection group is selected.

    def _create_task(self, sample, shape, comments=None):
        data_collections = []

        if self.count_time is not None:
            if not shape:
                cpos = queue_model_objects.CentredPosition()
                cpos.snapshot_image = HWR.beamline.sample_view.get_snapshot()
            else:
                # Shapes selected and sample is mounted, get the
                # centred positions for the shapes
                if isinstance(shape, GraphicsItemPoint):
                    snapshot = HWR.beamline.sample_view.get_snapshot(
                        shape=shape)

                    cpos = copy.deepcopy(shape.get_centred_position())
                    cpos.snapshot_image = snapshot

            path_template = self._create_path_template(sample,
                                                       self._path_template)

            xrf_spectrum = queue_model_objects.XRFSpectrum(
                sample, path_template, cpos)
            xrf_spectrum.set_name(path_template.get_prefix())
            xrf_spectrum.set_number(path_template.run_number)
            xrf_spectrum.count_time = self.count_time
            xrf_spectrum.adjust_transmission = self.adjust_transmission_cbox.isChecked(
            )

            data_collections.append(xrf_spectrum)
        else:
            logging.getLogger("GUI").error("No count time specified.")

        return data_collections

    def adjust_transmission_state_changed(self, state):
        self.xrf_spectrum_model.adjust_transmission = state > 0