| 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 |
import re |
| 28 |
|
| 29 |
from .Qt import QtGui, QtWidgets |
| 30 |
|
| 31 |
log = logging.getLogger(__name__) |
| 32 |
log.addHandler(logging.NullHandler()) |
| 33 |
|
| 34 |
|
| 35 |
class Input: |
| 36 |
""" |
| 37 |
Mix-in class that connects a :mod:`Parameter <.parameters>` object to a GUI |
| 38 |
input box. |
| 39 |
|
| 40 |
:param parameter: The parameter to connect to this input box. |
| 41 |
:attr parameter: Read-only property to access the associated parameter. |
| 42 |
""" |
| 43 |
|
| 44 |
def __init__(self, parameter, **kwargs): |
| 45 |
super().__init__(**kwargs) |
| 46 |
self._parameter = None |
| 47 |
self.set_parameter(parameter) |
| 48 |
|
| 49 |
def set_parameter(self, parameter): |
| 50 |
""" |
| 51 |
Connects a new parameter to the input box, and initializes the box |
| 52 |
value. |
| 53 |
|
| 54 |
:param parameter: parameter to connect. |
| 55 |
""" |
| 56 |
self._parameter = parameter |
| 57 |
|
| 58 |
if parameter.is_set(): |
| 59 |
self.setValue(parameter.value) |
| 60 |
|
| 61 |
if hasattr(parameter, 'units') and parameter.units: |
| 62 |
self.setSuffix(" %s" % parameter.units) |
| 63 |
|
| 64 |
def update_parameter(self): |
| 65 |
""" |
| 66 |
Update the parameter value with the Input GUI element's current value. |
| 67 |
""" |
| 68 |
self._parameter.value = self.value() |
| 69 |
|
| 70 |
@property |
| 71 |
def parameter(self): |
| 72 |
""" |
| 73 |
The connected parameter object. Read-only property; see |
| 74 |
:meth:`set_parameter`. |
| 75 |
|
| 76 |
Note that reading this property will have the side-effect of updating |
| 77 |
its value from the GUI input box. |
| 78 |
""" |
| 79 |
self.update_parameter() |
| 80 |
return self._parameter |
| 81 |
|
| 82 |
|
| 83 |
class StringInput(Input, QtWidgets.QLineEdit): |
| 84 |
""" |
| 85 |
String input box connected to a :class:`Parameter`. Parameter subclasses |
| 86 |
that are string-based may also use this input, but non-string parameters |
| 87 |
should use more specialised input classes. |
| 88 |
""" |
| 89 |
|
| 90 |
def __init__(self, parameter, parent=None, **kwargs): |
| 91 |
super().__init__(parameter=parameter, parent=parent, **kwargs) |
| 92 |
|
| 93 |
def setValue(self, value): |
| 94 |
# QtWidgets.QLineEdit has a setText() method instead of setValue() |
| 95 |
return super().setText(value) |
| 96 |
|
| 97 |
def setSuffix(self, value): |
| 98 |
pass |
| 99 |
|
| 100 |
def value(self): |
| 101 |
# QtWidgets.QLineEdit has a text() method instead of value() |
| 102 |
return super().text() |
| 103 |
|
| 104 |
|
| 105 |
class IntegerInput(Input, QtWidgets.QSpinBox): |
| 106 |
""" |
| 107 |
Spin input box for integer values, connected to a :class:`IntegerParameter`. |
| 108 |
""" |
| 109 |
|
| 110 |
def __init__(self, parameter, parent=None, **kwargs): |
| 111 |
super().__init__(parameter=parameter, parent=parent, **kwargs) |
| 112 |
if parameter.step: |
| 113 |
self.setButtonSymbols(QtWidgets.QAbstractSpinBox.ButtonSymbols.UpDownArrows) |
| 114 |
self.setSingleStep(parameter.step) |
| 115 |
self.setEnabled(True) |
| 116 |
else: |
| 117 |
self.setButtonSymbols(QtWidgets.QAbstractSpinBox.ButtonSymbols.NoButtons) |
| 118 |
|
| 119 |
def set_parameter(self, parameter): |
| 120 |
# Override from :class:`Input` |
| 121 |
self.setMinimum(parameter.minimum) |
| 122 |
self.setMaximum(parameter.maximum) |
| 123 |
super().set_parameter(parameter) # default gets set here, after min/max |
| 124 |
|
| 125 |
def stepEnabled(self): |
| 126 |
if self.parameter.step: |
| 127 |
return QtWidgets.QAbstractSpinBox.StepEnabledFlag.StepUpEnabled | \ |
| 128 |
QtWidgets.QAbstractSpinBox.StepEnabledFlag.StepDownEnabled |
| 129 |
else: |
| 130 |
return QtWidgets.QAbstractSpinBox.StepEnabledFlag.StepNone |
| 131 |
|
| 132 |
|
| 133 |
class BooleanInput(Input, QtWidgets.QCheckBox): |
| 134 |
""" |
| 135 |
Checkbox for boolean values, connected to a :class:`BooleanParameter`. |
| 136 |
""" |
| 137 |
|
| 138 |
def __init__(self, parameter, parent=None, **kwargs): |
| 139 |
super().__init__(parameter=parameter, parent=parent, **kwargs) |
| 140 |
|
| 141 |
def set_parameter(self, parameter): |
| 142 |
# Override from :class:`Input` |
| 143 |
self.setText(parameter.name) |
| 144 |
super().set_parameter(parameter) |
| 145 |
|
| 146 |
def setValue(self, value): |
| 147 |
return super().setChecked(value) |
| 148 |
|
| 149 |
def setSuffix(self, value): |
| 150 |
pass |
| 151 |
|
| 152 |
def value(self): |
| 153 |
return super().isChecked() |
| 154 |
|
| 155 |
|
| 156 |
class ListInput(Input, QtWidgets.QComboBox): |
| 157 |
""" |
| 158 |
Dropdown for list values, connected to a :class:`ListParameter`. |
| 159 |
""" |
| 160 |
|
| 161 |
def __init__(self, parameter, parent=None, **kwargs): |
| 162 |
super().__init__(parameter=parameter, parent=parent, **kwargs) |
| 163 |
self._stringChoices = None |
| 164 |
self.setEditable(False) |
| 165 |
|
| 166 |
def set_parameter(self, parameter): |
| 167 |
# Override from :class:`Input` |
| 168 |
try: |
| 169 |
if hasattr(parameter, 'units') and parameter.units: |
| 170 |
suffix = " %s" % parameter.units |
| 171 |
else: |
| 172 |
suffix = "" |
| 173 |
|
| 174 |
self._stringChoices = tuple((str(choice) + suffix) for choice in parameter.choices) |
| 175 |
except TypeError: # choices is None |
| 176 |
self._stringChoices = tuple() |
| 177 |
self.clear() |
| 178 |
self.addItems(self._stringChoices) |
| 179 |
|
| 180 |
super().set_parameter(parameter) |
| 181 |
|
| 182 |
def setValue(self, value): |
| 183 |
try: |
| 184 |
index = self._parameter.choices.index(value) |
| 185 |
self.setCurrentIndex(index) |
| 186 |
except (TypeError, ValueError) as e: # no choices or choice invalid |
| 187 |
raise ValueError("Invalid choice for parameter. " |
| 188 |
"Must be one of %s" % str(self._parameter.choices)) from e |
| 189 |
|
| 190 |
def setSuffix(self, value): |
| 191 |
pass |
| 192 |
|
| 193 |
def value(self): |
| 194 |
return self._parameter.choices[self.currentIndex()] |
| 195 |
|
| 196 |
|
| 197 |
class ScientificInput(Input, QtWidgets.QDoubleSpinBox): |
| 198 |
""" |
| 199 |
Spinner input box for floating-point values, connected to a |
| 200 |
:class:`FloatParameter`. This box will display and accept values in |
| 201 |
scientific notation when appropriate. |
| 202 |
|
| 203 |
.. seealso:: |
| 204 |
Class :class:`~.FloatInput` |
| 205 |
For a non-scientific floating-point input box. |
| 206 |
""" |
| 207 |
|
| 208 |
def __init__(self, parameter, parent=None, **kwargs): |
| 209 |
super().__init__(parameter=parameter, parent=parent, **kwargs) |
| 210 |
if parameter.step: |
| 211 |
self.setButtonSymbols(QtWidgets.QAbstractSpinBox.ButtonSymbols.UpDownArrows) |
| 212 |
self.setSingleStep(parameter.step) |
| 213 |
self.setEnabled(True) |
| 214 |
else: |
| 215 |
self.setButtonSymbols(QtWidgets.QAbstractSpinBox.ButtonSymbols.NoButtons) |
| 216 |
|
| 217 |
def set_parameter(self, parameter): |
| 218 |
# Override from :class:`Input` |
| 219 |
self._parameter = parameter # required before super().set_parameter |
| 220 |
# for self.validate which is called when setting self.decimals() |
| 221 |
self.validator = QtGui.QDoubleValidator( |
| 222 |
parameter.minimum, |
| 223 |
parameter.maximum, |
| 224 |
parameter.decimals, |
| 225 |
self) |
| 226 |
self.setDecimals(parameter.decimals) |
| 227 |
self.setMinimum(parameter.minimum) |
| 228 |
self.setMaximum(parameter.maximum) |
| 229 |
self.validator.setNotation(QtGui.QDoubleValidator.Notation.ScientificNotation) |
| 230 |
super().set_parameter(parameter) # default gets set here, after min/max |
| 231 |
|
| 232 |
def validate(self, text, pos): |
| 233 |
if self._parameter.units: |
| 234 |
text = text[:-(len(self._parameter.units) + 1)] |
| 235 |
result = self.validator.validate(text, pos) |
| 236 |
return result[0], result[1] + " %s" % self._parameter.units, result[2] |
| 237 |
else: |
| 238 |
return self.validator.validate(text, pos) |
| 239 |
|
| 240 |
def fixCase(self, text): |
| 241 |
self.lineEdit().setText(text.toLower()) |
| 242 |
|
| 243 |
def toDouble(self, string): |
| 244 |
value, success = self.validator.locale().toDouble(string) |
| 245 |
if not success: |
| 246 |
raise ValueError('String could not be converted to a double') |
| 247 |
else: |
| 248 |
return value |
| 249 |
|
| 250 |
def toString(self, value, format='g', precision=6): |
| 251 |
return self.validator.locale().toString(value, format, precision) |
| 252 |
|
| 253 |
def valueFromText(self, text): |
| 254 |
text = str(text) |
| 255 |
if self._parameter.units: |
| 256 |
text = text[:-(len(self._parameter.units) + 1)] |
| 257 |
try: |
| 258 |
val = self.toDouble(text) |
| 259 |
except ValueError: |
| 260 |
val = self._parameter.default |
| 261 |
return val |
| 262 |
|
| 263 |
def textFromValue(self, value): |
| 264 |
string = self.toString(value).replace("e+", "e") |
| 265 |
string = re.sub(r"e(-?)0*(\d+)", r"e\1\2", string) |
| 266 |
return string |
| 267 |
|
| 268 |
def stepEnabled(self): |
| 269 |
if self.parameter.step: |
| 270 |
return QtWidgets.QAbstractSpinBox.StepEnabledFlag.StepUpEnabled | \ |
| 271 |
QtWidgets.QAbstractSpinBox.StepEnabledFlag.StepDownEnabled |
| 272 |
else: |
| 273 |
return QtWidgets.QAbstractSpinBox.StepEnabledFlag.StepNone |