예제 #1
0
    def __init__(self, parent=None, collumns=None, **settings):
        """
        Parameters
        ----------
        collumns: list, optional
            List of lists of headers included in each collumn
            The list at index 0 will be the first collumn, etc.

        settings: kwargs
            Mapping of header to list of Setting objects
        """
        self.settings = {}
        self.window = QDialog(parent=parent)
        self.window.setWindowTitle('Skywalker Settings')
        main_layout = QVBoxLayout()
        self.window.setLayout(main_layout)
        self.window.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
        all_cols_layout = QHBoxLayout()
        main_layout.addLayout(all_cols_layout)
        if collumns is None:
            collumns = [list(settings.keys())]
        for col in collumns:
            col_layout = QVBoxLayout()
            all_cols_layout.addLayout(col_layout)
            for header in col:
                title = QLabel()
                title.setText(header.capitalize())
                title.setAlignment(Qt.AlignCenter)
                col_layout.addWidget(title)
                form = QFormLayout()
                col_layout.addLayout(form)
                for setting in settings[header]:
                    self.settings[setting.name] = setting
                    label = QLabel()
                    label.setText(setting.name.capitalize())
                    label.setSizePolicy(QSizePolicy.Minimum,
                                        QSizePolicy.Minimum)
                    form.addRow(label, setting.layout)
            vertical_spacer = QSpacerItem(20, 10, QSizePolicy.Minimum,
                                          QSizePolicy.Expanding)
            col_layout.addItem(vertical_spacer)
        confirm_layout = QHBoxLayout()
        main_layout.addLayout(confirm_layout)
        horizontal_spacer = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                        QSizePolicy.Minimum)
        cancel_button = QPushButton('Cancel')
        cancel_button.pressed.connect(self.window.reject)
        apply_button = QPushButton('Apply')
        apply_button.pressed.connect(self.window.accept)
        confirm_layout.addItem(horizontal_spacer)
        confirm_layout.addWidget(cancel_button)
        confirm_layout.addWidget(apply_button)
예제 #2
0
class LightRow(InactiveRow):
    """
    Basic Widget to display LightDevice information

    The widget shows the device information and state, updating looking at the
    devices :attr:`.inserted` and :attr:`.removed` attributes. The
    :attr:`.remove_button` also allows the user to remove devices by calling
    the :meth:`.remove` method of the given device. The identical button is
    setup if the device is determined to have an `insert` method. Finally,
    PyDMRectangle is used to show the current path of the beam through the
    table

    Parameters
    ----------
    device : obj

    path : BeamPath

    parent : QObject, optional
    """
    def __init__(self, device, parent=None):
        super().__init__(device, parent=parent)
        # Create button widget
        self.buttons = QWidget(parent=parent)
        self.button_layout = QHBoxLayout()
        self.buttons.setLayout(self.button_layout)
        # Create Insert PushButton
        if hasattr(device, 'insert'):
            self.insert_button = QPushButton('Insert', parent=parent)
            self.insert_button.setFont(self.font)
            self.button_layout.addWidget(self.insert_button)
            self.button_layout.addItem(QSpacerItem(10, 20))
        # Create Remove PushButton
        if hasattr(device, 'remove'):
            self.remove_button = QPushButton('Remove', parent=parent)
            self.remove_button.setFont(self.font)
            self.button_layout.addWidget(self.remove_button)
        # Subscribe device to state changes
        try:
            # Wait for later to update widget
            self.device.subscribe(self.update_state,
                                  event_type=self.device.SUB_STATE,
                                  run=False)
        except Exception:
            logger.error("Widget is unable to subscribe to device %s",
                         device.name)

    def update_state(self, *args, **kwargs):
        """
        Update the state label

        The displayed state can be one of ``Unknown`` , ``Inserted``,
        ``Removed`` or ``Error``, with ``Unknown`` being if the device is not
        inserted or removed, and error being if the device is reporting as both
        inserted and removed. The color of the label is also adjusted to either
        green or red to quickly
        """
        states = Enum('states', ('Unknown', 'Inserted', 'Removed', 'Error'))
        # Interpret state
        try:
            state = 1 + int(
                self.device.inserted) + 2 * int(self.device.removed)
        except Exception as exc:
            logger.error(exc)
            state = states.Error.value
        # Set label to state description
        self.state_label.setText(states(state).name)
        # Set the color of the label
        if state == states.Removed.value:
            self.state_label.setStyleSheet("QLabel {color : rgb(124,252,0)}")
        elif state == states.Unknown.value:
            self.state_label.setStyleSheet("QLabel {color : rgb(255, 215, 0)}")
        else:
            self.state_label.setStyleSheet("QLabel {color : red}")
        # Disable buttons if necessary
        if hasattr(self, 'insert_button'):
            self.insert_button.setEnabled(state != states.Inserted.value)
        if hasattr(self, 'remove_button'):
            self.remove_button.setEnabled(state != states.Removed.value)

    @property
    def widgets(self):
        """
        Ordered list of widgets to add to designer
        """
        return [
            self.indicator, self.name_label, self.prefix_label, self.spacer,
            self.state_label, self.buttons
        ]

    def clear_sub(self):
        """
        Clear the subscription event
        """
        self.device.clear_sub(self.update_state)