Example #1
0
class DuplicateNormConfig(SiriusDialog):
    """Auxiliary window to duplicate a normalized config."""

    insertConfig = Signal(float, str, dict)

    def __init__(self, parent, psname2strength):
        """Initialize object."""
        super().__init__(parent)
        self.setObjectName('BOApp')
        self.setWindowTitle('Duplicate Normalized Configuration')
        self.psname2strength = psname2strength
        self._setupUi()

    def _setupUi(self):
        self.le_label = QLineEdit(self)
        self.sb_time = QDoubleSpinBoxPlus(self)
        self.sb_time.setMaximum(490)
        self.sb_time.setDecimals(3)
        self.bt_duplic = QPushButton('Duplicate', self)
        self.bt_duplic.setAutoDefault(False)
        self.bt_duplic.setDefault(False)
        self.bt_duplic.clicked.connect(self._emitConfigData)
        self.bt_cancel = QPushButton('Cancel', self)
        self.bt_cancel.setAutoDefault(False)
        self.bt_cancel.setDefault(False)
        self.bt_cancel.clicked.connect(self.close)

        # layout
        lay = QGridLayout()
        lay.setVerticalSpacing(15)
        lay.addWidget(QLabel('<h4>Duplicate Normalized Configuration</h4>',
                             self),
                      0,
                      0,
                      1,
                      2,
                      alignment=Qt.AlignCenter)
        lay.addWidget(
            QLabel(
                'Choose a label and a time to insert\n'
                'the new configuration:', self), 1, 0, 1, 2)
        lay.addWidget(QLabel('Label: ', self), 2, 0)
        lay.addWidget(self.le_label, 2, 1)
        lay.addWidget(QLabel('Time [ms]: ', self), 3, 0)
        lay.addWidget(self.sb_time, 3, 1)
        lay.addWidget(self.bt_cancel, 4, 0)
        lay.addWidget(self.bt_duplic, 4, 1)
        self.setLayout(lay)

    def _emitConfigData(self):
        time = self.sb_time.value()
        label = self.le_label.text()
        psname2strength = self.psname2strength
        self.insertConfig.emit(time, label, psname2strength)
        self.close()
Example #2
0
class InsertNormalizedConfig(SiriusDialog):
    """Auxiliary window to insert a new normalized config."""

    insertConfig = Signal(float, str, dict)

    def __init__(self, parent, ramp_config):
        """Initialize object."""
        super().__init__(parent)
        self.setObjectName('BOApp')
        self.ramp_config = ramp_config
        self.setWindowTitle('Insert Normalized Configuration')
        self._setupUi()

    def _setupUi(self):
        # selection widgets
        self.rb_interp = QRadioButton('Interpolate')
        self.rb_confsrv = QRadioButton('Take value from Config Server')
        self.rb_create = QRadioButton('Create from template')

        # data widget
        self.config_data = QWidget()
        glay_data = QGridLayout(self.config_data)
        self.le_interp_label = QLineEdit(self)  # interpolate
        self.le_confsrv_name = _ConfigLineEdit(
            parent=self, config_type='bo_normalized')  # from ConfigDB
        self.le_confsrv_name.textChanged.connect(
            self._handleInsertExistingConfig)
        self.le_confsrv_name.setVisible(False)
        self.le_nominal_label = QLineEdit(self)  # from template
        self.le_nominal_label.setVisible(False)
        self.sb_time = QDoubleSpinBoxPlus(self)
        self.sb_time.setMaximum(490)
        self.sb_time.setDecimals(3)
        self.bt_insert = QPushButton('Insert', self)
        self.bt_insert.setAutoDefault(False)
        self.bt_insert.setDefault(False)
        self.bt_insert.clicked.connect(self._emitConfigData)
        self.bt_cancel = QPushButton('Cancel', self)
        self.bt_cancel.setAutoDefault(False)
        self.bt_cancel.setDefault(False)
        self.bt_cancel.clicked.connect(self.close)
        glay_data.addWidget(QLabel('Label: ', self), 0, 0)
        glay_data.addWidget(self.le_interp_label, 0, 1)
        glay_data.addWidget(self.le_confsrv_name, 0, 1)
        glay_data.addWidget(self.le_nominal_label, 0, 1)
        glay_data.addWidget(QLabel('Time [ms]: ', self), 1, 0)
        glay_data.addWidget(self.sb_time, 1, 1)
        glay_data.addWidget(self.bt_cancel, 2, 0)
        glay_data.addWidget(self.bt_insert, 2, 1)

        # connect visibility signals
        self.rb_interp.toggled.connect(self.le_interp_label.setVisible)
        self.rb_interp.setChecked(True)
        self.rb_confsrv.toggled.connect(self.le_confsrv_name.setVisible)
        self.rb_create.toggled.connect(self.le_nominal_label.setVisible)

        # layout
        lay = QVBoxLayout()
        lay.addWidget(QLabel('<h4>Insert a Normalized Configuration</h4>',
                             self),
                      alignment=Qt.AlignCenter)
        lay.addWidget(self.rb_interp)
        lay.addWidget(self.rb_confsrv)
        lay.addWidget(self.rb_create)
        lay.addStretch()
        lay.addWidget(self.config_data)
        self.setLayout(lay)

    @Slot(str)
    def _handleInsertExistingConfig(self, configname):
        try:
            self.norm_config = ramp.BoosterNormalized()
            self.norm_config.name = configname
            self.norm_config.load()
            energy = self.norm_config[ramp.BoosterRamp.PSNAME_DIPOLE_REF]
            time = self.ramp_config.ps_waveform_interp_time(energy)
            self.sb_time.setValue(time)
        except _ConfigDBException as err:
            QMessageBox.critical(self, 'Error', str(err), QMessageBox.Ok)

    def _emitConfigData(self):
        time = self.sb_time.value()
        if self.le_interp_label.isVisible():
            label = self.le_interp_label.text()
            psname2strength = dict()
        elif self.le_confsrv_name.isVisible():
            label = self.le_confsrv_name.text()
            psname2strength = {
                _PVName(k).device_name: v
                for k, v, d in self.norm_config.value['pvs']
            }
        elif self.le_nominal_label.isVisible():
            label = self.le_nominal_label.text()
            psname2strength = \
                self.ramp_config.ps_normalized_config_nominal_values
        self.insertConfig.emit(time, label, psname2strength)
        self.close()
Example #3
0
class EnergyButton(QWidget):
    """Set dipole energy."""

    def __init__(self, section, parent=None):
        """Setups widget interface."""
        super().__init__(parent)
        self._section = section
        self.dips, self.mags = init_section(section.upper())
        self._items_fail = []
        self._items_success = []
        self._setup_ui()
        color = QColor(get_appropriate_color(section.upper()))
        pal = self.palette()
        pal.setColor(QPalette.Background, color)
        self.setAutoFillBackground(True)
        self.setPalette(pal)

    def _setup_ui(self):
        self.setLayout(QVBoxLayout())

        self.energy_value = QDoubleSpinBoxPlus(self)
        self.energy_value.setSingleStep(0.01)
        self.energy_value.setMinimum(self.lower_limit)
        self.energy_value.setMaximum(self.upper_limit)
        self.energy_value.setDecimals(4)

        if self.section == 'tb':
            sp_channel = 'TB-Fam:PS-B:Energy-RB'
        elif self.section == 'bo':
            sp_channel = 'BO-Fam:PS-B-1:Energy-RB'
        elif self.section == 'ts':
            sp_channel = 'TS-Fam:PS-B:Energy-RB'
        elif self.section == 'si':
            sp_channel = 'SI-Fam:PS-B1B2-1:Energy-RB'
        else:
            raise RuntimeError
        sp_channel = _PVName(sp_channel).substitute(prefix=_VACA_PREFIX)
        self.energy_sp = PyDMLabel(self)
        self.energy_sp.channel = sp_channel
        self.energy_sp.showUnits = True

        self._tree = PVNameTree(
            items=self.mags, tree_levels=('dis', 'mag_group'), parent=self)
        self._tree.tree.setHeaderHidden(True)
        self._tree.tree.setColumnCount(1)
        self._tree.check_all()
        self._tree.collapse_all()

        self.set_energy = QPushButton('Set', self)
        self.set_energy.clicked.connect(self._process)

        # forml = Q
        hbl = QHBoxLayout()
        # hbl.addStretch()
        hbl.addWidget(QLabel('<h4>New Energy:</h4> ', self))
        hbl.addWidget(self.energy_value)
        self.layout().addLayout(hbl)
        hbl = QHBoxLayout()
        # hbl.addStretch()
        hbl.addWidget(QLabel('Current Energy: ', self))
        hbl.addWidget(self.energy_sp)
        self.layout().addLayout(hbl)
        self.layout().addWidget(self._tree)
        self.layout().addWidget(self.set_energy)

    @property
    def section(self):
        return self._section

    @property
    def upper_limit(self):
        if self.section == 'tb':
            return 0.155
        elif self.section == 'bo':
            return 0.160
        elif self.section == 'ts':
            return 3.2
        elif self.section == 'si':
            return 3.014
        else:
            raise RuntimeError

    @property
    def lower_limit(self):
        if self.section == 'tb':
            return 0.0
        elif self.section == 'bo':
            return 0.0
        elif self.section == 'ts':
            return 0.0
        elif self.section == 'si':
            return 0.0
        else:
            raise RuntimeError

    def _process(self):
        self._items_success = []
        self._items_fail = []

        # Get selected PVs
        selected_pvs = set(self._tree.checked_items())
        mags = [mag for mag in self.mags if mag in selected_pvs]
        if not mags:
            report = ReportDialog(['Select at least one PS!'], self)
            report.exec_()
            return

        pvs = self.dips + mags

        conn = EpicsConnector(pvs, parent=self)
        get_task = EpicsGetter(pvs, parent=self)
        get_task.itemRead.connect(self._read_success)
        get_task.itemNotRead.connect(self._read_fail)
        dlg = ProgressDialog(
            ['Connecting', 'Reading'], [conn, get_task], parent=self)
        ret = dlg.exec_()
        if ret == dlg.Rejected:
            return
        if self._items_fail:
            failed = ['Failed to read some PVs', ]
            failed += self._items_fail + ['Aborting!', ]
            report = ReportDialog(failed, self)
            report.exec_()
            return

        energy = self.energy_value.value()
        # remove dipole
        pvs, values = zip(*self._items_success[len(self.dips):])
        delays = [0.0, ] * len(pvs)
        self._items_success = []

        energies = [energy, ] * len(self.dips)
        dly_dips = [0.0, ] * len(self.dips)
        conn = EpicsConnector(self.dips, parent=self)
        set_dip = EpicsSetter(self.dips, energies, dly_dips, parent=self)
        sleep_task = EpicsWait([None, ]*10, wait_time=2.0, parent=self)
        check_dip = EpicsChecker(self.dips, energies, dly_dips, parent=self)
        check_dip.itemChecked.connect(self._check_status)

        dlg = ProgressDialog(
            ['Connecting', 'Setting Dipole', 'Waiting Dipole',
             'Checking Dipole'],
            [conn, set_dip, sleep_task, check_dip], parent=self)
        ret = dlg.exec_()
        if ret == dlg.Rejected:
            return
        if self._items_fail:
            failed = ['Failed to set Dipole. Aborting!']
            report = ReportDialog(failed, self)
            report.exec_()
            return

        conn = EpicsConnector(pvs, parent=self)
        set_mags = EpicsSetter(pvs, values, delays, parent=self)
        sleep_task = EpicsWait([None, ]*10, wait_time=2.0, parent=self)
        check_mags = EpicsChecker(pvs, values, delays, parent=self)
        check_mags.itemChecked.connect(self._check_status)

        dlg = ProgressDialog(
            ['Connecting to Magnets', 'Setting Magnets', 'Waiting Magnets',
             'Checking Magnets'],
            [conn, set_mags, sleep_task, check_mags], parent=self)
        ret = dlg.exec_()
        if ret == dlg.Rejected:
            return

        failed = []
        if self._items_fail:
            failed = ['Failed to set magnets:', ] + self._items_fail
        report = ReportDialog(failed, self)
        report.exec_()

    @Slot(str)
    def _read_fail(self, item):
        self._items_fail.append(item)

    @Slot(str, QVariant)
    def _read_success(self, item, value):
        self._items_success.append((item, value))

    @Slot(str, bool)
    def _check_status(self, item, status):
        if status:
            self._items_success.append((item, True))
        else:
            self._items_fail.append(item)