Skip to content

device_view

frog.gui.hardware_set.device_view ¤

Provides a control for viewing and connecting to devices.

Attributes¤

ParameterWidget = ComboParameterWidget | TextParameterWidget module-attribute ¤

A type alias for a widget which supports getting and setting a parameter.

Classes¤

ComboParameterWidget(values) ¤

Bases: QComboBox

A widget showing the possible parameter values in a combo box.

Create a new ComboParameterWidget.

Parameters:

Name Type Description Default
values Sequence

The possible values for this parameter

required
Source code in frog/gui/hardware_set/device_view.py
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(self, values: Sequence) -> None:
    """Create a new ComboParameterWidget.

    Args:
        values: The possible values for this parameter
    """
    super().__init__()
    self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)

    # Keep the "real" value along with its string representation, so that we can
    # pass it back to the backend on device open
    for value in values:
        self.addItem(str(value), value)
Attributes¤
value property writable ¤

The currently selected parameter value.

Functions¤

ConnectionStatusControl(initial_status=ConnectionStatus.DISCONNECTED) ¤

Bases: QWidget

A widget to show whether a device's connection status.

Create a new ConnectionStatusControl.

Source code in frog/gui/hardware_set/device_view.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def __init__(
    self, initial_status: ConnectionStatus = ConnectionStatus.DISCONNECTED
):
    """Create a new ConnectionStatusControl."""
    super().__init__()
    self._status: ConnectionStatus

    self._led = LEDIcon.create_green_icon()
    self._label = QLabel()

    layout = QHBoxLayout()
    layout.addWidget(self._led)
    layout.addWidget(self._label)
    self.setLayout(layout)

    self.set_status(initial_status)
Functions¤
set_status(status) ¤

Set the device status to display.

Source code in frog/gui/hardware_set/device_view.py
419
420
421
422
423
424
425
426
427
428
429
430
def set_status(self, status: ConnectionStatus) -> None:
    """Set the device status to display."""
    match status:
        case ConnectionStatus.DISCONNECTED:
            self._led.turn_off()
            self._label.setText("Disconnected")
        case ConnectionStatus.CONNECTING:
            self._led.turn_off()
            self._label.setText("Connecting...")
        case ConnectionStatus.CONNECTED:
            self._led.turn_on()
            self._label.setText("Connected")

DeviceControl(connected_devices) ¤

Bases: QGroupBox

Allows for viewing and connecting to devices.

Create a new DeviceControl.

Source code in frog/gui/hardware_set/device_view.py
352
353
354
355
356
357
358
359
360
361
362
def __init__(self, connected_devices: Set[OpenDeviceArgs]) -> None:
    """Create a new DeviceControl."""
    super().__init__("Device control")
    self.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
    self.setLayout(QVBoxLayout())
    self._connected_devices = connected_devices
    """The devices already connected when the control is created."""

    # Retrieve the list of device plugins
    pub.subscribe(self._on_device_list, "device.list.response")
    pub.sendMessage("device.list.request")
Functions¤

DeviceParametersWidget(device_type) ¤

Bases: QWidget

A widget containing controls for setting a device's parameters.

Create a new DeviceParametersWidget.

Parameters:

Name Type Description Default
device_type DeviceTypeInfo

The device type whose parameters will be used

required
Source code in frog/gui/hardware_set/device_view.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def __init__(self, device_type: DeviceTypeInfo) -> None:
    """Create a new DeviceParametersWidget.

    Args:
        device_type: The device type whose parameters will be used
    """
    super().__init__()

    self.device_type = device_type
    """This value is not used within the class, but is stored for convenience."""

    layout = QHBoxLayout()
    layout.setContentsMargins(0, 0, 0, 0)
    self.setLayout(layout)

    # Make a widget for each parameter
    self._param_widgets: dict[str, ParameterWidget] = {}
    for name, param in device_type.parameters.items():
        widget: ParameterWidget
        if isinstance(param.possible_values, Sequence):
            widget = ComboParameterWidget(param.possible_values)
        else:
            widget = TextParameterWidget(param.possible_values)

        widget.setToolTip(param.description)

        if param.default_value is not None:
            widget.value = param.default_value

        layout.addWidget(widget)
        self._param_widgets[name] = widget

    # If there are saved parameter values, load them now
    self.load_saved_parameter_values()
Attributes¤
current_parameter_values property ¤

Get all parameters and their current values.

device_type = device_type instance-attribute ¤

This value is not used within the class, but is stored for convenience.

Functions¤
load_saved_parameter_values() ¤

Set the combo boxes' parameter values according to their saved values.

Source code in frog/gui/hardware_set/device_view.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def load_saved_parameter_values(self) -> None:
    """Set the combo boxes' parameter values according to their saved values."""
    params = cast(
        dict[str, Any] | None,
        settings.value(f"device/params/{self.device_type.class_name}"),
    )
    if not params:
        return

    for param, value in params.items():
        try:
            self._param_widgets[param].value = value
        except Exception as error:
            logging.warn(f"Error while setting param {param}: {error!s}")

DeviceTypeControl(description, instance, device_types, active_device_type=None, device_status=ConnectionStatus.DISCONNECTED) ¤

Bases: QGroupBox

A set of widgets for choosing a device and its params and connecting to it.

Create a new DeviceTypeControl.

Parameters:

Name Type Description Default
description str

A description of the device type

required
instance DeviceInstanceRef

The device instance this panel is for

required
device_types Sequence[DeviceTypeInfo]

The available devices for this base device type

required
active_device_type str | None

The class name for this device type, if opened

None
device_status ConnectionStatus

The connection status for this device type

DISCONNECTED
Source code in frog/gui/hardware_set/device_view.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
def __init__(
    self,
    description: str,
    instance: DeviceInstanceRef,
    device_types: Sequence[DeviceTypeInfo],
    active_device_type: str | None = None,
    device_status: ConnectionStatus = ConnectionStatus.DISCONNECTED,
) -> None:
    """Create a new DeviceTypeControl.

    Args:
        description: A description of the device type
        instance: The device instance this panel is for
        device_types: The available devices for this base device type
        active_device_type: The class name for this device type, if opened
        device_status: The connection status for this device type
    """
    if not device_types:
        raise ValueError("At least one device type must be specified")
    if device_status == ConnectionStatus.DISCONNECTED:
        if active_device_type is not None:
            raise ValueError(
                "active_device_type supplied even though status is disconnected"
            )
    elif not active_device_type:
        raise ValueError("Missing active_device_type")

    self._device_instance = instance

    super().__init__(description)
    self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)

    layout = QHBoxLayout()
    self.setLayout(layout)

    self._device_combo = QComboBox()
    """Combo box allowing the user to choose the device."""
    self._device_combo.setSizePolicy(
        QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed
    )

    # Add names for devices to combo box along with relevant user data
    self._device_widgets: list[DeviceParametersWidget] = []
    for t in device_types:
        widget = DeviceParametersWidget(t)
        widget.hide()  # will be shown when used

        self._device_combo.addItem(t.description, widget)

        # YUCK: We have to keep our own reference to widget, as self._device_combo
        # seemingly won't prevent it from being GC'd
        self._device_widgets.append(widget)

    # Select the last device that was successfully opened, if there is one
    previous_device = cast(str | None, settings.value(f"device/type/{instance!s}"))
    if previous_device:
        self._select_device(previous_device)

    layout.addWidget(self._device_combo)

    # Show the combo boxes for the device's parameters
    current_widget = self.current_device_type_widget
    current_widget.show()
    layout.addWidget(current_widget)

    self._status_control = ConnectionStatusControl()
    layout.addWidget(self._status_control)

    self._open_close_btn = QPushButton()
    self._open_close_btn.setSizePolicy(
        QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed
    )
    self._open_close_btn.clicked.connect(self._on_open_close_clicked)
    layout.addWidget(self._open_close_btn)

    if active_device_type:
        self._select_device(active_device_type)
    self._set_device_status(device_status)

    # Determine whether the button should be enabled or not
    self._update_open_btn_enabled_state()

    self._device_combo.currentIndexChanged.connect(self._on_device_selected)

    # pubsub subscriptions
    pub.subscribe(self._on_device_open_start, f"device.before_opening.{instance!s}")
    pub.subscribe(self._on_device_open_end, f"device.after_opening.{instance!s}")
    pub.subscribe(self._on_device_closed, f"device.closed.{instance!s}")
Attributes¤
current_device_type_widget property ¤

Get information about the currently selected device type.

Functions¤

TextParameterWidget(param_type) ¤

Bases: QLineEdit

A widget allowing the user to enter parameter values into a text box.

Create a new TextParameterWidget.

Parameters:

Name Type Description Default
param_type type

The type that the parameter must be

required
Source code in frog/gui/hardware_set/device_view.py
65
66
67
68
69
70
71
72
def __init__(self, param_type: type) -> None:
    """Create a new TextParameterWidget.

    Args:
        param_type: The type that the parameter must be
    """
    super().__init__()
    self._param_type = param_type
Attributes¤
value property writable ¤

The currently selected parameter value.

Raises:

Type Description
Exception

If relevant type cannot be constructed from string

Functions¤

Functions¤