| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
|
| 27 |
from functools import partial |
| 28 |
|
| 29 |
from ..inputs import BooleanInput, IntegerInput, ListInput, ScientificInput, StringInput |
| 30 |
from ..Qt import QtWidgets, QtCore |
| 31 |
from ...experiment import parameters |
| 32 |
|
| 33 |
log = logging.getLogger(__name__) |
| 34 |
log.addHandler(logging.NullHandler()) |
| 35 |
|
| 36 |
|
| 37 |
class InputsWidget(QtWidgets.QWidget): |
| 38 |
""" |
| 39 |
Widget wrapper for various :doc:`inputs` |
| 40 |
""" |
| 41 |
|
| 42 |
# tuple of Input classes that do not need an external label |
| 43 |
NO_LABEL_INPUTS = (BooleanInput,) |
| 44 |
|
| 45 |
def __init__(self, procedure_class, inputs=(), parent=None, hide_groups=True, |
| 46 |
inputs_in_scrollarea=False): |
| 47 |
super().__init__(parent) |
| 48 |
self._procedure_class = procedure_class |
| 49 |
self._procedure = procedure_class() |
| 50 |
self._inputs = inputs |
| 51 |
self._setup_ui() |
| 52 |
self._layout(inputs_in_scrollarea) |
| 53 |
self._hide_groups = hide_groups |
| 54 |
self._setup_visibility_groups() |
| 55 |
|
| 56 |
def _setup_ui(self): |
| 57 |
parameter_objects = self._procedure.parameter_objects() |
| 58 |
for name in self._inputs: |
| 59 |
parameter = parameter_objects[name] |
| 60 |
if parameter.ui_class is not None: |
| 61 |
element = parameter.ui_class(parameter) |
| 62 |
|
| 63 |
elif isinstance(parameter, parameters.FloatParameter): |
| 64 |
element = ScientificInput(parameter) |
| 65 |
|
| 66 |
elif isinstance(parameter, parameters.IntegerParameter): |
| 67 |
element = IntegerInput(parameter) |
| 68 |
|
| 69 |
elif isinstance(parameter, parameters.BooleanParameter): |
| 70 |
element = BooleanInput(parameter) |
| 71 |
|
| 72 |
elif isinstance(parameter, parameters.ListParameter): |
| 73 |
element = ListInput(parameter) |
| 74 |
|
| 75 |
elif isinstance(parameter, parameters.Parameter): |
| 76 |
element = StringInput(parameter) |
| 77 |
|
| 78 |
setattr(self, name, element) |
| 79 |
|
| 80 |
def _layout(self, inputs_in_scrollarea): |
| 81 |
vbox = QtWidgets.QVBoxLayout(self) |
| 82 |
vbox.setSpacing(6) |
| 83 |
vbox.setContentsMargins(0, 0, 0, 0) |
| 84 |
|
| 85 |
self.labels = {} |
| 86 |
parameters = self._procedure.parameter_objects() |
| 87 |
for name in self._inputs: |
| 88 |
if not isinstance(getattr(self, name), self.NO_LABEL_INPUTS): |
| 89 |
label = QtWidgets.QLabel(self) |
| 90 |
label.setText("%s:" % parameters[name].name) |
| 91 |
vbox.addWidget(label) |
| 92 |
self.labels[name] = label |
| 93 |
|
| 94 |
vbox.addWidget(getattr(self, name)) |
| 95 |
|
| 96 |
if inputs_in_scrollarea: |
| 97 |
scroll_area = QtWidgets.QScrollArea() |
| 98 |
scroll_area.setWidgetResizable(True) |
| 99 |
scroll_area.setFrameStyle(QtWidgets.QScrollArea.Shape.NoFrame) |
| 100 |
scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff) |
| 101 |
|
| 102 |
inputs = QtWidgets.QWidget(self) |
| 103 |
inputs.setLayout(vbox) |
| 104 |
inputs.setSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, |
| 105 |
QtWidgets.QSizePolicy.Policy.Fixed) |
| 106 |
scroll_area.setWidget(inputs) |
| 107 |
|
| 108 |
vbox = QtWidgets.QVBoxLayout(self) |
| 109 |
vbox.setContentsMargins(0, 0, 0, 0) |
| 110 |
vbox.addWidget(scroll_area, 1) |
| 111 |
|
| 112 |
self.setLayout(vbox) |
| 113 |
|
| 114 |
def _setup_visibility_groups(self): |
| 115 |
groups = {} |
| 116 |
parameters = self._procedure.parameter_objects() |
| 117 |
for name in self._inputs: |
| 118 |
parameter = parameters[name] |
| 119 |
|
| 120 |
group_state = {g: True for g in parameter.group_by} |
| 121 |
|
| 122 |
for group_name, condition in parameter.group_by.items(): |
| 123 |
if group_name not in self._inputs or group_name == name: |
| 124 |
continue |
| 125 |
|
| 126 |
if isinstance(getattr(self, group_name), BooleanInput): |
| 127 |
# Adjust the boolean condition to a condition suitable for a checkbox |
| 128 |
condition = bool(condition) |
| 129 |
|
| 130 |
if group_name not in groups: |
| 131 |
groups[group_name] = [] |
| 132 |
|
| 133 |
groups[group_name].append((name, condition, group_state)) |
| 134 |
|
| 135 |
for group_name, group in groups.items(): |
| 136 |
toggle = partial(self.toggle_group, group_name=group_name, group=group) |
| 137 |
group_el = getattr(self, group_name) |
| 138 |
if isinstance(group_el, BooleanInput): |
| 139 |
group_el.toggled.connect(toggle) |
| 140 |
toggle(group_el.isChecked()) |
| 141 |
elif isinstance(group_el, StringInput): |
| 142 |
group_el.textChanged.connect(toggle) |
| 143 |
toggle(group_el.text()) |
| 144 |
elif isinstance(group_el, (IntegerInput, ScientificInput)): |
| 145 |
group_el.valueChanged.connect(toggle) |
| 146 |
toggle(group_el.value()) |
| 147 |
elif isinstance(group_el, ListInput): |
| 148 |
group_el.currentTextChanged.connect(toggle) |
| 149 |
toggle(group_el.currentText()) |
| 150 |
else: |
| 151 |
raise NotImplementedError( |
| 152 |
f"Grouping based on {group_name} ({group_el}) is not implemented.") |
| 153 |
|
| 154 |
def toggle_group(self, state, group_name, group): |
| 155 |
for (name, condition, group_state) in group: |
| 156 |
if callable(condition): |
| 157 |
group_state[group_name] = condition(state) |
| 158 |
else: |
| 159 |
group_state[group_name] = (state == condition) |
| 160 |
|
| 161 |
visible = all(group_state.values()) |
| 162 |
|
| 163 |
if self._hide_groups: |
| 164 |
getattr(self, name).setHidden(not visible) |
| 165 |
else: |
| 166 |
getattr(self, name).setDisabled(not visible) |
| 167 |
|
| 168 |
if name in self.labels: |
| 169 |
if self._hide_groups: |
| 170 |
self.labels[name].setHidden(not visible) |
| 171 |
else: |
| 172 |
self.labels[name].setDisabled(not visible) |
| 173 |
|
| 174 |
def set_parameters(self, parameter_objects): |
| 175 |
for name in self._inputs: |
| 176 |
element = getattr(self, name) |
| 177 |
element.set_parameter(parameter_objects[name]) |
| 178 |
|
| 179 |
def get_procedure(self): |
| 180 |
""" Returns the current procedure """ |
| 181 |
self._procedure = self._procedure_class() |
| 182 |
parameter_values = {} |
| 183 |
for name in self._inputs: |
| 184 |
element = getattr(self, name) |
| 185 |
parameter_values[name] = element.parameter.value |
| 186 |
self._procedure.set_parameters(parameter_values) |
| 187 |
return self._procedure |