From c0360d44316aafaa184ffd82c78e950940bc4bba Mon Sep 17 00:00:00 2001 From: hennie-k Date: Fri, 20 Sep 2024 09:11:37 +0200 Subject: [PATCH 1/2] Includes checks to ensure proper conversions between str and num for spinners --- geest/gui/widgets/geest_widget_factory.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/geest/gui/widgets/geest_widget_factory.py b/geest/gui/widgets/geest_widget_factory.py index c881adf8..0912ee4f 100644 --- a/geest/gui/widgets/geest_widget_factory.py +++ b/geest/gui/widgets/geest_widget_factory.py @@ -50,7 +50,7 @@ def create_widgets(layer_data: dict, parent=None): "type": "spinbox", "min": 0, "max": 10000, - "default": layer_data.get("Default Single Buffer Distances", 0), + "default": layer_data.get("Default Single Buffer Distance", 0), "tooltip": "Enter buffer distance." }, "Use Create Grid": { @@ -202,10 +202,14 @@ def create_specific_widget(mapping: dict, layer_data: dict): if widget_type == "doublespinbox": widget = QDoubleSpinBox() - widget.setMinimum(mapping.get("min", 0.0)) - widget.setMaximum(mapping.get("max", 100.0)) - widget.setDecimals(mapping.get("decimals", 1)) - widget.setValue(mapping.get("default", 0.0)) + min_value = mapping.get("min", 0.0) + widget.setMinimum(float(min_value) if min_value != '' else 0.0) + max_value = mapping.get("max", 100.0) + widget.setMaximum(float(max_value) if max_value != '' else 100.0) + decimals = mapping.get("decimals", 1) + widget.setDecimals(int(float(decimals)) if decimals != '' else 1) + default_value = mapping.get("default", 0.0) + widget.setValue(float(default_value) if default_value != '' else 0.0) widget.setToolTip(mapping.get("tooltip", "")) return widget @@ -213,13 +217,15 @@ def create_specific_widget(mapping: dict, layer_data: dict): widget = QSpinBox() widget.setMinimum(mapping.get("min", 0)) widget.setMaximum(mapping.get("max", 10000)) - widget.setValue(mapping.get("default", 0)) + default_value = mapping.get("default", 0) + widget.setValue(int(float(default_value)) if default_value != '' else 0) widget.setToolTip(mapping.get("tooltip", "")) return widget elif widget_type == "lineedit": widget = QLineEdit() - widget.setText(mapping.get("default", "")) + default_value = mapping.get("default", "") + widget.setText(str(default_value)) widget.setToolTip(mapping.get("tooltip", "")) return widget From df8821cb7eda51cecfc352e059bed61c449b81f2 Mon Sep 17 00:00:00 2001 From: hennie-k Date: Fri, 20 Sep 2024 09:28:28 +0200 Subject: [PATCH 2/2] Includes GIGO guard --- geest/gui/widgets/geest_widget_factory.py | 33 ++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/geest/gui/widgets/geest_widget_factory.py b/geest/gui/widgets/geest_widget_factory.py index 0912ee4f..85d93d72 100644 --- a/geest/gui/widgets/geest_widget_factory.py +++ b/geest/gui/widgets/geest_widget_factory.py @@ -200,25 +200,34 @@ def create_specific_widget(mapping: dict, layer_data: dict): """ widget_type = mapping["type"] + #-- guard against GIGO + def safe_float(value, default): + try: + return float(value) if value != '' else default + except (ValueError, TypeError): + return default + + # -- guard against GIGO + def safe_int(value, default): + try: + return int(float(value)) if value != '' else default + except (ValueError, TypeError): + return default + if widget_type == "doublespinbox": widget = QDoubleSpinBox() - min_value = mapping.get("min", 0.0) - widget.setMinimum(float(min_value) if min_value != '' else 0.0) - max_value = mapping.get("max", 100.0) - widget.setMaximum(float(max_value) if max_value != '' else 100.0) - decimals = mapping.get("decimals", 1) - widget.setDecimals(int(float(decimals)) if decimals != '' else 1) - default_value = mapping.get("default", 0.0) - widget.setValue(float(default_value) if default_value != '' else 0.0) + widget.setMinimum(safe_float(mapping.get("min"), 0.0)) + widget.setMaximum(safe_float(mapping.get("max"), 100.0)) + widget.setDecimals(safe_int(mapping.get("decimals"), 1)) + widget.setValue(safe_float(mapping.get("default"), 0.0)) widget.setToolTip(mapping.get("tooltip", "")) return widget elif widget_type == "spinbox": widget = QSpinBox() - widget.setMinimum(mapping.get("min", 0)) - widget.setMaximum(mapping.get("max", 10000)) - default_value = mapping.get("default", 0) - widget.setValue(int(float(default_value)) if default_value != '' else 0) + widget.setMinimum(safe_int(mapping.get("min"), 0)) + widget.setMaximum(safe_int(mapping.get("max"), 10000)) + widget.setValue(safe_int(mapping.get("default"), 0)) widget.setToolTip(mapping.get("tooltip", "")) return widget