Ejemplo n.º 1
0
class AxisSettingsDisplay(Display):
    def __init__(self, main_display, parent=None):
        super(AxisSettingsDisplay, self).__init__(parent=parent)
        self.main_layout = QFormLayout()
        self.main_display = main_display

        self.chart = self.main_display.chart

        self.x_axis_lbl = QLabel("x-axis Label")
        self.x_axis_label_line_edt = QLineEdit()
        current_x_label = self.chart.labels["bottom"]
        if current_x_label:
            current_x_label = current_x_label[current_x_label.
                                              find(X_AXIS_LABEL_SEPARATOR) +
                                              len(X_AXIS_LABEL_SEPARATOR):]
            self.x_axis_label_line_edt.setText(current_x_label)
        self.x_axis_label_line_edt.textChanged.connect(
            partial(self.handle_axis_label_change, "bottom"))

        self.x_axis_font_btn = QPushButton()
        self.x_axis_font_btn.setMaximumHeight(32)
        self.x_axis_font_btn.setMaximumWidth(32)
        self.x_axis_font_btn.setIcon(IconFont().icon("font"))
        self.x_axis_font_btn.clicked.connect(
            partial(self.handle_font_change, "bottom"))

        self.x_axis_unit_lbl = QLabel("x-axis Unit")
        self.x_axis_unit_edt = QLineEdit()
        self.x_axis_unit_edt.setMaximumWidth(150)
        self.x_axis_unit_edt.setText(self.chart.units["bottom"])
        self.x_axis_unit_edt.textChanged.connect(
            partial(self.handle_axis_label_change, "bottom", is_unit=True))

        self.y_axis_lbl = QLabel("y-axis Label")
        self.y_axis_label_line_edt = QLineEdit()
        self.y_axis_label_line_edt.setText(self.chart.labels["left"])
        self.y_axis_label_line_edt.textChanged.connect(
            partial(self.handle_axis_label_change, "left"))

        self.y_axis_font_btn = QPushButton()
        self.y_axis_font_btn.setMaximumHeight(32)
        self.y_axis_font_btn.setMaximumWidth(32)
        self.y_axis_font_btn.setIcon(IconFont().icon("font"))
        self.y_axis_font_btn.clicked.connect(
            partial(self.handle_font_change, "left"))

        self.y_axis_unit_lbl = QLabel("y-axis Unit")
        self.y_axis_unit_edt = QLineEdit()
        self.y_axis_unit_edt.setMaximumWidth(150)
        self.y_axis_unit_edt.setText(self.chart.units["left"])
        self.y_axis_unit_edt.textChanged.connect(
            partial(self.handle_axis_label_change, "left", is_unit=True))

        self.right_y_axis_label_line_edt = QLineEdit()

        self.display_right_y_axis_chk = QCheckBox("Display the right y-axis")
        self.display_right_y_axis_chk.setChecked(self.chart.getShowRightAxis())
        self.display_right_y_axis_chk.clicked.connect(
            self.handle_right_y_axis_checkbox_changed)
        self.display_right_y_axis_chk.setChecked(self.chart.getShowRightAxis())

        self.right_y_axis_lbl = None
        self.right_y_axis_unit_edt = None
        self.right_y_axis_unit_lbl = None
        self.right_y_axis_unit_edt = None

        self.close_dialog_btn = QPushButton("Close")
        self.close_dialog_btn.clicked.connect(self.handle_close_button_clicked)

        self.setWindowTitle("Axis Settings")
        self.setMinimumSize(500, 300)
        self.setWindowModality(Qt.ApplicationModal)

        self.setup_ui()

    def ui_filepath(self):
        """
        The path to the UI file created by Qt Designer, if applicable.
        """
        # No UI file is being used
        return None

    def ui_filename(self):
        """
        The name of the UI file created by Qt Designer, if applicable.
        """
        # No UI file is being used
        return None

    def setup_ui(self):
        x_axis_layout = QHBoxLayout()
        x_axis_layout.addWidget(self.x_axis_label_line_edt)
        x_axis_layout.addWidget(self.x_axis_font_btn)

        y_axis_layout = QHBoxLayout()
        y_axis_layout.addWidget(self.y_axis_label_line_edt)
        y_axis_layout.addWidget(self.y_axis_font_btn)

        # Add widgets to the form layout
        self.main_layout.setSpacing(10)
        self.main_layout.addRow(self.x_axis_lbl, x_axis_layout)
        self.main_layout.addRow(self.x_axis_unit_lbl, self.x_axis_unit_edt)
        self.main_layout.addRow(self.y_axis_lbl, y_axis_layout)
        self.main_layout.addRow(self.y_axis_unit_lbl, self.y_axis_unit_edt)
        self.main_layout.addRow(self.display_right_y_axis_chk, None)
        self.main_layout.addRow(None, self.close_dialog_btn)

        self.display_right_y_axis_chk.setChecked(self.chart.getShowRightAxis())
        if self.chart.getShowRightAxis():
            self.display_right_y_axis_chk.clicked.emit(
                self.chart.getShowRightAxis())

        self.setLayout(self.main_layout)

    def handle_right_y_axis_checkbox_changed(self, is_checked):
        self.chart.setShowRightAxis(is_checked)

        if is_checked:
            right_label = self.chart.labels["right"]
            if not right_label:
                right_label = self.y_axis_label_line_edt.text()
            right_unit = self.chart.units["right"]
            if not right_unit:
                right_unit = self.y_axis_unit_edt.text()

            self.right_y_axis_font_btn = QPushButton()
            self.right_y_axis_font_btn.setMaximumHeight(32)
            self.right_y_axis_font_btn.setMaximumWidth(32)
            self.right_y_axis_font_btn.setIcon(IconFont().icon("font"))
            self.right_y_axis_font_btn.clicked.connect(
                partial(self.handle_font_change, "right"))

            self.right_y_axis_lbl = QLabel("Right y-axis Label")
            self.right_y_axis_label_line_edt = QLineEdit()
            self.right_y_axis_label_line_edt.textChanged.connect(
                partial(self.handle_axis_label_change, "right"))

            self.right_y_axis_unit_lbl = QLabel("Right y-axis Unit")
            self.right_y_axis_unit_edt = QLineEdit()
            self.right_y_axis_unit_edt.setMaximumWidth(150)
            self.right_y_axis_unit_edt.textChanged.connect(
                partial(self.handle_axis_label_change, "right", is_unit=True))

            self.right_y_axis_label_line_edt.setText(right_label)
            self.right_y_axis_unit_edt.setText(right_unit)

            right_y_axis_layout = QHBoxLayout()
            right_y_axis_layout.addWidget(self.right_y_axis_label_line_edt)
            right_y_axis_layout.addWidget(self.right_y_axis_font_btn)

            self.main_layout.insertRow(self.main_layout.rowCount() - 1,
                                       self.right_y_axis_lbl,
                                       right_y_axis_layout)
            self.main_layout.insertRow(self.main_layout.rowCount() - 1,
                                       self.right_y_axis_unit_lbl,
                                       self.right_y_axis_unit_edt)
        else:
            self.chart.showAxis("right", show=False)
            self.right_y_axis_lbl.deleteLater()
            self.right_y_axis_label_line_edt.deleteLater()
            self.right_y_axis_unit_lbl.deleteLater()
            self.right_y_axis_unit_edt.deleteLater()
            self.right_y_axis_font_btn.deleteLater()

    def handle_axis_label_change(self,
                                 axis_position,
                                 new_label,
                                 is_unit=False):
        if is_unit:
            self.chart.setLabel(axis_position, units=new_label)
            self.chart.units[axis_position] = new_label
        else:
            if axis_position == "bottom":
                current_label = self.chart.getBottomAxisLabel()
                if X_AXIS_LABEL_SEPARATOR in current_label:
                    current_label = current_label[:current_label.find(
                        X_AXIS_LABEL_SEPARATOR) - len(X_AXIS_LABEL_SEPARATOR)]
                new_label = current_label + X_AXIS_LABEL_SEPARATOR + new_label
            self.chart.setLabel(axis_position, text=new_label)
            self.chart.labels[axis_position] = new_label
        return

    def change_axis_font(self, axis, font):
        axis.setTickFont(font)
        axis.label.setFont(font)

    def handle_font_change(self, axis_position):
        axis = self.chart.getAxis(axis_position)
        label = axis.label
        initial = axis.tickFont

        dialog = QFontDialog(self)
        if initial:
            dialog.setCurrentFont(initial)
        dialog.setOption(QFontDialog.DontUseNativeDialog, True)
        dialog.fontSelected.connect(partial(self.change_axis_font, axis))
        dialog.open()

    def closeEvent(self, event):
        self.handle_close_button_clicked()

    def handle_close_button_clicked(self):
        """
        Close the dialog when the Close button is clicked.
        """
        self.close()
Ejemplo n.º 2
0
class SteeringDisplay(Display):
    def __init__(self, parent=None, macros=None, args=[]):
        super(SteeringDisplay, self).__init__(parent=parent,
                                              macros=macros,
                                              args=args)
        self._live_orbit = None
        self.setup_ui()

    def ui_filename(self):
        return None

    def ui_filepath(self):
        return None

    def setup_ui(self):
        self.setWindowTitle("Steering Panel")
        self.draw_timer = QTimer(self)
        self.draw_timer.setInterval(int(1000 / 5))
        self.setLayout(QVBoxLayout())
        self.current_progress = 0
        self.total_progress = 0
        self.loading_label = QLabel(self)
        self.progress_bar = QProgressBar(self)
        self.progress_bar.setMinimum(0)
        self.progress_bar.setMaximum(100)
        self.x_magnet_list = None
        self.layout().addStretch()
        self.layout().addWidget(self.loading_label)
        self.layout().addWidget(self.progress_bar)
        self.layout().addStretch()
        position_scale = 2.0
        self.orbit_view = OrbitView(parent=self,
                                    axis="x",
                                    name="X Orbit",
                                    label="X Orbit",
                                    units="mm",
                                    ymin=-position_scale,
                                    ymax=position_scale,
                                    draw_timer=self.draw_timer)
        self.orbit_view.hide()
        self.layout().addWidget(self.orbit_view)
        self.layout().setStretchFactor(self.orbit_view, 1)
        QTimer.singleShot(50, self.initialize_orbit)

    @Slot()
    def initialize_orbit(self):
        QApplication.instance().processEvents(
        )  #Need to call processEvents to make the status bar message show up before the live orbit connection stuff starts.
        orbit = Orbit.lcls_bpms(auto_connect=False, parent=self)
        self.x_magnet_list = MagnetList("X", "xcor_list.json", parent=self)
        self.total_progress = orbit.progress_total(
        ) + self.x_magnet_list.progress_total()
        num_pvs = orbit.pv_count() + self.x_magnet_list.pv_count()
        self.loading_label.setText("Connecting to {} PVs...".format(num_pvs))
        self.loading_label.setAlignment(Qt.AlignCenter)
        self.progress_bar.setMaximum(self.total_progress)
        orbit.connectionProgress.connect(self.increment_progress)
        self.x_magnet_list.connectionProgress.connect(self.increment_progress)
        orbit.connect()
        orbit.name = "Live Orbit"
        self.live_orbit = orbit
        self.initialize_magnet_lists()
        self.connection_complete()

    @Slot()
    def increment_progress(self):
        self.current_progress += 1
        self.progress_bar.setValue(self.current_progress)

    @Slot()
    def connection_complete(self):
        self.layout().removeWidget(self.progress_bar)
        self.layout().removeWidget(self.loading_label)
        self.progress_bar.deleteLater()
        self.loading_label.deleteLater()
        self.orbit_view.show()

    def initialize_magnet_lists(self):
        self.x_magnet_list.connect()
        self.orbit_view.set_magnet_list(self.x_magnet_list)
        self.orbit_view.show_magnet_views(True)

    @property
    def live_orbit(self):
        return self._live_orbit

    @live_orbit.setter
    def live_orbit(self, new_live_orbit):
        if new_live_orbit == self._live_orbit:
            return
        self._live_orbit = new_live_orbit
        self.orbit_view.set_orbit(self._live_orbit, reset_range=False)