Merge pull request #2001 from pypeclub/feature/1999_number_with_configurable_steps

Settings UI: Number with configurable steps
This commit is contained in:
Jakub Trllo 2021-09-09 21:34:07 +02:00 committed by GitHub
commit a46d1d8bec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 2 deletions

View file

@ -379,6 +379,11 @@ class NumberEntity(InputEntity):
# UI specific attributes
self.show_slider = self.schema_data.get("show_slider", False)
steps = self.schema_data.get("steps", None)
# Make sure that steps are not set to `0`
if steps == 0:
steps = None
self.steps = steps
def _convert_to_valid_type(self, value):
if isinstance(value, str):

View file

@ -316,6 +316,7 @@ How output of the schema could look like on save:
- key `"decimal"` defines how many decimal places will be used, 0 is for integer input (Default: `0`)
- key `"minimum"` as minimum allowed number to enter (Default: `-99999`)
- key `"maxium"` as maximum allowed number to enter (Default: `99999`)
- key `"steps"` will change single step value of UI inputs (using arrows and wheel scroll)
- for UI it is possible to show slider to enable this option set `show_slider` to `true`
```
{

View file

@ -28,7 +28,8 @@
"type": "number",
"key": "AVALON_TIMEOUT",
"minimum": 0,
"label": "Avalon Mongo Timeout (ms)"
"label": "Avalon Mongo Timeout (ms)",
"steps": 100
},
{
"type": "path",

View file

@ -411,7 +411,8 @@ class NumberWidget(InputWidget):
kwargs = {
"minimum": self.entity.minimum,
"maximum": self.entity.maximum,
"decimal": self.entity.decimal
"decimal": self.entity.decimal,
"steps": self.entity.steps
}
self.input_field = NumberSpinBox(self.content_widget, **kwargs)
input_field_stretch = 1
@ -426,6 +427,10 @@ class NumberWidget(InputWidget):
int(self.entity.minimum * slider_multiplier),
int(self.entity.maximum * slider_multiplier)
)
if self.entity.steps is not None:
slider_widget.setSingleStep(
self.entity.steps * slider_multiplier
)
self.content_layout.addWidget(slider_widget, 1)

View file

@ -92,11 +92,15 @@ class NumberSpinBox(QtWidgets.QDoubleSpinBox):
min_value = kwargs.pop("minimum", -99999)
max_value = kwargs.pop("maximum", 99999)
decimals = kwargs.pop("decimal", 0)
steps = kwargs.pop("steps", None)
super(NumberSpinBox, self).__init__(*args, **kwargs)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setDecimals(decimals)
self.setMinimum(min_value)
self.setMaximum(max_value)
if steps is not None:
self.setSingleStep(steps)
def focusInEvent(self, event):
super(NumberSpinBox, self).focusInEvent(event)