Skip to content

Commit

Permalink
Reformats source
Browse files Browse the repository at this point in the history
  • Loading branch information
hennie-k committed Sep 23, 2024
1 parent 2c0ac6b commit 96ed2f2
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 87 deletions.
1 change: 0 additions & 1 deletion geest/gui/tree_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from geest.core.workflow_queue_manager import WorkflowQueueManager



class TreePanel(QWidget):
def __init__(self, parent=None, json_file=None):
super().__init__(parent)
Expand Down
4 changes: 3 additions & 1 deletion geest/gui/widgets/classify_poly_into_classes_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def __init__(self, parent=None):
print("[ClassifyPolyIntoClassesWidget] No initial layer selected")

def update_fields(self, layer):
print(f"[ClassifyPolyIntoClassesWidget] Updating fields for layer: {layer.name() if layer else 'None'}")
print(
f"[ClassifyPolyIntoClassesWidget] Updating fields for layer: {layer.name() if layer else 'None'}"
)
self.field_selector.clear()
if isinstance(layer, QgsVectorLayer):
fields = [field.name() for field in layer.fields()]
Expand Down
176 changes: 133 additions & 43 deletions geest/gui/widgets/geest_config_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
QDoubleSpinBox,
QComboBox,
QButtonGroup,
QLayout
QLayout,
)
from qgis.PyQt.QtCore import pyqtSignal
from qgis.gui import QgsMapLayerComboBox
Expand All @@ -33,14 +33,18 @@ def create_widgets(self):
self.setLayout(layout)

print("Calling GeestWidgetFactory.create_widgets")
widgets_container = GeestWidgetFactory.create_widgets(self.original_config, self)
widgets_container = GeestWidgetFactory.create_widgets(
self.original_config, self
)

if widgets_container is None:
print("GeestWidgetFactory.create_widgets returned None")
return

if not isinstance(widgets_container, QWidget):
print(f"GeestWidgetFactory.create_widgets returned unexpected type: {type(widgets_container)}")
print(
f"GeestWidgetFactory.create_widgets returned unexpected type: {type(widgets_container)}"
)
return

if widgets_container.layout() is None:
Expand Down Expand Up @@ -70,21 +74,42 @@ def recursive_find_and_store_widgets(self, widget, depth=0):
if isinstance(widget, QRadioButton):
self.widgets[use_key]["radio"] = widget
print(" " * depth + f"Stored QRadioButton for key: {use_key}")
elif isinstance(widget, (QLineEdit, QSpinBox, QDoubleSpinBox, QComboBox, QgsMapLayerComboBox)):
elif isinstance(
widget,
(QLineEdit, QSpinBox, QDoubleSpinBox, QComboBox, QgsMapLayerComboBox),
):
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored {type(widget).__name__} for key: {use_key}")
elif isinstance(widget, QWidget) and widget.property("widget_type") == "multibuffer":
print(
" " * depth + f"Stored {type(widget).__name__} for key: {use_key}"
)
elif (
isinstance(widget, QWidget)
and widget.property("widget_type") == "multibuffer"
):
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored multibuffer widget for key: {use_key}")
elif isinstance(widget, QWidget) and widget.property("widget_type") == "classify_poly_into_classes":
elif (
isinstance(widget, QWidget)
and widget.property("widget_type") == "classify_poly_into_classes"
):
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored classify_poly_into_classes widget for key: {use_key}")
elif isinstance(widget, QWidget) and widget.findChild(QgsMapLayerComboBox) and widget.findChild(QComboBox):
print(
" " * depth
+ f"Stored classify_poly_into_classes widget for key: {use_key}"
)
elif (
isinstance(widget, QWidget)
and widget.findChild(QgsMapLayerComboBox)
and widget.findChild(QComboBox)
):
self.widgets[use_key]["widget"] = widget
print(" " * depth + f"Stored composite widget (polygon_layer_with_field_selector) for key: {use_key}")
print(
" " * depth
+ f"Stored composite widget (polygon_layer_with_field_selector) for key: {use_key}"
)

# Check if the widget has a layout
layout = widget.layout() if callable(getattr(widget, 'layout', None)) else None
layout = widget.layout() if callable(getattr(widget, "layout", None)) else None
if layout:
for i in range(layout.count()):
item = layout.itemAt(i)
Expand All @@ -101,54 +126,82 @@ def setup_connections(self):

# Always set up the radio button connection
if radio:
radio.toggled.connect(lambda checked, k=key: self.handle_option_change(k, checked))
radio.toggled.connect(
lambda checked, k=key: self.handle_option_change(k, checked)
)
print(f"Set up radio connection for {key}")

# Retrieve the widget_type property, if set
widget_type = widget.property("widget_type") if widget else None

# Handle specific widget types
if widget_type == "classify_poly_into_classes":
print(f"Setting up specific connections for widget_type: {widget_type} (key: {key})")
if hasattr(widget, 'selectionsChanged'):
print(
f"Setting up specific connections for widget_type: {widget_type} (key: {key})"
)
if hasattr(widget, "selectionsChanged"):
# Connect the custom signal to update_classify_poly_config
widget.selectionsChanged.connect(lambda k=key: self.update_classify_poly_config(k))
widget.selectionsChanged.connect(
lambda k=key: self.update_classify_poly_config(k)
)
print(f"Connected selectionsChanged signal for key: {key}")

# Existing generic connection logic
if widget:
print(f"Setting up connection for widget type: {type(widget).__name__} for key: {key}")
print(
f"Setting up connection for widget type: {type(widget).__name__} for key: {key}"
)
if isinstance(widget, QgsMapLayerComboBox):
widget.layerChanged.connect(lambda layer, k=key: self.update_layer_path(k, layer))
widget.layerChanged.connect(
lambda layer, k=key: self.update_layer_path(k, layer)
)
elif isinstance(widget, QLineEdit):
widget.textChanged.connect(lambda text, k=key: self.update_sub_widget_state(k, text))
widget.textChanged.connect(
lambda text, k=key: self.update_sub_widget_state(k, text)
)
elif isinstance(widget, (QSpinBox, QDoubleSpinBox)):
widget.valueChanged.connect(lambda value, k=key: self.update_sub_widget_state(k, value))
widget.valueChanged.connect(
lambda value, k=key: self.update_sub_widget_state(k, value)
)
elif isinstance(widget, QComboBox):
widget.currentTextChanged.connect(lambda text, k=key: self.update_sub_widget_state(k, text))
elif isinstance(widget, QWidget) and widget.findChild(QgsMapLayerComboBox) and widget.findChild(
QComboBox):
widget.currentTextChanged.connect(
lambda text, k=key: self.update_sub_widget_state(k, text)
)
elif (
isinstance(widget, QWidget)
and widget.findChild(QgsMapLayerComboBox)
and widget.findChild(QComboBox)
):
layer_selector = widget.findChild(QgsMapLayerComboBox)
field_selector = widget.findChild(QComboBox)

def update_fields(layer):
print(
f"[setup_connections] populate_field_selector called for key: {key} with layer: {layer.name() if layer else 'None'}")
f"[setup_connections] populate_field_selector called for key: {key} with layer: {layer.name() if layer else 'None'}"
)
self.populate_field_selector(layer, field_selector)
self.update_polygon_layer_and_field(key, layer, field_selector)

layer_selector.layerChanged.connect(update_fields)
field_selector.currentTextChanged.connect(
lambda text, k=key, ls=layer_selector: self.update_polygon_layer_and_field(k, ls.currentLayer(),
field_selector))
lambda text, k=key, ls=layer_selector: self.update_polygon_layer_and_field(
k, ls.currentLayer(), field_selector
)
)
elif widget_type == "multibuffer":
travel_mode_group = widget.travel_mode_group
measurement_group = widget.measurement_group
increment_edit = widget.increment_edit

travel_mode_group.buttonClicked.connect(lambda btn, k=key: self.update_multibuffer_state(k))
measurement_group.buttonClicked.connect(lambda btn, k=key: self.update_multibuffer_state(k))
increment_edit.textChanged.connect(lambda text, k=key: self.update_multibuffer_state(k))
travel_mode_group.buttonClicked.connect(
lambda btn, k=key: self.update_multibuffer_state(k)
)
measurement_group.buttonClicked.connect(
lambda btn, k=key: self.update_multibuffer_state(k)
)
increment_edit.textChanged.connect(
lambda text, k=key: self.update_multibuffer_state(k)
)

print(f"Set up widget connection for {key}: {type(widget).__name__}")

Expand All @@ -157,7 +210,9 @@ def populate_field_selector(layer, field_selector):
if isinstance(layer, QgsVectorLayer):
field_selector.clear()
field_selector.addItems([field.name() for field in layer.fields()])
print(f"Populated field selector with: {[field.name() for field in layer.fields()]}")
print(
f"Populated field selector with: {[field.name() for field in layer.fields()]}"
)
else:
print(f"Invalid layer type for populating field selector: {type(layer)}")

Expand All @@ -172,20 +227,24 @@ def update_polygon_layer_and_field(self, key, layer, field):
print(f"Layer URI: {uri}")
decoded = QgsProviderRegistry.instance().decodeUri(provider_key, uri)
print(f"Decoded URI: {decoded}")
path = decoded.get('path') or decoded.get('url') or decoded.get('layerName')
path = decoded.get("path") or decoded.get("url") or decoded.get("layerName")

if path:
value = f"{path};{field}"
print(f"Setting {key} to {value}")
self.modified_config[key] = value
else:
print(f"Unable to determine path for layer {layer.name()} with provider {provider_key}")
print(
f"Unable to determine path for layer {layer.name()} with provider {provider_key}"
)
self.modified_config[key] = ""
else:
print(f"No valid layer or field selected for {key}")
self.modified_config[key] = ""

print(f"Modified config after update_polygon_layer_and_field: {self.modified_config}")
print(
f"Modified config after update_polygon_layer_and_field: {self.modified_config}"
)
self.stateChanged.emit(self.get_state())

def update_classify_poly_config(self, key):
Expand All @@ -199,22 +258,30 @@ def update_classify_poly_config(self, key):
print(f"Layer URI: {uri}")
decoded = QgsProviderRegistry.instance().decodeUri(provider_key, uri)
print(f"Decoded URI: {decoded}")
path = decoded.get('path') or decoded.get('url') or decoded.get('layerName')
path = (
decoded.get("path")
or decoded.get("url")
or decoded.get("layerName")
)

if path:
value = f"{path};{field}"
print(f"Setting {key} to {value}")
self.modified_config[key] = value
else:
print(f"Unable to determine path for layer {layer.name()} with provider {provider_key}")
print(
f"Unable to determine path for layer {layer.name()} with provider {provider_key}"
)
self.modified_config[key] = ""
else:
print(f"No layer or field selected for {key}")
self.modified_config[key] = ""
else:
print(f"Widget for {key} is not a ClassifyPolyIntoClassesWidget")
self.modified_config[key] = ""
print(f"Modified config after update_classify_poly_config: {self.modified_config}")
print(
f"Modified config after update_classify_poly_config: {self.modified_config}"
)
self.stateChanged.emit(self.get_state())

def update_layer_path(self, key, layer):
Expand All @@ -225,12 +292,14 @@ def update_layer_path(self, key, layer):
print(f"Layer URI: {uri}")
decoded = QgsProviderRegistry.instance().decodeUri(provider_key, uri)
print(f"Decoded URI: {decoded}")
path = decoded.get('path') or decoded.get('url') or decoded.get('layerName')
path = decoded.get("path") or decoded.get("url") or decoded.get("layerName")
if path:
print(f"Path found: {path}")
self.update_sub_widget_state(key, path)
else:
print(f"Unable to determine path for layer {layer.name()} with provider {provider_key}")
print(
f"Unable to determine path for layer {layer.name()} with provider {provider_key}"
)
self.update_sub_widget_state(key, uri) # Fallback to using the full URI
else:
print(f"No layer selected for {key}")
Expand All @@ -245,7 +314,9 @@ def handle_option_change(self, option, checked):
if widget is None:
print(f"No widget found for {key}")
self.modified_config[key] = 1
elif isinstance(widget, QWidget) and hasattr(widget, 'get_selections'):
elif isinstance(widget, QWidget) and hasattr(
widget, "get_selections"
):
print(f"Handling polygon_layer_with_field_selector for {key}")
layer, field = widget.get_selections()
if layer and field:
Expand All @@ -255,10 +326,17 @@ def handle_option_change(self, option, checked):
elif isinstance(widget, QgsMapLayerComboBox):
print(f"Handling QgsMapLayerComboBox for {key}")
self.update_layer_path(key, widget.currentLayer())
elif isinstance(widget, QWidget) and widget.property("widget_type") == "multibuffer":
elif (
isinstance(widget, QWidget)
and widget.property("widget_type") == "multibuffer"
):
print(f"Handling multibuffer for {key}")
self.update_multibuffer_state(key)
elif isinstance(widget, QWidget) and widget.property("widget_type") == "classify_poly_into_classes":
elif (
isinstance(widget, QWidget)
and widget.property("widget_type")
== "classify_poly_into_classes"
):
print(f"Handling ClassifyPolyIntoClassesWidget for {key}")
self.update_classify_poly_config(key)
else:
Expand All @@ -284,8 +362,16 @@ def update_sub_widget_state(self, option, value):

def update_multibuffer_state(self, key):
widget = self.widgets[key]["widget"]
travel_mode = "Driving" if widget.travel_mode_group.checkedButton().text() == "Driving" else "Walking"
measurement = "Distance" if widget.measurement_group.checkedButton().text() == "Distance" else "Time"
travel_mode = (
"Driving"
if widget.travel_mode_group.checkedButton().text() == "Driving"
else "Walking"
)
measurement = (
"Distance"
if widget.measurement_group.checkedButton().text() == "Distance"
else "Time"
)
increments = widget.increment_edit.text()

# If increments is empty, use the default value
Expand All @@ -302,7 +388,11 @@ def dump_widget_hierarchy(self, widget, level=0):
output = []
output.append(" " * level + f"{widget.__class__.__name__}")

layout = widget.layout() if callable(getattr(widget, 'layout', None)) else getattr(widget, 'layout', None)
layout = (
widget.layout()
if callable(getattr(widget, "layout", None))
else getattr(widget, "layout", None)
)

if isinstance(layout, QLayout):
for i in range(layout.count()):
Expand Down
Loading

0 comments on commit 96ed2f2

Please sign in to comment.